Skip to content

Commit

Permalink
Adding wkt format for time-map-fast (#149)
Browse files Browse the repository at this point in the history
* Adding wkt format for time-map-fast

* Adding remove_water_bodies param to readme
  • Loading branch information
arnasbr authored Oct 24, 2024
1 parent 9167e3b commit a84cbb6
Show file tree
Hide file tree
Showing 5 changed files with 252 additions and 0 deletions.
58 changes: 58 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ Given origin coordinates, find shapes of zones reachable within corresponding tr
* search_range: Range - When enabled, range adds an arrival window to the arrival time, and results are returned for any
journeys that arrive during this window.
* [level_of_detail](#level-of-detail): LevelOfDetail - When enabled, allows the user to specify how detailed the isochrones should be.
* remove_water_bodies: bool - if set to true (default) - returned shape will not cover large nearby water bodies.
False - returned shape may cover nearby water bodies like large lakes, wide rivers and seas.
* [snapping](#snapping): Snapping - Adjusts the process of looking up the nearest roads from the departure / arrival points.

### JSON response
Expand Down Expand Up @@ -350,6 +352,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
58 changes: 58 additions & 0 deletions tests/time_map_fast_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,61 @@ async def test_many_to_one_geojson(sdk: TravelTimeSdk):
)

assert len(results) == 2


@pytest.mark.asyncio
async def test_one_to_many_wkt(sdk: TravelTimeSdk):
response = await sdk.time_map_fast_wkt_async(
coordinates=[
Coordinates(lat=51.507609, lng=-0.128315),
Coordinates(lat=51.517609, lng=-0.138315),
],
travel_time=900,
transportation=Transportation(type="public_transport"),
)

assert len(response.results) == 2


@pytest.mark.asyncio
async def test_many_to_one_wkt(sdk: TravelTimeSdk):
response = await sdk.time_map_fast_wkt_async(
coordinates=[
Coordinates(lat=51.507609, lng=-0.128315),
Coordinates(lat=51.517609, lng=-0.138315),
],
travel_time=900,
transportation=Transportation(type="public_transport"),
one_to_many=False,
)

assert len(response.results) == 2


@pytest.mark.asyncio
async def test_one_to_many_wkt_no_holes(sdk: TravelTimeSdk):
response = await sdk.time_map_fast_wkt_no_holes_async(
coordinates=[
Coordinates(lat=51.507609, lng=-0.128315),
Coordinates(lat=51.517609, lng=-0.138315),
],
travel_time=900,
transportation=Transportation(type="public_transport"),
)

assert len(response.results) == 2


@pytest.mark.asyncio
async def test_many_to_one_wkt_no_holes(sdk: TravelTimeSdk):
response = await sdk.time_map_fast_wkt_no_holes_async(
coordinates=[
Coordinates(lat=51.507609, lng=-0.128315),
Coordinates(lat=51.517609, lng=-0.138315),
],
travel_time=900,
transportation=Transportation(type="public_transport"),
one_to_many=False,
)

assert len(response.results) == 2
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 a84cbb6

Please sign in to comment.