Visualizing spatial data with geojson heatmaps | by Jan Majewski | Towards Data Science

Have you ever worked with spatial data and faced the challenge of having to choose between overcrowded scatter plots or choropleth maps limited to administrative boundaries such as districts?
Working as a data scientist in the real estate industry, I often deal with spatial data, however, I was struggling to find the optimal way of visualizing spatial data in a way that would make it easy to grasp key spatial features from large data sets.
The basic features provided by Plotly or Folium felt limiting so I decided to do a bit of research on how to create custom heatmaps on top of a map, which I want to share with you in this article. I will be working on a data set of >10k Real Estate offers in Warsaw from May 2020, which you can download from my GitHub in the link below.
Whole code and data sources are available on GitHub: https://github.com/Jan-Majewski/Project_Portfolio/blob/master/03_Real_Estate_pricing_in_Warsaw/03_02_Data_Vizualization.ipynb
For interactive visualizations, I recommend using nbviewer link: https://nbviewer.jupyter.org/github/Jan-Majewski/Project_Portfolio/blob/c770f21dc410b1495dbdf5b9f4f443ecd4d986ba/03_Real_Estate_pricing_in_Warsaw/03_02_Data_Vizualization.ipynb
The data used can be downloaded from GitHub
df = pd.read_excel(r"https://raw.githubusercontent.com/Jan-Majewski/Project_Portfolio/master/03_Real_Estate_pricing_in_Warsaw/Warsaw_RE_data.xlsx")
As the data has many more features than needed for this project let’s select just key 8 columns.
key_columns=['Id', 'Area', 'Price', 'latitude', 'longitude',      'City','district']df=df[key_columns]df.head(10)
First, let’s explore the most common ways of visualizing spatial data with basic Plotly features.
Scatter plots on a map are probably the most common and easiest ways of visualizing spatial data. You take the coordinates, set one feature as colour and there is your map. It even looks quite effective at first glance, but if you are dealing with a larger data set it gets overcrowded and you actually lose sight of the map below.
We can simplify the scatter by creating a grid by rounding coordinates and calculating the mean for each tile. It looks easier to read but we also lose a lot of detail.
Grid plot is based on data summarized by clipping lat and on and creating lat_mod and lon_mod features and grouping data to create df_map.
Not being satisfied with the previous outcomes, I decided to look for ways to create heatmaps based on contour plots.
For me the best solution turned out to be a 4 step process:
Creating a contour plot in matplotlib
Converting it to geojson with use of geojsoncountour library
Extracting values from geojson to df_contour
Creating a choropleth map using plotly
We got quite a nice contour plot, now all we need to do is convert it to geojson so that it is compatible with plotly.
Warning! When choosing boundaries for contour plot, make sure that each layer has at least one data point. If the max and min are to wide or the step size is too small and one of the layers is empty it could make the whole geojson not appear at all in plotly.
To convert contour plot to geojson you will need geojsoncontour library.
pip install geojsoncontour
As plotly needs a DataFrame with values used for fill colour for spatial objects within geojson, we need to extract them by iterating the “features” key in geojson dict. This key contains a list of dictionaries, one for each layer of the heatmap. The value is stored in “properties” key of the subdictionary as “title” key. Let’s also add “id” key to each dictionary within the list to match spatial object with DataFrame row.
Now we have all we need to create a beautiful heatmap. Let’s plot!
I recommend referencing nbviewer link at the top of the page to explore the full scope of possibilities to zoom and deep-dive the most interesting areas of the plot.
I hope this tutorial will help you make your spatial visualizations more effective and easier to grasp the key insights at a glance.
This type of visualization works with all sorts of data, where spatial features are one of the key drivers. To wrap up enjoy a similar viz, based on google commute data. If you are interested in the analysis, the notebook referenced at the top of the article also deals with extraction and processing drivetime data using Google API.