geojson
Python bindings and utilities for GeoJSON. This library provides functions for encoding and decoding GeoJSON formatted data, classes for all GeoJSON Objects as defined by the GeoJSON Format Specification, and implements the Python `__geo_interface__` Specification. It is currently at version 3.2.0 and is actively maintained by the Jazzband community, compatible with Python 3.7 and above.
Warnings
- breaking Version 3.0.0 dropped support for Python 2 and now requires Python 3.7 or newer. Additionally, the `crs` module and related features were removed to strictly conform to the official GeoJSON specification, which mandates WGS84 coordinates.
- gotcha When loading or decoding GeoJSON data, always use `geojson.load` or `geojson.loads` instead of the standard `json.load` or `json.loads`. Using the standard `json` module will return plain Python dictionaries, losing the rich GeoJSON object functionality and validation provided by this library.
- gotcha GeoJSON object classes in this library apply a default coordinate precision of 6 decimal places (approximately 0.1 meters). While this is often suitable, it may lead to data loss for highly precise geospatial coordinates if not explicitly managed.
Install
-
pip install geojson
Imports
- Point
from geojson import Point
- LineString
from geojson import LineString
- Polygon
from geojson import Polygon
- Feature
from geojson import Feature
- FeatureCollection
from geojson import FeatureCollection
- dumps
from geojson import dumps
- loads
from geojson import loads
- dump
from geojson import dump
- load
from geojson import load
Quickstart
from geojson import Point, Feature, FeatureCollection, dumps
# Create a Point geometry
point = Point((-115.81, 37.24))
# Create a Feature with the Point geometry and properties
feature = Feature(geometry=point, properties={"name": "Area 51"})
# Create a FeatureCollection containing the feature
feature_collection = FeatureCollection([feature])
# Serialize the FeatureCollection to a GeoJSON string with indentation
geojson_str = dumps(feature_collection, indent=2)
print(geojson_str)