Geopandas 密度栅格创建

问题描述 投票:0回答:1

我有一个 geopandas 数据框,其中包含任意纬度/经度点的点。 我想创建一个栅格数据集,以便规则网格中的每个点都包含特定半径的圆中包含的数据帧的元素数量。困难在于这个数字应该只包含特定列的唯一数字。

bb 是我的数据框,半径是距单元格中心的距离,我想要这些数字。

我目前正在使用此代码执行此操作:

   mappa = numpy.full((nrows, ncols), 0) 
   for col in range(ncols):
        for row in range(nrows):
            lat=row*cellsize+bbox[1]
            lon=col*cellsize+bbox[0]   
            ff=bb.cx[lon-radius:lon+radius,lat-radius:lat+radius]  #  those give the rows falling in the limitss
            mappa[row,col]=len(ff['flkey'].unique())/period     # with this i count only unique points respect to a column flkey

数据帧 bb 包含大量点,因此该方法非常慢。

我想改进仅在有足够数量的点中执行检查,因为地图中的大多数点都没有点。所以我想生成一个初步的密度图,然后仅分析密度显着的纬度/经度点。

是否有类似于 ESRI 函数 PointDensity_sa 的点密度函数?

谢谢

geopandas density-plot
1个回答
0
投票

下面使用 sklearn.neighbors.KDTree 进行从单元格中点到数据点的快速查询。 它产生从特定半径到网格单元中点存在的唯一值的数量。

import numpy as np
from sklearn.neighbors import KDTree

n = 100_00
cell_size = 0.01
radius = 0.05


coords = np.random.random_sample((n, 2))
values = np.random.randint(0, n//10, n)

x_axis = np.linspace(0, 1, int(1 / cell_size))
y_axis = np.linspace(0, 1, int(1 / cell_size))
xv, yv = np.meshgrid(x_axis, y_axis)
tree = KDTree(coords)

results = tree.query_radius(list(zip(xv.ravel(), yv.ravel())), radius)
unique_results = [len(np.unique(values[indices])) for indices in results]
grid_values = np.array(unique_results).reshape((len(x_axis), len(x_axis)))

这会产生如下所示的栅格: enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.