-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
195 lines (176 loc) · 9.29 KB
/
index.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="user-scalable=no initial-scale=1.2">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Edmonton Municipality</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.3.1/leaflet.css"/>
<style>
html, body {
height: 100%;
margin: 0;
overflow: hidden;
}
#map {
height: 100%;
width: 100%;
}
.leaflet-overlay-pane svg path { pointer-events: auto; }
.leaflet-overlay-pane svg path:hover {
fill: transparent;
stroke: black;
}
</style>
</head>
<body>
<div id="spinner"></div>
<div id="map"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.3.1/leaflet.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.17/d3.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3-queue/1.2.3/queue.min.js"></script>
<script src="https://unpkg.com/topojson-client@3"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/spin.js/2.3.2/spin.min.js"></script>
<script>
document.addEventListener("DOMContentLoaded", function() {
var print = console.log;
var dir = console.dir;
//Set up loading spinner
var opts = {
lines: 13,
length: 22,
width: 9,
radius: 30,
corners: 1,
rotate: 2,
direction: 1,
speed: 1,
trail: 60,
shadow: false,
hwaccel: false,
className: 'spinner',
zIndex: 2e9,
top:'50%',
left: '50%'
};
var target = document.getElementById("spinner");
var spinner = new Spinner(opts).spin(target);
//------------map------------
var map = L.map("map", {
zoomSnap: 0.01,
zoom: zoomSelector(window.innerWidth),
touchZoom: false,
minZoom: 10.06
});
var positron = L.tileLayer("https://cartodb-basemaps-{s}.global.ssl.fastly.net/light_all/{z}/{x}/{y}.png", {
attribution: '© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> © <a href="http://cartodb.com/attributions">CartoDB</a>',
subdomains: 'abcd',
maxZoom: 19
});
map.setView( [53.55589, -113.506643] );
map.attributionControl.setPosition('topright');
map.zoomControl.setPosition('bottomright');
L.control.scale().setPosition('topleft').addTo(map);
positron.addTo(map);
//making the map responsive
map.on("resize", function() {
map.setZoom(zoomSelector(window.innerWidth));
map.getBounds().getCenter();
});
//to set the zoom based on window width
function zoomSelector(size) {
//http://leafletjs.com/examples/choropleth/
//cleaner format than three one line if statements
return size < 768 ? 10.06:
size >= 768 && size != 1024 && size <= 1366 ? 10.71:
11.21;
}
//------------map------------
//------------Data Loading and Processing------------
//loading the data
queue()
.defer(d3.json, "data/wards-topo.json") //wards
.defer(d3.json, "data/neighbourhood-topo.json") //neighbourhoods
.defer(d3.json, "data/zoning-topo.json") //zoning
.await(dataProcessor);
function dataProcessor(error, wards, neighbourhoods, zones) {
if(error) throw error;
var dataObjects =
[
{
file: wards,
colors: ['#67001f','#b2182b','#d6604d','#f4a582','#fddbc7','#d1e5f0','#92c5de','#4393c3','#2166ac','#053061'],
lineColor: "#bdc1bd"
}
,{
file: neighbourhoods,
colors: ['#a50026','#d73027','#f46d43','#fdae61','#fee090','#ffffbf','#abd9e9','#74add1','#4575b4','#313695'],
lineColor: "#7b7f7b"
},
{
file: zones,
colors: ['#d53e4f','#f46d43','#fdae61','#fee08b','#ffffbf','#e6f598','#abdda4','#66c2a5','#3288bd'],
lineColor: "#6e6a6a",
name: "zoning"
}
];
var overlays = [];
var overlayObject = new Object();
// adding name attribute for identification
dataObjects.forEach(function(d, i){
d.name = Object.keys(d.file.objects)[0];
});
//~-~-~-~-~-~-~-~overlay~-~-~-~-~-~-~-~
dataObjects.forEach(function(d, i){
var features = topojson.feature(d.file, d.file["objects"][d.name]).features;
var dataLength = features.length;
var colorScale = d3.scale.quantize().domain([0, dataLength -1]).range(d.colors);
//http://leafletjs.com/examples/choropleth/
var geojsonLayer = L.geoJSON(features, {
style: function style(feature, index) {
return {
color: d.lineColor,
weight: 1,
opacity: 0.45,
fillColor: colorScale(getRandomIntInclusive(0, dataLength)),
fillOpacity:0.45
};
},
onEachFeature: function onEachFeature(feature, layer) {
var property = feature.properties;
switch (d.name) {
case "zoning":
return layer.bindPopup(property["zoning"] + "<br/>" + " " + property["descript"]);
break;
default:
return layer.bindPopup(property["name"] + "<br/>" + " " + parseFloat(property["area_km2"]).toFixed(3)+" "+"km^"+"2");
break;
}
}
});
//adding layer to map
geojsonLayer.addTo(map);
//push layer and name to overlays to build layercontrol later
overlays.push([d.name, geojsonLayer]);
});
//~-~-~-~-~-~-~-~overlay~-~-~-~-~-~-~-~
//~-~-~-~-~-~-~-~Layer Control~-~-~-~-~-~-~-~
//turning the overlays array[] to an associative array{}(object)
overlays.forEach(function(d){
overlayObject[d[0]] = d[1];
});
//using the object to make layer control
L.control.layers({"Tilelayer": positron}, overlayObject).setPosition('bottomleft').addTo(map);
d3.select("#spinner").remove();//close after all files and layers are loaded
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random
function getRandomIntInclusive(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min; //The maximum is inclusive and the minimum is inclusive
}
};//dataProcessor
//------------Data Loading and Processing------------
})//document.addEventListener
</script>
</body>
</html>