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

Geopandas : difference() methode between polygon and points

我正在测试 geopandas 以使其变得非常简单:使用 the difference method 删除圆圈内的 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 笔记本显示的结果:

现在,目标是删除绿色圆圈内的红色点。为此,我认为差异方法就足够了。但是当我写的时候:

selection = points_df['geometry'].difference(circle)
selection.plot(color = 'green', markersize=5)

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

我猜 difference() 方法仅适用于多边形 GeoDataFrames,点和多边形之间的混合是不可能的。但也许我错过了什么!

在这种情况下,测试圆中是否存在一个点的函数是否会比差分法更好?

I guess that the difference() method works only with polygons GeoDataFrames and the mix between points and polygons is not posible.

这似乎是问题所在,您不能使用带点的叠加层。

而且对于这种空间操作,简单的空间连接似乎是最简单的解决方案。

从最后一个例子开始 ;):

%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)

留给我们的问题是如何确定一个点是在多边形内部还是外部...实现这一点的一种方法是连接多边形内部的所有点,并创建一个具有所有点之间差异的 DataFrame点与圆内点:

# 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