GeoJSON parsing and validation using Pydantic
Project description
geodantic
Lightweight, type-safe and spec-conforming GeoJSON parsing and validation using Pydantic.
Installation
pip install geodantic
Examples
Parse and validate GeoJSON features with custom properties:
import pydantic
from geodantic import Feature, Point
# Use your own properties Pydantic models
@pydantic.dataclasses.dataclass
class MyProperties:
foo: str
data = {
"type": "Feature",
"geometry": {"type": "Point", "coordinates": [1, 2]},
"properties": {"foo": "abc"},
}
# Parse and validate data into spec-conforming objects
parsed = Feature[Point, MyProperties](**data)
"""
Feature[Point, MyProperties](
type=<GeoJSONObjectType.FEATURE: 'Feature'>,
bbox=None,
geometry=Point(
type=<GeoJSONObjectType.POINT: 'Point'>,
bbox=None,
coordinates=(1.0, 2.0)
),
properties=MyProperties(foo='abc'),
id=None
)
"""
# Dump back into JSON using standard Pydantic methods
parsed.model_dump_json(exclude_unset=True, indent=2)
"""
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
1.0,
2.0
]
},
"properties": {
"foo": "abc"
}
}
"""
Parse objects of arbitrary types:
from geodantic import GeometryCollection
data = {
"type": "GeometryCollection",
"geometries": [
{
"type": "Polygon",
"coordinates": [[[1, 2], [3, 4], [5, 6], [1, 2]]],
"bbox": [1, 2, 3, 4],
},
{
"type": "GeometryCollection",
"geometries": [{"type": "Point", "coordinates": [1, 2]}],
},
],
}
# Parse any geometry type
parsed = GeometryCollection(**data)
"""
GeometryCollection(
type=<GeoJSONObjectType.GEOMETRY_COLLECTION: 'GeometryCollection'>,
bbox=None,
geometries=[
Polygon(
type=<GeoJSONObjectType.POLYGON: 'Polygon'>,
bbox=(1.0, 2.0, 3.0, 4.0),
coordinates=[[(1.0, 2.0), (3.0, 4.0), (5.0, 6.0), (1.0, 2.0)]]
),
GeometryCollection(
type=<GeoJSONObjectType.GEOMETRY_COLLECTION: 'GeometryCollection'>,
bbox=None,
geometries=[
Point(
type=<GeoJSONObjectType.POINT: 'Point'>,
bbox=None,
coordinates=(1.0, 2.0)
)
]
)
]
)
"""
Contributing
Set up the project using Poetry:
poetry install
Format the code:
make lint
Run tests:
make test
Check for typing and format issues:
make check
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file geodantic-0.3.0.tar.gz.
File metadata
- Download URL: geodantic-0.3.0.tar.gz
- Upload date:
- Size: 4.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.2.2 CPython/3.12.1 Linux/6.2.0-1018-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2508951f7ad7d7fe4e5c0e38ed52bfc3d48290bb7eb3fa13c8145bfe17bdf8ef
|
|
| MD5 |
f13be1a14b37a16fdd521ca50c01f270
|
|
| BLAKE2b-256 |
196e5027b935a078035174cd2b5d57cac5a064635b7b712f947b3a02652a3f1b
|
File details
Details for the file geodantic-0.3.0-py3-none-any.whl.
File metadata
- Download URL: geodantic-0.3.0-py3-none-any.whl
- Upload date:
- Size: 5.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.2.2 CPython/3.12.1 Linux/6.2.0-1018-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f1d8a8e68e229adb515a5bd82d4c716ae2f5a5111dc2a1d4f8e131458135d45b
|
|
| MD5 |
40aa0cbe4483c7088675598713b28de3
|
|
| BLAKE2b-256 |
32726e821314d7282614d3e884a9ed9d0ed636ecc82ba1a9f67c4d22e98d7c93
|