【问题标题】:Move polygon points based on center location?根据中心位置移动多边形点?
【发布时间】:2018-04-21 10:49:10
【问题描述】:

有人会举一个例子吗?使用 Turf 根据新的中心点将所有多边形点从一个位置移动到另一个位置?

例如,假设我根据最初创建点的位置计算了一个中心。

然后我需要根据计算出的中心将这些点移动到新位置。

一如既往,任何帮助都会很棒!

【问题讨论】:

    标签: turfjs


    【解决方案1】:

    你需要使用transformTranslate

    var poly = turf.polygon([[[0,29], [3.5,29], [2.5,32], [0,29]]]);
    
    //calculates the centroid of the polygon
    var center = turf.centroid(poly);
    
    var from = turf.point([center.geometry.coordinates[0], center.geometry.coordinates[1]]);
    
    var to = turf.point([4, 35]);
    
    //finds the bearing angle between the points
    var bearing = turf.rhumbBearing(from, to);
    
    //calculates the distance between the points
    var distance = turf.rhumbDistance(from, to);
    
    //moves the polygon to the distance on the direction angle.
    var translatedPoly = turf.transformTranslate(poly, distance, bearing)
    

    【讨论】:

    • 哇,谢谢!你能解释一下每一行发生了什么吗?
    • 我已经修复了代码并添加了一些 cmets。请检查。
    • 谢谢你!这非常有帮助。这适用于线条、范围、圆圈和省略号吗?
    • 它应该适用于任何 geojson 特征或几何图形。
    • @AnatolySukhanov 如果我需要获取保存在我的数据库中的最近位置,我正在使用 turf.nearestPoint,他们只返回我位置附近的 一个点,如何获取距离我所在位置约 50 公里的所有点
    猜你喜欢
    • 2015-10-16
    • 2021-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-02
    • 2013-11-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多