-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot_data.py
101 lines (78 loc) · 2.64 KB
/
plot_data.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import os
import asyncio
import osmnx as ox
import networkx as nx
import geopandas as gpd
from datetime import datetime
from dotenv import load_dotenv
import matplotlib.pyplot as plt
import matplotlib
from geopy.geocoders import Nominatim
from traveltimepy import (
TravelTimeSdk,
Coordinates,
Range,
PublicTransport,
Transportation,
)
from download_data import data_dir, network_path, boundary_path
# load environment variables from .env file
load_dotenv()
api_key = os.getenv("API_KEY")
app_id = os.getenv("APP_ID")
# set destination location
destination = "Central Station, Sydney, Australia"
# get coords for station
geolocator = Nominatim(user_agent="SydneyIsochrones")
location = geolocator.geocode(destination)
# traveltime sdk request
async def main():
sdk = TravelTimeSdk(app_id, api_key)
# create list of isochrones
isochrones = []
# iterate over request, modifying the travel time
for traveltime in [10, 20, 30, 45, 60, 90, 120]:
results = await sdk.time_map_geojson_async(
coordinates=[Coordinates(lat=location.latitude, lng=location.longitude)],
arrival_time="2024-04-22T09:30:00-10:00",
travel_time=(traveltime * 60),
transportation=PublicTransport(walking_time=(traveltime * 60)),
search_range=Range(enabled=True, width=(30 * 60)),
)
# add result (geojson) data to geodataframe
isochrone = gpd.GeoDataFrame.from_features(results)
# add gdf to list
isochrones.append(isochrone)
return isochrones
# run the traveltime async function and return list of isochrones
isochrones = asyncio.run(main())
# load data
network = ox.load_graphml(network_path)
boundary = gpd.read_file(boundary_path)
# plot the network, but do not show it or close it yet
fig, ax = ox.plot_graph(
network,
show=False,
close=False,
bgcolor="white",
edge_color="white",
edge_linewidth=0.3,
node_size=0,
)
# to this matplotlib axis, add the place shape(s)
boundary.plot(ax=ax, fc="lightgrey", ec="white", lw=1, alpha=1, zorder=-1)
# get the colormap for the isochrones
colors = matplotlib.colormaps["RdYlGn_r"]
for i, isochrone in enumerate(isochrones):
# get the color as a tuple of RGBA values
rgba_color = colors(i / len(isochrones))
# convert the RGBA color to a hexadecimal color code
hex_color = matplotlib.colors.to_hex(rgba_color)
# plot the isochrone
isochrone.plot(
ax=ax, fc=hex_color, ec=None, lw=1, alpha=1, zorder=(len(isochrones) - i)
)
# plot station location on the map
plt.scatter(location.longitude, location.latitude, color="red", zorder=100)
# show plot
plt.show()