-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSeaTurtleParser.java
154 lines (133 loc) · 4.63 KB
/
SeaTurtleParser.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// For parsing Chinook salmon dataset from Arnold Ammann.
package fishtracking;
import java.text.SimpleDateFormat;
import java.text.ParseException;
import processing.core.PApplet;
import processing.data.JSONArray;
import processing.data.JSONObject;
// Loads a csv file and saves it as a JSon.
public class SeaTurtleParser extends PApplet {
public String[][] loadCSV(String filename) {
// for importing csv files into a 2d array
// by che-wei wang
String lines[] = loadStrings(filename);
String[][] csv;
int csvWidth = 0;
// calculate max width of csv file
for (int i = 0; i < lines.length; i++) {
String[] chars = split(lines[i], ',');
if (chars.length > csvWidth) {
csvWidth = chars.length;
}
}
// create csv array based on # of rows and columns in csv file
csv = new String[lines.length][csvWidth];
// parse values into 2d array
for (int i = 0; i < lines.length; i++) {
String[] temp = new String[lines.length];
temp = split(lines[i], ',');
for (int j = 0; j < temp.length; j++) {
csv[i][j] = temp[j];
}
}
// test
println(csv[2][2]);
return csv;
}
/*
* Adds data from a 2D string array from a CSV file into the provided JSON object. Assumptions: First row is header
* row, first column is ID's, second column is datetimes, third column is latitude, fourth column is longitude, row
* 11 is weight, row 13 is length, rows are sorted by time
*/
public JSONObject addDetectionsToJSON(JSONObject fishDictionary, String[][] csv) {
float minLat = Float.POSITIVE_INFINITY;
float minLon = Float.POSITIVE_INFINITY;
float maxLat = Float.NEGATIVE_INFINITY;
float maxLon = Float.NEGATIVE_INFINITY;
long minTime = Long.MAX_VALUE;
long maxTime = Long.MIN_VALUE;
// SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
fishDictionary.setJSONObject("fishes", new JSONObject());
for (String[] row : csv) {
JSONObject fish;
String id = row[0];
if (id.equals("toppID")) { // skip the header row
continue;
}
if (fishDictionary.getJSONObject("fishes").hasKey(id)) {
fish = fishDictionary.getJSONObject("fishes").getJSONObject(id);
} else {
// New ID, create a new fish
fish = new JSONObject();
fish.setJSONArray("pings", new JSONArray()); // An array of
// times and
// locations
// that this
// fish was seen
// Add other fish-related data here
// fish.setFloat("weight", Float.parseFloat(row[11]));
// fish.setFloat("length", Float.parseFloat(row[13]));
System.out.println("New fish in detections, no length and weight data");
}
// Parse values
JSONObject ping = new JSONObject();
long curTime;
try {
curTime = format.parse(row[1]).getTime();
} catch (ParseException e) {
System.out.println(e.getMessage());
continue;
}
float curLat = Float.parseFloat(row[2]);
float curLon = Float.parseFloat(row[3]);
// Keep track of mins and maxes
if (curLat < minLat) {
minLat = curLat;
} else if (curLat > maxLat) {
maxLat = curLat;
}
if (curLon < minLon) {
minLon = curLon;
} else if (curLon > maxLon) {
maxLon = curLon;
}
if (curTime < minTime) {
minTime = curTime;
} else if (curTime > maxTime) {
maxTime = curTime;
}
// Add locations and times to fish's pings array
ping.setFloat("latitude", curLat);
ping.setFloat("longitude", curLon);
ping.setLong("dateTime", curTime);
fish.getJSONArray("pings").append(ping);
fishDictionary.getJSONObject("fishes").setJSONObject(id, fish);
}
// Add mins and maxes
fishDictionary.setLong("startTime", minTime);
fishDictionary.setLong("endTime", maxTime);
// fishDictionary.setFloat("minLongitude", minLon);
// fishDictionary.setFloat("maxLongitude", maxLon);
// fishDictionary.setFloat("minLatitude", minLat);
// fishDictionary.setFloat("maxLatitude", maxLat);
return fishDictionary;
}
public void setup() {
// String[][] info = this.loadCSV("seaturtledetections.csv");
// JSONObject jData = this.infoToJSON(info);
String[][] detections = this.loadCSV("seaturtledetections.csv");
JSONObject jData = this.addDetectionsToJSON(new JSONObject(), detections);
boolean successfullySaved = saveJSONObject(jData, "data/seaturtles.json");
if (successfullySaved)
background(0, 255, 0);
else
background(255, 0, 0);
}
public void draw() {
}
public static void main(String _args[]) {
PApplet.main(new String[] { fishtracking.SeaTurtleParser.class.getName() });
}
}