-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathleaflet-full-example.html
More file actions
204 lines (174 loc) · 5.52 KB
/
leaflet-full-example.html
File metadata and controls
204 lines (174 loc) · 5.52 KB
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
<!DOCTYPE html>
<html>
<head>
<title>Leaflet Map Intro</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Leaflet -->
<link rel="stylesheet" href="_leaflet/plugins/leaflet/leaflet.css" />
<script src="_leaflet/plugins/leaflet/leaflet.js"></script>
<script src="https://api.mapbox.com/mapbox.js/plugins/leaflet-hash/v0.2.1/leaflet-hash.js"></script>
<!-- Omnivore -->
<script src="_leaflet/plugins/omnivore/leaflet-omnivore.min.js"></script>
<!-- Search -->
<link rel="stylesheet" href="_leaflet/plugins/search/leaflet-search.min.css" />
<script src="_leaflet/plugins/search/leaflet-search.min.js"></script>
<!--Choropleth -->
<script src="_leaflet/plugins/choropleth/choropleth.js"></script>
<!--Turf -->
<script src="_leaflet/plugins/turf/turf.min.js"></script>
<style>
body,
html {
height: 100%;
width: 100%;
margin: 0;
}
#map {
width: 100%;
height: 100%;
background: whitesmoke;
}
</style>
</head>
<body>
<div id="map"></div>
<script>
var map = L.map("map");
map.setView([40.3, -96.6], 5);
L.hash(map);
L.control.scale().addTo(map);
var layerControl = L.control.layers().addTo(map);
/*Easily add basemaps or baselayers with L.tileLayer
WMS layers can also be added with L.tileLayer.wms */
var tileUrl = 'https://{s}.tile.openstreetmap.fr/hot/{z}/{x}/{y}.png';
var attribution = '© OpenStreetMap, Tiles courtesy of Humanitarian OpenStreetMap Team';
var OpenStreetMap_HOT = L.tileLayer(tileUrl, {
maxZoom: 19,
attribution: attribution,
opacity: 0.8
}).addTo(map);
layerControl.addBaseLayer(OpenStreetMap_HOT, "Streets");
var url = "tutorial-data/counties.topojson";
var counties = L.geoJson(null, {
style: function(feature) {
switch (feature.properties.winner) {
case "Trump":
return {
fillColor: "red",
fillOpacity: 0.4,
color: 'white',
weight: 1
}
default:
return {
fillColor: "blue",
fillOpacity: 0.4,
color: 'white',
weight: 1
}
}
},
onEachFeature: function(feature, county) {
var info = county.feature.properties.NAME +
"<br>" + county.feature.properties.winner;
county.bindPopup(info);
}
}).addTo(map);
layerControl.addOverlay(counties, "Counties");
var cData = omnivore.topojson(url, null, counties);
// Once our county data is loaded,
// add search functionality using
// leaflet-search with default settings
var choropleth;
// Choropleth
cData.on("ready", function() {
//map.removeLayer(counties);
choropleth = L.choropleth(cData.toGeoJSON(), {
valueProperty: "POP_SQMI", // which property in the features to use
scale: ["white", "#006d2c"], // chroma.js scale - include as many as you like
steps: 7, // number of breaks or steps in range
mode: "q", // q for quantile, e for equidistant, k for k-means
style: {
color: "#fff", // border color
weight: 1,
fillOpacity: 0.9
},
onEachFeature: function(feature, layer) {
layer.bindTooltip(feature.properties.NAME + "<br>" + feature.properties.POPULATION)
}
});
layerControl.addOverlay(choropleth, "Choropleth");
});
var url2 = "tutorial-data/airports.geojson"
var aStyle = {
color: "black",
radius: 5,
weight: 0,
fillOpacity: 1,
pane: "markerPane"
};
var airports = L.geoJson(null, {
pointToLayer: function(feature, latlng) {
return L.circleMarker(latlng, aStyle)
}
});
var aData = omnivore.geojson(url2, null, airports);
var airportJson;
aData.on('ready', function() {
aData.toGeoJSON();
airportJson = aData.toGeoJSON();
});
var highlightStyle = {
color: "goldenrod",
fillColor: "goldenrod",
opacity: 1,
fillOpacity: 1,
pane: "markerPane"
}
var highlight = new L.geoJson(null, {
pointToLayer: function(feature, latlng) {
return new L.circleMarker(latlng, highlightStyle)
}
}).addTo(map);
counties.on("mouseover", function(e) {
highlight.clearLayers();
var selLayer = new L.geoJson(e.layer.toGeoJSON());
var within = turf.within(airportJson, selLayer.toGeoJSON());
highlight.addData(within);
});
/*var countyPoints = L.geoJson(null, {
pane: 'makerPane'
}).addTo(map);
layerControl.addOverlay(countyPoints, "Winner Percentage * Population")
var x = 0;
var r = 1;
var v = 1;
var c = 'gray';
var o = 0.7;
cData.on('ready', function(data) {
counties.eachLayer(function(layer) {
var center = turf.centerOfMass(layer.toGeoJSON());
if (x == 0) {console.log(center);}
x = 1;
if (layer.feature.properties.winner == 'Trump') {
v = ((layer.feature.properties.POPULATION) * (layer.feature.properties["per_gop"]));
r = v/60000;
c = 'red';
o = layer.feature.properties["per_gop"]
}
if (layer.feature.properties.winner == 'Hillary') {
v = ((layer.feature.properties.POPULATION) * (layer.feature.properties["per_dem"]));
r = v/60000;
c = 'blue';
o = layer.feature.properties["per_dem"]
}
if (r < 1 ) {r = 1}
var marker = L.circleMarker([center.geometry.coordinates[1],center.geometry.coordinates[0]], {radius: r, opacity: o, fillOpacity: o, color: c, fillColor: c});
marker.bindPopup('County Population * Percent of Winner Vote: ' + v.toFixed(0));
countyPoints.addLayer(marker);
})
});*/
</script>
</body>
</html>