Skip to content

Instantly share code, notes, and snippets.

@TravelTime-Frontend
Last active July 11, 2023 14:42
Show Gist options
  • Save TravelTime-Frontend/73091d1f0f1021c42378c10266d268a0 to your computer and use it in GitHub Desktop.
Save TravelTime-Frontend/73091d1f0f1021c42378c10266d268a0 to your computer and use it in GitHub Desktop.
Point in polygon Python
import asyncio
import csv
import json
from datetime import datetime
from pathlib import Path
from traveltimepy import TravelTimeSdk, Location, Coordinates, Property, Driving
async def main():
sdk = TravelTimeSdk("YOUR_APP_ID", "YOUR_APP_KEY")
CURRENT_TIME = "2022-11-17T15:00:00-05:00"
DATA_FILE = 'data/customers.csv'
RESULT_FILE = f'results/time_filter_{CURRENT_TIME}.json'
locations = []
with open(DATA_FILE) as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
is_first_row = True
for index, row in enumerate(csv_reader):
if is_first_row == False:
locations.append(
Location(id=row[0], coords=Coordinates(
lat=float(row[1]), lng=float(row[2])))
)
else:
is_first_row = False
departure_location = locations.pop(0)
location_ids = [location.id for location in locations]
results = await sdk.time_filter_async(
locations=[departure_location] + locations,
search_ids={departure_location.id: [
departure_location.id, *location_ids]},
departure_time=CURRENT_TIME,
travel_time=1800,
transportation=Driving(),
properties=[Property.TRAVEL_TIME],
)
Path("results").mkdir(parents=True, exist_ok=True)
f = open(RESULT_FILE, "w")
f.write(json.dumps([result.dict() for result in results], indent=2))
f.close()
print(
f"The number of unreachable customers - {len(results[0].unreachable)}")
asyncio.run(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment