Run this example in a full screen, or click "Open".
Last active
May 11, 2022 10:04
-
-
Save kkdd/fb33634a49dbecf5d5236430ceb4d4f8 to your computer and use it in GitHub Desktop.
Leaflet map: reading GeoJSON files dropped-onto
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<title>Leaflet</title> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css"> | |
<script src="https://unpkg.com/[email protected]/dist/leaflet.js"></script> | |
<style> | |
body { margin:0; padding:0; } | |
#map { position:absolute; top:0; bottom:0; width:100%; } | |
</style> | |
</head> | |
<body> | |
<div id='map'></div> | |
<script> | |
const par = {lon: 40, lat: 0, zoom: 2, minZoom: 1, maxZoom: 21, map: 'CartoDB', mapDivId: 'map'}; | |
const pointStyle = {radius:3, color:"#6666aa", opacity:1}; | |
const otherStyle = {weight:1, color:"#00ff00", opacity:1}; | |
const OSM_URL = 'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png'; | |
const OSM_ATTR = 'Map data © 2013 OpenStreetMap contributors'; | |
const CD_URL = 'http://{s}.basemaps.cartocdn.com/{style}/{z}/{x}/{y}.png'; | |
const CD_ATTR = '© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors, © <a href="http://cartodb.com/attributions">CartoDB</a>'; | |
const baseMaps = { | |
'OSM': { | |
url: OSM_URL, | |
attribution: OSM_ATTR, | |
maxNativeZoom: 19 }, | |
'CartoDB': { | |
url: CD_URL, | |
attribution: CD_ATTR, | |
maxNativeZoom: 19, | |
style: 'light_all' }, | |
}; | |
Object.keys(baseMaps).forEach((name) => { | |
const layer = {...baseMaps[name], minZoom: par['minZoom'], maxZoom: par['maxZoom']}; | |
baseMaps[name] = L.tileLayer(layer.url, layer); | |
}); | |
const mapOption = {center: [par['lat'], par['lon']], zoom: par['zoom'], layers: baseMaps[par['map']]}; | |
const map = L.map(par['mapDivId'], mapOption); | |
L.control.layers(baseMaps).addTo(map); | |
L.control.scale().addTo(map); | |
map.createPane("pane620").style.zIndex = 620; | |
const dropArea = document.getElementById(par['mapDivId']); | |
dropArea.ondragover = () => { | |
return false; | |
}; | |
dropArea.ondragend = () => { | |
return false; | |
}; | |
dropArea.ondrop = (event) => { | |
readFiles(event.dataTransfer.files); | |
event.preventDefault(); | |
return false; | |
}; | |
const readFiles = (files) => { | |
const bounds = Array.prototype.map.call(files, readFile); | |
Promise.all(bounds).then(bounds => {map_fitBounds(bounds);}); | |
} | |
const readFile = (file) => { | |
const {fname, fileExtension} = fileName(file); | |
return new Promise((resolve, reject) => { | |
const reader = new FileReader(); | |
reader.readAsText(file, 'utf-8'); | |
reader.onload = (event) => { | |
try { | |
const result = event.target.result; | |
const bounds = map_addData(readData(result, fileExtension)); | |
resolve(bounds); | |
} catch (err) { | |
console.log(`${fname}: ${err}`); | |
} | |
}; | |
}); | |
} | |
const readData = (result, fileExtension) => { | |
const data = {"geojson": {"type":"FeatureCollection", "features": []}}; | |
switch (fileExtension){ | |
case "json": | |
case "geojson": | |
data["geojson"] = JSON.parse(result); | |
break; | |
} | |
return data; | |
} | |
const style = (feature) => { | |
if (feature.geometry.type=="Point" || feature.geometry.type=="MultiPoint") { | |
return {radius:pointStyle["radius"], fillColor:pointStyle["color"], fillOpacity:pointStyle["opacity"], weight:0, color:"#000000", opacity:0}; | |
} else { | |
return {...otherStyle, fillColor: otherStyle["color"], fillOpacity: otherStyle["opacity"]*0.5}; | |
} | |
} | |
const map_addData = (data) => { | |
const layerOptions = { | |
style: style, | |
pointToLayer: (feature, latlng) => { | |
return L.circleMarker(latlng, {pane: "pane620"}); | |
}, | |
}; | |
const layer = L.geoJSON(data["geojson"], layerOptions); | |
layer.addTo(map); | |
return layer.getBounds(); | |
} | |
const map_fitBounds = (bounds) => { | |
const bounds_merged = bounds.pop(); | |
bounds.forEach(bb => bounds_merged.extend(bb)) | |
if (bounds_merged.isValid()) map.fitBounds(bounds_merged); | |
} | |
const fileName = (file) => { | |
const fname = file.name; | |
const fileExtension = fname.split('.').pop(); | |
return {fname, fileExtension}; | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment