Skip to content

Commit

Permalink
Implemented solution with own WKT lib
Browse files Browse the repository at this point in the history
  • Loading branch information
arnasbr committed Oct 24, 2023
1 parent 03d931c commit 0c47d8d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 27 deletions.
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ install_requires =
pydantic>=1.7.4,<2.0
typing-extensions
geojson-pydantic
simple-wkt
shapely
dacite
certifi >= 2021.5.30
Expand Down
36 changes: 9 additions & 27 deletions traveltimepy/dto/responses/time_map_wkt.py
Original file line number Diff line number Diff line change
@@ -1,43 +1,25 @@
from typing import List, Union, Dict, Any, TypeVar, Iterator, Optional
from pydantic import BaseModel, Field, validator
from pydantic.generics import GenericModel
from shapely import wkt
from shapely.errors import WKTReadingError

Props = TypeVar("Props", bound=Union[Dict[str, Any], BaseModel])


class WKT(BaseModel):
value: str = Field(..., description="WKT string representation of the geometry.")
from simple_wkt import WKTObject, parse_wkt

@validator("value", pre=True)
def validate_wkt(cls, value: str) -> str:
try:
wkt.loads(value)
return value
except WKTReadingError:
raise ValueError("Invalid WKT string")
Props = TypeVar("Props", bound=Union[Dict[str, Any], BaseModel])


class TimeMapWKTResult(GenericModel):
search_id: str
shape: WKT
shape: WKTObject
properties: Optional[Props] = Field(None)

@validator("shape", pre=True)
def transform_shape(cls, shape: str) -> Dict[str, str]:
return {"value": shape}

# Used for getting json format response
#
# Example:
# results_dicts = [result.dict() for result in results]
# results_json_str = json.dumps(results_dicts)
# print(results_json_str)
def dict(self, **kwargs):
original_dict = super().dict(**kwargs)
original_dict['shape'] = original_dict['shape']['value']
return original_dict
def transform_shape(cls, shape: str) -> WKTObject:
try:
# Assuming WKTObject's constructor can handle a WKT string
return parse_wkt(shape)
except WKTReadingError:
raise ValueError("Invalid WKT string")


class WKTResponseCollection(BaseModel):
Expand Down

0 comments on commit 0c47d8d

Please sign in to comment.