가장 가까운 레스토랑까지 얼마나 멀까요?
이번에는 에펠탑 근처의 레스토랑에 관심이 있다고 해볼게요. 이를 살펴보기 위해, 에펠탑 자체와 반경 1km 이내의 레스토랑을 시각화해 보겠습니다.
이를 위해 각 레스토랑에서 에펠탑까지의 거리를 계산할 수 있어요. 이 결과를 바탕으로, 레스토랑이 1km 이내이면 True, 그렇지 않으면 False가 되는 마스크를 만들어 restaurants GeoDataFrame을 필터링합니다. 마지막으로, 이렇게 얻은 부분집합을 시각화해요.
restaurants GeoDataFrame은 이미 로드되어 있고, eiffel_tower 객체도 생성되어 있어요. 또한 matplotlib, GeoPandas, contextily가 임포트되어 있습니다.
이 연습은 강의의 일부입니다
Python으로 지리공간 데이터 다루기
연습 안내
- 각 레스토랑에서 에펠탑까지의 거리를 계산하고 결과를
dist_eiffel이라고 하세요. - 가장 가까운 레스토랑까지의 거리(즉,
dist_eiffel의 최솟값)를 출력하세요. - 에펠탑까지의 거리가 1km 미만인 행만
restaurantsGeoDataFrame에서 선택하세요(거리는 미터 단위임에 유의하세요).
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
# The distance from each restaurant to the Eiffel Tower
dist_eiffel = ____
# The distance to the closest restaurant
print(dist_eiffel.____)
# Filter the restaurants for closer than 1 km
restaurants_eiffel = ____
# Make a plot of the close-by restaurants
ax = restaurants_eiffel.plot()
geopandas.GeoSeries([eiffel_tower]).plot(ax=ax, color='red')
contextily.add_basemap(ax)
ax.set_axis_off()
plt.show()