A map can clearly present information in terms of geography. Recently I learnt
how to realize geovisualization with folium
module in Python. In this blog, I
will talk about how to draw a map like the one above with folium
with the
following points:
- Data preparation
- Geovisualization with
folium
Data preparation
Import datasets
The map above describes the apartments’ average price per m2 of Ile-de-France, between 2014 and 2018. Before all, we need sold apartments’ data for calculating average price per m2, need communities’ and departments’ polygon data to draw areas on the map.
In the sold apartments’ dataset, we have data like “id_mutation” to identify the transaction, “nature_mutation” specifies the sale’s nature, “valeur_fonciere” presents the sold price, “code_commune”, “nom_commune” and “code_departement” specify the communities and departments, “surface_reelle_bati” describes the real surface area, longitude and latitude can help us to determine the location.
For the polygon data, we only need “code” and “C_INSEE” to match the department and community and “geometry” to draw polygons.
Data cleaning
Now we have sold apartments and polygon data for the whole France, what we need is the data that relative to Ile-de-France:
Then we use “valeur_fonciere” and surface to calculate average price per m2 for each community with the following function:
When the sold nature is “sols”, “jardins” or “terrains d’agrément”, the sold surface area is indicated as “surface_terrain”, so the price per m2 is obtained by dividing “valeur_fonciere” by “surface_terrain”; otherwise, divide “valeur_fonciere” by “surface_reelle_bati”.
The last 2 steps before drawing the map are transform the coordinates to
geopoints with shapely.geometry.Point()
, match each polygon to community,
and find out which communities(polygons) have no sales.
Now, we have 2 dataframes vf_community_df
and vf_zero_df
to display average
price per m2 for each community.
Geovisualization with “folium”
In this part, I’ll complete the map with the following elements:
- Colormap
- Map base
- Sold apartments layer
- No-sales layer
- Department layer
- Add customized title and legend
Colormap
branca.colormap.StepColormap
creates a ColorMap based on linear interpolation
of a set of colors over a given index. index
presents the values
corresponding to each color, it has to be sorted, and have the same length as
colors; if None, a regular grid between vmin and vmax is created.
Map base
Sold apartments layer
I created the style_function
to assign community-color in terms of
“valeurK_m2”, set color
as ''
since the default color for border is blue,
but in my case I don’t need color. Furthermore, I used folium.GeoJsonTooltip
to add the popup.
No-sales layer
Before creating a new layer, I created style_zero_function
to specify
no-sales communities’ pattern. For creating a new layer, it’s the same as the
first layer.
Department layer
This step is to add border for each department, so we don’t need fillcolor
.
Add customized title and legend
Inspired by this example, I customized title for the map, for
more details, you can find it here. To customizing the colormap
legend, you only need to replace legend-title
and legend-scale
by the
following codes:
Reference
- DGFiP, “Demandes de valeurs foncières géolocalisées”, data.gouv.fr. [Online]. Available: https://www.data.gouv.fr/fr/datasets/demandes-de-valeurs-foncieres-geolocalisees/
- Grégoire David, “Departments polygon”, github.com. [Online]. Available: https://github.com/gregoiredavid/france-geojson/blob/master/departements.geojson
- APUR, “APUR : Communes - Ile de France”, data.gouv.fr. [Online]. Available: https://www.data.gouv.fr/fr/datasets/apur-communes-ile-de-france/
- branca, “branca.colormap”, python-visualization.github.io/branca. [Online]. Available: https://python-visualization.github.io/branca/colormap.html#
- folium, “Preleminary demo of the pattern plugin for Folium”, nbviewer.jupyter.org. [Online]. Available: https://nbviewer.jupyter.org/github/python-visualization/folium/blob/master/examples/plugin-patterns.ipynb
- “How does one add a legend (categorical) to a folium map”, nbviewer.jupyter.org. [Online]. Available: https://nbviewer.jupyter.org/gist/talbertc-usgs/18f8901fc98f109f2b71156cf3ac81cd
- WikiImages, “Earth map summer July continents”, pixabay.com_. [Online]. Available: https://pixabay.com/photos/earth-map-summer-july-continents-11048/