Geopandas:多边形和点之间的差异(difference())方法

4

我正在测试geopandas,以制作一些非常简单的东西:使用差异方法删除位于圆形内部的GeoDataFrame的某些点。

这是我的脚本开头:

%matplotlib inline
# previous line is because I used ipynb
import pandas as pd
import geopandas as gp
from shapely.geometry import Point
[...]
points_df = gp.GeoDataFrame(csv_file, crs=None, geometry=geometry)

这是points_df的第一行:

    Name        Adress      geometry
0   place1      street1     POINT (6.182674 48.694416)
1   place2      street2     POINT (6.177306 48.689889)
2   place3      street3     POINT (6.18 48.69600000000001)
3   place4      street4     POINT (6.1819 48.6938)
4   place5      street5     POINT (6.175694 48.690833)

然后,我会添加一个包含第一个GeoDF的多个点的点。
base = points_df.plot(marker='o', color='red', markersize=5)

center_coord = [Point(6.18, 48.689900)]
center = gp.GeoDataFrame(crs=None, geometry=center_coord)
center.plot(ax=base, color = 'blue',markersize=5)

circle = center.buffer(0.015)
circle.plot(ax=base, color = 'green')

以下是 iPython 笔记本显示的结果:

Polygon and points

现在的目标是删除绿色圆圈内的红点。为了达到这个目的,我认为差分方法已经足够了。但是当我写下以下代码时:
selection = points_df['geometry'].difference(circle)
selection.plot(color = 'green', markersize=5)

结果是... points_df 没有任何变化:

No changes

我猜测difference()方法仅适用于多边形GeoDataFrames,而点和多边形的混合不可能。但也许我错过了什么!
在这种情况下,检测一个点是否在圆内的函数会比difference方法更好吗?
1个回答

5
我猜测difference()方法仅适用�多边形GeoDataFrames,点�多边形之间的混���能。

这似�是个问题,你无法在点上使用�加。

对�这�空间�作,最简�的解决方案似�是简�的空间��。

�最�一个例�开始😉:

%matplotlib inline
import pandas as pd
import geopandas as gp
import numpy as np
import matplotlib.pyplot as plt
from shapely.geometry import Point

# Create Fake Data
df = pd.DataFrame(np.random.randint(10,20,size=(35, 3)), columns=['Longitude','Latitude','data'])

# create Geometry series with lat / longitude
geometry = [Point(xy) for xy in zip(df.Longitude, df.Latitude)]

df = df.drop(['Longitude', 'Latitude'], axis = 1)

# Create GeoDataFrame
points = gp.GeoDataFrame(df, crs=None, geometry=geometry)

# Create Matplotlib figure
fig, ax = plt.subplots()

# Set Axes to equal (otherwise plot looks weird)
ax.set_aspect('equal')

# Plot GeoDataFrame on Axis ax
points.plot(ax=ax,marker='o', color='red', markersize=5)

# Create new point
center_coord = [Point(15, 13)]
center = gp.GeoDataFrame(crs=None, geometry=center_coord)

# Plot new point
center.plot(ax=ax,color = 'blue',markersize=5)
# Buffer point and plot it
circle = gp.GeoDataFrame(crs=None, geometry=center.buffer(2.5))

circle.plot(color = 'white',ax=ax)

留给我们的问题是如何确定一个点是否在多边形内部或者外部......其中一种实现方法是连接多边形内部的所有点,并创建一个数据帧,其中包含所有点与圆内点之间的差异:

问题

# Calculate the points inside the circle 

pointsinside = gp.sjoin(points,circle,how="inner")

# Now the points outside the circle is just the difference 
# between  points and points inside (see the ~)

pointsoutside = points[~points.index.isin(pointsinside.index)]


# Create a nice plot 
fig, ax = plt.subplots()
ax.set_aspect('equal')
circle.plot(color = 'white',ax=ax)
center.plot(ax=ax,color = 'blue',markersize=5)
pointsinside.plot(ax=ax,marker='o', color='green', markersize=5)

pointsoutside.plot(ax=ax,marker='o', color='yellow', markersize=5)

print('Total points:' ,len(points))
print('Points inside circle:' ,len(pointsinside))
print('Points outside circle:' ,len(pointsoutside))

总分数:35

圆内得分:10

圆外得分:25

问题已解决 ;)


再次感谢您,schlump ;-)!我在我的机器上安装rtree时遇到了问题,但我相信您的答案是正确的,所以我会验证它 :-) - Raphadasilva
谢谢:)我也遇到了一些小问题,让Geopandas与所有东西顺利运行。 - schlump

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接