最寄りのレストランはどれくらい遠いですか?
エッフェル塔の近くにあるレストランに興味があるとします。調べるために、エッフェル塔そのものと、1km 圏内のレストランを可視化してみましょう。
そのために、各レストランからエッフェル塔までの距離を計算します。この結果に基づいて、レストランが 1km 以内なら True、それ以外は False となるマスクを作成し、それを使って restaurants の GeoDataFrame をフィルタリングします。最後に、このサブセットを可視化します。
restaurants GeoDataFrame は読み込まれており、eiffel_tower オブジェクトも作成済みです。さらに、matplotlib、GeoPandas、contextily がインポートされています。
この演習はコースの一部です
Pythonで扱う地理空間データ
演習の手順
- 各レストランからエッフェル塔までの距離を計算し、結果を
dist_eiffelと名付けます。 - 最も近いレストランまでの距離(
dist_eiffelの最小値)を出力します。 - エッフェル塔までの距離が 1 km 未満の行を
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()