Debian Codegraph @

docs/src/migrations/v1.0_migration.md

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