-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEarthquakeCityMap.java
551 lines (463 loc) · 15.5 KB
/
EarthquakeCityMap.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
package module6;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
import de.fhpotsdam.unfolding.UnfoldingMap;
import de.fhpotsdam.unfolding.data.Feature;
import de.fhpotsdam.unfolding.data.GeoJSONReader;
import de.fhpotsdam.unfolding.data.PointFeature;
import de.fhpotsdam.unfolding.geo.Location;
import de.fhpotsdam.unfolding.marker.AbstractShapeMarker;
import de.fhpotsdam.unfolding.marker.Marker;
import de.fhpotsdam.unfolding.marker.MultiMarker;
import de.fhpotsdam.unfolding.providers.Google;
import de.fhpotsdam.unfolding.providers.MBTilesMapProvider;
import de.fhpotsdam.unfolding.providers.Microsoft;
import de.fhpotsdam.unfolding.utils.MapUtils;
import parsing.ParseFeed;
import processing.core.PApplet;
/** EarthquakeCityMap
* An application with an interactive map displaying earthquake data.
* Author: UC San Diego Intermediate Software Development MOOC team
* @author Your name here
* Date: July 17, 2015
* */
public class EarthquakeCityMap extends PApplet {
// We will use member variables, instead of local variables, to store the data
// that the setUp and draw methods will need to access (as well as other methods)
// You will use many of these variables, but the only one you should need to add
// code to modify is countryQuakes, where you will store the number of earthquakes
// per country.
// It's to get rid of eclipse warnings
private static final long serialVersionUID = 1L;
// IF YOU ARE WORKING OFFILINE, change the value of this variable to true
private static final boolean offline = false;
/** This is where to find the local tiles, for working without an Internet connection */
public static String mbTilesString = "blankLight-1-3.mbtiles";
//feed with magnitude 2.5+ Earthquakes
private String earthquakesURL = "https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_week.atom";
// The files containing city names and info and country names and info
private String cityFile = "city-data.json";
private String countryFile = "countries.geo.json";
// The map
private UnfoldingMap map;
// my variables
private int triangleRedCol = 150, triangleGreenCol = 30, triangleBlueCol = 30;
Random rand = new Random();
double triangleArea;
private int shallRed = 255, shallGreen = 255, shallBlue = 0;
private int intRed = 0, intGreen = 0, intBlue = 255;
private int deepRed = 255, deepGreen = 0, deepBlue = 0;
private int zoomLevel = 0;
private float lat, lon;
private boolean locationEntered = false;
Scanner sc = new Scanner(System.in);
//-------------------------------------
// Markers for each city
private List<Marker> cityMarkers;
// Markers for each earthquake
private List<Marker> quakeMarkers;
// A List of country markers
private List<Marker> countryMarkers;
// NEW IN MODULE 5
private CommonMarker lastSelected;
private CommonMarker lastClicked;
public void setup() {
// (1) Initializing canvas and map tiles
size(1100, 700, OPENGL);
if (offline) {
map = new UnfoldingMap(this, 200, 50, 650, 600, new MBTilesMapProvider(mbTilesString));
earthquakesURL = "2.5_week.atom"; // The same feed, but saved August 7, 2015
}
else {
map = new UnfoldingMap(this, 180, 50, 900, 600, new Microsoft.HybridProvider());
// TO TEST WITH A LOCAL FILE :
//earthquakesURL = "2.5_week.atom";
}
MapUtils.createDefaultEventDispatcher(this, map);
// FOR TESTING: Set earthquakesURL to be one of the testing files by uncommenting
// one of the lines below. This will work whether you are online or offline
//earthquakesURL = "test1.atom";
// earthquakesURL = "test2.atom";
earthquakesURL = "quiz2.atom";
// (2) Reading in earthquake data and geometric properties
// STEP 1: load country features and markers
List<Feature> countries = GeoJSONReader.loadData(this, countryFile);
countryMarkers = MapUtils.createSimpleMarkers(countries);
// STEP 2: read in city data
List<Feature> cities = GeoJSONReader.loadData(this, cityFile);
cityMarkers = new ArrayList<Marker>();
for(Feature city : cities) {
cityMarkers.add(new CityMarker(city));
}
// STEP 3: read in earthquake RSS feed
List<PointFeature> earthquakes = ParseFeed.parseEarthquake(this, earthquakesURL);
quakeMarkers = new ArrayList<Marker>();
for(PointFeature feature : earthquakes) {
//check if LandQuake
if(isLand(feature)) {
quakeMarkers.add(new LandQuakeMarker(feature));
}
// OceanQuakes
else {
quakeMarkers.add(new OceanQuakeMarker(feature));
}
}
// could be used for debugging
printQuakes();
// (3) Add markers to map
// NOTE: Country markers are not added to the map. They are used
// for their geometric properties
map.addMarkers(quakeMarkers);
map.addMarkers(cityMarkers);
sortAndPrint(20);
triangleArea = calculateArea(60,95,55, 105, 65, 105);
} // End setup
public void draw() {
background(0,0,0);
map.draw();
addKey();
}
public void keyPressed() {
if(locationEntered == false || key == 'c') {
System.out.println("Enter latitude : ");
lat = sc.nextFloat();
System.out.println("Enter longitude : ");
lon = sc.nextFloat();
locationEntered = true;
zoomLevel += 2;
zoomIt();
}
if(key == 'z') {
if(zoomLevel <= 10) {
zoomLevel += 1;
zoomIt();
}
}
else if(key == 'x') {
if(zoomLevel >= 1) {
System.out.println(zoomLevel);
zoomLevel -= 1;
zoomIt();
}
}
}
private void zoomIt() {
map.zoomAndPanTo(zoomLevel, new Location(lat, lon));
}
private void sortAndPrint(int numToPrint) {
Object[] quakes = quakeMarkers.toArray();
int len = quakes.length, j, i;
EarthquakeMarker tmp;
for(i = 1; i<len; i++) {
j = i;
while(j > 0) {
if(((EarthquakeMarker)quakes[j]).compareTo(((EarthquakeMarker)quakes[j-1])) == 1) {
tmp = ((EarthquakeMarker)quakes[j-1]);
quakes[j-1] = ((EarthquakeMarker)quakes[j]);
quakes[j] = tmp;
j--;
}
else {
break;
}
}
}
i = 0;
System.out.println("Printing out quakes");
while((i < len) && (i < numToPrint)) {
System.out.println(quakes[i]);
i++;
}
}
/** Event handler that gets called automatically when the
* mouse moves.
*/
@Override
public void mouseMoved()
{
// clear the last selection
if (lastSelected != null) {
lastSelected.setSelected(false);
lastSelected = null;
}
selectMarkerIfHover(quakeMarkers);
selectMarkerIfHover(cityMarkers);
//loop();
}
// If there is a marker selected
private void selectMarkerIfHover(List<Marker> markers)
{
// Abort if there's already a marker selected
if (lastSelected != null) {
return;
}
for (Marker m : markers)
{
CommonMarker marker = (CommonMarker)m;
if (marker.isInside(map, mouseX, mouseY)) {
lastSelected = marker;
marker.setSelected(true);
return;
}
}
}
/** The event handler for mouse clicks
* It will display an earthquake and its threat circle of cities
* Or if a city is clicked, it will display all the earthquakes
* where the city is in the threat circle
*/
@Override
public void mouseClicked()
{
if (lastClicked != null) {
unhideMarkers();
lastClicked = null;
}
else if (lastClicked == null)
{
checkEarthquakesForClick();
if (lastClicked == null) {
checkCitiesForClick();
}
}
changeColorForIcon();
}
private void changeColorForIcon() {
mouseInsideTriangle();
mouseInsideShallowCircle();
mouseInsideInterCircle();
mouseInsideDeepCircle();
}
// helper method to draw key in GUI
private boolean mouseInsideCircle(int x1, int y1) {
double radiusSquare = 12 * 12;
if(((double)((x1 - mouseX) * (x1 - mouseX) + (y1 - mouseY) * (y1 - mouseY))) <= radiusSquare) {
return true;
}
else
return false;
}
private void mouseInsideShallowCircle() {
if(mouseInsideCircle(60, 190)) {
shallRed = rand.nextInt(255);
shallGreen = rand.nextInt(255);
shallBlue = rand.nextInt(255);
EarthquakeMarker.updateShallowColor(shallRed, shallGreen, shallBlue);
}
}
private void mouseInsideInterCircle() {
if(mouseInsideCircle(60, 210)) {
intRed = rand.nextInt(255);
intGreen = rand.nextInt(255);
intBlue = rand.nextInt(255);
EarthquakeMarker.updateIntermediateColor(intRed, intGreen, intBlue);
}
}
private void mouseInsideDeepCircle() {
if(mouseInsideCircle(60, 230)) {
deepRed = rand.nextInt(255);
deepGreen = rand.nextInt(255);
deepBlue = rand.nextInt(255);
EarthquakeMarker.updateDeepColor(deepRed, deepGreen, deepBlue);
}
}
private void mouseInsideTriangle() {
double tArea1 = calculateArea(mouseX, mouseY, 55, 105, 65, 105);
double tArea2 = calculateArea(60, 95 ,mouseX, mouseY, 65, 105);
double tArea3 = calculateArea(60, 95, 55, 105, mouseX, mouseY);
if((tArea1 + tArea2 + tArea3) == triangleArea) {
triangleRedCol = rand.nextInt(255);
triangleBlueCol = rand.nextInt(255);
triangleGreenCol = rand.nextInt(255);
CityMarker.setColor(triangleRedCol, triangleBlueCol, triangleGreenCol);
}
}
private static double calculateArea(int x1, int y1, int x2, int y2, int x3, int y3) {
int a1 = x1 * (y2 - y3);
int a2 = x2 * (y3 - y1);
int a3 = x3 * (y1 - y2);
return Math.abs(((double)(a1 + a2 + a3))/2);
}
// Helper method that will check if a city marker was clicked on
// and respond appropriately
private void checkCitiesForClick()
{
if (lastClicked != null) return;
// Loop over the earthquake markers to see if one of them is selected
for (Marker marker : cityMarkers) {
if (!marker.isHidden() && marker.isInside(map, mouseX, mouseY)) {
lastClicked = (CommonMarker)marker;
// Hide all the other earthquakes and hide
for (Marker mhide : cityMarkers) {
if (mhide != lastClicked) {
mhide.setHidden(true);
}
}
for (Marker mhide : quakeMarkers) {
EarthquakeMarker quakeMarker = (EarthquakeMarker)mhide;
if (quakeMarker.getDistanceTo(marker.getLocation())
> quakeMarker.threatCircle()) {
quakeMarker.setHidden(true);
}
}
return;
}
}
}
// Helper method that will check if an earthquake marker was clicked on
// and respond appropriately
private void checkEarthquakesForClick()
{
if (lastClicked != null) return;
// Loop over the earthquake markers to see if one of them is selected
for (Marker m : quakeMarkers) {
EarthquakeMarker marker = (EarthquakeMarker)m;
if (!marker.isHidden() && marker.isInside(map, mouseX, mouseY)) {
lastClicked = marker;
// Hide all the other earthquakes and hide
for (Marker mhide : quakeMarkers) {
if (mhide != lastClicked) {
mhide.setHidden(true);
}
}
for (Marker mhide : cityMarkers) {
if (mhide.getDistanceTo(marker.getLocation())
> marker.threatCircle()) {
mhide.setHidden(true);
}
}
return;
}
}
}
// loop over and unhide all markers
private void unhideMarkers() {
for(Marker marker : quakeMarkers) {
marker.setHidden(false);
}
for(Marker marker : cityMarkers) {
marker.setHidden(false);
}
}
private void addKey() {
// Remember you can use Processing's graphics methods here
fill(255, 250, 240);
int xbase = 25;
int ybase = 50;
rect(xbase, ybase, 150, 250);
fill(0);
textAlign(LEFT, CENTER);
textSize(12);
text("Earthquake Key", xbase+25, ybase+25);
fill(triangleRedCol, triangleGreenCol, triangleBlueCol);
int tri_xbase = xbase + 35;
int tri_ybase = ybase + 50;
triangle(tri_xbase, tri_ybase-CityMarker.TRI_SIZE, tri_xbase-CityMarker.TRI_SIZE,
tri_ybase+CityMarker.TRI_SIZE, tri_xbase+CityMarker.TRI_SIZE,
tri_ybase+CityMarker.TRI_SIZE);
fill(0, 0, 0);
textAlign(LEFT, CENTER);
text("City Marker", tri_xbase + 15, tri_ybase);
text("Land Quake", xbase+50, ybase+70);
text("Ocean Quake", xbase+50, ybase+90);
text("Size ~ Magnitude", xbase+25, ybase+110);
fill(255, 255, 255);
ellipse(xbase+35,
ybase+70,
10,
10);
rect(xbase+35-5, ybase+90-5, 10, 10);
fill(color(shallRed, shallGreen, shallBlue));
ellipse(xbase+35, ybase+140, 12, 12);
fill(color(intRed, intGreen, intBlue));
ellipse(xbase+35, ybase+160, 12, 12);
fill(color(deepRed, deepGreen, deepBlue));
ellipse(xbase+35, ybase+180, 12, 12);
textAlign(LEFT, CENTER);
fill(0, 0, 0);
text("Shallow", xbase+50, ybase+140);
text("Intermediate", xbase+50, ybase+160);
text("Deep", xbase+50, ybase+180);
text("Past hour", xbase+50, ybase+200);
fill(255, 255, 255);
int centerx = xbase+35;
int centery = ybase+200;
ellipse(centerx, centery, 12, 12);
strokeWeight(2);
line(centerx-8, centery-8, centerx+8, centery+8);
line(centerx-8, centery+8, centerx+8, centery-8);
}
// Checks whether this quake occurred on land. If it did, it sets the
// "country" property of its PointFeature to the country where it occurred
// and returns true. Notice that the helper method isInCountry will
// set this "country" property already. Otherwise it returns false.
private boolean isLand(PointFeature earthquake) {
// loop over all countries to check if location is in any of them
// If it is, add 1 to the entry in countryQuakes corresponding to this country.
for (Marker country : countryMarkers) {
if (isInCountry(earthquake, country)) {
return true;
}
}
// not inside any country
return false;
}
// prints countries with number of earthquakes
// You will want to loop through the country markers or country features
// (either will work) and then for each country, loop through
// the quakes to count how many occurred in that country.
// Recall that the country markers have a "name" property,
// And LandQuakeMarkers have a "country" property set.
private void printQuakes() {
int totalWaterQuakes = quakeMarkers.size();
for (Marker country : countryMarkers) {
String countryName = country.getStringProperty("name");
int numQuakes = 0;
for (Marker marker : quakeMarkers)
{
EarthquakeMarker eqMarker = (EarthquakeMarker)marker;
if (eqMarker.isOnLand()) {
if (countryName.equals(eqMarker.getStringProperty("country"))) {
numQuakes++;
}
}
}
if (numQuakes > 0) {
totalWaterQuakes -= numQuakes;
System.out.println(countryName + ": " + numQuakes);
}
}
System.out.println("OCEAN QUAKES: " + totalWaterQuakes);
}
// helper method to test whether a given earthquake is in a given country
// This will also add the country property to the properties of the earthquake feature if
// it's in one of the countries.
// You should not have to modify this code
private boolean isInCountry(PointFeature earthquake, Marker country) {
// getting location of feature
Location checkLoc = earthquake.getLocation();
// some countries represented it as MultiMarker
// looping over SimplePolygonMarkers which make them up to use isInsideByLoc
if(country.getClass() == MultiMarker.class) {
// looping over markers making up MultiMarker
for(Marker marker : ((MultiMarker)country).getMarkers()) {
// checking if inside
if(((AbstractShapeMarker)marker).isInsideByLocation(checkLoc)) {
earthquake.addProperty("country", country.getProperty("name"));
// return if is inside one
return true;
}
}
}
// check if inside country represented by SimplePolygonMarker
else if(((AbstractShapeMarker)country).isInsideByLocation(checkLoc)) {
earthquake.addProperty("country", country.getProperty("name"));
return true;
}
return false;
}
}