| 1 |
|
| 2 |
## Usage |
| 3 |
|
| 4 |
```python |
| 5 |
from geojson_pydantic import Feature, FeatureCollection, Point |
| 6 |
|
| 7 |
geojson_feature = { |
| 8 |
"type": "Feature", |
| 9 |
"geometry": { |
| 10 |
"type": "Point", |
| 11 |
"coordinates": [13.38272, 52.46385], |
| 12 |
}, |
| 13 |
"properties": { |
| 14 |
"name": "jeff", |
| 15 |
}, |
| 16 |
} |
| 17 |
|
| 18 |
|
| 19 |
feat = Feature(**geojson_feature) |
| 20 |
assert feat.type == "Feature" |
| 21 |
assert type(feat.geometry) == Point |
| 22 |
assert feat.properties["name"] == "jeff" |
| 23 |
|
| 24 |
fc = FeatureCollection( |
| 25 |
type="FeatureCollection", features=[geojson_feature, geojson_feature] |
| 26 |
) |
| 27 |
assert fc.type == "FeatureCollection" |
| 28 |
assert len(fc.features) == 2 |
| 29 |
assert type(fc.features[0].geometry) == Point |
| 30 |
assert fc.features[0].properties["name"] == "jeff" |
| 31 |
``` |
| 32 |
|
| 33 |
## Geometry Model methods and properties |
| 34 |
|
| 35 |
- `__geo_interface__`: GeoJSON-like protocol for geo-spatial (GIS) vector data ([spec](https://gist.github.com/sgillies/2217756#__geo_interface)). |
| 36 |
- `has_z`: returns true if any coordinate has a Z value. |
| 37 |
- `wkt`: returns the Well Known Text representation of the geometry. |
| 38 |
- `create`: create a geometry object without providing the `type` information |
| 39 |
|
| 40 |
##### For Polygon geometry |
| 41 |
|
| 42 |
- `exterior`: returns the exterior Linear Ring of the polygon. |
| 43 |
- `interiors`: returns the interiors (Holes) of the polygon. |
| 44 |
- `Polygon.from_bounds(xmin, ymin, xmax, ymax)`: creates a Polygon geometry from a bounding box. |
| 45 |
|
| 46 |
##### For GeometryCollection and FeatureCollection |
| 47 |
|
| 48 |
- `iter()`: iterates over geometries or features |
| 49 |
- `length`: returns geometries or features count |
| 50 |
|
| 51 |
## Advanced usage |
| 52 |
|
| 53 |
In `geojson_pydantic` we've implemented pydantic's [Generic Models](https://pydantic-docs.helpmanual.io/usage/models/#generic-models) which allow the creation of more advanced models to validate either the geometry type or the properties. |
| 54 |
|
| 55 |
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. |
| 56 |
|
| 57 |
By default `Feature` and `FeatureCollections` are defined using `geojson_pydantic.geometries.Geometry` for the geometry and `typing.Dict` for the properties. |
| 58 |
|
| 59 |
Here's an example where we want to validate that GeoJSON features have Polygon types, but don't do any specific property validation. |
| 60 |
|
| 61 |
```python |
| 62 |
from typing import Dict |
| 63 |
|
| 64 |
from geojson_pydantic import Feature, Polygon |
| 65 |
from pydantic import BaseModel |
| 66 |
|
| 67 |
geojson_feature = { |
| 68 |
"type": "Feature", |
| 69 |
"geometry": { |
| 70 |
"type": "Point", |
| 71 |
"coordinates": [13.38272, 52.46385], |
| 72 |
}, |
| 73 |
"properties": { |
| 74 |
"name": "jeff", |
| 75 |
}, |
| 76 |
} |
| 77 |
|
| 78 |
# Define a Feature model with Geometry as `Polygon` and Properties as `Dict` |
| 79 |
MyPolygonFeatureModel = Feature[Polygon, Dict] |
| 80 |
|
| 81 |
feat = MyPolygonFeatureModel(**geojson_feature) # should raise Validation Error because `geojson_feature` is a point |
| 82 |
>>> ValidationError: 3 validation errors for Feature[Polygon, Dict] |
| 83 |
... |
| 84 |
geometry -> type |
| 85 |
unexpected value; permitted: 'Polygon' (type=value_error.const; given=Point; permitted=['Polygon']) |
| 86 |
|
| 87 |
|
| 88 |
geojson_feature = { |
| 89 |
"type": "Feature", |
| 90 |
"geometry": { |
| 91 |
"type": "Polygon", |
| 92 |
"coordinates": [ |
| 93 |
[ |
| 94 |
[13.38272, 52.46385], |
| 95 |
[13.42786, 52.46385], |
| 96 |
[13.42786, 52.48445], |
| 97 |
[13.38272, 52.48445], |
| 98 |
[13.38272, 52.46385], |
| 99 |
] |
| 100 |
], |
| 101 |
}, |
| 102 |
"properties": { |
| 103 |
"name": "jeff", |
| 104 |
}, |
| 105 |
} |
| 106 |
|
| 107 |
feat = MyPolygonFeatureModel(**geojson_feature) |
| 108 |
assert type(feat.geometry) == Polygon |
| 109 |
``` |
| 110 |
|
| 111 |
Or with optional geometry |
| 112 |
|
| 113 |
```python |
| 114 |
from geojson_pydantic import Feature, Point |
| 115 |
from typing import Optional |
| 116 |
|
| 117 |
MyPointFeatureModel = Feature[Optional[Point], Dict] |
| 118 |
|
| 119 |
assert ( |
| 120 |
MyPointFeatureModel(type="Feature", geometry=None, properties={}).geometry is None |
| 121 |
) |
| 122 |
assert ( |
| 123 |
MyPointFeatureModel( |
| 124 |
type="Feature", geometry=Point(type="Point", coordinates=(0, 0)), properties={} |
| 125 |
).geometry |
| 126 |
is not None |
| 127 |
) |
| 128 |
``` |
| 129 |
|
| 130 |
And now with constrained properties |
| 131 |
|
| 132 |
```python |
| 133 |
from typing_extensions import Annotated |
| 134 |
from geojson_pydantic import Feature, Point |
| 135 |
from pydantic import BaseModel |
| 136 |
|
| 137 |
# Define a Feature model with Geometry as `Point` and Properties as a constrained Model |
| 138 |
class MyProps(BaseModel): |
| 139 |
name: Annotated[str, Field(pattern=r"^(drew|vincent)$")] |
| 140 |
|
| 141 |
MyPointFeatureModel = Feature[Point, MyProps] |
| 142 |
|
| 143 |
geojson_feature = { |
| 144 |
"type": "Feature", |
| 145 |
"geometry": { |
| 146 |
"type": "Point", |
| 147 |
"coordinates": [13.38272, 52.46385], |
| 148 |
}, |
| 149 |
"properties": { |
| 150 |
"name": "jeff", |
| 151 |
}, |
| 152 |
} |
| 153 |
|
| 154 |
feat = MyPointFeatureModel(**geojson_feature) |
| 155 |
>>> ValidationError: 1 validation error for Feature[Point, MyProps] |
| 156 |
properties -> name |
| 157 |
string does not match regex "^(drew|vincent)$" (type=value_error.str.regex; pattern=^(drew|vincent)$) |
| 158 |
|
| 159 |
geojson_feature["properties"]["name"] = "drew" |
| 160 |
feat = MyPointFeatureModel(**geojson_feature) |
| 161 |
assert feat.properties.name == "drew" |
| 162 |
``` |
| 163 |
|
| 164 |
## Enforced `type` Keys |
| 165 |
|
| 166 |
Starting with version `0.6.0`, geojson-pydantic's classes will not define default keys such has `type`, `geometry` or `properties`. |
| 167 |
This is to make sure the library does well its first goal, which is `validating` GeoJSON object based on the [specification](https://datatracker.ietf.org/doc/html/rfc7946#section-3.1.1) |
| 168 |
|
| 169 |
o A GeoJSON object has a member with the name "type". The value of |
| 170 |
the member MUST be one of the GeoJSON types. |
| 171 |
|
| 172 |
o A Feature object HAS a "type" member with the value "Feature". |
| 173 |
|
| 174 |
o A Feature object HAS a member with the name "geometry". The value |
| 175 |
of the geometry member SHALL be either a Geometry object as |
| 176 |
defined above or, in the case that the Feature is unlocated, a |
| 177 |
JSON null value. |
| 178 |
|
| 179 |
o A Feature object HAS a member with the name "properties". The |
| 180 |
value of the properties member is an object (any JSON object or a |
| 181 |
JSON null value). |
| 182 |
|
| 183 |
|
| 184 |
```python |
| 185 |
from geojson_pydantic import Point |
| 186 |
|
| 187 |
## Before 0.6 |
| 188 |
Point(coordinates=(0,0)) |
| 189 |
>> Point(type='Point', coordinates=(0.0, 0.0), bbox=None) |
| 190 |
|
| 191 |
## After 0.6 |
| 192 |
Point(coordinates=(0,0)) |
| 193 |
>> ValidationError: 1 validation error for Point |
| 194 |
type |
| 195 |
field required (type=value_error.missing) |
| 196 |
|
| 197 |
Point(type="Point", coordinates=(0,0)) |
| 198 |
>> Point(type='Point', coordinates=(0.0, 0.0), bbox=None) |
| 199 |
``` |
| 200 |
|
| 201 |
Starting with `2.1.0`, users can use the `.create()` methods to create geometries without the `type` information |
| 202 |
|
| 203 |
```python |
| 204 |
from geojson_pydantic import Point |
| 205 |
|
| 206 |
Point.create(coordinates=(0, 0)) |
| 207 |
# is equivalent to |
| 208 |
Point(bbox=None, type="Point", coordinates=Position2D(longitude=0.0, latitude=0.0)) |
| 209 |
``` |