Skip to content

Commit 1fddfca

Browse files
committed
Don't use @Property for wkt function.
1 parent 22c3b2b commit 1fddfca

File tree

1 file changed

+17
-44
lines changed

1 file changed

+17
-44
lines changed

geojson_pydantic/geometries.py

Lines changed: 17 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
"""pydantic models for GeoJSON Geometry objects."""
2-
from __future__ import annotations
3-
42
import abc
53
from typing import Any, Dict, Iterator, List, Literal, Protocol, Union
64

@@ -80,6 +78,8 @@ class _GeometryBase(BaseModel, abc.ABC):
8078
type: str
8179
coordinates: Any
8280

81+
__wkt_coordinates__: _WktCallable
82+
8383
@property
8484
def __geo_interface__(self) -> Dict[str, Any]:
8585
"""GeoJSON-like protocol for geo-spatial (GIS) vector data.
@@ -94,27 +94,17 @@ def has_z(self) -> bool:
9494
"""Checks if any coordinate has a Z value."""
9595
...
9696

97-
@property
98-
@abc.abstractmethod
99-
def _wkt_coordinates(self) -> _WktCallable:
100-
...
101-
102-
@property
103-
def _wkt_type(self) -> str:
104-
"""Return the WKT name of the geometry."""
105-
return self.type.upper()
106-
10797
@property
10898
def wkt(self) -> str:
10999
"""Return the Well Known Text representation."""
110100
# Start with the WKT Type
111-
wkt = self._wkt_type
101+
wkt = self.type.upper()
112102
has_z = self.has_z
113103
if self.coordinates:
114104
# If any of the coordinates have a Z add a "Z" to the WKT
115105
wkt += " Z " if has_z else " "
116106
# Add the rest of the WKT inside parentheses
117-
wkt += f"({self._wkt_coordinates(self.coordinates, force_z=has_z)})"
107+
wkt += f"({self.__wkt_coordinates__(self.coordinates, force_z=has_z)})"
118108
else:
119109
# Otherwise it will be "EMPTY"
120110
wkt += " EMPTY"
@@ -128,63 +118,55 @@ class Point(_GeometryBase):
128118
type: Literal["Point"]
129119
coordinates: Position
130120

121+
__wkt_coordinates__ = staticmethod(_position_wkt_coordinates)
122+
131123
@property
132124
def has_z(self) -> bool:
133125
"""Checks if any coordinate has a Z value."""
134126
return _position_has_z(self.coordinates)
135127

136-
@property
137-
def _wkt_coordinates(self) -> _WktCallable:
138-
return _position_wkt_coordinates
139-
140128

141129
class MultiPoint(_GeometryBase):
142130
"""MultiPoint Model"""
143131

144132
type: Literal["MultiPoint"]
145133
coordinates: MultiPointCoords
146134

135+
__wkt_coordinates__ = staticmethod(_position_list_wkt_coordinates)
136+
147137
@property
148138
def has_z(self) -> bool:
149139
"""Checks if any coordinate has a Z value."""
150140
return _position_list_has_z(self.coordinates)
151141

152-
@property
153-
def _wkt_coordinates(self) -> _WktCallable:
154-
return _position_list_wkt_coordinates
155-
156142

157143
class LineString(_GeometryBase):
158144
"""LineString Model"""
159145

160146
type: Literal["LineString"]
161147
coordinates: LineStringCoords
162148

149+
__wkt_coordinates__ = staticmethod(_position_list_wkt_coordinates)
150+
163151
@property
164152
def has_z(self) -> bool:
165153
"""Checks if any coordinate has a Z value."""
166154
return _position_list_has_z(self.coordinates)
167155

168-
@property
169-
def _wkt_coordinates(self) -> _WktCallable:
170-
return _position_list_wkt_coordinates
171-
172156

173157
class MultiLineString(_GeometryBase):
174158
"""MultiLineString Model"""
175159

176160
type: Literal["MultiLineString"]
177161
coordinates: MultiLineStringCoords
178162

163+
__wkt_coordinates__ = staticmethod(_lines_wtk_coordinates)
164+
179165
@property
180166
def has_z(self) -> bool:
181167
"""Checks if any coordinate has a Z value."""
182168
return _lines_has_z(self.coordinates)
183169

184-
@property
185-
def _wkt_coordinates(self) -> _WktCallable:
186-
return _lines_wtk_coordinates
187-
188170

189171
class LinearRingGeom(LineString):
190172
"""LinearRing model."""
@@ -204,6 +186,8 @@ class Polygon(_GeometryBase):
204186
type: Literal["Polygon"]
205187
coordinates: PolygonCoords
206188

189+
__wkt_coordinates__ = staticmethod(_lines_wtk_coordinates)
190+
207191
@validator("coordinates")
208192
def check_closure(cls, coordinates: List) -> List:
209193
"""Validate that Polygon is closed (first and last coordinate are the same)."""
@@ -229,10 +213,6 @@ def has_z(self) -> bool:
229213
"""Checks if any coordinates have a Z value."""
230214
return _lines_has_z(self.coordinates)
231215

232-
@property
233-
def _wkt_coordinates(self) -> _WktCallable:
234-
return _lines_wtk_coordinates
235-
236216
@classmethod
237217
def from_bounds(
238218
cls, xmin: float, ymin: float, xmax: float, ymax: float
@@ -252,15 +232,13 @@ class MultiPolygon(_GeometryBase):
252232
type: Literal["MultiPolygon"]
253233
coordinates: MultiPolygonCoords
254234

235+
__wkt_coordinates__ = staticmethod(_polygons_wkt_coordinates)
236+
255237
@property
256238
def has_z(self) -> bool:
257239
"""Checks if any coordinates have a Z value."""
258240
return any(_lines_has_z(polygon) for polygon in self.coordinates)
259241

260-
@property
261-
def _wkt_coordinates(self) -> _WktCallable:
262-
return _polygons_wkt_coordinates
263-
264242
@validator("coordinates")
265243
def check_closure(cls, coordinates: List) -> List:
266244
"""Validate that Polygon is closed (first and last coordinate are the same)."""
@@ -294,11 +272,6 @@ def __getitem__(self, index: int) -> Geometry:
294272
"""get geometry at a given index"""
295273
return self.geometries[index]
296274

297-
@property
298-
def _wkt_type(self) -> str:
299-
"""Return the WKT name of the geometry."""
300-
return self.type.upper()
301-
302275
@property
303276
def wkt(self) -> str:
304277
"""Return the Well Known Text representation."""
@@ -307,7 +280,7 @@ def wkt(self) -> str:
307280
if self.geometries
308281
else "EMPTY"
309282
)
310-
return f"{self._wkt_type} {coordinates}"
283+
return f"{self.type.upper()} {coordinates}"
311284

312285
@property
313286
def __geo_interface__(self) -> Dict[str, Any]:

0 commit comments

Comments
 (0)