티스토리 뷰

728x90
반응형

 

geopandas 는 pip install geopandas로 설치되지 않습니다 !!

연관된 모든 라이브러리를 설치해야 하며,

서치해보니 라이브러리들을 순서대로 설치하지 않으면 에러가 난다고 하네요. 

 

저는 아래 블로그를 참고하여 설치하였습니다

https://blog.naver.com/PostView.nhn?blogId=kokoyou7620&logNo=222175644751 

 

geopandas설치 에러 총 정리!! ERROR: Command errored out with exit status 1

Anaconda pompt에서 geopandas를 설치하면 이런 에러가 발생하는것을 볼 수 있습니다. 제가 다른 에러도 ...

blog.naver.com

 

 

 

 polygon 데이터를 준비합니다

저의 경우, mongoDB에 데이터를 삽입해놓았습니다

GID마다 geometry를 가지고 있어요

 

 

 

 

포함되는지 확인하고 싶은 데이터에는 경도, 위도가 있습니다.

요 경도, 위도가 포함되는 geometry를 가진 GID를 가져오고자 합니다

 

 

 

두 데이터를 몽고DB에서 불러와 pandas DataFrame으로 만들었습니다

geopandas DataFrame으로 만드려는 경우 geometry 컬럼이 필수로 존재해야합니다

 

from shapely.geometry import shape, Point
from shapely.geometry.polygon import Polygon


# GID CSV 파일
celldata = collection.find({}, {'_id':0})
cellfile = pd.DataFrame(celldata)
cellfile['geometry'] = cellfile.apply(lambda c: shape(c.geometry), axis=1)
gdf = gpd.GeoDataFrame(cellfile, crs='epsg:4326')

file['geometry'] = file.apply(lambda c: Point(c['경도'], c['위도']), axis=1)
file = gpd.GeoDataFrame(file, crs='epsg:4326')

 

 

sjoin (spatial join)으로  file 데이터에 GID를 생성해봅니다
left에 GID를 가진 gdf 데이터를 넣었고, right에 경도,위도 데이터를 넣었으므로
how에는 "right" 값을 넣었습니다

op에는 경도, 위도가 어느 geometry에 포함되는지 확인하도록 contains를 넣습니다
op에는 contains 말고도 within, intersects가 있습니다

 

file = gpd.sjoin(gdf, file, how='right', op="contains")
file = file.drop(["geometry", "index_left"], axis=1)

 

실행하고 나면
file에 있는 경도, 위도데이터에 매칭되는,  gdf의 geometry, GID가 생성되어있습니다
저는 경도, 위도가 포함된 GID를 가져오려고 사용한 것이므로 컬럼을 드랍해주면 끝!

 

728x90
반응형
댓글