🔍 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 #6: Adding Basemaps to Your Maps with Contextily & XYZ Tiles

 

Title: Add Stunning Basemaps to Your Python Maps with Contextily


📍Introduction

When you're plotting GIS data, the basemap can make a huge difference. Whether it’s satellite imagery, terrain, or streets, basemaps provide context to your spatial data, helping you communicate your analysis better.

In this post, we’ll show you how to easily integrate basemaps (like OpenStreetMap, Google Satellite, or Esri) into your maps using Contextily and XYZ tiles.


🧰 Step 1: Install Contextily

Contextily makes it super easy to add basemaps to your GeoPandas plots. First, install it using pip:

bash
pip install contextily

🗂️ Step 2: Load the Data

We’ll continue using the districts and wells data from previous posts. If you don’t have them, any polygon and point layers will work.

python
import geopandas as gpd import matplotlib.pyplot as plt import contextily as ctx # Load polygons and points districts = gpd.read_file("data/districts.shp") wells = gpd.read_file("data/water_wells.shp") # Ensure the CRS is the same districts = districts.to_crs("EPSG:4326") wells = wells.to_crs("EPSG:4326")

🌍 Step 3: Add a Basemap

Use the Contextily basemaps. We’ll overlay OpenStreetMap as the background for our plot.

python
fig, ax = plt.subplots(figsize=(12, 12)) # Plot your spatial data districts.plot(ax=ax, color='lightgreen', edgecolor='black') wells.plot(ax=ax, color='blue', markersize=20) # Add basemap ctx.add_basemap(ax, crs=districts.crs.to_string(), source=ctx.providers.OpenStreetMap.Mapnik) ax.set_title("Water Wells with Districts and OpenStreetMap Basemap", fontsize=14) plt.axis("off") plt.tight_layout() plt.show()

🧠 What Just Happened?

  • ctx.add_basemap() adds a base layer beneath your map.

  • crs=districts.crs.to_string() ensures the basemap aligns with your data’s CRS (Coordinate Reference System).

  • ctx.providers.OpenStreetMap.Mapnik selects the OpenStreetMap basemap style. You can change this to other basemaps, like Esri or Stamen.


📍 Step 4: Use Different Basemap Providers

Contextily provides many different basemaps. You can easily swap between them by choosing a different provider.

Here are some options:

  • Stamen Terrain

python
ctx.providers.Stamen.Terrain
  • Stamen Toner (Black & White)

python
ctx.providers.Stamen.Toner
  • Esri World Imagery (Satellite)

python
ctx.providers.Esri.WorldImagery
python
# Example: Add Esri Satellite Basemap ctx.add_basemap(ax, crs=districts.crs.to_string(), source=ctx.providers.Esri.WorldImagery)

🗺️ Step 5: Customize the Map Further

You can still apply your own styling to the GIS data even with the basemap in place. For example, you can adjust marker size, add labels, or change colors:

python
fig, ax = plt.subplots(figsize=(12, 12)) # Plot districts with custom style districts.plot(ax=ax, color='lightgreen', edgecolor='black') # Plot wells with custom marker size and color wells.plot(ax=ax, color='red', markersize=50) # Add Esri Satellite basemap ctx.add_basemap(ax, crs=districts.crs.to_string(), source=ctx.providers.Esri.WorldImagery) ax.set_title("Wells with Esri Satellite Basemap", fontsize=14) plt.axis("off") plt.tight_layout() plt.show()

💾 Step 6: Export the Map with Basemap

You can easily save the map with the basemap as a PNG or PDF:

python
fig.savefig("output/wells_districts_satellite_map.png", dpi=300) fig.savefig("output/wells_districts_satellite_map.pdf")

🧠 Why Add a Basemap?

  • Context: Gives viewers a reference point (like streets, satellite imagery, or terrain) for your spatial data.

  • Professional Quality: A basemap adds polish to your map, making it more suitable for presentations and reports.

  • Layer Flexibility: You can mix multiple basemaps with your own layers (e.g., roads over satellite imagery).


🎯 Conclusion

Integrating basemaps into your maps is easy with Contextily. Now you can create beautiful, informative maps with rich backgrounds like satellite imagery, streets, or terrain—perfect for reports, web applications, or just better data visualization.


📌 Next Up:

➡️ Post 7: Interactive Maps with Folium and Plotly

No comments:

Post a Comment