-
Notifications
You must be signed in to change notification settings - Fork 29
/
map8.html
278 lines (237 loc) · 11.2 KB
/
map8.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Cell Towers in Oregon (2009)</title>
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.3.1/dist/leaflet.css"/>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.css"/>
<link href="https://fonts.googleapis.com/css?family=Titillium+Web" rel="stylesheet">
<style>
html, body, #map { width: 100%; height: 100%; margin: 0; background: #fff;}
.legend {
line-height: 16px;
width: 140px;
color: #333333;
font-family: 'Titillium Web', sans-serif;
padding: 6px 8px;
background: white;
background: rgba(255,255,255,0.5);
box-shadow: 0 0 15px rgba(0,0,0,0.2);
border-radius: 5px;
}
.legend i {
width: 16px;
height: 16px;
float: left;
margin-right: 8px;
opacity: 0.9;
}
.legend img {
width: 16px;
height: 16px;
margin-right: 3px;
float: left;
}
.legend p {
font-size: 12px;
line-height: 16px;
margin: 0;
}
.leaflet-tooltip.feature-label {
background-color: transparent;
border: transparent;
box-shadow: none;
font-weight: bold;
font-size: 12px;
font-family: 'Titillium Web', sans-serif;
text-shadow: 0 0 2px #FFFFFF;
color: rgba(35, 35, 35, 0.78)
}
</style>
<script src="https://unpkg.com/rbush@2.0.1/rbush.min.js"></script>
<script src="https://unpkg.com/labelgun@6.0.0/lib/labelgun.min.js"></script>
<script src="https://unpkg.com/leaflet@1.3.1/dist/leaflet.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet-ajax/2.1.0/leaflet.ajax.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/proj4js/2.4.4/proj4.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/proj4leaflet/1.0.2/proj4leaflet.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/chroma-js/1.3.4/chroma.min.js"></script>
<script type="text/javascript" src="https://cloudybay.github.io/leaflet.latlng-graticule/leaflet.latlng-graticule.js"></script>
</head>
<body>
<!-- Our web map and content will go here -->
<div id="map"></div>
<script>
// 14. This is core of how Labelgun works. We must provide two functions, one
// that hides our labels, another that shows the labels. These are essentially
// callbacks that labelgun uses to actually show and hide our labels
// In this instance we set the labels opacity to 0 and 1 respectively.
var hideLabel = function(label){ label.labelObject.style.opacity = 0;};
var showLabel = function(label){ label.labelObject.style.opacity = 1;};
var labelEngine = new labelgun.default(hideLabel, showLabel);
var labels = [];
// 18. define the coordinate reference system (CRS)
mycrs = new L.Proj.CRS('EPSG:2991',
'+proj=lcc +lat_1=43 +lat_2=45.5 +lat_0=41.75 +lon_0=-120.5 +x_0=400000 +y_0=0 +ellps=GRS80 +datum=NAD83 +units=m +no_defs',
{
resolutions: [8192, 4096, 2048, 1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 1] // example zoom level resolutions
}
);
// 1. Create a map object.
var mymap = L.map('map', {
crs: mycrs, // 19. assign the custom crs to the crs option. change the zoom levels due to the change of projection.
center: [44.13, -119.93],
zoom: 3, // we choose zoom level 3
maxZoom: 10,
minZoom: 3,
detectRetina: true});
// 2. Add a base map.
// 20. comment off the base map, because the publicly shared base map usually cannot reprojected
// L.tileLayer('http://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}.png').addTo(mymap);
// 3. Add cell towers GeoJSON Data
// Null variable that will hold cell tower data
var cellTowers = null;
// 4. build up a set of colors from colorbrewer's dark2 category
var colors = chroma.scale('Dark2').mode('lch').colors(9);
// 5. dynamically append style classes to this page. This style classes will be used for colorize the markers.
for (i = 0; i < 9; i++) {
$('head').append($("<style> .marker-color-" + (i + 1).toString() + " { color: " + colors[i] + "; font-size: 15px; text-shadow: 0 0 3px #ffffff;} </style>"));
}
// Get GeoJSON and put on it on the map when it loads
cellTowers= L.geoJson.ajax("assets/cell_towers.geojson", {
// assign a function to the onEachFeature parameter of the cellTowers object.
// Then each (point) feature will bind a popup window.
// The content of the popup window is the value of `feature.properties.company`
onEachFeature: function (feature, layer) {
layer.bindPopup(feature.properties.company);
},
pointToLayer: function (feature, latlng) {
var id = 0;
if (feature.properties.company == "New Cingular") { id = 0; }
else if (feature.properties.company == "Cellco") { id = 1; }
else if (feature.properties.company == "RCC Minnesota") { id = 2; }
else if (feature.properties.company == "Verizon") { id = 3; }
else if (feature.properties.company == "US Cellular") { id = 4; }
else if (feature.properties.company == "Hood River Cellular") { id = 5; }
else if (feature.properties.company == "Medford Cellular") { id = 6; }
else if (feature.properties.company == "Oregon RSA") { id = 7; }
else { id = 8;} // "Salem Cellular"
return L.marker(latlng, {icon: L.divIcon({className: 'fa fa-signal marker-color-' + (id + 1).toString() })});
},
attribution: 'Cell Tower Data © Map Cruzin | Oregon counties © Oregon Explorer | Base Map © CartoDB | Made By Bo Zhao'
}).addTo(mymap);
// 6. Set function for color ramp
colors = chroma.scale('OrRd').colors(5); //colors = chroma.scale('OrRd').colors(5);
function setColor(density) {
var id = 0;
if (density > 18) { id = 4; }
else if (density > 13 && density <= 18) { id = 3; }
else if (density > 10 && density <= 13) { id = 2; }
else if (density > 5 && density <= 10) { id = 1; }
else { id = 0; }
return colors[id];
}
// 7. Set style function that sets fill color.md property equal to cell tower density
function style(feature) {
return {
fillColor: setColor(feature.properties.CT_CNT),
fillOpacity: 0.4,
weight: 2,
opacity: 1,
color: '#b4b4b4',
dashArray: '4'
};
}
// 8. Add county polygons
// create counties variable, and assign null to it.
// 15. Create a label for each county.
var counties = null;
counties = L.geoJson.ajax("assets/counties.geojson", {
style: style,
onEachFeature: function (feature, label) {
label.bindTooltip(feature.properties.NAME, {className: 'feature-label', permanent:true, direction: 'center'});
labels.push(label);
}
}).addTo(mymap);
// 9. Create Leaflet Control Object for Legend
var legend = L.control({position: 'topright'});
// 10. Function that runs when legend is added to map
legend.onAdd = function () {
// Create Div Element and Populate it with HTML
var div = L.DomUtil.create('div', 'legend');
div.innerHTML += '<b># Cell Tower</b><br />';
div.innerHTML += '<i style="background: ' + colors[4] + '; opacity: 0.5"></i><p>19+</p>';
div.innerHTML += '<i style="background: ' + colors[3] + '; opacity: 0.5"></i><p>14-18</p>';
div.innerHTML += '<i style="background: ' + colors[2] + '; opacity: 0.5"></i><p>11-13</p>';
div.innerHTML += '<i style="background: ' + colors[1] + '; opacity: 0.5"></i><p> 6-10</p>';
div.innerHTML += '<i style="background: ' + colors[0] + '; opacity: 0.5"></i><p> 0- 5</p>';
div.innerHTML += '<hr><b>Company<b><br />';
div.innerHTML += '<i class="fa fa-signal marker-color-1"></i><p> New Cingular</p>';
div.innerHTML += '<i class="fa fa-signal marker-color-2"></i><p> Cello</p>';
div.innerHTML += '<i class="fa fa-signal marker-color-3"></i><p> RCC Minnesota</p>';
div.innerHTML += '<i class="fa fa-signal marker-color-4"></i><p> Verizon</p>';
div.innerHTML += '<i class="fa fa-signal marker-color-5"></i><p> US Cellular</p>';
div.innerHTML += '<i class="fa fa-signal marker-color-6"></i><p> Hood River Cellular</p>';
div.innerHTML += '<i class="fa fa-signal marker-color-7"></i><p> Medford Cellular</p>';
div.innerHTML += '<i class="fa fa-signal marker-color-8"></i><p> Oregon RSA</p>';
div.innerHTML += '<i class="fa fa-signal marker-color-9"></i><p> Salem Cellular</p>';
// Return the Legend div containing the HTML content
return div;
};
// 11. Add a legend to map
legend.addTo(mymap);
// 12. Add a scale bar to map
// 21. comment off the scale bar, because in same project, scalebar does not make sense.
// L.control.scale({position: 'bottomleft'}).addTo(mymap);
// 13. Add a latlng graticules.
L.latlngGraticule({
showLabel: true,
opacity: 0.2,
color: "#747474",
zoomInterval: [
{start: 3, end: 7, interval: 2},
{start: 8, end: 10, interval: 0.5}
]
}).addTo(mymap);
// 16. create an addLabel function to dynamically update the visible labels, aiming to avoid the lable overlap.
function addLabel(layer, id) {
// This is ugly but there is no getContainer method on the tooltip :(
var label = layer.getTooltip()._source._tooltip._container;
if (label) {
// We need the bounding rectangle of the label itself
var rect = label.getBoundingClientRect();
// We convert the container coordinates (screen space) to Lat/lng
var bottomLeft = mymap.containerPointToLatLng([rect.left, rect.bottom]);
var topRight = mymap.containerPointToLatLng([rect.right, rect.top]);
var boundingBox = {
bottomLeft : [bottomLeft.lng, bottomLeft.lat],
topRight : [topRight.lng, topRight.lat]
};
// Ingest the label into labelgun itself
labelEngine.ingestLabel(
boundingBox,
id,
parseInt(Math.random() * (5 - 1) + 1), // Weight
label,
label.innerText,
false
);
// If the label hasn't been added to the map already
// add it and set the added flag to true
if (!layer.added) {
layer.addTo(mymap);
layer.added = true;
}
}
}
// 17. We will update the visualization of the labels whenever you zoom the map.
mymap.on("zoomend", function(){
var i = 0;
counties.eachLayer(function(label){
addLabel(label, ++i);
});
labelEngine.update();
});
</script>
</body>
</html>