【发布时间】:2017-11-21 04:16:36
【问题描述】:
为什么这不返回每个邻域(边界框)中的点数?
import geopandas as gpd
def radius(points_neighbour, points_center, new_field_name, r):
"""
:param points_neighbour:
:param points_center:
:param new_field_name: new field_name attached to points_center
:param r: radius around points_center
:return:
"""
sindex = points_neighbour.sindex
pts_in_neighbour = []
for i, pt_center in points_center.iterrows():
nearest_index = list(sindex.intersection((pt_center.LATITUDE-r, pt_center.LONGITUDE-r, pt_center.LATITUDE+r, pt_center.LONGITUDE+r)))
pts_in_this_neighbour = points_neighbour[nearest_index]
pts_in_neighbour.append(len(pts_in_this_neighbour))
points_center[new_field_name] = gpd.GeoSeries(pts_in_neighbour)
每个循环都给出相同的结果。
第二个问题,如何找到第 k 个最近的邻居?
有关问题本身的更多信息:
我们正在以非常小的规模进行,例如美国华盛顿州或加拿大不列颠哥伦比亚省
我们希望尽可能多地利用 geopandas,因为它与 pandas 类似,并且支持空间索引:RTree
比如这里的sindex有方法最近,交集等
如果您需要更多信息,请发表评论。这是 GeoPandasBase 类中的代码
@property
def sindex(self):
if not self._sindex_generated:
self._generate_sindex()
return self._sindex
我尝试了 Richard 的示例,但没有成功
def radius(points_neighbour, points_center, new_field_name, r):
"""
:param points_neighbour:
:param points_center:
:param new_field_name: new field_name attached to points_center
:param r: radius around points_center
:return:
"""
sindex = points_neighbour.sindex
pts_in_neighbour = []
for i, pt_center in points_center.iterrows():
pts_in_this_neighbour = 0
for n in sindex.intersection(((pt_center.LATITUDE-r, pt_center.LONGITUDE-r, pt_center.LATITUDE+r, pt_center.LONGITUDE+r))):
dist = pt_center.distance(points_neighbour['geometry'][n])
if dist < radius:
pts_in_this_neighbour = pts_in_this_neighbour + 1
pts_in_neighbour.append(pts_in_this_neighbour)
points_center[new_field_name] = gpd.GeoSeries(pts_in_neighbour)
要下载形状文件,请转到 https://catalogue.data.gov.bc.ca/dataset/hellobc-activities-and-attractions-listing 并选择 ArcView 下载
【问题讨论】:
-
你能发布你用来生成rtree的代码吗?
-
@Richard points_neighbour.sindex 这是你想要的吗?
-
是的,应该就是这样。
-
只是想知道您是否有
points_neighbour.sindex代码? -
@Richard 看到这个geoffboeing.com/2016/10/r-tree-spatial-index-python
标签: gis geospatial spatial-index r-tree geopandas