2016-11-26 104 views
2

我正在測試geopandas以使某些事情變得非常簡單:使用t he difference method可以刪除圓內的GeoDataFrame的某些點。Geopandas:多邊形和點之間的差異()方法

這裏是我的腳本的開頭:

%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

我猜差()方法與多邊形GeoDataFrames只能之間的組合點和多邊形是不可能的。但也許我錯過了一些東西!

在這種情況下,測試圓中點的存在的函數是否會比差異方法更好?

回答

2

我想差異()方法只適用於多邊形 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) 

Problem

留給我們如何確定一個點是否是內部或多邊形外的問題...實現這一目標的方法之一是加入所有點多邊形內部,並創建一個數據框與圓內的所有點與點之間的區別:

# 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

Problem solved ;)

+0

再次謝謝schlump ;-)!我有一個問題在我的機器上安裝rtree,但我相信你的答案是好的,所以我驗證它:-) – Raphadasilva

+0

謝謝:)。 Yeha我也有一些輕微的問題讓Geopandas在一切順利運行... – schlump