我正在使用以下代码通过
osmnx
下载国家/地区边界:
country = ox.geocode_to_gdf(['United Kingdom'])
ax = ox.project_gdf(country).plot()
这会产生扭曲的
United Kingdom
轮廓,但似乎适用于其他国家/地区。
从 Nominatim 获取英国边界,而不是如JsFiddle 演示所示:
如果您希望减少边框细节,请将 &polygon_threshold=0.1 参数附加到我的 Javascript 代码中的 Nominatim
url
:
'use strict';
function processNominatimReply(data) {
data.features.forEach(function(feature) {
bordersGroup.addData(feature);
});
var bbox = data.features[0].bbox;
var topLeft = L.latLng(bbox[1], bbox[0]);
var bottomRight = L.latLng(bbox[3], bbox[2]);
var countryBounds = L.latLngBounds(topLeft, bottomRight);
var name = data.features[0].properties.display_name;
var tooltip = L.tooltip()
.setLatLng(countryBounds.getCenter())
.setContent(name)
.addTo(namesGroup);
myMap.flyToBounds(countryBounds);
}
function sendNominatimRequest(countryCode) {
var url = 'https://nominatim.openstreetmap.org/search' +
'?polygon_geojson=1' +
'&format=geojson' +
'&country=' + countryCode;
var request = new XMLHttpRequest();
request.open('GET', url, true);
request.onload = function() {
if (this.status >= 200 && this.status < 400) {
var data = JSON.parse(this.response);
processNominatimReply(data);
}
};
request.send();
}
var startPosition = [51.5072, 0.1276];
var myMap = L.map('myMap').setView(startPosition, 14);
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}).addTo(myMap);
var namesGroup = L.layerGroup().addTo(myMap);
var bordersGroup = L.geoJSON().addTo(myMap);
var overlays = {
'Show country names': namesGroup,
'Show country borders': bordersGroup
};
L.control.layers(null, overlays, {
collapsed: false
}).addTo(myMap);
sendNominatimRequest('uk');
html, body {
margin: 0;
padding: 0;
}
#myMap {
position: absolute;
width: 100%;
height: 100%;
}
<link href="https://cdn.jsdelivr.net/npm/leaflet@1/dist/leaflet.min.css" type="text/css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/leaflet@1/dist/leaflet-src.min.js"></script>
<div id="myMap"></div>