You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何排序地理坐标以生成直线最多的规整多边形?

多边形坐标排序问题

我拥有一组可构成多边形的地理坐标,目标是生成直线尽可能多的规整多边形并将其绘制到地图上。我尝试过坐标排序的代码,但未得到预期结果。

示例代码:

import math
import numpy as np
from statistics import mean
import folium

def order_points(pp):
    lat, lon = zip(*pp)
    coords = np.array(pp)
    centroid = (mean(lat), mean(lon))
    
    # 计算质心
    cent = (sum([p[0] for p in pp])/len(pp), sum([p[1] for p in pp])/len(pp))
    # 按极角排序
    pp.sort(key=lambda p: math.atan2(p[1]-cent[1], p[0]-cent[0]))
    
    return pp

polygon_coordinates = [
    (40.5332064016964, -8.78427738454133),
    (41.25358575070879, -8.662562329821816),
    (41.34395074064536, -8.78162348005526),
    (41.34366188895376, -8.662097742704521),
    (41.3427118223502, -8.423053362078234),
    (41.25263867182198, -8.423846579910334),
    (41.339322521192834, -7.945013920157079),
    (40.52870776612322, -7.957832320269227)
]

m = folium.Map(location=(41.25387369401857, -8.781923733405996), zoom_start=12)
polygon_coordinates_reorder = order_points(polygon_coordinates)
polygon = folium.Polygon(locations=polygon_coordinates_reorder, color='red', fill_color='red')
m.save("17_05_map.html")
m

当前代码生成的多边形效果:
当前多边形效果

期望得到的规整多边形效果(黑色轮廓样式):
期望多边形效果


内容的提问来源于stack exchange,提问作者Beatriz Santos

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 06:10:24