Fundamentals 4 min read

Drawing Terrain Maps with Python Using rasterio, geopandas, and cartopy

This article demonstrates how to create detailed terrain maps in Python by installing and importing rasterio, geopandas, cartopy, and related libraries, configuring map fonts, and executing a comprehensive script that reads vector and raster data, applies custom color maps, and renders the final visualization.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Drawing Terrain Maps with Python Using rasterio, geopandas, and cartopy

This guide explains how to generate terrain maps with Python, requiring third‑party modules such as rasterio , geopandas , cartopy , and matplotlib , which can be installed via pip .

1.1 Import modules

import rasterio
import geopandas as gpd
import numpy as np
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap

1.2 Set map font and size

plt.rcParams["font.family"] = "Times New Roman"
plt.rcParams["font.size"] = 14

1.3 Draw the map

# Set projection: Mercator
projection = ccrs.Mercator()

# Create figure and axis
fig, ax = plt.subplots(figsize=(20, 10), subplot_kw={'projection': projection})

# Define map extent (coordinates in the chosen projection)
ax.set_extent([13530000, 14630000, 4960000, 5850000], crs=projection)

# Read vector shapefile and plot
shp = gpd.read_file("Data/Jilin_Mercator.shp")
shp.plot(ax=ax, transform=projection, edgecolor="black", linewidth=1, facecolor="none")

# Create custom colormap
colors = ["#369121", "#95C769", "#FFFFBF", "#E6865A", "#D14E30", "#BA1414"]
n_bins = 100
cmap_name = "green_brown"
cm = LinearSegmentedColormap.from_list(cmap_name, colors, N=n_bins)

# Load terrain raster data
dataset = rasterio.open("Data/DEM_Jilin_Mercator.tif")
data = dataset.read(1)  # first band
nodata_value = dataset.nodata
# Mask NoData values
data = np.ma.masked_where(data == nodata_value, data)

# Add raster to map
extent = [dataset.bounds.left, dataset.bounds.right, dataset.bounds.bottom, dataset.bounds.top]
im = ax.imshow(data, origin="upper", extent=extent, transform=projection, cmap=cm)

# Add gridlines with labels
gl = ax.gridlines(draw_labels=True, linestyle="--", color="#4F4F4F")
gl.xlocator = plt.FixedLocator(range(120, 135, 3))
gl.ylocator = plt.FixedLocator(range(40, 50, 2))

# Add colorbar
cbar = plt.colorbar(im, ax=ax, orientation="horizontal", shrink=0.4, pad=0.1)
cbar.set_label("Elevation (m)", labelpad=10)
cbar.ax.xaxis.set_label_position("top")

# Save and show the figure
plt.savefig("Pic.jpg", dpi=600)
plt.show()

The resulting map image is displayed below, illustrating the terrain elevation with the custom green‑brown colormap and overlaid vector boundaries.

mappingData VisualizationgeospatialgeopandasCartopyrasterio
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.