| 1 |
`geojson-pydantic` version 1.0 introduced [many breaking changes](../release-notes.md). This |
| 2 |
document aims to help with migrating your code to use `geojson-pydantic` 1.0. |
| 3 |
|
| 4 |
|
| 5 |
## Pydantic 2.0 |
| 6 |
|
| 7 |
The biggest update introduced in **1.0** is the new pydantic *major* version requirement [**~2.0**](https://docs.pydantic.dev/2.0/migration/). |
| 8 |
|
| 9 |
In addition to being faster, this new major version has plenty of new API which we used in `geojson-pydantic` (like the new `model_serializer` method). |
| 10 |
|
| 11 |
```python |
| 12 |
from geojson_pydantic import Point |
| 13 |
|
| 14 |
# Before |
| 15 |
Point.dict() # serialize to dict object |
| 16 |
Point.json() # serialize to json string |
| 17 |
|
| 18 |
with open("point.geojson") as f: |
| 19 |
Point.parse_file(f) # parse file content to model |
| 20 |
|
| 21 |
p = {} |
| 22 |
Point.parse_obj(obj) # parse dict object |
| 23 |
|
| 24 |
################## |
| 25 |
# Now (geojson-pydantic ~= 1.0) |
| 26 |
|
| 27 |
Point.model_dump() |
| 28 |
Point.model_dump_json() |
| 29 |
|
| 30 |
with open("point.geojson") as f: |
| 31 |
Point.model_validate_json(f.read()) |
| 32 |
|
| 33 |
p = {} |
| 34 |
Point.model_validate(obj) |
| 35 |
``` |
| 36 |
|
| 37 |
ref: https://github.com/developmentseed/geojson-pydantic/pull/130 |
| 38 |
|
| 39 |
## FeatureCollection Generic model |
| 40 |
|
| 41 |
In **1.0** we updated the generic FeatureCollection model to depends only on a generic Feature model. |
| 42 |
|
| 43 |
```python |
| 44 |
# Before |
| 45 |
FeatureCollection[Geometry, Properties] |
| 46 |
|
| 47 |
# Now (geojson-pydantic ~= 1.0) |
| 48 |
FeatureCollection[Feature[Geometry, Properties]] |
| 49 |
``` |
| 50 |
|
| 51 |
e.g |
| 52 |
|
| 53 |
```python |
| 54 |
from pydantic import BaseModel |
| 55 |
from geojson_pydantic import Feature, FeatureCollection, Polygon |
| 56 |
|
| 57 |
|
| 58 |
class CustomProperties(BaseModel): |
| 59 |
id: str |
| 60 |
description: str |
| 61 |
size: int |
| 62 |
|
| 63 |
|
| 64 |
# Create a new FeatureCollection Model which should only |
| 65 |
# Accept Features with Polygon geometry type and matching the properties |
| 66 |
MyFc = FeatureCollection[Feature[Polygon, CustomProperties]] |
| 67 |
``` |
| 68 |
|
| 69 |
ref: https://github.com/developmentseed/geojson-pydantic/issues/134 |
| 70 |
|
| 71 |
## Exclude `bbox` and `id` if null |
| 72 |
|
| 73 |
Using the new pydantic `model_serializer` method, we are now able to `customize` JSON output for the models to better match the GeoJSON spec |
| 74 |
|
| 75 |
```python |
| 76 |
# Before |
| 77 |
Point(type="Point", coordinates=[0, 0]).json() |
| 78 |
>> '{"type":"Point","coordinates":[0.0,0.0],"bbox":null}' |
| 79 |
|
| 80 |
# Now (geojson-pydantic ~= 1.0) |
| 81 |
Point(type="Point", coordinates=[0, 0]).model_dump_json() |
| 82 |
>> '{"type":"Point","coordinates":[0.0,0.0]}' |
| 83 |
``` |
| 84 |
|
| 85 |
ref: https://github.com/developmentseed/geojson-pydantic/issues/125 |
| 86 |
|
| 87 |
|
| 88 |
## Change in WKT output for Multi* geometries |
| 89 |
|
| 90 |
```python |
| 91 |
from geojson_pydantic import MultiPoint |
| 92 |
|
| 93 |
geom = MultiPoint(type='MultiPoint', coordinates=[(1.0, 2.0, 3.0)]) |
| 94 |
|
| 95 |
# Before |
| 96 |
print(geom.wkt) |
| 97 |
>> MULTIPOINT Z (1 2 3) |
| 98 |
|
| 99 |
# Now (geojson-pydantic ~= 1.0) |
| 100 |
print(geom.wkt) |
| 101 |
>> MULTIPOINT Z ((1 2 3)) |
| 102 |
``` |
| 103 |
|
| 104 |
ref: https://github.com/developmentseed/geojson-pydantic/issues/139 |