-
Notifications
You must be signed in to change notification settings - Fork 0
/
leaflet_db.html
471 lines (378 loc) · 14.7 KB
/
leaflet_db.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
<!DOCTYPE html> <!-- Specifies the version of HTML, in this case HTML5 -->
<html>
<!-- This is a comment -->
<head>
<title>Basic map</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"> <!-- Meta uses both a name and a content for an attribute -->
<!-- Leaflet -->
<link rel="stylesheet" href="css/leaflet.css"> <!-- External CSS Code, rel needs the value stylesheet and href tells the location -->
<script src="js/leaflet.js"></script> <!-- JavaScript Code -->
<!-- Awesome markers -->
<link rel="stylesheet" href="http://code.ionicframework.com/ionicons/1.5.2/css/ionicons.min.css">
<link rel="stylesheet" href="css/leaflet.awesome-markers.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/Leaflet.awesome-markers/2.0.2/leaflet.awesome-markers.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/Leaflet.awesome-markers/2.0.2/leaflet.awesome-markers.min.js"></script>
<script src="js/leaflet.ajax.min.js"></script>
<script src="js/leaflet.awesome-markers.js"></script>
<!-- Leaflet Plugins MiniMap-->
<link rel="stylesheet" href="css/Control.MiniMap.css" />
<script src="js/Control.MiniMap.js" type="text/javascript"></script>
<!-- Leaflet Plugins Drawing Control -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/leaflet.draw/1.0.4/leaflet.draw.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet.draw/1.0.4/leaflet.draw.js"></script>
<style>
body {
padding: 0;
margin: 0;
}
html, body, #map {
height: 100%;
width: 100%;
}
</style>
</head>
<body>
<div id="map"></div> <!-- div groups set of elements together in one block-level box -->
<form>
Description:<br><input type="text" id="input_desc"><br>
Name:<br><input type="text" id="input_name"><br>
<input type="button" value="Submit" id="submit">
</form>
<script>
// BASIS OF THE MAP : Tile, map, minimap
// Tile layer
let osm = L.tileLayer("https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png", {attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a>'});
let imagery = L.tileLayer('https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}', {attribution: 'Tiles © Esri — Source: Esri, i-cubed, USDA, USGS, AEX, GeoEye, Getmapping, Aerogrid, IGN, IGP, UPR-EGP, and the GIS User Community'});
let stoner = L.tileLayer('https://stamen-tiles-{s}.a.ssl.fastly.net/toner/{z}/{x}/{y}{r}.{ext}', {
attribution: 'Map tiles by <a href="http://stamen.com">Stamen Design</a>, <a href="http://creativecommons.org/licenses/by/3.0">CC BY 3.0</a> — Map data © <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors',
subdomains: 'abcd',
minZoom: 0,
maxZoom: 20,
ext: 'png'
});
// Map
let map = L.map('map', {center: [40.41, -3.7], zoom: 13, layers: stoner});
// Minimap
var osmUrl='http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png';
var osmAttrib='Map data © OpenStreetMap contributors';
var osm3 = new L.TileLayer(osmUrl, {minZoom: 5, maxZoom: 18, attribution: osmAttrib});
var osm2 = new L.TileLayer(osmUrl, {minZoom: 0, maxZoom: 13, attribution: osmAttrib });
var miniMap = new L.Control.MiniMap(osm2, { toggleDisplay: true }).addTo(map);
// SET UP THE URL LINK TO THE DIGITAL OCEAN DROPLET
let droplet_IP = "http://178.62.83.246:4000";
let url = droplet_IP + "/sql?format=GeoJSON&q=";
// LOAD THE SITES ON THE GISDB TABLE SITES_SUB
// Create layer for submitted points
let submittedData = L.layerGroup().addTo(map);
// Load existing data points
let sqlQuery_sub = "SELECT * FROM sites_sub";
function addPopup(feature, layer) {
layer.bindPopup(
"<b>NUEVO SITIO</b></br><b>Nombre: </b>"+feature.properties.nombre+
"<br><b>Barrio: </b>"+feature.properties.barrio+
"<br><b>Tipo: </b>"+feature.properties.tipo+
"<br><b>Ubicacion: </b>"+feature.properties.ubicacion+
"<br><b>Correo: </b>"+feature.properties.contacto
);
}
fetch(url + sqlQuery_sub)
.then(function(response) {
return response.json();
})
.then(function(data) {
L.geoJSON(data, {onEachFeature: addPopup}).addTo(submittedData);
});
// DRAWING CONTROL
// Set it up
let drawnItems = L.featureGroup().addTo(map);
new L.Control.Draw({
draw : {
polygon : false,
polyline : false,
rectangle : false, // Rectangles disabled
circle : false, // Circles disabled
circlemarker : false, // Circle markers disabled
marker: true
},
edit : {
featureGroup: drawnItems
}
}).addTo(map);
// On draw - Create editable popup
map.addEventListener("draw:created", function(e) {
e.layer.addTo(drawnItems);
drawnItems.eachLayer(function(layer) {
let geojson = JSON.stringify(layer.toGeoJSON().geometry);
console.log(geojson);
})
createFormPopup();
});
// On edit or delete - Close popup
map.addEventListener("draw:editstart", function(e) {
drawnItems.closePopup();
});
map.addEventListener("draw:deletestart", function(e) {
drawnItems.closePopup();
});
map.addEventListener("draw:editstop", function(e) {
drawnItems.openPopup();
});
map.addEventListener("draw:deletestop", function(e) {
if(drawnItems.getLayers().length > 0) {
drawnItems.openPopup();
}
});
// Create editable form, on top of HTML code above script
function createFormPopup() {
let popupContent =
'<form>' +
'Nombre:<br><input type="text" id="input_nombre"><br>' +
'Barrio:<br><input type="text" id="input_barrio"><br>' +
'Tipo:<br><input type="text" id="input_tipo"><br>' +
'Ubicacion:<br><input type="text" id="input_ubicacion"><br>' +
'<input type="button" value="Submit" id="submit">' +
'</form>';
drawnItems.bindPopup(popupContent).openPopup();
}
// SUBMISSION FUNCTION
// Sending to GISDB Table sites_sub
function setData(e) {
if(e.target && e.target.id == "submit") {
// Get user name and description
let enteredNombre = document.getElementById("input_nombre").value;
let enteredBarrio = document.getElementById("input_barrio").value;
let enteredTipo = document.getElementById("input_tipo").value;
let enteredUbicacion = document.getElementById("input_ubicacion").value;
// Print user name and description
console.log(enteredNombre);
console.log(enteredBarrio);
console.log(enteredTipo);
console.log(enteredUbicacion);
// Get and print GeoJSON for each drawn layer
drawnItems.eachLayer(function(layer) {
let drawing = JSON.stringify(layer.toGeoJSON().geometry);
let sql =
"INSERT INTO sites_sub (geom, nombre, barrio, tipo, ubicacion) " +
"VALUES (ST_SetSRID(ST_GeomFromGeoJSON('" +
drawing + "'), 4326), '" +
enteredNombre + "', '" +
enteredBarrio + "', '" +
enteredTipo + "', '" +
enteredUbicacion + "')";
let encoded = encodeURI(sql);
console.log(sql);
console.log(encoded);
console.log(url);
// Send the data
fetch(url + encodeURI(sql))
.then(function(response) {
return response.json();
})
.then(function(data) {
console.log("Data saved:", data);
})
.catch(function(error) {
console.log("Problem saving the data:", error);
});
// Transfer submitted drawing to the CARTO layer
let newData = layer.toGeoJSON();
newData.properties.nombre = enteredNombre;
newData.properties.barrio = enteredBarrio;
newData.properties.tipo = enteredTipo;
newData.properties.ubicacion = enteredUbicacion;
L.geoJSON(newData, {
onEachFeature: addPopup
}).addTo(submittedData);
});
// Clear drawn items layer
drawnItems.closePopup();
drawnItems.clearLayers();
}};
// Click on 'submit' event listener
document.addEventListener("click", setData);
// INFORMATION BOX AND BASEMAP/OVERLAY FOR MUNICIPIOS & DISTRITOS
// Information box
let info = L.control({position: "topright"});
info.onAdd = function() {
let div = L.DomUtil.create("div", "info");
div.innerHTML = '<h4>Municipios de Madrid</h4><p id="currentMunicipios"></p><br><h4>Distritos de Madrid</h4><p id="currentDistritos">';
return div;
};
info.addTo(map);
// OverlayMaps
let baseMaps = {
"Stoner": stoner,
"OSM": osm,
"Imagery": imagery
};
let punto = L.marker([40.91, -3.1]).bindPopup('Soy un puntazo');
let overlayMaps = {
"Puntazo": punto
};
let control = L.control.layers(baseMaps, overlayMaps, {
position: 'topright', // 'topleft', 'bottomleft', 'bottomright'
collapsed: false // true
}).addTo(map);
control.removeLayer(punto);
// FUNCTIONS FOR THE STYLES OF MUNICIPIOS & DISTRITOS
// Highlight functions
let geojson_dis;
let geojson_mun;
let info_mun = document.getElementById("currentMunicipios");
let info_dis = document.getElementById("currentDistritos");
let highlightStyle = {
weight: 5,
color: "black",
fillOpacity: 0.5
};
function highlightFeature(e) {
e.target.setStyle(highlightStyle);
e.target.bringToFront();
info_mun.innerHTML =
e.target.feature.properties.NAMEUNIT;
info_dis.innerHTML =
e.target.feature.properties.NOMDIS;
}
function resetHighlight(e) {
geojson_mun.resetStyle(e.target);
info_mun.innerHTML = "";
}
// Function for random colors style
function style(feature) {
return {
weight: 2,
opacity: 1,
color: 'white',
dashArray: '3',
fillOpacity: 0.2,
fillColor: '#'+(0x1000000+(Math.random())*0xffffff).toString(16).substr(1,6)
};
}
// ADD THE MUNICIPIOS & DISTRITOS
// Add the municipios of Madrid
fetch("data/municipios.geojson")
.then(function(response) {
return response.json();
})
.then(function(data) {
geojson_mun = L.geoJSON(data, {
style: style,
onEachFeature: function(feature, layer) {
layer.addEventListener("mouseover", highlightFeature);
layer.addEventListener("mouseout", resetHighlight);
}
}).addTo(map);
let overlayMaps = {
"Municipios": geojson_mun
};
let control_mun = L.control.layers(baseMaps, overlayMaps, {
position: 'topleft', // 'topleft', 'bottomleft', 'bottomright'
collapsed: false // true
}).addTo(map);
control_mun.removeLayer(stoner);
control_mun.removeLayer(osm);
control_mun.removeLayer(imagery);
});
// Add the district of Madrid
let distritos = fetch("data/distritos.geojson")
.then(function(response) {
return response.json();
})
.then(function(data) {
geojson_dis = L.geoJSON(data, {
style: style,
onEachFeature: function(feature, layer) {
layer.addEventListener("mouseover", highlightFeature);
layer.addEventListener("mouseout", resetHighlight);
}
}).addTo(map);
let overlayMaps = {
"Distritos M": geojson_dis
};
let control_dis = L.control.layers(baseMaps, overlayMaps, {
position: 'topleft', // 'topleft', 'bottomleft', 'bottomright'
collapsed: false // true
}).addTo(map);
control_dis.removeLayer(stoner);
control_dis.removeLayer(osm);
control_dis.removeLayer(imagery);
});
// FUNCTIONS FOR THE STYLE OF SITES (ICONS) AND THE POPUP INFO
// Function of style of sites
function styles_sites(feature, latlng) {
if (feature.properties.tipo === "Libreria") {
return L.marker(latlng, {
icon: L.AwesomeMarkers.icon({icon: 'book', iconColor: 'black', markerColor: 'red', prefix: 'fa'})
});
}
else if (feature.properties.tipo === "CSOA") {
return L.marker(latlng, {
icon: L.AwesomeMarkers.icon({icon: 'users', iconColor: 'black', markerColor: 'blue', prefix: 'fa'})
});
}
else if (feature.properties.tipo === "Huerto") {
return L.marker(latlng, {
icon: L.AwesomeMarkers.icon({icon: 'tree', iconColor: 'black', markerColor: 'green', prefix: 'fa'})
});
}
else if (feature.properties.tipo === "NGO") {
return L.marker(latlng, {
icon: L.AwesomeMarkers.icon({icon: 'globe', iconColor: 'black', markerColor: 'pink', prefix: 'fa'})
});
}
else if (feature.properties.tipo === "CSA") {
return L.marker(latlng, {
icon: L.AwesomeMarkers.icon({icon: 'users', iconColor: 'black', markerColor: 'darkblue', prefix: 'fa'})
});
}
};
// Function for the popup of sites
function sites_pop(feature, layer) {
var popupText = "<b>" + feature.properties.nombre + "<br>";
layer.bindPopup(popupText);
category = feature.properties.tipo;
// Initialize the category array if not already set.
if (typeof categories[category] === "undefined") {
categories[category] = L.layerGroup().addTo(map);
layersControl.addOverlay(categories[category], category);
}
categories[category].addLayer(layer);
layer.on('mouseover', function() {
layer.openPopup(layer.bindPopup("<b>"+feature.properties.nombre+"</b>"))
});
layer.on('mouseout', function() {
layer.closePopup();
});
layer.on('click', function () {
layer.bindPopup("<b>Nombre: </b>"+feature.properties.nombre+"<br><b>Barrio: </b>"+feature.properties.barrio+"<br><b>Tipo: </b>"+feature.properties.tipo+"<br><b>Ubicacion: </b>"+feature.properties.ubicacion+"<br><b>Correo: </b>"+feature.properties.contacto);
});
}
// Set up categories for sites legend breakdown
var categories = {},
category;
var layersControl = L.control.layers(null, null,{
position: 'topright', // 'topleft', 'bottomleft', 'bottomright'
collapsed: false // true
} ).addTo(map);
// ADD THE SITES FROM GISDB VALIDATED TABLE 'NOMBRE'
// Insert the URL for the Droplet DB
let sqlQuery = "SELECT lat, long, tipo, barrio, nombre, contacto, ubicacion, geometry AS geom FROM nombre LIMIT 25";
// Load the sites from droplet gisdb nombre db with fetch
async function getData() {
const response = await fetch(url + sqlQuery);
const sites = await response.json();
return sites;
}
let sites = getData()
.then((function(data) {
L.geoJSON(data, {
pointToLayer: styles_sites,
onEachFeature: sites_pop
}).addTo(map);
}));
// OTHER MAP FEATURES
</script>
</body>
</html>