Skip to content

Instantly share code, notes, and snippets.

@Zia-
Created August 14, 2018 13:41
Show Gist options
  • Save Zia-/d01c3af6275f1af0c1183d5bfcf8008a to your computer and use it in GitHub Desktop.
Save Zia-/d01c3af6275f1af0c1183d5bfcf8008a to your computer and use it in GitHub Desktop.
Download OSM Data from rectangle draw with different baselayers.
<!DOCTYPE html>
<html>
<head>
<title>Download OSM File from rectangle drag</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="shortcut icon" type="image/x-icon" href="docs/images/favicon.ico" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.3.3/leaflet.css" crossorigin=""/>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
</head>
<body>
<div id="mapid" style="width:100%;bottom:0;top:0;position:absolute;">
<button type="button" class="btn btn-primary" style="z-index:1000;position:relative;margin-left: 50px;" onclick="enableDraw()">Draw Rectangle</button>
<button type="button" class="btn btn-danger" style="z-index:1000;position:relative;margin-left: 5px;" onclick="removeLayer()">Remove Rectangle</button>
<button type="button" class="btn btn-success" style="z-index:1000;position:relative;margin-left: 500px;" onclick="getOSM()">Get OSM Data</button>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.3.3/leaflet.js" crossorigin=""></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet.draw/1.0.2/leaflet.draw.js" crossorigin=""></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.bundle.min.js" integrity="sha384-pjaaA8dDz/5BgdFUPX6M/9SUZv4d12SUPF0axWc+VRZkx5xU3daN+lYb49+Ax+Tl" crossorigin="anonymous"></script>
<script src="https://cdn.rawgit.com/eligrey/FileSaver.js/5ed507ef8aa53d8ecfea96d96bc7214cd2476fd2/FileSaver.min.js"></script>
<script>
var map = L.map('mapid').setView([49.415121, 8.673310], 13);
// Add Basemaps ************
var mbAttr = 'Map data &copy; <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors, ' +
'<a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, ' +
'Imagery © <a href="https://www.mapbox.com/">Mapbox</a>',
mbUrl = 'https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token=pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpejY4NXVycTA2emYycXBndHRqcmZ3N3gifQ.rJcFIG214AriISLbB6B5aw';
var satellite = L.tileLayer(mbUrl, {id: 'mapbox.satellite', maxZoom: 22, attribution: mbAttr}).addTo(map),
streetsSat = L.tileLayer(mbUrl, {id: 'mapbox.streets-satellite', maxZoom: 22, attribution: mbAttr}),
streetsBasic = L.tileLayer(mbUrl, {id: 'mapbox.streets-basic', maxZoom: 22, attribution: mbAttr}),
outdoors = L.tileLayer(mbUrl, {id: 'mapbox.outdoors', maxZoom: 22, attribution: mbAttr});
var baseLayers = {
"Satellite": satellite,
"Streets-Satellite": streetsSat,
"Streets-Basic": streetsBasic,
"Outdoors": outdoors
};
L.control.layers(baseLayers).addTo(map);
var drawnItems = new L.FeatureGroup();
map.addLayer(drawnItems);
var polygonDrawer = new L.Draw.Rectangle(map);
function enableDraw(){
// Draw Rectangle ************
polygonDrawer.enable();
};
// Get Extent ************
var minx, miny, maxx, maxy;
map.on('draw:created', function (e) {
var layer = e.layer;
drawnItems.addLayer(layer);
minx = layer.getBounds().getWest();
miny = layer.getBounds().getSouth();
maxx = layer.getBounds().getEast();
maxy = layer.getBounds().getNorth();
layer.editing.enable();
map.on('draw:editresize', function (e) {
minx = layer.getBounds().getWest();
miny = layer.getBounds().getSouth();
maxx = layer.getBounds().getEast();
maxy = layer.getBounds().getNorth();
})
});
function removeLayer(){
// Remove Layers ************
drawnItems.clearLayers();
}
function getOSM(){
// Download OSM ************
const Http = new XMLHttpRequest(); // https://medium.freecodecamp.org/here-is-the-most-popular-ways-to-make-an-http-request-in-javascript-954ce8c95aaa
var baseurl = "https://api.openstreetmap.org/api/0.6/map?bbox="
var url = baseurl.concat(minx.toString(), ",", miny.toString(), ",", maxx.toString(), ",", maxy.toString())
Http.open("GET", url);
Http.send();
Http.onreadystatechange=function(){
if(this.readyState==4 && this.status==200){
var filename = prompt("Enter Filename without extension", "eg. Heidelberg");
var blob = new Blob([Http.responseText], {
type: "text/plain;charset=utf-8"
});
saveAs(blob, filename.concat(".osm"));
}
}
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment