[英]Convert GeoJson polygon coordinates to lat / lng
我只是想问你,是否有任何方法可以将 GeoJson 多边形坐标转换为 lat 和 lng。 我在网上没有找到任何有用的东西。
这是来自 GeoJson 文件的片段
{
"type": "Feature",
"properties": { "Id": 0 },
"geometry": {
"type": "Polygon",
"coordinates": [
[ -526814.91964827105, -1166902.422062967 ],
[ -526808.96392099187, -1166933.277827289 ],
[ -526824.83895273879, -1166938.0403368101 ],
...
]
}
},
我没有找到将这些数字转换为 lat / lng 的任何公式或方法。 如果您有这方面的经验,请告诉我!
谢谢你。
我已经测试了这个 function 在 JavaScript 中用于多边形和多多边形。 它应该可以扩展以覆盖更简单的几何图形,例如点和线串。 我希望它可以帮助你:
/* convert polygons from lng/lat to lat/lng. we need to get the number of geometries
contained in the territory object. note that a single polygon may have more than one
geometry if it has a hole in it. a multiPolygon should always have more than one
geometry because they represent polygons */
function swapCoordinates(GeoJSON) {
for (var i = 0; i < GeoJSON.geometry.coordinates.length; i++) {
/* if this is a polygon, length is the number of co-ordinates for this polygon. if
this is a multiPolygon, length is the number of geometries in this polygon */
if (type == "Polygon") {
// iterate over this polygon's co-ordinates
for (var j = 0; j < GeoJSON.geometry.coordinates[i].length; j++) {
if (!paths) {
paths = [];
}
if (!paths[i]) {
paths[i] = [];
}
paths[i].push({
lat: GeoJSON.geometry.coordinates[i][j][1],
lng: GeoJSON.geometry.coordinates[i][j][0]
});
}
} else if (type == "MultiPolygon") {
// for multiPolygons, GeoJSON.geometry.coordinates.length represents the number of polygons, so we need to go one level deeper to get the number of geometries
objectPolygon = []; // reset the polygon object
// iterate over this multiPolygon's polygons
for (var j = 0; j < GeoJSON.geometry.coordinates[i].length; j++) {
innerCoords = []; // reset the inner coordinates
length = GeoJSON.geometry.coordinates[i][j].length; // get the number of co-ordinates for this polygon
clockwise = turf.booleanClockwise(GeoJSON.geometry.coordinates[i][j]); // check the co-ordinates winding. clockwise is a normal polygon, counter-clockwise is a hole
// if this polygon's co-ordinates winding is counter-clockwise, this is a multiPolygon with holes so it needs to be handled differently
if (!clockwise) {
holes = true;
}
// iterate over this polygon's co-ordinates
for (var k = 0; k < length; k++) {
coordinates = {
lat: GeoJSON.geometry.coordinates[i][j][k][1],
lng: GeoJSON.geometry.coordinates[i][j][k][0]
};
// push the polygon geometry into objectPolygon
if (clockwise) {
objectPolygon.push(coordinates);
}
// push the holes into innerCoords
else {
innerCoords.push(coordinates);
}
}
}
if (!paths) {
paths = [];
}
if (holes) {
paths.push([objectPolygon, innerCoords]);
} else {
paths.push(objectPolygon);
}
}
}
return paths;
}
假设您有一个包含以下数据的变量feature
:
var feature = {
"type": "Feature",
"properties": { "Id": 0 },
"geometry": {
"type": "Polygon",
"coordinates": [
[ -526814.91964827105, -1166902.422062967 ],
[ -526808.96392099187, -1166933.277827289 ],
[ -526824.83895273879, -1166938.0403368101 ]
]
}
};
要将其转换为具有 lat/lng 格式坐标的数组,您需要做的就是:
var coordinates = feature.geometry.coordinates.map(v => ({lat: v[0], lng: v[1]}));
如果您需要在末尾复制第一个坐标,只需添加以下行:
coordinates.push(coordinates[0]);
var feature = { "type": "Feature", "properties": { "Id": 0 }, "geometry": { "type": "Polygon", "coordinates": [ [ -526814.91964827105, -1166902.422062967 ], [ -526808.96392099187, -1166933.277827289 ], [ -526824.83895273879, -1166938.0403368101 ] ] } }; var coordinates = feature.geometry.coordinates.map(v => ({lat: v[0], lng: v[1]})); coordinates.push(coordinates[0]); console.log(coordinates);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.