Skip to content

Commit

Permalink
Merge pull request #90 from traveltime-dev/add-distance-proto
Browse files Browse the repository at this point in the history
Added distance calculation support for proto
  • Loading branch information
arnasbr authored Oct 30, 2023
2 parents ab2844c + 7e284e1 commit 745e367
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 16 deletions.
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -342,17 +342,19 @@ larger limits on the amount of destination points.
* country: ProtoCountry - Return the results that are within the specified country.
* one_to_many: boolean - if one_to_many is equal to true, then it'll be a forward search (one to many matrix), false -
backward search (many to one matrix). Default value is True.
* properties: List[PropertyProto] - specifies which extra properties should be calculated in the response.

#### Returns:

* travel_times: List[int] - The responses are in the form of a list where each position denotes either a travel time (in
seconds) of a journey, or if negative that the journey from the origin to the destination point is impossible.

* results: TimeFilterProtoResponse - The response contains:
* list of travel times, where each position denotes either a travel time (in seconds)
of a journey, or if travel time is negative, that the journey from the origin to the destination point is impossible.
* (optional) list of distances where each position denotes distance (in meters) to the specified location.
#### Example:

```python
import asyncio
from traveltimepy import ProtoCountry, Coordinates, ProtoTransportation, TravelTimeSdk
from traveltimepy import ProtoCountry, Coordinates, ProtoTransportation, TravelTimeSdk, PropertyProto

async def main():
sdk = TravelTimeSdk("YOUR_APP_ID", "YOUR_APP_KEY")
Expand All @@ -364,7 +366,8 @@ async def main():
],
transportation=ProtoTransportation.DRIVING_FERRY,
travel_time=7200,
country=ProtoCountry.UNITED_KINGDOM
country=ProtoCountry.UNITED_KINGDOM,
properties=[PropertyProto.DISTANCE],
)

print(travel_times)
Expand Down
44 changes: 39 additions & 5 deletions tests/time_filter_proto_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest

from traveltimepy import Coordinates
from traveltimepy.dto.common import PropertyProto
from traveltimepy.dto.requests.time_filter_proto import (
ProtoTransportation,
ProtoCountry,
Expand All @@ -9,7 +10,7 @@

@pytest.mark.asyncio
async def test_one_to_many(proto_sdk):
travel_times = await proto_sdk.time_filter_proto_async(
results = await proto_sdk.time_filter_proto_async(
origin=Coordinates(lat=51.425709, lng=-0.122061),
destinations=[
Coordinates(lat=51.348605, lng=-0.314783),
Expand All @@ -19,12 +20,12 @@ async def test_one_to_many(proto_sdk):
travel_time=7200,
country=ProtoCountry.UNITED_KINGDOM,
)
assert len(travel_times) == 2
assert len(results.travel_times) == 2 and len(results.distances) == 0


@pytest.mark.asyncio
async def test_many_to_one(proto_sdk):
travel_times = await proto_sdk.time_filter_proto_async(
results = await proto_sdk.time_filter_proto_async(
origin=Coordinates(lat=51.425709, lng=-0.122061),
destinations=[
Coordinates(lat=51.348605, lng=-0.314783),
Expand All @@ -33,6 +34,39 @@ async def test_many_to_one(proto_sdk):
transportation=ProtoTransportation.DRIVING_FERRY,
travel_time=7200,
country=ProtoCountry.UNITED_KINGDOM,
one_to_many=False
one_to_many=False,
)
assert len(travel_times) == 2
assert len(results.travel_times) == 2 and len(results.distances) == 0


@pytest.mark.asyncio
async def test_one_to_many_with_distances(proto_sdk):
results = await proto_sdk.time_filter_proto_async(
origin=Coordinates(lat=51.425709, lng=-0.122061),
destinations=[
Coordinates(lat=51.348605, lng=-0.314783),
Coordinates(lat=51.337205, lng=-0.315793),
],
transportation=ProtoTransportation.DRIVING_FERRY,
travel_time=7200,
country=ProtoCountry.UNITED_KINGDOM,
properties=[PropertyProto.DISTANCE],
)
assert len(results.travel_times) == 2 and len(results.distances) == 2


@pytest.mark.asyncio
async def test_many_to_one_with_distances(proto_sdk):
results = await proto_sdk.time_filter_proto_async(
origin=Coordinates(lat=51.425709, lng=-0.122061),
destinations=[
Coordinates(lat=51.348605, lng=-0.314783),
Coordinates(lat=51.337205, lng=-0.315793),
],
transportation=ProtoTransportation.DRIVING_FERRY,
travel_time=7200,
country=ProtoCountry.UNITED_KINGDOM,
one_to_many=False,
properties=[PropertyProto.DISTANCE],
)
assert len(results.travel_times) == 2 and len(results.distances) == 2
2 changes: 2 additions & 0 deletions traveltimepy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
Coordinates,
Location,
Property,
PropertyProto,
FullRange,
Range,
Rectangle,
Expand All @@ -43,6 +44,7 @@
"Coordinates",
"Location",
"Property",
"PropertyProto",
"FullRange",
"Range",
"Rectangle",
Expand Down
4 changes: 4 additions & 0 deletions traveltimepy/dto/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ class Property(str, Enum):
FARES = "fares"


class PropertyProto(int, Enum):
DISTANCE = 1


class FullRange(BaseModel):
enabled: bool
max_results: int
Expand Down
1 change: 1 addition & 0 deletions traveltimepy/dto/responses/time_filter_proto.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@

class TimeFilterProtoResponse(BaseModel):
travel_times: List[int]
distances: List[int]
6 changes: 6 additions & 0 deletions traveltimepy/mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
Property,
Range,
LevelOfDetail,
PropertyProto,
)
from traveltimepy.dto.transportation import (
PublicTransport,
Expand Down Expand Up @@ -667,6 +668,7 @@ def create_proto_request(
origin: Coordinates,
destinations: List[Coordinates],
transportation: ProtoTransportation,
properties: Optional[List[PropertyProto]],
travel_time: int,
one_to_many: bool = True,
) -> TimeFilterFastRequest_pb2.TimeFilterFastRequest:
Expand All @@ -681,6 +683,8 @@ def create_proto_request(
request.oneToManyRequest.arrivalTimePeriod = (
TimeFilterFastRequest_pb2.TimePeriod.WEEKDAY_MORNING
)
if properties is not None:
request.oneToManyRequest.properties.extend(properties)

mult = math.pow(10, 5)
for destination in destinations:
Expand All @@ -696,6 +700,8 @@ def create_proto_request(
request.manyToOneRequest.arrivalTimePeriod = (
TimeFilterFastRequest_pb2.TimePeriod.WEEKDAY_MORNING
)
if properties is not None:
request.manyToOneRequest.properties.extend(properties)

mult = math.pow(10, 5)
for destination in destinations:
Expand Down
5 changes: 3 additions & 2 deletions traveltimepy/proto_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ async def send_proto_async(
data: TimeFilterFastRequest,
app_id: str,
api_key: str,
) -> TimeFilterFastResponse:
) -> TimeFilterProtoResponse:
async with ClientSession(connector=TCPConnector(ssl=False)) as session:
async with session.post(
url=url,
Expand All @@ -36,5 +36,6 @@ async def _process_response(response: ClientResponse) -> TimeFilterProtoResponse
response_body = TimeFilterFastResponse()
response_body.ParseFromString(content)
return TimeFilterProtoResponse(
travel_times=response_body.properties.travelTimes[:]
travel_times=response_body.properties.travelTimes[:],
distances=response_body.properties.distances[:],
)
14 changes: 11 additions & 3 deletions traveltimepy/sdk.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
FullRange,
Range,
LevelOfDetail,
PropertyProto,
)
from traveltimepy.dto.responses.time_filter_proto import TimeFilterProtoResponse
from traveltimepy.dto.transportation import (
PublicTransport,
Driving,
Expand Down Expand Up @@ -361,17 +363,23 @@ async def time_filter_proto_async(
transportation: ProtoTransportation,
travel_time: int,
one_to_many: bool = True,
) -> List[int]:
properties: Optional[List[PropertyProto]] = None,
) -> TimeFilterProtoResponse:
resp = await send_proto_async(
f"https://{self._sdk_params.proto_host}/api/v2/{country.value}/time-filter/fast/{transportation.value.name}", # noqa
self._proto_headers(),
create_proto_request(
origin, destinations, transportation, travel_time, one_to_many
origin,
destinations,
transportation,
properties,
travel_time,
one_to_many,
),
self._app_id,
self._api_key,
)
return resp.travel_times
return resp

async def intersection_async(
self,
Expand Down
2 changes: 1 addition & 1 deletion traveltimepy/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "3.6.2"
__version__ = "3.7.0"

0 comments on commit 745e367

Please sign in to comment.