You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
`geojson_pydantic` provides a suite of Pydantic models matching the GeoJSON specification [rfc7946](https://datatracker.ietf.org/doc/html/rfc7946). Those models can be used for creating or validating geojson data.
@@ -48,181 +56,9 @@ Install with conda from [`conda-forge`](https://anaconda.org/conda-forge/geojson
48
56
$ conda install -c conda-forge geojson-pydantic
49
57
```
50
58
51
-
## Usage
52
-
53
-
```python
54
-
from geojson_pydantic import Feature, FeatureCollection, Point
55
-
56
-
geojson_feature = {
57
-
"type": "Feature",
58
-
"geometry": {
59
-
"type": "Point",
60
-
"coordinates": [13.38272, 52.46385],
61
-
},
62
-
"properties": {
63
-
"name": "jeff",
64
-
},
65
-
}
66
-
67
-
68
-
feat = Feature(**geojson_feature)
69
-
assert feat.type =="Feature"
70
-
asserttype(feat.geometry) == Point
71
-
assert feat.properties["name"] =="jeff"
72
-
73
-
fc = FeatureCollection(type="FeatureCollection", features=[geojson_feature, geojson_feature])
74
-
assert fc.type =="FeatureCollection"
75
-
assertlen(fc) ==2
76
-
asserttype(fc.features[0].geometry) == Point
77
-
assert fc.features[0].properties["name"] =="jeff"
78
-
```
79
-
80
-
### Advanced usage
81
-
82
-
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.
83
-
84
-
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.
85
-
86
-
By default `Feature` and `FeatureCollections` are defined using `geojson_pydantic.geometries.Geometry` for the geometry and `typing.Dict` for the properties.
87
-
88
-
Here's an example where we want to validate that GeoJSON features have Polygon types, but don't do any specific property validation.
89
-
90
-
```python
91
-
from typing import Dict
92
-
93
-
from geojson_pydantic import Feature, Polygon
94
-
from pydantic import BaseModel
95
-
96
-
geojson_feature = {
97
-
"type": "Feature",
98
-
"geometry": {
99
-
"type": "Point",
100
-
"coordinates": [13.38272, 52.46385],
101
-
},
102
-
"properties": {
103
-
"name": "jeff",
104
-
},
105
-
}
106
-
107
-
# Define a Feature model with Geometry as `Polygon` and Properties as `Dict`
108
-
MyPolygonFeatureModel = Feature[Polygon, Dict]
109
-
110
-
feat = MyPolygonFeatureModel(**geojson_feature) # should raise Validation Error because `geojson_feature` is a point
111
-
>>> ValidationError: 3 validation errors for Feature[Polygon, Dict]
# Define a Feature model with Geometry as `Point` and Properties as a constrained Model
159
-
class MyProps(BaseModel):
160
-
name: constr(pattern=r'^(drew|vincent)$')
161
-
162
-
MyPointFeatureModel = Feature[Point, MyProps]
163
-
164
-
geojson_feature = {
165
-
"type": "Feature",
166
-
"geometry": {
167
-
"type": "Point",
168
-
"coordinates": [13.38272, 52.46385],
169
-
},
170
-
"properties": {
171
-
"name": "jeff",
172
-
},
173
-
}
174
-
175
-
feat = MyPointFeatureModel(**geojson_feature)
176
-
>>> ValidationError: 1 validation error for Feature[Point, MyProps]
177
-
properties -> name
178
-
string does not match regex "^(drew|vincent)$" (type=value_error.str.regex; pattern=^(drew|vincent)$)
179
-
180
-
geojson_feature["properties"]["name"] = "drew"
181
-
feat = MyPointFeatureModel(**geojson_feature)
182
-
assert feat.properties.name =="drew"
183
-
```
184
-
185
-
## Enforced Keys
186
-
187
-
Starting with version `0.6.0`, geojson-pydantic's classes will not define default keys such has `type`, `geometry` or `properties`.
188
-
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)
189
-
190
-
o A GeoJSON object has a member with the name "type". The value of
191
-
the member MUST be one of the GeoJSON types.
192
-
193
-
o A Feature objectHAS a "type" member with the value "Feature".
194
-
195
-
o A Feature objectHAS a member with the name "geometry". The value
196
-
of the geometry member SHALL be either a Geometry objectas
197
-
defined above or, in the case that the Feature is unlocated, a
198
-
JSON null value.
199
-
200
-
o A Feature objectHAS a member with the name "properties". The
201
-
value of the properties member is an object (anyJSONobjector a
0 commit comments