GeoSeries and folium
First you will learn to get information about the geometries in your data with three different GeoSeries attributes and methods. Then you will learn to create a street map layer using folium. This is the Summary of lecture "Visualizing Geospatial Data in Python", via datacamp.
- GeoSeries attributes and methods I
- GeoSeries attributes and methods II
- Street maps with folium
- Creating markers and popups in folium
Note :Folium map doesn't embed this page correctly. So use nbviewer for interactive map link
import pandas as pd
import numpy as np
import geopandas as gpd
import matplotlib.pyplot as plt
from IPython.display import HTML, IFrame
plt.rcParams['figure.figsize'] = (10, 5)
neighborhoods = gpd.read_file('./dataset/neighborhoods.geojson')
# Get just the Urban Residents neighborhood polygon and save it as urban_polygon
urban_polygon = neighborhoods.loc[neighborhoods.name == "Urban Residents"]
print(urban_polygon.head())
# Create a copy of the urban_polygon using EPSG:3857 and print the head
urban_poly_3857 = urban_polygon.to_crs(epsg=3857)
print(urban_poly_3857.head())
# Print the area of urban_poly_3857 in kilometers squared
area = urban_poly_3857.geometry.area / 10**6
print('The area of the Urban Residents neighborhood is ', area.values, ' km squared')
The Urban Residents neighborhood is around 1.13 square km. That seems manageable for walking to see the public art!
downtown_center = urban_poly_3857.geometry.centroid
# Print the type of downtown_center
print(type(downtown_center))
# Plot the urban_poly_3857 as ax and add the center point
ax = urban_poly_3857.plot(color='lightgreen', figsize=(7, 7));
downtown_center.plot(ax=ax, color='black');
plt.xticks(rotation=45);
You can see the centroid of the Urban Residents neighborhood. Notice that latitude and longitude are in meters instead of degrees. In the next exercise you will calculate the distance in meters of each artwork from the centroid of the Urban Residents neighborhood.
Prepare to calculate distances
In this exercise you will prepare a GeoDataFrame called art_dist_meters
with the locations of downtown art converted to meters using EPSG:3857. You will use art_dist_meters
in the next exercise to calculate the distance of each artwork from the center of the Urban Residents neighborhood in meters.
from shapely.geometry import Point
art = gpd.read_file('./dataset/public_art.csv')
art['geometry'] = art.apply(lambda x: Point(float(x.Longitude), float(x.Latitude)), axis=1)
center_point = art.geometry.centroid
from shapely.geometry import Point
# Create art_dist_meters using art and the geometry from art
art_dist_meters = gpd.GeoDataFrame(art, geometry=art.geometry)
art_dist_meters.crs='epsg:4326'
print(art_dist_meters.head(2))
# Set the crs of art_dist_meters to use EPSG:3857
art_dist_meters.geometry = art_dist_meters.geometry.to_crs(epsg=3857)
print(art_dist_meters.head(2))
# Add a column to art_meters, center
art_dist_meters['center'] = center_point
art_dist_meters['center']
The art_dist_meters Data Frame has the location of each artwork and the center point of the Urban Residents neighborhood -- both using meters for distance.
import pprint
# Build a dictionary of titles and distances for Urban Residents art
art_distances = {}
for row in art_dist_meters.iterrows():
vals = row[1]
key = vals['Title']
ctr = vals['center']
art_distances[key] = vals['geometry'].distance(other=ctr)
# Pretty print the art_distances
pprint.pprint(art_distances)
urban_polygon.crs = 'epsg:4326'
urban_polygon.crs
print(urban_polygon.head())
# Create urban_center from the urban_polygon center
urban_center = urban_polygon.to_crs(epsg=4326).centroid
# Print urban_center
print(urban_center)
# Create array for folium called urban_location
urban_location = [urban_center.y, urban_center.x]
# Print urban_location
print(urban_location)
Folium locations are arrays with latitude first. You just created a folium location from the center of the Urban Residents neighborhood.
import folium
# Construct a folium map with urban_location
downtown_map = folium.Map(location=urban_location, zoom_start=15, width=500, height=500)
# Display the map
downtown_map.save('./html/downtown_map.html')
Folium locations are arrays with latitude first. You just created a folium location from the center of the Urban Residents neighborhood.
urban_polygon['center'] = urban_polygon.loc[:, 'geometry'].centroid
point = urban_polygon.center
folium_loc = [point.values.y, point.values.x]
# Construct a map from folium_loc: downtown_map
downtown_map = folium.Map(location = folium_loc, zoom_start = 15, width=500, height=500)
# Draw our neighborhood: Urban Residents
folium.GeoJson(urban_polygon.geometry).add_to(downtown_map)
downtown_map.save('html/downtown_map2.html')
Adding markers for the public art
Now that you have added the polygon for the Urban Residents neighborhood to your folium street map, it's time to add the locations of the art within the neighborhood. You can do that by creating folium markers. Each marker needs a location assigned. Use iterrows()
to loop through the data to grab the values you need.
art_geo = gpd.GeoDataFrame(art, crs=neighborhoods.crs, geometry=art.geometry)
# Spartially join neighborhoods with art_geo
neighborhood_art = gpd.sjoin(art_geo, neighborhoods, op='within')
# Create urban_art from neighborhood_art where the neighborhood name is Urban Residents
urban_art = neighborhood_art.loc[neighborhood_art.name == 'Urban Residents']
for row in urban_art.iterrows():
print('first part: ', row[0])
print('second part: ', row[1])
# Create a location and marker with each iteration for the downtown_map
for row in urban_art.iterrows():
row_values = row[1]
location = [row_values['Latitude'], row_values['Longitude']]
marker = folium.Marker(location=location)
marker.add_to(downtown_map)
downtown_map.save('html/downtown_map3.html')
You have created a folium map of downtown Nashville, a polygon showing the Urban Residents neighborhood, and markers to show the location of the artworks. All that's left is to create some popups for the markers!
print(urban_art.Title)
# Print the urban_art description
print(urban_art.Description)
# Replace Nan and ' values in description
urban_art.Description.fillna('', inplace=True)
urban_art.Description.str.replace("'", "`")
# Print the urban_art descriptions again
print(urban_art.Description)
A map of downtown art
Now you will assign a popup to each marker to give information about the artwork at each location. In particular you will assign the art title and description to the popup for each marker. You will do so by creating the map object downtown_map, then add the popups, and finally use the display function to show your map.
One warning before you start: you'll need to ensure that all instances of single quotes (') are removed from the pop-up message, otherwise your plot will not render!
downtown_map = folium.Map(location=urban_location, zoom_start=15, width=500, height=500)
folium.GeoJson(urban_polygon.geometry).add_to(downtown_map)
# Create popups inside the loop you built to create the markers
for row in urban_art.iterrows():
row_values = row[1]
location = [row_values['Latitude'], row_values['Longitude']]
popup = (str(row_values['Title']) + ': ' + str(row_values['Description'])[:20])
marker = folium.Marker(location = location, popup=popup)
marker.add_to(downtown_map)
downtown_map.save('./html/downtown_map4.html')