nodejs利用JSTS提取Geometry中心點座標
getCentroidPoint
const jsts = require('jsts')
/**
* 提取中心點座標
* @param {Object} geometry GeoJSON 的geometry屬性
*/
function getCentroid(geometry) {
const reader = new jsts.io.GeoJSONReader()
const geom = reader.read(geometry)
const centroid = new jsts.algorithm.Centroid(geom)
const centroidPoint = centroid.getCentroid()
return centroidPoint
}
const polygon = {
"type":"Feature",
"properties":{},
"geometry":{
"type":"Polygon",
"coordinates":
[
[
[100,30],
[116,30],
[116,32],
[100,32],
[100,30]
]
]
}
}
// 注意傳入的參數是GeoJSON的geometry屬性,且其type爲Polygon
const centroidPointOfPolygon = getCentroid(polygon.geometry)
console.log(centroidPointOfPolygon) //{ x: 108, y: 31, z: undefined }