Skip to content

Commit e4d7044

Browse files
committed
Add highres option for Strava (requires auth)
1 parent f599269 commit e4d7044

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

config.example.yaml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1+
# PostGIS database details
12
database:
23
dbname: dbname
34
user: user
45
password: password
56
host: host
6-
port: 5432
7+
port: 5432
8+
9+
# Strava authed endpoint (optional)
10+
strava:
11+
headers:
12+
Cookie: cookie string

dataproviders.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,11 @@ class CachedTiledDataProvider(TiledDataProvider):
6161
DataProvider class for tiled maps with simple dictionary based caching.
6262
"""
6363

64-
def __init__(self, url_template, value_fn, tile_size=256, zoom=12, convert_args=None):
64+
def __init__(self, url_template, value_fn, tile_size=256, zoom=12, convert_args=None, headers=None):
6565
super().__init__(url_template, value_fn, tile_size, zoom)
6666
self.logger = logging.getLogger('dataproviders.CachedTiledDataProvider')
6767
self.convert_args = convert_args
68+
self.headers = headers
6869
self.cache = {}
6970

7071
def get_value(self, lng: float, lat: float) -> float:
@@ -94,7 +95,7 @@ def get_fresh_tile(self, x: int, y: int, z: int) -> Optional[Image.Image]:
9495
"""
9596
url = self.url.substitute({'x': x, 'y': y, 'z': z})
9697
try:
97-
response = requests.get(url)
98+
response = requests.get(url, headers=self.headers)
9899
img = Image.open(BytesIO(response.content))
99100
if self.convert_args:
100101
return img.convert(**self.convert_args)

update_ways_metadata.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,26 @@ def strava_value(img, x, y):
7272
upsert_ways_metadata(cursor, 'popularity', ways_metadata)
7373

7474

75+
def process_strava_heatmap_highres(cursor, config) -> None:
76+
"""
77+
Gathers data from Strava heatmap tile maps
78+
79+
:param cursor: database cursor object
80+
:param config: parsed config.yaml
81+
"""
82+
83+
def strava_value(img, x, y):
84+
return float(img.getpixel((x, y))) / 255
85+
86+
logger.info('Gathering Highres Strava popularity data.')
87+
heaturl = 'https://heatmap-external-b.strava.com/tiles-auth/all/hot/${z}/${x}/${y}.png'
88+
strava_provider = CachedTiledDataProvider(heaturl, strava_value, tile_size=512, zoom=15, convert_args={'mode': 'L'},
89+
headers=config['strava']['headers'])
90+
91+
ways_metadata = extract_ways_metadata(cursor, strava_provider)
92+
upsert_ways_metadata(cursor, 'popularity_highres', ways_metadata)
93+
94+
7595
def process_gmaps_satellite(cursor) -> None:
7696
"""
7797
Gathers data from Google satellite tile maps
@@ -103,6 +123,7 @@ def main():
103123
with psycopg2.connect(**config['database']) as conn:
104124
cursor = conn.cursor()
105125
process_strava_heatmap(cursor)
126+
# process_strava_heatmap_highres(cursor, config)
106127
process_gmaps_satellite(cursor)
107128
cursor.close()
108129
logger.info('Script finished.')

0 commit comments

Comments
 (0)