如何计算形状相交的多边形数量? [英] How can I count the number of polygons a shape intersects?

查看:157
本文介绍了如何计算形状相交的多边形数量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常大的数据集,上面有一个多边形,点周围有缓冲区.我想在点数据中创建一个新列,其中包括点缓冲区相交的多边形数量.

I have a very large dataset with a polygons and points with buffers around them. I would like to creat a new column in the points data which includes the number of polygons that point's buffer intersects.

这里有一个简化的示例:

Heres a simplified example:

import pandas as pd
import geopandas as gp
from shapely.geometry import Polygon
from shapely.geometry import Point
import matplotlib.pyplot as plt

## Create polygons and points ##

df = gp.GeoDataFrame([['a',Polygon([(1, 0), (1, 1), (2,2), (1,2)])],
                     ['b',Polygon([(1, 0.25), (2,1.25), (3,0.25)])]],
                     columns = ['name','geometry'])
df = gp.GeoDataFrame(df, geometry = 'geometry')

points = gp.GeoDataFrame( [['box', Point(1.5, 1.115), 4],
                        ['triangle', Point(2.5,1.25), 8]],
                     columns=['name', 'geometry', 'value'], 
                     geometry='geometry')

##Set a buffer around the points##

buf = points.buffer(0.5)
points['buffer'] = buf
points = points.drop(['geometry'], axis = 1) 
points = points.rename(columns = {'buffer': 'geometry'})

此数据如下所示: 我想做的是在点数据框中创建另一列,其中包括点相交的多边形数量.

This data looks like this: What I'd like to do is create another column in the points dataframe that includes the number of polygons that point intersects.

我已经尝试过使用for循环:

I've tried utilising a for loop as such:

points['intersect'] = []
for geo1 in points['geometry']:
    for geo2 in df['geometry']:
        if geo1.intersects(geo2):
            points['intersect'].append('1')

然后我将求和得出相交的总数. 但是,我得到一个错误:值的长度与索引的长度不匹配".我知道这是因为它试图将三行数据分配给只有两行的帧.

Which I would then sum to get the total number of intersects. However, I get the error: 'Length of values does not match length of index'. I know this is because it is attempting to assign three rows of data to a frame with only two rows.

我该如何对计数进行计数,使第一个点的值为2,第二个点的值为1?

How can I aggrigate the counts so the first point is assigned a value of 2 and the second a value of 1?

推荐答案

如果您有大型数据集,我将使用rtree空间索引来寻求解决方案,诸如此类.

If you have large dataset, I would go for solution using rtree spatial index, something like this.

import pandas as pd
import geopandas as gp
from shapely.geometry import Polygon
from shapely.geometry import Point
import matplotlib.pyplot as plt

## Create polygons and points ##

df = gp.GeoDataFrame([['a',Polygon([(1, 0), (1, 1), (2,2), (1,2)])],
                     ['b',Polygon([(1, 0.25), (2,1.25), (3,0.25)])]],
                     columns = ['name','geometry'])
df = gp.GeoDataFrame(df, geometry = 'geometry')

points = gp.GeoDataFrame( [['box', Point(1.5, 1.115), 4],
                        ['triangle', Point(2.5,1.25), 8]],
                     columns=['name', 'geometry', 'value'], 
                     geometry='geometry')

# generate spatial index
sindex = df.sindex
# define empty list for results
results_list = []
# iterate over the points
for index, row in points.iterrows():
    buffer = row['geometry'].buffer(0.5)  # buffer
    # find approximate matches with r-tree, then precise matches from those approximate ones
    possible_matches_index = list(sindex.intersection(buffer.bounds))
    possible_matches = df.iloc[possible_matches_index]
    precise_matches = possible_matches[possible_matches.intersects(buffer)]
    results_list.append(len(precise_matches))
# add list of results as a new column
points['polygons'] = pd.Series(results_list)

这篇关于如何计算形状相交的多边形数量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆