计算 geopandas 中多边形网格上的每个线交点
Counting each line intersection on a grid of polygons in geopandas
我有一个大型数据集 (~20000),包含过去 40 年的风暴,其中包含 3 小时间隔的中心点列表。我正在尝试将网格覆盖到一个大区域上,我想从中计算每场风暴经过任何给定网格单元的次数,但是我当前的实现只跟踪这三个小时间隔的位置,导致在某些情况下曲目跳格space,而它也应该被计算在内。
我正在尝试使用 geopandas 来解决这个问题,而不是为每个风暴轨道创建一个线系列,然后对网格执行交集,但是,我找不到任何允许我这样做的功能实现。
为了在 geopandas 中创建网格,我使用了上一个问题中的 :
lonCount = ((plotExtent[1]+360) - (plotExtent[0]+360)) * gridResolution
latCount = ((plotExtent[3]) - (plotExtent[2])) * gridResolution
lons = np.linspace(plotExtent[0], plotExtent[1], lonCount)
lats = np.linspace(plotExtent[2], plotExtent[3], latCount)
# Store the meshgrid in polygon format
xlines = [((x1, yi), (x2, yi)) for x1, x2 in zip(lons[:-1], lons[1:]) for yi in lats]
ylines = [((xi, y1), (xi, y2)) for y1, y2 in zip(lats[:-1], lats[1:]) for xi in lons]
# Save as a Shapely object, then store in geopandas
grids = list(polygonize(MultiLineString(xlines + ylines)))
polyFrame = gpd.GeoDataFrame(grids)
这将创建一个包含约 5600 个多边形对象的 geoDataSeries。然后我遍历我的每个风暴对象以去除 lat/lon 列表对,并将它们转换成一个匀称的 LineSeries 对象,然后将其读入 geopandas 中:
polyLine = LineString(list(zip(storm_lons, storm_lats)))
coord_tests = gpd.GeoSeries(polyLine)
我的目标是简单地做这样的事情:
I = coord_tests.intersects(polyFrame)
要收集与LineString相交的多边形列表,但是,这会提示以下错误:
AttributeError: No geometry data set yet (expected in column 'geometry'.)
我想知道是不是我这里的格式有误,是否错误地将调用传递给了这个函数,或者是否有更有效的方法来完成我在这里想做的事情。
如有任何帮助,我们将不胜感激。
谢谢!
polyFrame = gpd.GeoDataFrame(geometry=grids)
:-)
我有一个大型数据集 (~20000),包含过去 40 年的风暴,其中包含 3 小时间隔的中心点列表。我正在尝试将网格覆盖到一个大区域上,我想从中计算每场风暴经过任何给定网格单元的次数,但是我当前的实现只跟踪这三个小时间隔的位置,导致在某些情况下曲目跳格space,而它也应该被计算在内。
我正在尝试使用 geopandas 来解决这个问题,而不是为每个风暴轨道创建一个线系列,然后对网格执行交集,但是,我找不到任何允许我这样做的功能实现。
为了在 geopandas 中创建网格,我使用了上一个问题中的
lonCount = ((plotExtent[1]+360) - (plotExtent[0]+360)) * gridResolution
latCount = ((plotExtent[3]) - (plotExtent[2])) * gridResolution
lons = np.linspace(plotExtent[0], plotExtent[1], lonCount)
lats = np.linspace(plotExtent[2], plotExtent[3], latCount)
# Store the meshgrid in polygon format
xlines = [((x1, yi), (x2, yi)) for x1, x2 in zip(lons[:-1], lons[1:]) for yi in lats]
ylines = [((xi, y1), (xi, y2)) for y1, y2 in zip(lats[:-1], lats[1:]) for xi in lons]
# Save as a Shapely object, then store in geopandas
grids = list(polygonize(MultiLineString(xlines + ylines)))
polyFrame = gpd.GeoDataFrame(grids)
这将创建一个包含约 5600 个多边形对象的 geoDataSeries。然后我遍历我的每个风暴对象以去除 lat/lon 列表对,并将它们转换成一个匀称的 LineSeries 对象,然后将其读入 geopandas 中:
polyLine = LineString(list(zip(storm_lons, storm_lats)))
coord_tests = gpd.GeoSeries(polyLine)
我的目标是简单地做这样的事情:
I = coord_tests.intersects(polyFrame)
要收集与LineString相交的多边形列表,但是,这会提示以下错误:
AttributeError: No geometry data set yet (expected in column 'geometry'.)
我想知道是不是我这里的格式有误,是否错误地将调用传递给了这个函数,或者是否有更有效的方法来完成我在这里想做的事情。
如有任何帮助,我们将不胜感激。
谢谢!
polyFrame = gpd.GeoDataFrame(geometry=grids)
:-)