简体   繁体   中英

Geopandas : difference() methode between polygon and points

I'm testing geopandas to make something quite simple : use t he difference method to delete some points of a GeoDataFrame that are inside a circle.

Here's the begining of my script :

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

Here's the first rows of 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)

Then, I add a point that will contain several points of the first 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')

Here's the result displayed by the iPython notebook :

多边形和点

Now, the goal is to delete red points inside the green circle. To do that, I thought that difference method will be enough. But when I write :

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

The result is that... nothing changed with points_df :

没有变化

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

Will a function to test the presence of a point in the circle be better than the difference method in this case ?

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

That seems to be the issue, you cant use the overlay with points.

And also for that kind of spatial operation a simple spatial join seems to be the easiest solution.

Starting with the last example ;):

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

问题

Leaves us with the problem on how to determine if a point is inside or outside of the polygon... one way of achieving that is to Join all points inside the polygon, and create a DataFrame with the difference between all points and points within the circle:

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

Total points: 35

Points inside circle: 10

Points outside circle: 25

问题解决了 ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM