-
Notifications
You must be signed in to change notification settings - Fork 0
/
logic.js
192 lines (156 loc) · 7.09 KB
/
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
// Function to determine marker size based on magnitude
function markerSize(mag) {
return mag * 10000;
}
// Function to determine marker color based on magnitude
function getColor(d) {
return d > 7 ? '#800026' :
d > 6 ? '#BD0026' :
d > 5 ? '#E31A1C' :
d > 4 ? '#FC4E2A' :
d > 3 ? '#FD8D3C' :
d > 2 ? '#FEB24C' :
d > 1 ? '#FED976' :
'#FFEDA0';
}
// Store our API endpoint inside queryUrl
var queryUrl =
"https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_week.geojson";
// Perform a GET request to the query URL
d3.json(queryUrl, function(data) {
// Once we get a response, send the data.features object to the createFeatures function
console.log(data);
createFeatures(data.features);
});
function createFeatures(earthquakeData) {
// Store our API endpoint for fault lines inside link
// var link =
// "https://raw.githubusercontent.com/fraxen/tectonicplates/master/GeoJSON/PB2002_plates.json";
var link =
"https://raw.githubusercontent.com/fraxen/tectonicplates/master/GeoJSON/PB2002_boundaries.json";
var faultData;
// Grabbing our GeoJSON data..
d3.json(link, function(data) {
// Creating a GeoJSON layer with the retrieved data
faultData = data;
console.log(faultData);
createMap(earthquakeData, faultData);
});
}
function createMap(earthquakeData, faultData) {
// Define variables for our base layers
var streetmap = L.tileLayer("https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?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 © <a href=\"https://www.mapbox.com/\">Mapbox</a>",
maxZoom: 18,
id: "mapbox.streets",
accessToken: API_KEY
});
var lightmap = L.tileLayer("https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?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 © <a href=\"https://www.mapbox.com/\">Mapbox</a>",
maxZoom: 18,
id: "mapbox.light",
accessToken: API_KEY
});
var darkmap = L.tileLayer("https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?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 © <a href=\"https://www.mapbox.com/\">Mapbox</a>",
maxZoom: 18,
id: "mapbox.dark",
accessToken: API_KEY
});
var outdoormap = L.tileLayer("https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?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 © <a href=\"https://www.mapbox.com/\">Mapbox</a>",
maxZoom: 18,
id: "mapbox.outdoors",
accessToken: API_KEY
});
var satellitemap = L.tileLayer("https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?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 © <a href=\"https://www.mapbox.com/\">Mapbox</a>",
maxZoom: 18,
id: "mapbox.satellite",
accessToken: API_KEY
});
// Define arrays to hold created quake markers
var quakeMarkers = [];
for (var i = 0; i < earthquakeData.length; i++) {
var latitude = earthquakeData[i].geometry.coordinates[1];
var longitude = earthquakeData[i].geometry.coordinates[0];
var mag = earthquakeData[i].properties.mag;
var fillColor = getColor(mag);
quakeMarkers.push(
L.circle([latitude, longitude], {
stroke: false,
fillOpacity: 0.75,
color: fillColor,
fillColor: fillColor,
radius: markerSize(mag)
}).bindPopup("<h3>" + earthquakeData[i].properties.place +
"</h3><hr><p>" + new Date(earthquakeData[i].properties.time) +
", magnitude: " + mag + "</p>")
);
}
var faultPolygons = [];
var faultFeatures = faultData.features;
for (var i = 0; i < faultFeatures.length; i++) {
var multipolygon = L.polygon(mapPolygon(faultFeatures[i].geometry.coordinates), {color: '#f00', weight:'5px'});
faultPolygons.push(multipolygon);
// This function creates the style definitions for all the layers. Each layer calls this function
// and is assigned the same style, except for the colour which again is defined dymanically over
// a function.
function faultStyle(feature){
return {
"weight": 2,
"opacity": 1,
"color": '#ff8000',
};
}
var faultGeoJson = L.geoJson(faultFeatures,{
style: faultStyle
});
function mapPolygon(poly){
return poly.map(function(line){return mapLineString(line)})
}
function mapLineString(line){
return line.map(function(d){return [d[1],d[0]]})
}
}
var earthquakes = L.layerGroup(quakeMarkers);
// Create a baseMaps object
var baseMaps = {
"Streets": streetmap,
"Light": lightmap,
"Dark": darkmap,
"Outdoors": outdoormap,
"Satellite": satellitemap
};
// Create an overlay object
var overlayMaps = {
"Fault Lines": faultGeoJson,
"Earthquakes": earthquakes
};
// Define a map object
var myMap = L.map("map", {
center: [37.09, -95.71],
zoom: 5,
layers: [streetmap, faultGeoJson, earthquakes]
});
// Pass our map layers into our layer control
// Add the layer control to the map
L.control.layers(baseMaps, overlayMaps, {
collapsed: false
}).addTo(myMap);
// create legend
var legend = L.control({position: 'bottomright'});
legend.onAdd = function (myMap) {
var div = L.DomUtil.create('div', 'info legend'),
magnitudes = [0, 1, 2, 3, 4, 5, 6, 7],
labels = [];
// loop through our density intervals and generate a label with a colored square for each interval
for (var i = 0; i < magnitudes.length; i++) {
div.innerHTML +=
'<b style="background:' + getColor(magnitudes[i] + 1) + '"> </b> ' +
magnitudes[i] + (magnitudes[i + 1] ? '–' + magnitudes[i + 1] + '<br>' : '+');
}
return div;
};
legend.addTo(myMap);
}