nodejs利用JSTS提取Geometry中心點座標

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 }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章