-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFishTracking.java
843 lines (722 loc) · 28.3 KB
/
FishTracking.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
package fishtracking;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.nio.charset.Charset;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import processing.core.*;
import processing.data.JSONArray;
import processing.data.JSONObject;
import de.fhpotsdam.unfolding.geo.*;
import com.google.gson.*;
public class FishTracking extends PApplet {
// assumes data will be in FILENAMES[i]+".json" and corresponding path data
// will be in FILENAMES[i]+"paths.ser"
// public static final String[] FILENAMES = { "chinook", "chinook2",
// "sturgeon" };
// CRASHES
public static final String[] FILENAMES = { "chinook", "chinook2", "sturgeon", "seaturtles" };
// NO CRASH
//public static final String[] FILENAMES = { "chinook", "sturgeon", "seaturtles" };
// corresponds to FILENAMES
public int[] COLORS = { color(255, 204, 204, 127), color(255, 102, 102, 127), color(153, 255, 102, 127), color(255, 255, 102, 127) };
public int[] TRACE_COLORS = { color(255, 204, 204, 10), color(255, 102, 102, 10), color(153, 255, 102, 10), color(255, 255, 102, 10) };
// public int[] COLORS = { color(255, 204, 204, 127), color(255, 102, 102, 127) };
// public int[] TRACE_COLORS = { color(255, 204, 204, 10), color(255, 102, 102, 10) };
// public static final String[] FILENAMES = { "sturgeon" };
// public int[] COLORS = { color(153, 255, 102, 127) };
// Positioning on the screen
public static int PROJECTION_WIDTH = 1500;
public static int PROJECTION_HEIGHT = 1200;
public static int TIMELINE_LEFT_MARGIN = 25;
public static int TIMELINE_RIGHT_MARGIN = 1100;
public static int TIMELINE_BOTTOM_MARGIN = 200;
public Debug debug;
public Boolean bDebug;
public Boolean bInitialized = false;
public int initializationStage = 0;
public int fishFileCounter = 0; // used like a for(int i=0) loop on initialization
// Background filename,
// public static final String LANDBOUNDARY_FILENAME = "landWaterBoundary.png"; // the one with a transparent look to it
public static final String LANDBOUNDARY_FILENAME = "data/landWaterBoundary_color.png"; // the one with a colors in it, especially blue water
public static final Location BAY_MODEL_UPPER_LEFT = new Location(38.2268534751704f, -122.917099f);
public static final Location BAY_MODEL_LOWER_RIGHT = new Location(37.37179426133591f, -121.56440734863281f);
public static final int TIMELINE_PADDING = 100; // how much space (in
// pixels) between the edge
// of the screen and the
// timeline
public static final float SECONDS_PER_MONTH = 6 * 60 * 24 * 30;
public static final float ANIMATION_RATE = SECONDS_PER_MONTH; // speedup
// multiplier
// of data
// time to
// real time
public static long StartTime = Long.MAX_VALUE;
public static long EndTime = Long.MIN_VALUE;
public long CurrentFrameTime, LastFrameTime;
public int TIMELINE_LEFT, TIMELINE_RIGHT;
private int PROJECTION_LEFT = 0;
private PImage boundary, backgroundImage;
private FishCollection fishCollection = new FishCollection();
private PFont smallFont = createFont("Explo", 12);
private static Calendar cal = Calendar.getInstance();
private long currentTime;
private long secondTimer = 0;
private int traceToDraw = 0;
private int drawMode = 0;
private PhysicalInterface physicalInterface;
protected enum LocationType {
PATH, PING
};
public void setup() {
size(1920, 1200, OPENGL);
PROJECTION_WIDTH = 1500;
PROJECTION_HEIGHT = 1200;
PROJECTION_LEFT = (width-PROJECTION_WIDTH)/2;
// Display the debug screen until we have loaded all the fish data
debug = new Debug(this,false); // 2nd param = true if we are producing file logs
bDebug = true;
debug.log("Starting Fish Tracker");
//** old positioning code **/
// TIMELINE_LEFT = TIMELINE_PADDING;
// TIMELINE_RIGHT = width - TIMELINE_PADDING;
TIMELINE_LEFT = TIMELINE_LEFT_MARGIN;
TIMELINE_RIGHT = PROJECTION_WIDTH - TIMELINE_RIGHT_MARGIN;
}
public void draw() {
if( !bInitialized )
initialize();
// if we are in debug mode, draw the debug screen, exit draw
if( bDebug ) {
debug.draw();
return;
}
// Save frame info for fps
LastFrameTime = CurrentFrameTime;
CurrentFrameTime = millis();
// Get info from physical controller
try {
if( physicalInterface != null ) {
physicalInterface.update();
this.currentTime = (long) map(physicalInterface.getPosition(), TIMELINE_LEFT, TIMELINE_RIGHT, StartTime, EndTime);
}
} catch (Exception e) {
debug.out("drawing error: " + e.getMessage());
}
// Regular screen-drawing
// background(backgroundImage);
background(0); // use for projector bleed-over
//image(backgroundImage, 0, 0, width, height);
image(backgroundImage, PROJECTION_LEFT, 0, PROJECTION_WIDTH, PROJECTION_HEIGHT );
if (drawMode == 0)
drawAnimatedPositions(LocationType.PATH);
// drawAnimatedPositions(LocationType.PING);
else if (drawMode == 1)
drawTraces(LocationType.PATH);
// drawLocations(LocationType.PING);
else if (drawMode == 2)
drawPings(LocationType.PING);
drawfps();
}
public void keyPressed() {
if( key == ' ' && key == SHIFT ) { // space bar toggles debug screen
bDebug = !bDebug;
}
if (key == CODED) {
if (keyCode == RIGHT)
this.traceToDraw++;
if (keyCode == LEFT)
this.traceToDraw--;
if (keyCode == UP)
this.drawMode++;
if (keyCode == DOWN)
this.drawMode--;
}
if (this.traceToDraw > TRACE_COLORS.length)
traceToDraw = 0;
if (this.traceToDraw < 0)
traceToDraw = TRACE_COLORS.length;
if (this.drawMode > 2)
drawMode = 0;
if (this.drawMode < 0)
drawMode = 2;
}
//-- we do a series of "stages" for initialization; the reason being is that in the runtime mode, we are failing somewhere
//-- this will be used to show where we are at in the initialization code. After each stage, we increment the initializationStage
//-- counter and initialize another object/portion
private void initialize() {
switch( initializationStage ) {
case 0:
debug.out("Initializing FishTracking" );
initializationStage++;
break;
case 1:
debug.out("Initializing PhysicalInterface" );
// UNCOMMENT when on the viz
// physicalInterface = new PhysicalInterface(this,TIMELINE_LEFT, TIMELINE_RIGHT );
initializationStage++;
break;
case 2:
debug.out("Fish data files:");
for (int i = 0; i < FILENAMES.length; i++) {
//debug.out(" " + dataPath(FILENAMES[i] + ".json"));
debug.out(" " + "data/" + FILENAMES[i] + ".json");
// fishCollection.merge(loadFishData("data/" + FILENAMES[i] + ".json", i));
}
initializationStage++;
break;
case 3:
debug.out("Fish .ser files:");
for (int i = 0; i < FILENAMES.length; i++) {
//debug.out(" " + dataPath(FILENAMES[i] + "paths" + "1" + ".ser"));
debug.out(" " + "data/" + FILENAMES[i] + "paths" + "1" + ".ser");
}
//---OLD
/**
for (int i = 0; i < FILENAMES.length; i++) {
try {
// for (Path path : Files.newDirectoryStream(FileSystems.getDefault().getPath("data/"), FILENAMES[i]
// + "paths?.ser")) // TODO: Support more than 9 path files per data set
for (Path path : Files.newDirectoryStream(FileSystems.getDefault().getPath(dataPath("")), FILENAMES[i]
+ "paths?.ser")) // TODO: Support more than 9 path files per data set
{
debug.out(" " + path.toString());
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}**/
initializationStage++;
fishFileCounter = 0; // set for next stage
break;
case 4:
// break out into separate fish data file initializers
debug.out("Loading Fish data from file: " + FILENAMES[fishFileCounter]);
// Load fish data
fishCollection.merge(loadFishData("data/" + FILENAMES[fishFileCounter] + ".json", fishFileCounter));
debug.log("fishCollection merge is finished" );
//fishCollection.merge(loadFishData(dataPath(FILENAMES[fishFileCounter] + ".json"), fishFileCounter));
fishFileCounter++;
/*
for (int i = 0; i < FILENAMES.length; i++) {
fishCollection.merge(loadFishData(dataPath(FILENAMES[i] + ".json"), i));
// fishCollection.merge(loadFishData("data/" + FILENAMES[i] + ".json", i));
}*/
if( fishFileCounter == FILENAMES.length ) {
// done loading, go to next stage
fishFileCounter = 0;
initializationStage++;
}
break;
case 5:
debug.out("Loading calculated paths: " + FILENAMES[fishFileCounter]);
// Load calculated paths
// PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher("glob:"+FILENAMES[i]+"paths?.ser");
this.loadFishPaths("data/" + FILENAMES[fishFileCounter] + "paths" + "1" + ".ser");
//this.loadFishPaths(dataPath(FILENAMES[fishFileCounter] + "paths" + "1" + ".ser"));
/**
try {
// for (Path path : Files.newDirectoryStream(FileSystems.getDefault().getPath("data/"), FILENAMES[i]
// + "paths?.ser")) // TODO: Support more than 9 path files per data set
for (Path path : Files.newDirectoryStream(FileSystems.getDefault().getPath(dataPath("")), FILENAMES[fishFileCounter]
+ "paths?.ser")) // TODO: Support more than 9 path files per data set
{
this.loadFishPaths(path.toString());
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}**/
fishFileCounter++;
/**for (int i = 0; i < FILENAMES.length; i++) {
// PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher("glob:"+FILENAMES[i]+"paths?.ser");
try {
// for (Path path : Files.newDirectoryStream(FileSystems.getDefault().getPath("data/"), FILENAMES[i]
// + "paths?.ser")) // TODO: Support more than 9 path files per data set
for (Path path : Files.newDirectoryStream(FileSystems.getDefault().getPath(dataPath("")), FILENAMES[i]
+ "paths?.ser")) // TODO: Support more than 9 path files per data set
{
this.loadFishPaths(path.toString());
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}**/
if( fishFileCounter == FILENAMES.length ) {
// done loading, go to next stage
fishFileCounter = 0;
initializationStage++;
}
break;
case 6:
debug.out("Creating timeline image");
// Create background image, including boundary, timeline, and graph
this.backgroundImage = createTimelineImage();
currentTime = StartTime;
CurrentFrameTime = millis();
initializationStage++;
debug.out("done creating timeline, we are done with initialization");
bInitialized = true;
bDebug = false;
break;
}
}
private int lastFrameCount = 0;
private int fps = 0;
private void drawfps() {
if (CurrentFrameTime - secondTimer > 1000) {
secondTimer = CurrentFrameTime;
fps = frameCount - lastFrameCount;
lastFrameCount = frameCount;
}
textFont(smallFont);
textAlign(LEFT, TOP);
text(fps, 0, 0);
}
private int pathsCounter = 0;
private void loadFishPathsOld(String filename) {
JSONObject jPaths = loadJSONObject(filename);
for (Object id : jPaths.keys()) {
JSONArray jPath = jPaths.getJSONArray((String) id);
ArrayList<Ping> path = new ArrayList<Ping>();
for (int i = 0; i < jPath.size(); i++) {
JSONObject jPing = jPath.getJSONObject(i);
Ping ping = new Ping();
ping.latitude = jPing.getFloat("latitude");
ping.longitude = jPing.getFloat("longitude");
ping.dateTime = jPing.getLong("dateTime");
if (ping.dateTime < StartTime)
System.out.println("Error: ping time of " + ping.dateTime + " is before StartTime of " + StartTime);
path.add(ping);
}
pathsCounter++;
System.out.println("Loaded " + pathsCounter + " paths");
debug.log("Loaded " + pathsCounter + " paths");
fishCollection.fishes.get(id).path = path;
}
}
private void loadFishPathsUsingGson(String filename) {
Gson gson = new Gson();
Path inFilePath = FileSystems.getDefault().getPath(filename);
BufferedReader inFile = null;
try {
inFile = Files.newBufferedReader(inFilePath, Charset.defaultCharset());
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
PathCollection pathCollection = gson.fromJson(inFile, PathCollection.class);
this.fishCollection.merge(pathCollection);
// JSONObject jPaths = loadJSONObject(filename);
// for (Object id : jPaths.keys()) {
// JSONArray jPath = jPaths.getJSONArray((String) id);
// ArrayList<Ping> path = new ArrayList<Ping>();
// for (int i = 0; i < jPath.size(); i++) {
// JSONObject jPing = jPath.getJSONObject(i);
// Ping ping = new Ping();
// ping.latitude = jPing.getFloat("latitude");
// ping.longitude = jPing.getFloat("longitude");
// ping.dateTime = jPing.getLong("dateTime");
// if (ping.dateTime < StartTime)
// System.out.println("Error: ping time of " + ping.dateTime +
// " is before StartTime of " + StartTime);
//
// path.add(ping);
// }
// pathsCounter++;
// System.out.println("Loaded " + pathsCounter + " paths");
// fishCollection.fishes.get(id).path = path;
// }
}
private void loadFishPaths(String filename) {
debug.log("load fish paths(), filename = " + filename );
//Path inFilePath = FileSystems.getDefault().getPath(filename);
//debug.log("inFilePath = " + inFilePath.toString() );
PathCollection pathCollection = null;
try {
//FileInputStream fis = new FileInputStream(inFilePath.toString());
FileInputStream fis = new FileInputStream(filename);
ObjectInputStream ois = new ObjectInputStream(fis);
pathCollection = (PathCollection) ois.readObject();
ois.close();
} catch (Exception e) {
debug.log("exception " + e.getMessage() );
e.printStackTrace();
}
debug.log("done loading fish paths()" );
this.fishCollection.merge(pathCollection);
debug.log("done merging fish paths()" );
}
private static int fishCounter = 0;
public FishCollection loadFishData(String filename, int dataset) {
debug.log("Entering loadFishData()");
debug.log("Filename = " + filename);
FishCollection fishCollection = new FishCollection();
debug.log("Allocated Fish Collection object");
JSONObject fishData = loadJSONObject(filename);
debug.log("loaded JSON Object");
if( fishData == null )
debug.log("fishData is NULL");
if (fishData.getLong("startTime") < StartTime)
StartTime = fishData.getLong("startTime");
if (fishData.getLong("endTime") > EndTime)
EndTime = fishData.getLong("endTime");
JSONObject fishList = fishData.getJSONObject("fishes");
debug.log("got fishList");
if( fishList == null )
debug.log("fishList = null");
for (Object key : fishList.keys()) {
fishCounter++;
String id = key.toString();
// Fish fish = gson.fromJson(fishList.getJSONObject(id).toString(),
// Fish.class);
Fish fish = new Fish();
JSONObject jFish = fishList.getJSONObject(id);
try {
fish.length = jFish.getFloat("length");
fish.weight = jFish.getFloat("weight");
}
catch(Exception e){
System.out.println("Missing length or weight");
}
fish.dataset = dataset;
JSONArray jPings = jFish.getJSONArray("pings");
ArrayList<Ping> pings = new ArrayList<Ping>();
for (int i = 0; i < jPings.size(); i++) {
// Ping ping = gson.fromJson(jPings.getString(i), Ping.class);
Ping ping = new Ping();
ping.latitude = jPings.getJSONObject(i).getFloat("latitude");
ping.longitude = jPings.getJSONObject(i).getFloat("longitude");
ping.dateTime = jPings.getJSONObject(i).getLong("dateTime");
pings.add(ping);
}
fish.pings = pings;
fishCollection.fishes.put(id, fish);
System.out.println("Loaded " + fishCounter + " fish");
debug.log("Loaded " + fishCounter + " fish");
}
debug.log("Total fish count = " + fishCounter );
return fishCollection;
}
private PImage createTimelineImage() {
PGraphics pg = createGraphics(PROJECTION_WIDTH, PROJECTION_HEIGHT);
debug.log("done creating graphics");
if( pg == null )
debug.log("pg = null");
pg.beginDraw();
boundary = loadImage(LANDBOUNDARY_FILENAME);
if( boundary == null )
debug.log("loadImage: " + LANDBOUNDARY_FILENAME );
if( boundary == null )
debug.log("boundary = null");
pg.background(0);
pg.image(boundary, 0, 0);//, width, height);
// draw a timeline
float timeY = height - TIMELINE_BOTTOM_MARGIN;
float timeLeftX = TIMELINE_LEFT;
float timeRightX = TIMELINE_RIGHT;
// OLD - remove
// float timeLeftX = TIMELINE_PADDING;
// float timeRightX = width - TIMELINE_PADDING;
// float timeX = map(currentTime, StartTime, EndTime, timeLeftX,
// timeRightX);
pg.stroke(255, 127);
pg.fill(255, 127);
pg.line(timeLeftX, timeY, timeRightX, timeY);
// Draw dashes for months
PFont exploFont = createFont("Explo", 14);//24);
pg.textFont(exploFont);
pg.textAlign(CENTER, BOTTOM);
Calendar cal2 = Calendar.getInstance();
cal2.setTimeInMillis(StartTime);
cal.clear();
cal.set(cal2.get(Calendar.YEAR), cal2.get(Calendar.MONTH), 1); // Zero
// out
// days,
// hours,
// minutes,
// etc.
cal.roll(Calendar.MONTH, true); // Add a month (assumes that first ping
// time is not exactly at start of
// month)
if (cal.get(Calendar.MONTH) == Calendar.JANUARY) // Just rolled to
// January, so need
// to add a year
cal.roll(Calendar.YEAR, true); // Add a year
while (cal.getTimeInMillis() < EndTime) {
float monthX = map(cal.getTimeInMillis(), StartTime, EndTime, timeLeftX, timeRightX);
if (cal.get(Calendar.MONTH) == Calendar.JANUARY) { // Make the first
// month of the
// year line
// longer
// pg.strokeWeight(3f);
pg.line(monthX, timeY - 10, monthX, timeY + 10);
pg.text(cal.get(Calendar.YEAR), monthX, timeY - 10); // Label
// years
} else {
pg.strokeWeight(1f);
pg.line(monthX, timeY - 5, monthX, timeY + 5);
}
cal.roll(Calendar.MONTH, true); // Add a month
if (cal.get(Calendar.MONTH) == Calendar.JANUARY) // Just rolled to
// January, so
// need to add a
// year
cal.roll(Calendar.YEAR, true); // Add a year
// System.out.println(cal.getTime().toString());
}
// Draw a graph of pings for each file
for (int i = 0; i < FILENAMES.length; i++) {
// Count how many pings are in each month from the specified file
HashMap<Long, Integer> monthlyPings = new HashMap<Long, Integer>();
for (Fish fish : fishCollection.fishes.values()) {
if (fish.dataset == i) {
for (Ping ping : fish.pings) {
cal2.setTimeInMillis(ping.dateTime);
cal.clear();
cal.set(cal2.get(Calendar.YEAR), cal2.get(Calendar.MONTH), 1); // Reset to
// beginning of
// current
// month
long monthStart = cal.getTimeInMillis();
if (monthlyPings.containsKey(monthStart)) {
monthlyPings.put(monthStart, monthlyPings.get(monthStart) + 1);
} else {
monthlyPings.put(monthStart, 1);
}
}
}
}
// Find max value in monthlyPings
int maxPingsPerMonth = 0;
for (int numPings : monthlyPings.values()) {
if (numPings > maxPingsPerMonth)
maxPingsPerMonth = numPings;
}
// Draw a graph of pings per month
pg.noStroke();
pg.fill(COLORS[i]);
pg.beginShape();
pg.vertex(timeLeftX, timeY);
cal2.setTimeInMillis(StartTime);
cal.clear();
cal.set(cal2.get(Calendar.YEAR), cal2.get(Calendar.MONTH), 1); // Zero
// out
// days,
// hours,
// minutes,
// etc.
cal.roll(Calendar.MONTH, true); // Add a month (assumes that first
// ping time is not exactly at start
// of
// month)
if (cal.get(Calendar.MONTH) == Calendar.JANUARY) // Just rolled to
// January, so
// need to add a
// year
cal.roll(Calendar.YEAR, true); // Add a year
while (cal.getTimeInMillis() < EndTime) {
long monthStartTime = cal.getTimeInMillis();
cal.set(Calendar.DAY_OF_MONTH, 15);
long monthHalfTime = cal.getTimeInMillis();
cal.set(Calendar.DAY_OF_MONTH, 1);
// float monthX = map(monthStartTime, StartTime, EndTime,
// timeLeftX,
// timeRightX);
float monthHalfX = map(monthHalfTime, StartTime, EndTime, timeLeftX, timeRightX);
float monthHalfY = timeY;
if (monthlyPings.containsKey(monthStartTime))
monthHalfY = timeY - (float) monthlyPings.get(monthStartTime) / (float) maxPingsPerMonth * 50; // Normalize
// height
// of
// graph
// to
// this
// number
// of
// pixels
pg.vertex(monthHalfX, monthHalfY);
cal.roll(Calendar.MONTH, true); // Add a month
if (cal.get(Calendar.MONTH) == Calendar.JANUARY) // Just rolled
// to
// January,
// so need
// to add a
// year
cal.roll(Calendar.YEAR, true); // Add a year
// System.out.println(cal.getTime().toString());
}
pg.vertex(timeRightX, timeY);
pg.endShape();
}
pg.endDraw();
return pg.get();
}
private void drawPings(LocationType locationType) {
for (Fish fish : fishCollection.fishes.values()) {
if (this.traceToDraw < TRACE_COLORS.length && fish.dataset != this.traceToDraw)
continue;
boolean init = true;
stroke(TRACE_COLORS[fish.dataset]);
fill(TRACE_COLORS[fish.dataset]);
strokeWeight(1);
ArrayList<Ping> locationData = null;
if (locationType == LocationType.PATH)
locationData = fish.path;
else if (locationType == LocationType.PING)
locationData = fish.pings;
else
System.out.println("Error: no location data source specified");
for (Ping ping : locationData) {
if (init) {
init = false;
} else {
// Draw a circle at ping location
float pingX, pingY;
pingX = map(ping.longitude, BAY_MODEL_UPPER_LEFT.getLon(), BAY_MODEL_LOWER_RIGHT.getLon(), 0, width);
pingY = map(ping.latitude, BAY_MODEL_UPPER_LEFT.getLat(), BAY_MODEL_LOWER_RIGHT.getLat(), 0, height);
ellipse(pingX, pingY, 3, 3);
}
}
}
}
private void drawTraces(LocationType locationType) {
for (Fish fish : fishCollection.fishes.values()) {
if (this.traceToDraw < TRACE_COLORS.length && fish.dataset != this.traceToDraw)
continue;
boolean init = true;
Ping lastPing = null;
stroke(TRACE_COLORS[fish.dataset]);
strokeWeight(1);
noFill();
ArrayList<Ping> locationData = null;
if (locationType == LocationType.PATH)
locationData = fish.path;
else if (locationType == LocationType.PING)
locationData = fish.pings;
else
System.out.println("Error: no location data source specified");
for (Ping ping : locationData) {
if (init) {
init = false;
lastPing = ping;
} else {
// Draw a line from lastPing to ping
float lastPingX, lastPingY, pingX, pingY;
lastPingX = map(lastPing.longitude, BAY_MODEL_UPPER_LEFT.getLon(), BAY_MODEL_LOWER_RIGHT.getLon(),
0, 1500);
lastPingY = map(lastPing.latitude, BAY_MODEL_UPPER_LEFT.getLat(), BAY_MODEL_LOWER_RIGHT.getLat(),
0, 1200);
pingX = map(ping.longitude, BAY_MODEL_UPPER_LEFT.getLon(), BAY_MODEL_LOWER_RIGHT.getLon(), 0, width);
pingY = map(ping.latitude, BAY_MODEL_UPPER_LEFT.getLat(), BAY_MODEL_LOWER_RIGHT.getLat(), 0, height);
line(lastPingX, lastPingY, pingX, pingY);
lastPing = ping;
}
}
}
}
private void drawLocations(LocationType locationType) {
for (Fish fish : fishCollection.fishes.values()) {
ArrayList<Ping> locationData = null;
if (locationType == LocationType.PATH)
locationData = fish.path;
else if (locationType == LocationType.PING)
locationData = fish.pings;
else
System.out.println("Error: no location data source specified");
for (Ping ping : locationData) {
PVector drawPosition = new PVector();
drawPosition.x = map(ping.longitude, BAY_MODEL_UPPER_LEFT.getLon(), BAY_MODEL_LOWER_RIGHT.getLon(), 0,
1500);
drawPosition.y = map(ping.latitude, BAY_MODEL_UPPER_LEFT.getLat(), BAY_MODEL_LOWER_RIGHT.getLat(), 0,
1200);
ellipse(drawPosition.x, drawPosition.y, 10, 10);
}
}
}
private void drawAnimatedPositions(LocationType locationType) {
if (currentTime > EndTime) {
currentTime = StartTime;
}
// update all fish
for (String id : fishCollection.fishes.keySet()) {
fishCollection.fishes.get(id).update(currentTime, locationType);
}
// draw all fish
noStroke();
for (Fish fish : fishCollection.fishes.values()) {
if (fish.isVisible) {
fill(COLORS[fish.dataset]);
PVector drawPosition = new PVector();
drawPosition.x = lonToX(fish.position.getLon(), width);
drawPosition.y = latToY(fish.position.getLat(), height);
ellipse(drawPosition.x, drawPosition.y, 10, 10);
// break;
}
}
// draw current time indicator
float timeY = height - TIMELINE_BOTTOM_MARGIN; //TIMELINE_PADDING;
float timeLeftX = TIMELINE_LEFT; //TIMELINE_PADDING;
float timeRightX = TIMELINE_RIGHT; //width - TIMELINE_PADDING;
float timeX = map(currentTime, StartTime, EndTime, timeLeftX, timeRightX);
noStroke();
fill(255, 127);
float triangleX = timeX + (width-PROJECTION_WIDTH)/2;
triangle(triangleX, timeY, triangleX - 5, timeY + 10, triangleX + 5, timeY + 10);
//old-remove
// triangle(timeX, timeY, timeX - 5, timeY + 10, timeX + 5, timeY + 10);
// currentTime = currentTime + 86400000; // one day per frame
// currentTime = currentTime + 3600000; // one hour per frame
// currentTime += 1440000; // one day per second at 60 fps
// currentTime += 10080000; // one week per second at 60 fps
if (!mouseLocked)
currentTime += ANIMATION_RATE * (CurrentFrameTime - LastFrameTime);
}
public static void main(String _args[]) {
PApplet.main(new String[] { "--present", fishtracking.FishTracking.class.getName() });
//PApplet.main(new String[] { fishtracking.FishTracking.class.getName() });
}
/*
* A utility function to convert from latitude to Y-coordinate.
*/
public static float latToY(float latitude, float maxY) {
return map(latitude, BAY_MODEL_UPPER_LEFT.getLat(), BAY_MODEL_LOWER_RIGHT.getLat(), 0, maxY);
}
/*
* A utility function to convert from longitude to X-coordinate.
*/
public static float lonToX(float longitude, float maxX) {
return map(longitude, BAY_MODEL_UPPER_LEFT.getLon(), BAY_MODEL_LOWER_RIGHT.getLon(), 0, maxX);
}
private boolean mouseLocked = false;
public void mousePressed() {
int xAdjust = (width - PROJECTION_WIDTH)/2;
if (mouseX <= (TIMELINE_RIGHT+xAdjust) && mouseX >= (TIMELINE_LEFT+xAdjust) ) {
this.currentTime = (long) map(mouseX, TIMELINE_LEFT+xAdjust, TIMELINE_RIGHT+xAdjust, StartTime, EndTime);
mouseLocked = true;
}
}
public void mouseDragged() {
if (mouseLocked) {
int xAdjust = (width - PROJECTION_WIDTH)/2;
int cappedMouseX = mouseX;
if (cappedMouseX < TIMELINE_LEFT+xAdjust)
cappedMouseX = TIMELINE_LEFT+xAdjust;
else if (cappedMouseX > TIMELINE_RIGHT+xAdjust)
cappedMouseX = TIMELINE_RIGHT+xAdjust;
this.currentTime = (long) map(cappedMouseX, TIMELINE_LEFT+xAdjust, TIMELINE_RIGHT+xAdjust, StartTime, EndTime);
}
}
public void mouseReleased() {
mouseLocked = false;
}
}