사용자 정의 공간 연산 적용

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

Joris Van den Bossche

Open source software developer and teacher, GeoPandas maintainer

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

각 도시 반경 50km 내 총 하천 길이?

단일 포인트(cairo)에 대해:

area = cairo.buffer(50000)

rivers_within_area = rivers.intersection(area)
print(rivers_within_area.length.sum() / 1000)
186.397219642
Python으로 지리공간 데이터 다루기

apply() 메서드

Series.apply(): 시리즈의 각 값에 함수를 호출

Series.apply(function, **kwargs)

  • function: 각 값에 호출할 함수; 값은 첫 번째 인수로 전달됨
  • **kwargs: 함수에 전달할 추가 인수

GeoSeries의 경우, 각 geom에 대해 function(geom, **kwargs)로 호출됨

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

사용자 정의 공간 연산 적용

적용할 함수:

def river_length(geom, rivers):
    area = geom.buffer(50000)
    rivers_within_area = rivers.intersection(area)
    return rivers_within_area.length.sum() / 1000

단일 지오메트리에 호출:

river_length(cairo, rivers=rivers)
186.3972196423455

모든 도시에 적용:

cities.geometry.apply(river_length, rivers=rivers)
Python으로 지리공간 데이터 다루기

사용자 정의 공간 연산 적용

모든 도시에 적용:

cities.geometry.apply(river_length, rivers=rivers)
0        0.000000
1        0.000000
2      106.072198
          ...    
Python으로 지리공간 데이터 다루기

사용자 정의 공간 연산 적용

모든 도시에 적용하고 결과를 새 열에 저장:

cities['river_length'] = cities.geometry.apply(river_length, rivers=rivers)
cities.head()
             name                       geometry  river_length
0    Vatican City    POINT (1386304.6 5146502.5)      0.000000
1      San Marino    POINT (1385011.5 5455558.1)      0.000000
2           Vaduz    POINT (1059390.7 5963928.5)    106.072198
..            ...                            ...           ...
Python으로 지리공간 데이터 다루기

Laten we oefenen!

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

Preparing Video For Download...