1
1
"""pydantic models for GeoJSON Geometry objects."""
2
2
3
3
import abc
4
- from typing import Any , Dict , Iterator , List , Union
4
+ from typing import Any , Dict , Iterator , List , Literal , Union
5
5
6
- from pydantic import BaseModel , Field , ValidationError , validator
6
+ from pydantic import BaseModel , ValidationError , validator
7
7
from pydantic .error_wrappers import ErrorWrapper
8
8
9
9
from geojson_pydantic .types import (
@@ -56,7 +56,7 @@ def wkt(self) -> str:
56
56
class Point (_GeometryBase ):
57
57
"""Point Model"""
58
58
59
- type : str = Field ( default = "Point" , const = True )
59
+ type : Literal [ "Point" ]
60
60
coordinates : Position
61
61
62
62
@property
@@ -71,7 +71,7 @@ def _wkt_inset(self) -> str:
71
71
class MultiPoint (_GeometryBase ):
72
72
"""MultiPoint Model"""
73
73
74
- type : str = Field ( default = "MultiPoint" , const = True )
74
+ type : Literal [ "MultiPoint" ]
75
75
coordinates : MultiPointCoords
76
76
77
77
@property
@@ -80,14 +80,14 @@ def _wkt_inset(self) -> str:
80
80
81
81
@property
82
82
def _wkt_coordinates (self ) -> str :
83
- points = [Point (coordinates = p ) for p in self .coordinates ]
83
+ points = [Point (type = "Point" , coordinates = p ) for p in self .coordinates ]
84
84
return ", " .join (point ._wkt_coordinates for point in points )
85
85
86
86
87
87
class LineString (_GeometryBase ):
88
88
"""LineString Model"""
89
89
90
- type : str = Field ( default = "LineString" , const = True )
90
+ type : Literal [ "LineString" ]
91
91
coordinates : LineStringCoords
92
92
93
93
@property
@@ -96,14 +96,14 @@ def _wkt_inset(self) -> str:
96
96
97
97
@property
98
98
def _wkt_coordinates (self ) -> str :
99
- points = [Point (coordinates = p ) for p in self .coordinates ]
99
+ points = [Point (type = "Point" , coordinates = p ) for p in self .coordinates ]
100
100
return ", " .join (point ._wkt_coordinates for point in points )
101
101
102
102
103
103
class MultiLineString (_GeometryBase ):
104
104
"""MultiLineString Model"""
105
105
106
- type : str = Field ( default = "MultiLineString" , const = True )
106
+ type : Literal [ "MultiLineString" ]
107
107
coordinates : MultiLineStringCoords
108
108
109
109
@property
@@ -112,7 +112,9 @@ def _wkt_inset(self) -> str:
112
112
113
113
@property
114
114
def _wkt_coordinates (self ) -> str :
115
- lines = [LineString (coordinates = line ) for line in self .coordinates ]
115
+ lines = [
116
+ LineString (type = "LineString" , coordinates = line ) for line in self .coordinates
117
+ ]
116
118
return "," .join (f"({ line ._wkt_coordinates } )" for line in lines )
117
119
118
120
@@ -131,7 +133,7 @@ def check_closure(cls, coordinates: List) -> List:
131
133
class Polygon (_GeometryBase ):
132
134
"""Polygon Model"""
133
135
134
- type : str = Field ( default = "Polygon" , const = True )
136
+ type : Literal [ "Polygon" ]
135
137
coordinates : PolygonCoords
136
138
137
139
@validator ("coordinates" )
@@ -161,27 +163,28 @@ def _wkt_inset(self) -> str:
161
163
@property
162
164
def _wkt_coordinates (self ) -> str :
163
165
ic = "" .join (
164
- f", ({ LinearRingGeom (coordinates = interior )._wkt_coordinates } )"
166
+ f", ({ LinearRingGeom (type = 'LineString' , coordinates = interior )._wkt_coordinates } )"
165
167
for interior in self .interiors
166
168
)
167
- return f"({ LinearRingGeom (coordinates = self .exterior )._wkt_coordinates } ){ ic } "
169
+ return f"({ LinearRingGeom (type = 'LineString' , coordinates = self .exterior )._wkt_coordinates } ){ ic } "
168
170
169
171
@classmethod
170
172
def from_bounds (
171
173
cls , xmin : float , ymin : float , xmax : float , ymax : float
172
174
) -> "Polygon" :
173
175
"""Create a Polygon geometry from a boundingbox."""
174
176
return cls (
177
+ type = "Polygon" ,
175
178
coordinates = [
176
179
[(xmin , ymin ), (xmax , ymin ), (xmax , ymax ), (xmin , ymax ), (xmin , ymin )]
177
- ]
180
+ ],
178
181
)
179
182
180
183
181
184
class MultiPolygon (_GeometryBase ):
182
185
"""MultiPolygon Model"""
183
186
184
- type : str = Field ( default = "MultiPolygon" , const = True )
187
+ type : Literal [ "MultiPolygon" ]
185
188
coordinates : MultiPolygonCoords
186
189
187
190
@property
@@ -190,7 +193,9 @@ def _wkt_inset(self) -> str:
190
193
191
194
@property
192
195
def _wkt_coordinates (self ) -> str :
193
- polygons = [Polygon (coordinates = poly ) for poly in self .coordinates ]
196
+ polygons = [
197
+ Polygon (type = "Polygon" , coordinates = poly ) for poly in self .coordinates
198
+ ]
194
199
return "," .join (f"({ poly ._wkt_coordinates } )" for poly in polygons )
195
200
196
201
@@ -200,7 +205,7 @@ def _wkt_coordinates(self) -> str:
200
205
class GeometryCollection (BaseModel ):
201
206
"""GeometryCollection Model"""
202
207
203
- type : str = Field ( default = "GeometryCollection" , const = True )
208
+ type : Literal [ "GeometryCollection" ]
204
209
geometries : List [Geometry ]
205
210
206
211
def __iter__ (self ) -> Iterator [Geometry ]: # type: ignore [override]
0 commit comments