Skip to content

Commit

Permalink
Switch to requesting tiles from Protomaps instead of Mapbox
Browse files Browse the repository at this point in the history
  • Loading branch information
iandees committed Nov 19, 2024
1 parent 25e8d33 commit 694b4e6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 23 deletions.
29 changes: 14 additions & 15 deletions openaddr/preview.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import logging; _L = logging.getLogger('openaddr.preview')

from tempfile import mkstemp
from math import pow, sqrt, pi, log
from math import sqrt, pi, log
from argparse import ArgumentParser
import json, itertools, os, struct
import json, itertools

import requests, uritemplate, mapbox_vector_tile

Expand All @@ -15,7 +14,7 @@
# http://stackoverflow.com/questions/11491268/install-pycairo-in-virtualenv
import cairocffi as cairo

TILE_URL = 'http://a.tiles.mapbox.com/v4/mapbox.mapbox-streets-v7/{z}/{x}/{y}.mvt{?access_token}'
TILE_URL = 'https://api.protomaps.com/tiles/v3/{z}/{x}/{y}.mvt{?key}'
EARTH_DIAMETER = 6378137 * 2 * pi
FORMAT = 'ff'

Expand All @@ -25,7 +24,7 @@
# Web Mercator, https://trac.osgeo.org/openlayers/wiki/SphericalMercator
EPSG900913 = '+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +no_defs'

def render(src_filename, png_filename, width, resolution, mapbox_key):
def render(src_filename, png_filename, width, resolution, protomaps_key):
'''
'''
try:
Expand Down Expand Up @@ -56,7 +55,7 @@ def render(src_filename, png_filename, width, resolution, mapbox_key):
context.fill()

landuse_geoms, water_geoms, roads_geoms = \
get_map_features(xmin, ymin, xmax, ymax, resolution, scale, mapbox_key)
get_map_features(xmin, ymin, xmax, ymax, resolution, scale, protomaps_key)

fill_geometries(context, landuse_geoms, muppx, park_fill)
fill_geometries(context, water_geoms, muppx, water_fill)
Expand Down Expand Up @@ -107,7 +106,7 @@ def iterate_file_geoms(filename):

del project, geom

def get_map_features(xmin, ymin, xmax, ymax, resolution, scale, mapbox_key):
def get_map_features(xmin, ymin, xmax, ymax, resolution, scale, protomaps_key):
'''
'''
zoom = round(calculate_zoom(scale, resolution))
Expand Down Expand Up @@ -155,7 +154,7 @@ def projected_geom(geometry, mx, bx, my, by):
return geom

for (row, col) in row_cols:
url = uritemplate.expand(TILE_URL, dict(z=zoom, x=col, y=row, access_token=mapbox_key))
url = uritemplate.expand(TILE_URL, dict(z=zoom, x=col, y=row, key=protomaps_key))

_L.debug('Getting tile {}'.format(url))

Expand All @@ -176,12 +175,12 @@ def projected_geom(geometry, mx, bx, my, by):
if 'Polygon' in feature['geometry']['type']:
water_geoms.append(projected_geom(feature['geometry'], *water_xform))

if 'road' in tile:
road_xform = get_transform(tile['road']['extent'], *bounds)
for feature in tile['road']['features']:
if 'roads' in tile:
roads_xform = get_transform(tile['roads']['extent'], *bounds)
for feature in tile['roads']['features']:
if 'LineString' in feature['geometry']['type']:
if feature['properties'].get('class') in ('motorway', 'motorway_link', 'trunk', 'primary', 'secondary', 'tertiary', 'link', 'street', 'street_limited', 'pedestrian', 'construction', 'track', 'service', 'major_rail', 'minor_rail'):
roads_geoms.append(projected_geom(feature['geometry'], *road_xform))
roads_geoms.append(projected_geom(feature['geometry'], *roads_xform))

return landuse_geoms, water_geoms, roads_geoms

Expand Down Expand Up @@ -367,8 +366,8 @@ def draw_line(ctx, start, points):
parser.add_argument('--width', dest='width', type=int,
help='Width in pixels.')

parser.add_argument('--mapbox-key', dest='mapbox_key',
help='Mapbox API Key. See: https://mapbox.com/')
parser.add_argument('--protomaps-key', dest='protomaps_key',
help='Protomaps API Key. See: https://protomaps.com/dashboard')

parser.add_argument('-v', '--verbose', help='Turn on verbose logging',
action='store_const', dest='loglevel',
Expand All @@ -380,7 +379,7 @@ def draw_line(ctx, start, points):

def main():
args = parser.parse_args()
render(args.src_geojson, args.png_filename, args.width, args.resolution, args.mapbox_key)
render(args.src_geojson, args.png_filename, args.width, args.resolution, args.protomaps_key)

if __name__ == '__main__':
exit(main())
14 changes: 6 additions & 8 deletions openaddr/tests/preview.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ def test_render_geojson(self):
'''
'''
def response_content(url, request):
if url.hostname == 'a.tiles.mapbox.com' and url.path.startswith('/v4/mapbox.mapbox-streets-v7'):
if 'access_token=mapbox-XXXX' not in url.query:
if url.hostname == 'api.protomaps.com' and url.path.startswith('/tiles/v3'):
if 'access_token=protomaps-XXXX' not in url.query:
raise ValueError('Missing or wrong API key')
data = b'\x1a\'x\x02\n\x05water(\x80 \x12\x19\x18\x03"\x13\t\xe0\x7f\xff\x1f\x1a\x00\xe0\x9f\x01\xdf\x9f\x01\x00\x00\xdf\x9f\x01\x0f\x08\x00'
return response(200, data, headers={'Content-Type': 'application/vnd.mapbox-vector-tile'})
Expand All @@ -78,7 +78,7 @@ def response_content(url, request):
temp_dir = tempfile.mkdtemp(prefix='test_render_geojson-')

with HTTMock(response_content):
preview.render(join(dirname(__file__), 'outputs', 'denver-metro-preview.geojson'), png_filename, 668, 1, 'mapbox-XXXX')
preview.render(join(dirname(__file__), 'outputs', 'denver-metro-preview.geojson'), png_filename, 668, 1, 'protomaps-XXXX')

info = str(subprocess.check_output(('file', png_filename)))

Expand All @@ -93,8 +93,8 @@ def test_get_map_features(self):
'''
'''
def response_content(url, request):
if url.hostname == 'a.tiles.mapbox.com' and url.path.startswith('/v4/mapbox.mapbox-streets-v7'):
if 'access_token=mapbox-XXXX' not in url.query:
if url.hostname == 'api.protomaps.com' and url.path.startswith('/tiles/v3'):
if 'key=protomaps-XXXX' not in url.query:
raise ValueError('Missing or wrong API key')
with open(join(dirname(__file__), 'data', 'mapbox-tile.mvt'), 'rb') as file:
data = file.read()
Expand All @@ -106,10 +106,8 @@ def response_content(url, request):

with HTTMock(response_content):
landuse_geoms, water_geoms, roads_geoms = \
preview.get_map_features(xmin, ymin, xmax, ymax, 2, scale, 'mapbox-XXXX')
preview.get_map_features(xmin, ymin, xmax, ymax, 2, scale, 'protomaps-XXXX')

self.assertEqual(len(landuse_geoms), 90, 'Should have 90 landuse geometries')
self.assertEqual(len(water_geoms), 1, 'Should have 1 water geometry')
self.assertEqual(len(roads_geoms), 792, 'Should have 792 road geometries')


0 comments on commit 694b4e6

Please sign in to comment.