nodejs利用JSTS提取Geometry中心点坐标

305 阅读1分钟

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 }