将 GeoJson 多边形坐标转换为纬度/液化天然气



我只是想问你,是否有任何方法可以将GeoJson多边形坐标转换为纬度和液化天然气。我在网上没有找到任何有用的东西。

这是来自GeoJson文件的片段

{ 
"type": "Feature", 
"properties": { "Id": 0 }, 
"geometry": { 
"type": "Polygon", 
"coordinates": [ 
[ -526814.91964827105, -1166902.422062967 ], 
[ -526808.96392099187, -1166933.277827289 ], 
[ -526824.83895273879, -1166938.0403368101 ], 
...
]
} 
},

我没有找到任何将这些数字转换为纬度/液化天然气的公式或方法。如果您有这方面的经验,请告诉我!

谢谢。

我已经测试了这个函数在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 ]
]
} 
};

要将其转换为带有纬度/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);

相关内容

最新更新