-
Notifications
You must be signed in to change notification settings - Fork 0
/
challenge_logic.js
238 lines (206 loc) · 8.28 KB
/
challenge_logic.js
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
// Add console.log to check to see if our code is working.
console.log("working");
// We create the tile layer that will be the background of our map.
let streets = L.tileLayer('https://api.mapbox.com/styles/v1/mapbox/streets-v11/tiles/{z}/{x}/{y}?access_token={accessToken}', {
attribution: 'Map data © <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors, <a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery (c) <a href="https://www.mapbox.com/">Mapbox</a>',
maxZoom: 18,
accessToken: API_KEY
});
// We create the second tile layer that will be an option for our map.
let satelliteStreets = L.tileLayer('https://api.mapbox.com/styles/v1/mapbox/satellite-streets-v11/tiles/{z}/{x}/{y}?access_token={accessToken}', {
attribution: 'Map data © <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors, <a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery (c) <a href="https://www.mapbox.com/">Mapbox</a>',
maxZoom: 18,
accessToken: API_KEY
});
// We create the thrid tile layer that will be an option for of our map.
let dark = L.tileLayer('https://api.mapbox.com/styles/v1/mapbox/dark-v10/tiles/{z}/{x}/{y}?access_token={accessToken}', {
attribution: 'Map data © <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors, <a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery (c) <a href="https://www.mapbox.com/">Mapbox</a>',
maxZoom: 18,
accessToken: API_KEY
});
// Create a base layer that holds all three maps.
let baseMaps = {
"Streets": streets,
"Satellite": satelliteStreets,
"Dark": dark
};
// 1. Add a 1st layer group for the tectonic plate data.
let tectonicPlates = new L.LayerGroup();
// Add a 2nd layer group for the earthquakes data.
let allEarthquakes = new L.LayerGroup();
// Add a 3rd layer group for the major earthquake data.
let majorEQ = new L.LayerGroup();
// 2. Add a reference to the tectonic plates group to the overlays object.
let overlays = {
"Tectonic Plates": tectonicPlates,
"Earthquakes": allEarthquakes,
"Major Earthquakes": majorEQ
};
// Create the map object with center, zoom level and default layer.
// The earthquake and tectonic plates overlays are displayed by default when the map is loaded.
let map = L.map('mapid', {
center: [40.7, -94.5],
zoom: 3,
layers: [streets, allEarthquakes]
});
// Then we add a control to the map that will allow the user to change which
// layers are visible.
L.control.layers(baseMaps, overlays).addTo(map);
// Retrieve the earthquake GeoJSON data.
d3.json("https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_week.geojson").then(function(data) {
// This function returns the style data for each of the earthquakes we plot on
// the map. We pass the magnitude of the earthquake into two separate functions
// to calculate the color and radius.
function styleInfo(feature) {
return {
opacity: 0.6,
fillOpacity: 1,
fillColor: getColor(feature.properties.mag),
color: "#000000",
radius: getRadius(feature.properties.mag),
stroke: true,
weight: 1
};
}
// This function determines the color of the marker based on the magnitude of the earthquake.
function getColor(magnitude) {
if (magnitude > 5) {
return "#ea2c2c";
}
if (magnitude > 4) {
return "#ea822c";
}
if (magnitude > 3) {
return "#ee9c00";
}
if (magnitude > 2) {
return "#eecc00";
}
if (magnitude > 1) {
return "#d4ee00";
}
return "#98ee00";
}
// This function determines the radius of the earthquake marker based on its magnitude.
// Earthquakes with a magnitude of 0 were being plotted with the wrong radius.
function getRadius(magnitude) {
if (magnitude === 0) {
return 1;
}
return magnitude * 4;
}
// Creating a GeoJSON layer with the retrieved data.
L.geoJson(data, {
// We turn each feature into a circleMarker on the map.
pointToLayer: function(feature, latlng) {
// console.log(data);
return L.circleMarker(latlng);
},
// We set the style for each circleMarker using our styleInfo function.
style: styleInfo,
// We create a popup for each circleMarker to display the magnitude and location of the earthquake
// after the marker has been created and styled.
onEachFeature: function(feature, layer) {
layer.bindPopup("Magnitude: " + feature.properties.mag + "<br>Location: " + feature.properties.place);
}
}).addTo(allEarthquakes);
// Then we add the earthquake layer to our map.
allEarthquakes.addTo(map);
// 3. Retrieve the major earthquake GeoJSON data >4.5 mag for the week.
d3.json("https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/4.5_week.geojson").then(function(data) {
// 4. Use the same style as the earthquake data.
// This function returns the style data for each of the earthquakes we plot on
// the map. We pass the magnitude of the earthquake into two separate functions
// to calculate the color and radius.
function StyleInfo(feature) {
return {
opacity: 0.6,
fillOpacity: 1,
fillColor: GetColor(feature.properties.mag),
color: "#000000",
radius: GetRadius(feature.properties.mag),
stroke: true,
weight: 1
};
}
// 5. Change the color function to use three colors for the major earthquakes based on the magnitude of the earthquake.
function GetColor(magnitude) {
if (magnitude > 6) {
return "#ff0000";
}
if (magnitude > 5) {
return "#ff8000";
}
return "#ffff00";
}
// 6. Use the function that determines the radius of the earthquake marker based on its magnitude.
function GetRadius(magnitude) {
if (magnitude === 0) {
return 1;
}
return magnitude * 4;
}
// 7. Creating a GeoJSON layer with the retrieved data that adds a circle to the map
// sets the style of the circle, and displays the magnitude and location of the earthquake
// after the marker has been created and styled.
L.geoJson(data, {
// We turn each feature into a circleMarker on the map.
pointToLayer: function(feature, latlng) {
// console.log(data);
return L.circleMarker(latlng);
},
// We set the style for each circleMarker using our styleInfo function.
style: StyleInfo,
// We create a popup for each circleMarker to display the magnitude and location of the earthquake
// after the marker has been created and styled.
onEachFeature: function(feature, layer) {
layer.bindPopup("Magnitude: " + feature.properties.mag + "<br>Location: " + feature.properties.place);
}
}).addTo(majorEQ);
// 8. Add the major earthquakes layer to the map.
majorEQ.addTo(map);
// 9. Close the braces and parentheses for the major earthquake data.
});
// Here we create a legend control object.
let legend = L.control({
position: "bottomright"
});
// Then add all the details for the legend
legend.onAdd = function() {
let div = L.DomUtil.create("div", "info legend");
const magnitudes = [0, 1, 2, 3, 4, 5];
const colors = [
"#98ee00",
"#d4ee00",
"#eecc00",
"#ee9c00",
"#ea822c",
"#ea2c2c"
];
// Looping through our intervals to generate a label with a colored square for each interval.
for (var i = 0; i < magnitudes.length; i++) {
// console.log(colors[i]);
div.innerHTML +=
"<i style='background: " + colors[i] + "'></i> " +
magnitudes[i] + (magnitudes[i + 1] ? "–" + magnitudes[i + 1] + "<br>" : "+");
}
return div;
};
// Finally, we our legend to the map.
legend.addTo(map);
// Create a style for the lines of the tectronic plate (styled in orange with a weight of 2)
let myStyle = {
color: "#ee9c00",
weight: 2
}
// 3. Use d3.json to make a call to get our Tectonic Plate geoJSON data.
d3.json("https://raw.githubusercontent.com/fraxen/tectonicplates/master/GeoJSON/PB2002_boundaries.json").then(function(data) {
// console.log(data);
// Creating a GeoJSON layer with the retrieved data.
L.geoJson(data, {
style: myStyle
}).addTo(tectonicPlates);
// Add the tectonic layer to the map.
tectonicPlates.addTo(map);
});
});