python基于经纬度距离汇聚点_用Python / Shapely聚合地理点的最佳方法

我想将一长串纬度/经度坐标转换为它们所属的美国州(或县).考虑到我具有状态几何,一种可能的解决方案是针对所有状态检查每个点.

for point in points:

for state in states:

if point.within(state['shape']):

print state.name

有没有更优化的方法来做到这一点,可能在O(1)?

最佳答案 使用

Rtree作为空间索引可以非常快速地识别零个或多个多边形的边界框中的点,然后使用Shapely确定该点所在的多边形.

from shapely.geometry import Polygon, Point

from rtree import index

# List of non-overlapping polygons

polygons = [

Polygon([(0, 0), (0, 1), (1, 1), (0, 0)]),

Polygon([(0, 0), (1, 0), (1, 1), (0, 0)]),

]

# Populate R-tree index with bounds of polygons

idx = index.Index()

for pos, poly in enumerate(polygons):

idx.insert(pos, poly.bounds)

# Query a point to see which polygon it is in

# using first Rtree index, then Shapely geometry's within

point = Point(0.5, 0.2)

poly_idx = [i for

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值