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.
Common errors
-
ModuleNotFoundError: No module named 'geojson'
cause The 'geojson' library is not installed in the Python environment where you are running your code, or the environment is not correctly activated.fixInstall the package using pip: `pip install geojson` (or `pip3 install geojson` for Python 3 specific installations). -
ValueError: Expected 0 or more linear rings, got (...)
cause This error occurs when creating a `geojson.Polygon` object because the coordinates are not nested correctly according to the GeoJSON specification. A Polygon's coordinates should be a list of linear rings, where each linear ring is itself a list of coordinate pairs, and the first and last coordinate pair of each linear ring must be identical. A common mistake is providing a single linear ring as a direct list of points instead of a list containing that list.fixEnsure the coordinates for a `Polygon` are a list of lists of `(longitude, latitude)` tuples. For a simple polygon, it should be `[[exterior_ring_coords]]`. Example: `geojson.Polygon([[(0, 0), (1, 1), (1, 0), (0, 0)]])` -
AttributeError: 'str' object has no attribute '_geom'
cause This error typically arises when you are attempting to perform a geospatial operation (often with a library like Shapely) that expects a geometry object, but you are instead providing a raw GeoJSON string. The `_geom` attribute is internal to some geometry libraries and is not present on a standard Python string.fixIf you have a GeoJSON string, you need to parse it into a Python GeoJSON object (or a Shapely geometry object) before using it in operations that expect a geometry. Use `geojson.loads(json_string)` to get a `geojson` object, or `shapely.geometry.shape(geojson.loads(json_string))` to get a Shapely object. -
AttributeError: 'Map' object has no attribute 'GeoJson'
cause This error occurs when attempting to add GeoJSON data to a Folium map using a non-existent method or attribute. In Folium, `GeoJson` is a separate class used to create a GeoJSON layer, which is then added to the map using `add_to()` or `add_child()`, not by directly accessing an attribute on the map object.fixCreate a `folium.GeoJson` object separately and then add it to your map instance. Example: `geojson_data = geojson.loads(my_geojson_string)`; `folium.GeoJson(geojson_data).add_to(my_map)` or `my_map.add_child(folium.GeoJson(geojson_data))`.
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
import json; json.loads(...)
from geojson import loads
- dump
from geojson import dump
- load
import json; json.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)