πŸ” 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 #5: Automating Map Exports for Each Feature in Python

 

Title: Automatically Export One Map per Feature Using Python & GeoPandas


πŸ“Introduction

Ever had to manually export 100+ maps—one for each zone, district, or parcel?
Let’s stop doing it manually. With Python, you can loop through your features and save one map per feature as a PNG, PDF, or even a web map.

We’ll automate:

  • Filtering your layer

  • Plotting one feature at a time

  • Exporting maps with custom titles and names

Let’s build your own map production engine. πŸ› ️


🧰 Step 1: Load the Shapefiles

python
import geopandas as gpd import matplotlib.pyplot as plt import os # Load polygons (e.g., districts) and points (e.g., wells) districts = gpd.read_file("data/districts.shp") wells = gpd.read_file("data/water_wells.shp") # Make sure CRS matches districts = districts.to_crs("EPSG:4326") wells = wells.to_crs("EPSG:4326")

πŸ—‚️ Step 2: Create an Output Folder

python
output_folder = "output/maps" os.makedirs(output_folder, exist_ok=True)

πŸ” Step 3: Loop Through Features and Export Maps

python
for idx, district in districts.iterrows(): district_name = district["district_name"] single_district = gpd.GeoDataFrame([district], crs=districts.crs) # Filter wells inside the district wells_in_district = wells[wells.within(district.geometry)] # Plot fig, ax = plt.subplots(figsize=(10, 10)) single_district.plot(ax=ax, color='lightgreen', edgecolor='black') wells_in_district.plot(ax=ax, color='blue', markersize=20) ax.set_title(f"Wells in {district_name}", fontsize=14) plt.axis("off") plt.tight_layout() # Save filename = os.path.join(output_folder, f"map_{district_name}.png") plt.savefig(filename, dpi=300) plt.close()

πŸ’‘ Bonus: Clean Up Filenames

python
from slugify import slugify filename = os.path.join(output_folder, f"map_{slugify(district_name)}.png")

Install python-slugify with:

bash
pip install python-slugify

🧠 What Can You Use This For?

  • One PDF/PNG per municipality, parcel, plot, project area

  • Batch mapping for client reports

  • Export thematic maps for schools, wells, or zoning

  • Save per-feature maps to share on email, Google Drive, etc.


πŸ“Œ Want to Go Further?

  • Add labels using plt.text()

  • Include basemaps using contextily

  • Create PDFs with PdfPages

  • Combine multiple maps into a dashboard layout with subplot2grid


🎯 Conclusion

Map production doesn’t need to be manual. With just a loop and some Python logic, you can export hundreds of clean, high-quality maps—customized for each feature.

This is how GIS automation adds real productivity to your workflow.


πŸ“Œ Next Up:

➡️ Post 6: Add Basemaps to Your Maps with Contextily and XYZ Tiles

No comments:

Post a Comment