-
Notifications
You must be signed in to change notification settings - Fork 0
/
_leafmap.php
241 lines (186 loc) · 6.71 KB
/
_leafmap.php
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="assets/scripts/jquery-3.1.1.js" type="text/javascript"></script>
<link rel="stylesheet" href="assets/scripts/leaflet/leaflet.css" />
<!--[if lte IE 8]>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/leaflet/0.6.4/leaflet.ie.css" />
<![endif]-->
<script src="assets/scripts/leaflet/leaflet.js"></script>
<script src="assets/scripts/leaflet/leaflet-src.js"></script>
<script type="text/javascript" src="assets/scripts/leaflet/leaflet.ajax.js"></script>
<script src="assets/scripts/leaflet/spin.js"></script>
<script src="assets/scripts/leaflet/leaflet.spin.js"></script>
<style>
html { height: 100% }
body { height: 100%; margin: 0; padding: 0;}
#map{ /*height: 100%;*/ margin: auto; /*width: 1200px;*/ height: 580px; }
.info { padding: 6px 8px; font: 14px/16px Arial, Helvetica, sans-serif; background: white; background: rgba(255,255,255,0.8); box-shadow: 0 0 15px rgba(0,0,0,0.2); border-radius: 5px; } .info h4 { margin: 0 0 5px; color: #777; }
</style>
<title>Open Institute - Nakuru North GGLI 2018</title>
</head>
<body>
<div id="map"></div>
<div id="mapsubs"></div>
<script type="text/javascript" src="assets/scripts/maps/nakurunorthsubs-geojson.json"></script>
<script type="text/javascript" src="assets/scripts/maps/nakurunorthsubs-color.json"></script>
<script type="text/javascript">
function getJProps(array, value) {
return array.filter(function (object) {
return object["id"] === value;
});
}
/* ========== MERGING OPTIONS ============= */
function getObjects(obj, key, val) {
var objects = [];
for (var i in obj) {
if (!obj.hasOwnProperty(i)) continue;
if (typeof obj[i] == 'object') {
objects = objects.concat(getObjects(obj[i], key, val));
} else if (i == key && obj[key] == val) {
objects.push(obj);
}
}
return objects;
}
function mergeProperties(geom, props, id) {
var newgeom = {
type: "FeatureCollection",
features: []
};
if (geom.type == "FeatureCollection") {
for (var i in geom.features) {
var key = geom.features[i][id];
var g = geom.features[i].geometry;
var prp = geom.features[i].properties;
if (key === undefined) continue;
var p = getObjects(props, id, key);
var newPrp = jQuery.extend(prp, p[0].recprops);
newgeom.features.push({
"id": key,
"type": "Feature",
"properties": newPrp,
"geometry": g
});
}
//if it's a single feature, just get the single object
} else if (geom.type == "Feature") {
var key = geom[id];
var g = geom.geometry;
var prp = geom.properties;
if (key !== undefined) {
var p = getObjects(props, id, key);
var newPrp = jQuery.extend(prp, p[0].recprops);
newgeom.features.push({
"id": key,
"type": "Feature",
"properties": newPrp,
"geometry": g
});
}
//if it's a geometry collection (with ids within each geometry), add the geometries as features
} else if (geom.type == "GeometryCollection") {
for (var i in geom.geometries) {
var key = geom.geometries[i][id];
var g = geom.geometries[i];
var prp = geom.geometries[i].properties;
if (key === undefined) continue;
var p = getObjects(props, id, key);
var newPrp = jQuery.extend(prp, p[0].recprops);
newgeom.features.push({
"id": key,
"type": "Feature",
"properties": newPrp,
"geometry": g
});
}
}
return newgeom;
}
/* ========== END: MERGING OPTIONS ============= */
var mapsData = mergeProperties(locsMapData, propTable, "id");
/*console.log(mapsData);
console.log(mapsData.features[0].properties);
console.log(mapsData.features[0].recprops);*/
var map= L.map('map').setView([-0.18, 36.25], 11);
// disable drag and zoom handlers
// map.dragging.disable();
map.touchZoom.disable();
// map.doubleClickZoom.disable();
map.scrollWheelZoom.disable();
var mopt = {
url: 'https://api.mapbox.com/styles/v1/mapbox/streets-v9/tiles/256/{z}/{x}/{y}?access_token=pk.eyJ1IjoicmFnZW11bmVuZSIsImEiOiJjamtlcmZ0dHIwMGhvM2tvZ3JmOG96ZHN2In0.AwR5ZW21EN9AOJ8NuCXECw',
options: {maxZoom: 18,attribution:'© <a href="https://www.mapbox.com/map-feedback/">Mapbox</a> © <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>', id: 'mapbox.light'}
};
var mq = L.tileLayer(mopt.url, mopt.options);
mq.addTo(map);
var info = L.control();
info.onAdd = function (map) {
this._div = L.DomUtil.create('div', 'info');
this.update();
return this._div;
};
info.update = function (props) {
this._div.innerHTML = '<h4>Location Details</h4>' + (props ?
'<b>' + props.SLNAME + '</b><br />' + props.population + ' households'
: 'Hover over an area');
};
info.addTo(map);
function getColor(d) {
return d > 1000 ? '#800026' :
d > 500 ? '#BD0026' :
d > 200 ? '#E31A1C' :
d > 100 ? '#FC4E2A' :
d > 70 ? '#FD8D3C' :
d > 30 ? '#FEB24C' :
d > 10 ? '#FED976' :
'#FFEDA0';
}
function style(feature) {
return {weight: 2,opacity: 1,color: 'white',dashArray: '3',fillOpacity: 0.7, fillColor: getColor(feature.properties.population)};
}
var locStyle = {"color": "#6BCCF2","weight": 3,"opacity": 0.65};
var sublocStyle = {"color": "#ff7800","weight": 2,"opacity": 1, dashArray: '3', fillColor: "#FFEDA0", fillOpacity: 0.7};
function popUp(f,l){
var out = [];
if (f.properties){
for(key in f.properties){
if(key !== 'SLID'){
var keyalt = (key === 'SLNAME') ? 'Name' : key; out.push(keyalt+": "+f.properties[key]);
}
}
l.bindPopup(out.join("<br />"));
}
}
function highlightFeature(e) {
popUp(e.target.feature, e.target);
var layer = e.target;
layer.setStyle({weight: 5,color: '#666',dashArray: '',fillOpacity: 0.7});
if (!L.Browser.ie && !L.Browser.opera && !L.Browser.edge) { layer.bringToFront(); }
info.update(layer.feature.properties);
}
var geojson;
function resetHighlight(e) {
geojson.resetStyle(e.target);
info.update();
}
function zoomToFeature(e) {
/*map.fitBounds(e.target.getBounds());*/
}
function onEachFeature(feature, layer) {
layer.on({
mouseover: highlightFeature,
mouseout: resetHighlight,
click: zoomToFeature
});
}
//var jsonTest = new L.GeoJSON.AJAX(["maps/nakurunorth-geojson.json"],{style: locStyle},{onEachFeature:popUp}).addTo(map);
geojson = L.geoJson(mapsData, { style: style, onEachFeature: onEachFeature }).addTo(map);
// Prevent map zooming
// var map = new L.Map('map', { zoomControl:false });
</script>
</body>
</html>