How far is the closest restaurant?
Now, we might be interested in the restaurants nearby the Eiffel Tower. To explore them, let's visualize the Eiffel Tower itself as well as the restaurants within 1km.
To do this, we can calculate the distance to the Eiffel Tower for each of the restaurants. Based on this result, we can then create a mask that takes True
if the restaurant is within 1km, and False
otherwise, and use it to filter the restaurants GeoDataFrame. Finally, we make a visualization of this subset.
The restaurants
GeoDataFrame has been loaded, and the eiffel_tower
object created. Further, matplotlib, GeoPandas and contextily have been imported.
This exercise is part of the course
Working with Geospatial Data in Python
Exercise instructions
- Calculate the distance to the Eiffel Tower for each restaurant, and call the result
dist_eiffel
. - Print the distance to the closest restaurant (which is the minimum of
dist_eiffel
). - Select the rows the
restaurants
GeoDataFrame where the distance to the Eiffel Tower is less than 1 km (note that the distance is in meters).
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# 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()