Sha*_*gta 3 javascript polygon geojson leaflet turfjs
我有一个包含多个多边形的 GeoJson 文件。像这样的东西。
我使用 Leaflet 在网站中呈现这个 GeoJson。
我想围绕包围所有多边形的多边形绘制轮廓。像这样的东西。

我正在使用的 GeoJSOn 格式:
{
"features": [
{
"geometry": {
"coordinates": [
[
[
1074.426,
-1136.986
],
[
1088.241,
-1123.171
]
]
],
"type": "Polygon"
},
"properties": {
"number": "2009",
"type": "",
"spaceid": null,
"alias": null,
"roomkey": "5/2009"
},
"type": "Feature"
}
],
"bbox": [
2445.578,
2445.578
],
"crs": {
"properties": {
"name": "urn:ogc:def:crs:OGC:1.3:CRS84"
},
"type": "name"
},
"type": "FeatureCollection"
Run Code Online (Sandbox Code Playgroud)
}
任何指针都会有帮助:)谢谢
您正在寻找“凸包”:
在数学中,欧几里得平面或欧几里得空间(或更一般地说,在实数上的仿射空间)中的点集 X 的凸包或凸包络是包含 X 的最小凸集。
参考:https : //en.wikipedia.org/wiki/Convex_hull
你可以用 Turf.jsconvex方法做到这一点:
采用 Feature 或 FeatureCollection 并返回凸包 Polygon。
参考:http : //turfjs.org/docs/#convex
例子:
var map = new L.Map('leaflet', {center: [0, 0], zoom: 0});
var collection = turf.featureCollection([
turf.polygon([[[-80,-80],[-40,-80],[-40,-40],[-80,-40],[-80,-80]]]),
turf.polygon([[[80,80],[40,80],[40,40],[80,40],[80,80]]])
]);
new L.GeoJSON(collection, {color: 'red'}).addTo(map);
var polygon = turf.convex(collection);
new L.GeoJSON(polygon, {color: 'black', 'fill': false }).addTo(map);Run Code Online (Sandbox Code Playgroud)
body {
margin: 0;
}
html, body, #leaflet {
height: 100%;
}Run Code Online (Sandbox Code Playgroud)
<!DOCTYPE html>
<html>
<head>
<title>Leaflet 1.2.0</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link type="text/css" rel="stylesheet" href="//unpkg.com/leaflet@1.2.0/dist/leaflet.css" />
</head>
<body>
<div id="leaflet"></div>
<script type="application/javascript" src="//unpkg.com/leaflet@1.2.0/dist/leaflet.js"></script>
<script type="application/javascript" src="//npmcdn.com/@turf/turf/turf.min.js"></script>
</script>
</body>
</html>Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1527 次 |
| 最近记录: |