Skip to main content

Pydantic data models for the GeoJSON spec.

Project description

geojson-pydantic

Pydantic models for GeoJSON.

Test Coverage Package version Downloads Downloads Conda

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

$ pip install -U pip
$ pip install geojson-pydantic

Or install from source:

$ pip install -U pip
$ 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(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"

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

geojson-pydantic-0.4.3.tar.gz (7.6 kB view details)

Uploaded Source

Built Distribution

geojson_pydantic-0.4.3-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

Details for the file geojson-pydantic-0.4.3.tar.gz.

File metadata

  • Download URL: geojson-pydantic-0.4.3.tar.gz
  • Upload date:
  • Size: 7.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.28.1

File hashes

Hashes for geojson-pydantic-0.4.3.tar.gz
Algorithm Hash digest
SHA256 34c9e43509012ef6ad7b0f600aa856da23fb13edbf55964dcca4a00a267385e0
MD5 df8ab3e4ac934013298bda8cd82c5ea7
BLAKE2b-256 66caa94596d9a658ba6d78e9e28212cad4b0ef5aa1d01cf77b978e218c1ae2f4

See more details on using hashes here.

File details

Details for the file geojson_pydantic-0.4.3-py3-none-any.whl.

File metadata

File hashes

Hashes for geojson_pydantic-0.4.3-py3-none-any.whl
Algorithm Hash digest
SHA256 716cff5bbb2d3abafb7f45f40b22cb74858a4e282126c7a5871fbee3b888924f
MD5 a6e793f3bdc8bd93d24101a99bd06589
BLAKE2b-256 d4199f58c73ea99c438e1bb00c25a1e215933667301819440eee5f803a7bb9dd

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page