🔍 Extract Field Names Containing 'type' (Integer Fields Without Domain) from GDB Using ArcPy

  ⚙️ How the Script Works 🗂️ Geodatabase Setup The script starts by pointing to a target File Geodatabase (.gdb) and initializing a CSV ...

Saturday, April 19, 2025

Post #7: Create Interactive Maps with Folium and Plotly

 

Title: Make Your Maps Interactive with Folium and Plotly in Python


📍Introduction

Static maps are great, but sometimes you need an interactive map. With Folium and Plotly, you can create maps that users can zoom, pan, and interact with in a web browser. These maps are perfect for dashboards, data exploration, or interactive reports.

In this post, we'll cover:

  • Folium: Creating interactive maps with Leaflet.js (perfect for quick web maps)

  • Plotly: Advanced visualizations with interactivity for geospatial data


🧰 Step 1: Install Folium and Plotly

First, you need to install the libraries:

bash
pip install folium plotly

🗺️ Step 2: Create an Interactive Map with Folium

Folium is based on Leaflet.js, a popular JavaScript library for creating interactive maps. It’s extremely easy to use, and you can embed your maps in websites with just a few lines of code.

python
import folium # Create a base map m = folium.Map(location=[21.4225, 39.8262], zoom_start=13) # Coordinates of Mecca # Add a marker (for example, a well location) folium.Marker([21.4225, 39.8262], popup="Water Well").add_to(m) # Save to HTML m.save("output/mecca_map.html")

This will generate an interactive map centered on Mecca with a marker on the specified coordinates. You can open the mecca_map.html file in your browser to interact with the map.


🧠 What Just Happened?

  • folium.Map() creates a map with an initial location and zoom level.

  • folium.Marker() adds a marker with a popup message to the map.

  • .save() exports the map as an HTML file that can be viewed in any browser.


📍 Step 3: Create Interactive Maps with Plotly

If you want advanced customization and more interactivity (like hover effects or zoomable choropleths), Plotly is your best friend.

python
import plotly.express as px import geopandas as gpd # Load a GeoDataFrame districts = gpd.read_file("data/districts.shp") # Plot with Plotly fig = px.choropleth(districts, geojson=districts.geometry, locations=districts.index, color="population", color_continuous_scale="Viridis", labels={"population": "Population"}) # Update map layout fig.update_geos(fitbounds="locations") fig.update_layout(title="Districts Population", geo=dict(showcoastlines=True)) # Show the interactive map fig.show()

This code creates an interactive choropleth map where the color of each district reflects its population.


🧠 What Just Happened?

  • px.choropleth() makes a choropleth map using GeoPandas geometries.

  • fig.update_geos() adjusts the map to fit your data, adding coastlines and other settings.

  • fig.show() opens the interactive map in your browser.


🌍 Step 4: Add Tooltips and Hover Effects with Plotly

You can also customize the hover tooltips to display additional information.

python
fig = px.choropleth(districts, geojson=districts.geometry, locations=districts.index, color="population", hover_name="district_name", hover_data=["area", "elevation"], color_continuous_scale="Viridis", labels={"population": "Population"}) fig.update_geos(fitbounds="locations") fig.update_layout(title="Districts Population", geo=dict(showcoastlines=True)) # Show the interactive map fig.show()

In this example:

  • hover_name="district_name": When you hover over a district, it shows the district's name.

  • hover_data=["area", "elevation"]: Displays additional information, like area and elevation, when hovering over each feature.


📍 Step 5: Save the Interactive Plotly Map as HTML

You can save your Plotly map as an interactive HTML file:

python
fig.write_html("output/interactive_districts_map.html")

This saves the interactive map to an HTML file that you can embed in a web page or share directly.


🧠 Why Use Folium & Plotly for Interactive Maps?

  • Folium is quick and easy for simple interactive maps, perfect for embedding into reports or websites.

  • Plotly provides more powerful visualizations with customizable features like hover effects, popups, and custom tooltips.

  • Interactivity: Let users zoom, pan, and explore your maps, making the data come alive.


🎯 Conclusion

Both Folium and Plotly are fantastic tools for adding interactivity to your maps. Whether you need something simple and quick or an advanced, customizable map, these libraries make it easy to add value to your geospatial projects.


📌 Next Up:

➡️ Post 8: Geospatial Data Analysis with Machine Learning in Python

No comments:

Post a Comment