래스터 데이터 다루기

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

Joris Van den Bossche

Open source software developer and teacher, GeoPandas maintainer

값 격자 그림

이미지 출처: QGIS 문서

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

래스터 데이터

강수 확률 지도

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

다중 밴드 래스터 데이터

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

rasterio 패키지

import rasterio
  • GDAL에 대한 "파이써닉" 바인딩
  • 래스터 파일 읽기쓰기
  • 처리 도구(마스킹, 재투영, 리샘플링 등)

https://rasterio.readthedocs.io/en/latest/

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

래스터 파일 열기

import rasterio

src = rasterio.open("DEM_world.tif")

메타데이터:

src.count
1
src.width, src.height
(4320, 2160)
Python으로 지리공간 데이터 다루기

래스터 데이터 = numpy 배열

array = src.read()

표준 numpy 배열:

array
array([[[-4290, -4290, -4290, ..., -4290, -4290, -4290],
        [-4278, -4278, -4278, ..., -4278, -4278, -4278],
        [-4269, -4269, -4269, ..., -4269, -4269, -4269],
        ...,
        [ 2804,  2804,  2804, ...,  2804,  2804,  2804],
        [ 2804,  2804,  2804, ...,  2804,  2804,  2804],
        [ 2804,  2804,  2804, ...,  2804,  2804,  2804]]], dtype=int16)
Python으로 지리공간 데이터 다루기

래스터 데이터셋 시각화

rasterio.plot.show() 사용:

import rasterio.plot

rasterio.plot.show(src, cmap='terrain')

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

벡터 데이터로 정보 추출

rasterstats: 벡터 지오메트리를 기반으로 래스터 데이터의 요약 통계 제공 (https://github.com/perrygeo/python-rasterstats)

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

rasterstats로 래스터 값 추출

  • 포인트 벡터:

    rasterstats.point_query(geometries, "path/to/raster", 
                            interpolation='nearest'|'bilinear')
    
  • 폴리곤 벡터:

    rasterstats.zonal_stats(geometries, "path/to/raster",
                            stats=['min', 'mean', 'max'])
    
Python으로 지리공간 데이터 다루기

rasterstats로 래스터 값 추출

result = rasterstats.zonal_stats(countries.geometry, "DEM_gworld.tif", 
                                 stats=['mean'])

countries['mean_elevation'] = pd.DataFrame(result)
countries.sort_values('mean_elevation', ascending=False).head()
            name    continent                     geometry  mean_elevation
157   Tajikistan         Asia  POLYGON ((74.98 37.41, ...      3103.231105
85    Kyrgyzstan         Asia  POLYGON ((80.25 42.34, ...      2867.717142
24        Bhutan         Asia  POLYGON ((91.69 27.77, ...      2573.559846
119        Nepal         Asia  POLYGON ((81.11 30.18, ...      2408.907816
6     Antarctica   Antarctica  (POLYGON ((-59.57 -80.04...     2374.075028
..           ...          ...                          ...             ...
Python으로 지리공간 데이터 다루기

연습해 봅시다!

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

Preparing Video For Download...