Pydantic data models for the GeoJSON spec.
Project description
geojson-pydantic
Pydantic models for GeoJSON.
Description
geojson_pydantic
provides a suite of Pydantic models matching the GeoJSON specification rfc7946. Those models can be used for creating or validating geojson data.
Install
$ python -m pip install -U pip
$ python -m pip install geojson-pydantic
Or install from source:
$ python -m pip install -U pip
$ python -m pip install git+https://github.com/developmentseed/geojson-pydantic.git
Install with conda from conda-forge
:
$ conda install -c conda-forge geojson-pydantic
Usage
from geojson_pydantic import Feature, FeatureCollection, Point
geojson_feature = {
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [13.38272, 52.46385],
},
"properties": {
"name": "jeff",
},
}
feat = Feature(**geojson_feature)
assert feat.type == "Feature"
assert type(feat.geometry) == Point
assert feat.properties["name"] == "jeff"
fc = FeatureCollection(type="FeatureCollection", features=[geojson_feature, geojson_feature])
assert fc.type == "FeatureCollection"
assert len(fc) == 2
assert type(fc.features[0].geometry) == Point
assert fc.features[0].properties["name"] == "jeff"
Advanced usage
In geojson_pydantic
we've implemented pydantic's Generic Models which allow the creation of more advanced models to validate either the geometry type or the properties.
In order to make use of this generic typing, there are two steps: first create a new model, then use that model to validate your data. To create a model using a Generic
type, you HAVE TO pass Type definitions
to the Feature
model in form of Feature[Geometry Type, Properties Type]
. Then pass your data to this constructor.
By default Feature
and FeatureCollections
are defined using geojson_pydantic.geometries.Geometry
for the geometry and typing.Dict
for the properties.
Here's an example where we want to validate that GeoJSON features have Polygon types, but don't do any specific property validation.
from typing import Dict
from geojson_pydantic import Feature, Polygon
from pydantic import BaseModel
geojson_feature = {
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [13.38272, 52.46385],
},
"properties": {
"name": "jeff",
},
}
# Define a Feature model with Geometry as `Polygon` and Properties as `Dict`
MyPolygonFeatureModel = Feature[Polygon, Dict]
feat = MyPolygonFeatureModel(**geojson_feature) # should raise Validation Error because `geojson_feature` is a point
>>> ValidationError: 3 validation errors for Feature[Polygon, Dict]
...
geometry -> type
unexpected value; permitted: 'Polygon' (type=value_error.const; given=Point; permitted=['Polygon'])
geojson_feature = {
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[
[13.38272, 52.46385],
[13.42786, 52.46385],
[13.42786, 52.48445],
[13.38272, 52.48445],
[13.38272, 52.46385],
]
],
},
"properties": {
"name": "jeff",
},
}
feat = MyPolygonFeatureModel(**geojson_feature)
assert type(feature.geometry) == Polygon
Or with optional geometry
from geojson_pydantic import Feature, Point
from typing import Optional
MyPointFeatureModel = Feature[Optional[Point], Dict]
assert MyPointFeatureModel(type="Feature", geometry=None, properties={}).geometry is None
assert MyPointFeatureModel(type="Feature", geometry=Point(coordinates=(0,0)), properties={}).geometry is not None
And now with constrained properties
from geojson_pydantic import Feature, Point
from pydantic import BaseModel, constr
# Define a Feature model with Geometry as `Point` and Properties as a constrained Model
class MyProps(BaseModel):
name: constr(regex=r'^(drew|vincent)$')
MyPointFeatureModel = Feature[Point, MyProps]
geojson_feature = {
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [13.38272, 52.46385],
},
"properties": {
"name": "jeff",
},
}
feat = MyPointFeatureModel(**geojson_feature)
>>> ValidationError: 1 validation error for Feature[Point, MyProps]
properties -> name
string does not match regex "^(drew|vincent)$" (type=value_error.str.regex; pattern=^(drew|vincent)$)
geojson_feature["properties"]["name"] = "drew"
feat = MyPointFeatureModel(**geojson_feature)
assert feat.properties.name == "drew"
Enforced Keys
Starting with version 0.6.0
, geojson-pydantic's classes will not define default keys such has type
, geometry
or properties
.
This is to make sure the library does well its first goal, which is validating
GeoJSON object based on the specification
o A GeoJSON object has a member with the name "type". The value of
the member MUST be one of the GeoJSON types.
o A Feature object HAS a "type" member with the value "Feature".
o A Feature object HAS a member with the name "geometry". The value
of the geometry member SHALL be either a Geometry object as
defined above or, in the case that the Feature is unlocated, a
JSON null value.
o A Feature object HAS a member with the name "properties". The
value of the properties member is an object (any JSON object or a
JSON null value).
from geojson_pydantic import Point
## Before 0.6
Point(coordinates=(0,0))
>> Point(type='Point', coordinates=(0.0, 0.0), bbox=None)
## After 0.6
Point(coordinates=(0,0))
>> ValidationError: 1 validation error for Point
type
field required (type=value_error.missing)
Point(type="Point", coordinates=(0,0))
>> Point(type='Point', coordinates=(0.0, 0.0), bbox=None)
Contributing
See CONTRIBUTING.md.
Changes
See CHANGES.md.
Authors
Initial implementation by @geospatial-jeff; taken liberally from https://github.com/arturo-ai/stac-pydantic/
See contributors for a listing of individual contributors.
License
See LICENSE
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file geojson_pydantic-0.7.0.tar.gz
.
File metadata
- Download URL: geojson_pydantic-0.7.0.tar.gz
- Upload date:
- Size: 11.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.32.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 33ee12d883ea26ec9e6fffbdab563b5d23eeb74651c1966e9f4f1e09e8505e7f |
|
MD5 | 89c57996f8304ad47adfa4b128d06cfc |
|
BLAKE2b-256 | 54117e7d73c4f41a4becdab28d5fb76dce1b5e4247105949a97f27984b5132c7 |
File details
Details for the file geojson_pydantic-0.7.0-py3-none-any.whl
.
File metadata
- Download URL: geojson_pydantic-0.7.0-py3-none-any.whl
- Upload date:
- Size: 9.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.32.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b21fc545e732073912005213570a85c832208e30e6c92b82b51c14a0b9cdf053 |
|
MD5 | 82512220a92b1cb4c9868dbb31a96b19 |
|
BLAKE2b-256 | 4cda4fdae8cdb553c306fcbd8d4ad0ba503c2da7d8a246860f4e89c3ae819967 |