Pydantic GeoJSON Models

2.1.1 · active · verified Sun Apr 12

geojson-pydantic provides a suite of Pydantic models that strictly adhere to the GeoJSON specification (RFC 7946). These models are invaluable for creating, validating, and working with GeoJSON data in a type-safe manner. The library is actively maintained, supporting Python 3.9 and above, with a consistent release cadence that includes performance improvements and Pydantic V2 compatibility.

Warnings

Install

Imports

Quickstart

Demonstrates how to create a simple GeoJSON Point and Feature object using geojson-pydantic, validating and serializing them to JSON. Note the use of `model_dump_json()` for serialization with Pydantic V2.

from geojson_pydantic import Feature, Point

# Define a GeoJSON Point object data
geoj_point_data = {
    "type": "Point",
    "coordinates": [-105.01621, 39.57422]
}

# Create a Pydantic Point model instance
point_obj = Point(**geoj_point_data)
print(f"Point Object: {point_obj.model_dump_json()}")

# Define a GeoJSON Feature object data
geoj_feature_data = {
    "type": "Feature",
    "geometry": geoj_point_data, # Use the previously defined point data
    "properties": {"name": "Example Feature", "value": 123}
}

# Create a Pydantic Feature model instance
feature_obj = Feature(**geoj_feature_data)
print(f"Feature Object: {feature_obj.model_dump_json()}")

view raw JSON →