Interactive Geospatial Visualization in Python

Geospatial data can be interesting. One interactive geospatial visualization provides a lot of information about the data and the area and more. Python has so many libraries. It is hard to know which one to use. For a geospatial visualization, I will use Folium. It is very easy to use and it has several styles as well to match your choice and requirement. Let’s start.

I used a Jupyter Notebook environment for this. If you are using a Jupyter Notebook, you need to install folium using the anaconda prompt using the following command:

conda install -c conda-forge folium=0.5.0 — yes

Now we can import folium in the notebook. Folium has the world map built-in.

import folium
folium.Map()

Here is the world map. As you can see, you can zoom in, zoom out, and navigate around. You may want the map of a certain country, state, or city. Latitude and longitude of that specific place can be provided as a parameter to get the map of that particular place. I want to print the map of Florida.

folium.Map(location = [27.664827, -81.516], zoom_start = 4)

Here you can see the state of Florida. I put the zoom_start as 4. In your notebook, you can zoom in or zoom out and navigate to a certain part. But you can also start with a, increased zoom.

florida = folium.Map(location = [27.664827, -81.516], zoom_start = 7)

Let’s see some different styles. Make a high contrast black and white map. You can achieve it by using parameter tiles. For a high contrast black and white map, tiles will be ‘Stamen Toner’.

folium.Map(location= [27.665, -81.516], zoom_start = 8, tiles = 'Stamen Toner')

Isn’t it nice! This type of map is perfect for the coastal zone. Also good for data mashup.

In my next example, I will use the Stamen Terrain map. This will show the natural vegetation and hills shading.

folium.Map(location= [27.665, -81.516], zoom_start = 4, tiles = 'Stamen Terrain')




I am sure you are interested to see if we can plot events or incidents on the map. We certainly can. To demonstrate that I am going to use a dataset that will show the incidents in Florida. Please feel free to download the dataset from here. I downloaded the spreadsheet from this site. Import the dataset to the notebook.

import pandas as pd
florida_incidents = pd.read_csv('Florida_Subsidence_Incident_Reports.csv')

The dataset is too big. So I am not showing the screenshot here. But the dataset has an X and Y column which are latitudes and longitudes of different Florida locations. We can place these locations on the map creating a features group. We will include the positions and styles of the points. For the clarity f the image I will only put 100 data points.

florida_incidents = florida_incidents.iloc[0:100, :]
florida = folium.Map(location= [27.665, -81.516], zoom_start = 7)
incidents = folium.map.FeatureGroup()
for lat, lng, in zip(florida_incidents.Y, florida_incidents.X):
    incidents.add_child(
    folium.features.CircleMarker(
    [lat, lng],
    radius=5,
    color='yellow',
    fill=True,
    fill_color='blue',
    fill_opacity=0.6))
florida.add_child(incidents)

We can go further and add the markers on those points.

latitudes = list(florida_incidents.Y)
longitudes = list(florida_incidents.X)
for lat, lng in zip(latitudes, longitudes):
    folium.Marker([lat, lng]).add_to(florida)
florida.add_child(incidents)

Feel free to zoom in and navigate to see the incidents by a specific county or city.

Conclusion

I tried to show how to generate an interactive map, present some incidents or events on it with markers, and style it. This type of interactive map is always helpful in a dashboard, in a presentation, or any other information presented. I hope this was helpful.

#DataVisualization #DataScience #Python #Folium #DataAnalysis




Leave a Reply

Close Menu