From b4a93d0fd25dcfa2c5fcaabebc0724481e4f6ed7 Mon Sep 17 00:00:00 2001 From: arnasbr Date: Wed, 23 Oct 2024 12:10:25 +0300 Subject: [PATCH] Adding wkt format for time-map-fast --- README.md | 56 +++++++++++++++++++ .../dto/requests/time_map_fast_wkt.py | 31 ++++++++++ traveltimepy/mapper.py | 50 +++++++++++++++++ traveltimepy/sdk.py | 55 ++++++++++++++++++ 4 files changed, 192 insertions(+) create mode 100644 traveltimepy/dto/requests/time_map_fast_wkt.py diff --git a/README.md b/README.md index 2d9b8f9..a2a3ed4 100644 --- a/README.md +++ b/README.md @@ -350,6 +350,62 @@ async def main(): asyncio.run(main()) ``` +### WKT response + +#### Returns: + +* results: TimeMapWKTResponse - TimeMapWktResponse with isochrone shapes. + +#### Example: + +```python +import asyncio + +from traveltimepy import Coordinates, TravelTimeSdk +from traveltimepy.dto.requests.time_map_fast import Transportation + +async def main(): + sdk = TravelTimeSdk("YOUR_APP_ID", "YOUR_APP_KEY") + + results = await sdk.time_map_fast_wkt_async( + coordinates=[Coordinates(lat=51.507609, lng=-0.128315), Coordinates(lat=51.517609, lng=-0.138315)], + transportation=Transportation(type="driving+ferry"), + travel_time=900 + ) + + print(results) + +asyncio.run(main()) +``` + +### WKT_NO_HOLES response + +#### Returns: + +* results: TimeMapWKTResponse - TimeMapWktResponse with isochrone shapes (no holes). + +#### Example: + +```python +import asyncio + +from traveltimepy import Coordinates, TravelTimeSdk +from traveltimepy.dto.requests.time_map_fast import Transportation + +async def main(): + sdk = TravelTimeSdk("YOUR_APP_ID", "YOUR_APP_KEY") + + results = await sdk.time_map_fast_wkt_no_holes_async( + coordinates=[Coordinates(lat=51.507609, lng=-0.128315), Coordinates(lat=51.517609, lng=-0.138315)], + transportation=Transportation(type="driving+ferry"), + travel_time=900 + ) + + print(results) + +asyncio.run(main()) +``` + ### [Distance Map](https://docs.traveltime.com/api/reference/distance-map) Given origin coordinates, find shapes of zones reachable within corresponding travel distance. diff --git a/traveltimepy/dto/requests/time_map_fast_wkt.py b/traveltimepy/dto/requests/time_map_fast_wkt.py new file mode 100644 index 0000000..90f55e7 --- /dev/null +++ b/traveltimepy/dto/requests/time_map_fast_wkt.py @@ -0,0 +1,31 @@ +from typing import List +from traveltimepy.dto.requests.request import TravelTimeRequest +from traveltimepy.dto.requests.time_map_fast import ArrivalSearches +from traveltimepy.dto.responses.time_map_wkt import TimeMapWKTResponse +from traveltimepy.itertools import split, flatten + + +class TimeMapFastWKTRequest(TravelTimeRequest[TimeMapWKTResponse]): + arrival_searches: ArrivalSearches + + def split_searches(self, window_size: int) -> List[TravelTimeRequest]: + return [ + TimeMapFastWKTRequest( + arrival_searches=ArrivalSearches( + one_to_many=one_to_many, many_to_one=many_to_one + ), + ) + for one_to_many, many_to_one in split( + self.arrival_searches.one_to_many, + self.arrival_searches.many_to_one, + window_size, + ) + ] + + def merge(self, responses: List[TimeMapWKTResponse]) -> TimeMapWKTResponse: + return TimeMapWKTResponse( + results=sorted( + flatten([response.results for response in responses]), + key=lambda res: res.search_id, + ) + ) diff --git a/traveltimepy/mapper.py b/traveltimepy/mapper.py index 5958178..51a5e85 100644 --- a/traveltimepy/mapper.py +++ b/traveltimepy/mapper.py @@ -40,6 +40,7 @@ from traveltimepy.dto.requests.time_filter_fast import TimeFilterFastRequest from traveltimepy.dto.requests.time_map_fast import TimeMapFastRequest from traveltimepy.dto.requests.time_map_fast_geojson import TimeMapFastGeojsonRequest +from traveltimepy.dto.requests.time_map_fast_wkt import TimeMapFastWKTRequest from traveltimepy.dto.requests.time_filter_proto import ProtoTransportation from traveltimepy.dto.requests.postcodes_zones import ( PostcodesDistrictsRequest, @@ -220,6 +221,55 @@ def create_time_map_fast_geojson( ) +def create_time_map_fast_wkt( + coordinates: List[Coordinates], + transportation: time_map_fast.Transportation, + travel_time: int, + level_of_detail: Optional[LevelOfDetail], + snapping: Optional[Snapping], + polygons_filter: Optional[PolygonsFilter], + one_to_many: bool = True, +) -> TimeMapFastWKTRequest: + if one_to_many: + return TimeMapFastWKTRequest( + arrival_searches=time_map_fast.ArrivalSearches( + one_to_many=[ + time_map_fast.Search( + id=f"Search {ind}", + coords=cur_coordinates, + transportation=transportation, + travel_time=travel_time, + arrival_time_period="weekday_morning", + level_of_detail=level_of_detail, + snapping=snapping, + polygons_filter=polygons_filter, + ) + for ind, cur_coordinates in enumerate(coordinates) + ], + many_to_one=[], + ), + ) + else: + return TimeMapFastWKTRequest( + arrival_searches=time_map_fast.ArrivalSearches( + many_to_one=[ + time_map_fast.Search( + id=f"Search {ind}", + coords=cur_coordinates, + transportation=transportation, + travel_time=travel_time, + arrival_time_period="weekday_morning", + level_of_detail=level_of_detail, + snapping=snapping, + polygons_filter=polygons_filter, + ) + for ind, cur_coordinates in enumerate(coordinates) + ], + one_to_many=[], + ), + ) + + def create_time_filter_fast( locations: List[Location], search_ids: Dict[str, List[str]], diff --git a/traveltimepy/sdk.py b/traveltimepy/sdk.py index 9905816..5fd233a 100644 --- a/traveltimepy/sdk.py +++ b/traveltimepy/sdk.py @@ -72,6 +72,7 @@ create_intersection, create_time_map_fast, create_time_map_fast_geojson, + create_time_map_fast_wkt, create_union, create_time_map_geojson, create_time_map_wkt, @@ -273,6 +274,60 @@ async def time_map_fast_geojson_async( ) return resp + async def time_map_fast_wkt_async( + self, + coordinates: List[Coordinates], + transportation: time_map_fast.Transportation, + travel_time: int = 3600, + one_to_many: bool = True, + level_of_detail: Optional[LevelOfDetail] = None, + snapping: Optional[Snapping] = None, + polygons_filter: Optional[PolygonsFilter] = None, + ) -> TimeMapWKTResponse: + resp = await send_post_async( + TimeMapWKTResponse, + "time-map/fast", + self._headers(AcceptType.WKT), + create_time_map_fast_wkt( + coordinates, + transportation, + travel_time, + level_of_detail, + snapping, + polygons_filter, + one_to_many, + ), + self._sdk_params, + ) + return resp + + async def time_map_fast_wkt_no_holes_async( + self, + coordinates: List[Coordinates], + transportation: time_map_fast.Transportation, + travel_time: int = 3600, + one_to_many: bool = True, + level_of_detail: Optional[LevelOfDetail] = None, + snapping: Optional[Snapping] = None, + polygons_filter: Optional[PolygonsFilter] = None, + ) -> TimeMapWKTResponse: + resp = await send_post_async( + TimeMapWKTResponse, + "time-map/fast", + self._headers(AcceptType.WKT_NO_HOLES), + create_time_map_fast_wkt( + coordinates, + transportation, + travel_time, + level_of_detail, + snapping, + polygons_filter, + one_to_many, + ), + self._sdk_params, + ) + return resp + async def time_filter_fast_async( self, locations: List[Location],