開始使用免費開始

最近的餐廳有多遠?

現在,我們想找找看艾菲爾鐵塔附近有哪些餐廳。為了探索這些餐廳,先把艾菲爾鐵塔本身,以及 1 公里內的餐廳視覺化。

要做到這點,我們可以先計算每家餐廳到艾菲爾鐵塔的距離。根據這個結果,再建立一個遮罩:若餐廳在 1 公里內則為 True,否則為 False,並用它來篩選 restaurants 這個 GeoDataFrame。最後,將這個子集合視覺化。

restaurants GeoDataFrame 已載入,eiffel_tower 物件也已建立。此外,matplotlib、GeoPandas 與 contextily 也都已匯入。

本練習屬於課程

在 Python 中處理地理空間資料

檢視課程

練習說明

  • 計算每家餐廳到艾菲爾鐵塔的距離,並將結果命名為 dist_eiffel
  • 印出最近餐廳的距離(也就是 dist_eiffel 的最小值)。
  • restaurants 這個 GeoDataFrame 中,選出到艾菲爾鐵塔距離小於 1 公里的列(注意距離單位為公尺)。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# 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()
編輯並執行程式碼