簡體   English   中英

Geopandas:多邊形和點之間的差異()方法

[英]Geopandas : difference() methode between polygon and points

我正在測試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筆記本顯示的結果:

多邊形和點

現在,目標是刪除綠色圓圈內的紅點。 要做到這一點,我認為差異方法就足夠了。 但是當我寫道:

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

結果是......沒有改變points_df:

沒有變化

我想,difference()方法僅適用於多邊形GeoDataFrames,並且點和多邊形之間的混合不可行。 但也許我錯過了什么!

在這種情況下,測試圓中一個點的存在的函數是否優於差分方法?

我想,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)

問題

讓我們知道如何確定一個點是在多邊形的內部還是外部的問題...實現這一點的一種方法是加入多邊形內的所有點,並創建一個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

問題解決了 ;)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM