래스터 레이어에서 정보 추출하기
이제 벡터 파일을 바탕으로 래스터 레이어에서 정보를 추출해 보겠습니다. 이 기능은 rasterstats 패키지가 제공합니다. 이번 연습에서는 채굴지 데이터셋의 각 지점에 대해 가장 가까운 래스터 픽셀 값을 가져와, 모든 채굴지의 식생 유형을 판별하겠습니다.
채굴지 데이터셋의 일부(mining_sites)는 이미 로드되어 있으며, GeoPandas와 matplotlib도 이미 임포트되어 있습니다.
이 연습은 강의의 일부입니다
Python으로 지리공간 데이터 다루기
연습 안내
rasterstats패키지를 임포트하세요.rasterstats.point_query()함수를 사용해 채굴지 위치(mining_sites)의 가장 가까운 래스터 값을 추출하고, 이를 새 열'vegetation'에 할당하세요.'vegetation'열을 기준으로 점의 색을 지정해 채굴지 데이터를 플로팅하세요. 범례도 함께 표시하세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
# Import the rasterstats package
____
# Extract the nearest value in the raster for all mining sites
vegetation_raster = "central_africa_vegetation_map_foraf.tif"
mining_sites['vegetation'] = ____(____, ____, interpolate='nearest')
print(mining_sites.head())
# Replace numeric vegation types codes with description
mining_sites['vegetation'] = mining_sites['vegetation'].replace(vegetation_types)
# Make a plot indicating the vegetation type
____
plt.show()