CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-cartopy

A Python package designed to make drawing maps for data analysis and visualisation easy

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

Overview
Eval results
Files

geographic-features.mddocs/

Geographic Features

Ready-to-use geographic features from various data sources including Natural Earth and GSHHS datasets. Features provide automatic scaling, caching, and seamless integration with matplotlib plotting.

Capabilities

Base Feature Classes

Abstract base classes that define the feature interface for all geographic data sources.

class Feature:
    """Base class for all geographic features."""
    def __init__(self, crs, **kwargs): ...
    @property
    def crs(self): ...
    @property  
    def kwargs(self): ...
    def geometries(self): ...
    def intersecting_geometries(self, extent): ...

class Scaler:
    """Handle geometry scaling based on map extent."""
    def __init__(self, scale): ...
    @property
    def scale(self): ...
    def scale_from_extent(self, extent): ...

class AdaptiveScaler(Scaler):
    """Automatically select scale based on map extent."""
    def __init__(self, default_scale, limits): ...
    def scale_from_extent(self, extent): ...

Natural Earth Features

Access to Natural Earth datasets with automatic scaling and data management.

class NaturalEarthFeature(Feature):
    """Interface to Natural Earth shapefiles."""
    def __init__(self, category, name, scale, **kwargs): ...
    @property
    def scale(self): ...
    def geometries(self): ...
    def intersecting_geometries(self, extent): ...
    def with_scale(self, new_scale): ...

# Pre-defined Natural Earth features
BORDERS: NaturalEarthFeature  # Country boundaries
STATES: NaturalEarthFeature   # State/province boundaries  
COASTLINE: NaturalEarthFeature  # Coastlines and major islands
LAKES: NaturalEarthFeature    # Natural and artificial lakes
LAND: NaturalEarthFeature     # Land polygons
OCEAN: NaturalEarthFeature    # Ocean polygons
RIVERS: NaturalEarthFeature   # Rivers and lake centerlines

auto_scaler: AdaptiveScaler   # Default adaptive scaler

GSHHS Features

Access to Global Self-consistent Hierarchical High-resolution Shoreline database.

class GSHHSFeature(Feature):
    """Interface to GSHHS coastline dataset."""
    def __init__(self, scale='auto', levels=None, **kwargs): ...
    def geometries(self): ...
    def intersecting_geometries(self, extent): ...

Custom Features

Create features from user-provided geometric data.

class ShapelyFeature(Feature):
    """Feature from collection of shapely geometries."""
    def __init__(self, geometries, crs, **kwargs): ...
    def geometries(self): ...

Web Feature Service

Access features from OGC Web Feature Services.

class WFSFeature(Feature):
    """Feature collection from OGC Web Feature Service."""
    def __init__(self, wfs, features, **kwargs): ...
    def geometries(self): ...
    def intersecting_geometries(self, extent): ...

Nightshade Feature

Day/night terminator visualization.

class Nightshade(Feature):
    """Day/night terminator feature."""
    def __init__(self, date=None, delta=0.1, refraction=-0.83, 
                 color='k', alpha=0.5, **kwargs): ...
    def geometries(self): ...

Usage Examples

Using Pre-defined Features

import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.feature as cfeature

# Create map with pre-defined features
fig = plt.figure(figsize=(12, 8))
ax = plt.axes(projection=ccrs.PlateCarree())

# Add various features
ax.add_feature(cfeature.LAND, color='lightgray')
ax.add_feature(cfeature.OCEAN, color='lightblue')
ax.add_feature(cfeature.COASTLINE, linewidth=0.5)
ax.add_feature(cfeature.BORDERS, linestyle=':', linewidth=0.5)
ax.add_feature(cfeature.RIVERS, color='blue', linewidth=0.5)
ax.add_feature(cfeature.LAKES, color='blue', alpha=0.7)

ax.set_global()
plt.show()

Creating Custom Features

import cartopy.crs as ccrs
import cartopy.feature as cfeature
from shapely.geometry import Polygon

# Create custom feature from shapely geometries
# Example: highlight specific regions
custom_regions = [
    Polygon([(-74, 40.5), (-74, 41), (-73.5, 41), (-73.5, 40.5)]),  # NYC area
    Polygon([(-118.5, 33.5), (-118.5, 34.5), (-117.5, 34.5), (-117.5, 33.5)])  # LA area
]

custom_feature = cfeature.ShapelyFeature(
    custom_regions, 
    ccrs.PlateCarree(),
    facecolor='red',
    alpha=0.5,
    edgecolor='darkred'
)

# Use in plot
fig = plt.figure(figsize=(12, 8))
ax = plt.axes(projection=ccrs.PlateCarree())
ax.add_feature(cfeature.LAND)
ax.add_feature(cfeature.OCEAN)
ax.add_feature(custom_feature)
ax.set_extent([-125, -65, 25, 50])  # Continental US

Natural Earth Feature Customization

import cartopy.feature as cfeature

# Create custom Natural Earth feature
rivers_50m = cfeature.NaturalEarthFeature(
    'physical', 'rivers_lake_centerlines', '50m',
    edgecolor='blue', facecolor='none', linewidth=1.0
)

# Use specific scale
land_10m = cfeature.LAND.with_scale('10m')

# Custom scaler for different zoom levels
from cartopy.feature import AdaptiveScaler
custom_scaler = AdaptiveScaler('110m', (('50m', 30), ('10m', 10)))
coastline_adaptive = cfeature.NaturalEarthFeature(
    'physical', 'coastline', custom_scaler,
    edgecolor='black', facecolor='none'
)

GSHHS Features

import cartopy.feature as cfeature

# Different GSHHS scales and levels
coastline_auto = cfeature.GSHHSFeature(scale='auto', levels=[1])
coastline_high = cfeature.GSHHSFeature(scale='high', levels=[1, 2])

# Use in high-resolution coastal maps
fig = plt.figure(figsize=(12, 8))
ax = plt.axes(projection=ccrs.PlateCarree())
ax.add_feature(coastline_high)
ax.set_extent([-10, 5, 45, 60])  # Western Europe coast

Nightshade Feature

from datetime import datetime
from cartopy.feature.nightshade import Nightshade

# Add day/night terminator for specific date
date = datetime(2023, 6, 21, 12, 0)  # Summer solstice, noon UTC
night_shade = Nightshade(date, alpha=0.25)

fig = plt.figure(figsize=(12, 8))
ax = plt.axes(projection=ccrs.PlateCarree())
ax.add_feature(cfeature.LAND)
ax.add_feature(cfeature.OCEAN)
ax.add_feature(night_shade)
ax.set_global()

Constants and Color Schemes

COLORS: dict = {
    'land': np.array((240, 240, 220)) / 256.,
    'land_alt1': np.array((220, 220, 220)) / 256., 
    'water': np.array((152, 183, 226)) / 256.
}

Install with Tessl CLI

npx tessl i tessl/pypi-cartopy

docs

coordinate-systems.md

data-io.md

geodesic.md

geographic-features.md

index.md

matplotlib-integration.md

transformations.md

tile.json