Skip to content

Commit

Permalink
Adding wkt format for time-map-fast
Browse files Browse the repository at this point in the history
  • Loading branch information
arnasbr committed Oct 23, 2024
1 parent 4003e5e commit b4a93d0
Show file tree
Hide file tree
Showing 4 changed files with 192 additions and 0 deletions.
56 changes: 56 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
31 changes: 31 additions & 0 deletions traveltimepy/dto/requests/time_map_fast_wkt.py
Original file line number Diff line number Diff line change
@@ -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,
)
)
50 changes: 50 additions & 0 deletions traveltimepy/mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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]],
Expand Down
55 changes: 55 additions & 0 deletions traveltimepy/sdk.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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],
Expand Down

0 comments on commit b4a93d0

Please sign in to comment.