2016-11-26 8 views
2

geopandas를 테스트하여 무언가를 아주 간단하게 만듭니다. t he difference method을 사용하여 원 안에있는 GeoDataFrame의 일부 지점을 삭제합니다.Geopandas : 다각형과 점 사이의 차이() 방법

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의 여러 지점을 포함하는 지점을 추가 : 여기에 points_df의 첫 번째 행의

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

: 여기

내 스크립트의 시작이다

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

이제 목표는 녹색 원 안의 빨간색 점을 삭제하는 것입니다. 그렇게하기 위해서 차이점이 충분할 것이라고 생각했습니다. 그러나 나는 쓸 때 :

No changes

내가 차이() 메소드는 다각형의 GeoDataFrames에서만 작동 추측과 혼합 사이 :

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

결과는 아무것도 points_df으로 변경 없음입니다 ... 점과 다각형은 분명하지 않습니다. 그러나 어쩌면 나는 무언가를 놓쳤다!

원의 한 점의 존재를 테스트하는 함수가이 경우의 차이 방법보다 더 좋습니까?

답변

2

차이점() 메서드는 다각형에서만 작동합니다. GeoDataFrame 및 점과 다각형 간의 혼합이 가능하지 않습니다.

그건 문제가되는 것 같아요, 당신은 포인트 오버레이를 사용할 수 없습니다.

또한 이러한 종류의 공간 작동을 위해 단순한 공간 결합이 가장 쉬운 솔루션 인 것처럼 보입니다. 마지막 예제로 시작

) :

%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 그것을 달성하는 방법 중 하나는 다각형 내부의 모든 점에 참여하고, 원 내의 모든 점과 점 사이의 차이와 DataFrame를 만드는 것입니다

포인트 밖에 원 : 25

Problem solved ;)

+0

다시 schlump 감사합니다 ;-)! 내 컴퓨터에 rtree를 설치하는 데 문제가 있지만 확실하게 답변을 드리기 위해 확신합니다 :-) – Raphadasilva

+0

감사합니다 :). Yeha i에는 Geopandas가 모든 항목을 원활하게 실행하는 데 약간의 문제가있었습니다 ... – schlump