-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEarthquakeCityMap.java
413 lines (341 loc) · 11.9 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
package module6;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
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.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.
// You can ignore this. 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;
// 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(900, 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, 200, 50, 650, 600, new Google.GoogleMapProvider());
// IF YOU WANT TO TEST WITH A LOCAL FILE, uncomment the next line
//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";
// Uncomment this line to take the quiz
//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);
} // End setup
public void draw() {
background(0);
map.draw();
addKey();
}
// TODO: Add the method:
// private void sortAndPrint(int numToPrint)
// and then call that method from setUp
/** 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();
}
}
}
// 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);
}
}
// helper method to draw key in GUI
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(150, 30, 30);
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(255, 255, 0));
ellipse(xbase+35, ybase+140, 12, 12);
fill(color(0, 0, 255));
ellipse(xbase+35, ybase+160, 12, 12);
fill(color(255, 0, 0));
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) {
// IMPLEMENT THIS: 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;
}
}