解析WKT字符串以获得点数组

问题描述 投票:0回答:1
POLYGON ((162353.9901277053 564298.9605047705,162352.3101277038 564286.9905047683, 162353.9901277053 564298.9605047705))

输出需求

[[162353.9901277053, 564298.9605047705], [162352.3101277038, 564286.9905047683],[ 162353.9901277053, 564298.9605047705]]

我一直在看草皮,很遗憾没有找到

javascript leaflet turfjs wkt
1个回答
0
投票
<

您可以使用Terraformer.WKT.parse将WKT POLYGON转换为GeoJSON,然后从中获取坐标。这是可运行的代码。

// Use Terraformer.WKT.parse() to convert WKT to GeoJSON

var geojson_pgons = Terraformer.WKT.parse('POLYGON ((162353.9901277053 564298.9605047705,162352.3101277038 564286.9905047683, 162353.9901277053 564298.9605047705))');

// get coordinates list of the first object
var poly0xys = geojson_pgons.coordinates[0];

// collect what we need
var result = [];
for (let i=0; i<poly0xys.length; i++) {
    //console.log( poly0xys[i] );
    result.push(  poly0xys[i] );
}

console.log( result );
<script src="https://unpkg.com/[email protected]"></script>
<script src="https://unpkg.com/[email protected]"></script>
© www.soinside.com 2019 - 2024. All rights reserved.