Shapely 지오메트리와 공간 관계

Python으로 지리공간 데이터 다루기

Dani Arribas-Bel

Geographic Data Science Lab (University of Liverpool)

스칼라 지오메트리 값

cities = geopandas.read_file("ne_110m_populated_places.shp")
cities.head()
           name                                      geometry
0  Vatican City   POINT (12.45338654497177 41.90328217996012)
1    San Marino     POINT (12.44177015780014 43.936095834768)
2         Vaduz   POINT (9.516669472907267 47.13372377429357)
3       Lobamba  POINT (31.19999710971274 -26.46666746135247)
4    Luxembourg   POINT (6.130002806227083 49.61166037912108)
brussels = cities.loc[170, 'geometry']
print(brussels)
POINT (4.33137074969045 50.83526293533032)
Python으로 지리공간 데이터 다루기

스칼라 지오메트리 값

brussels = cities.loc[170, 'geometry']
print(brussels)
POINT (4.33137074969045 50.83526293533032)
type(brussels)
shapely.geometry.point.Point
Python으로 지리공간 데이터 다루기

Shapely 파이썬 패키지

type(brussels)
shapely.geometry.point.Point

Shapely

  • 기하 객체를 조작·분석하는 Python 패키지
  • Point, LineString, Polygon 객체 제공
  • GeoSeries(GeoDataFrame의 'geometry' 열)는 Shapely 객체로 구성됨
Python으로 지리공간 데이터 다루기

지오메트리 객체

GeoDataFrame에서 가져오기:

brussels = cities.loc[170, 'geometry']
paris = cities.loc[235, 'geometry']
belgium = countries.loc[countries['name'] == 'Belgium', 'geometry'].squeeze()
france = countries.loc[countries['name'] == 'France', 'geometry'].squeeze()
uk = countries.loc[countries['name'] == 'United Kingdom', 'geometry'].squeeze()

수동 생성:

from shapely.geometry import Point
p = Point(1, 2)
print(p)
POINT (1 2)
Python으로 지리공간 데이터 다루기

공간 메서드

지오메트리의 면적:

belgium.area
3.8299974609075753

두 지오메트리 간 거리:

brussels.distance(paris)
2.8049127723186214

그 외 다수! (예: centroid, simplify 등)

Python으로 지리공간 데이터 다루기

공간 관계

geopandas.GeoSeries([belgium, france, uk, paris,  brussels, line]).plot()

Python으로 지리공간 데이터 다루기

공간 관계

belgium.contains(brussels)
True
france.contains(brussels)
False
brussels.within(belgium)
True
belgium.touches(france)
True
line.intersects(france)
True
line.intersects(uk)
False
Python으로 지리공간 데이터 다루기

Ayo berlatih!

Python으로 지리공간 데이터 다루기

Preparing Video For Download...