Skip to content

Commit

Permalink
Merge pull request #67 from openaddresses/switch-to-protomaps-for-pre…
Browse files Browse the repository at this point in the history
…view

Switch to requesting tiles from Protomaps instead of Mapbox
  • Loading branch information
iandees authored Nov 19, 2024
2 parents 25e8d33 + e2e4c66 commit e315930
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 30 deletions.
33 changes: 16 additions & 17 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/v4/{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 @@ -167,7 +166,7 @@ def projected_geom(geometry, mx, bx, my, by):
landuse_xform = get_transform(tile['landuse']['extent'], *bounds)
for feature in tile['landuse']['features']:
if 'Polygon' in feature['geometry']['type']:
if feature['properties'].get('class') in ('cemetery', 'forest', 'golf_course', 'grave_yard', 'meadow', 'park', 'pitch', 'wood'):
if feature['properties'].get('kind') in ('cemetery', 'forest', 'golf_course', 'grave_yard', 'meadow', 'park', 'pitch', 'wood'):
landuse_geoms.append(projected_geom(feature['geometry'], *landuse_xform))

if 'water' in tile:
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))
if feature['properties'].get('kind') in ('highway') and feature['properties'].get('kind_detail') 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'], *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())
Binary file removed openaddr/tests/data/mapbox-tile.mvt
Binary file not shown.
Binary file added openaddr/tests/data/protomaps-tile-7-20-49.mvt
Binary file not shown.
24 changes: 11 additions & 13 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/v4'):
if 'key=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,23 +93,21 @@ 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/v4'):
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:
with open(join(dirname(__file__), 'data', 'protomaps-tile-7-20-49.mvt'), 'rb') as file:
data = file.read()
return response(200, data, headers={'Content-Type': 'application/vnd.mapbox-vector-tile'})
raise Exception("Uknown URL")
raise Exception("Unknown URL")

xmin, ymin, xmax, ymax = -13611952, 4551290, -13609564, 4553048
scale = 100 / (xmax - xmin)

with HTTMock(response_content):
landuse_geoms, water_geoms, roads_geoms = \
preview.get_map_features(xmin, ymin, xmax, ymax, 2, scale, 'mapbox-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')

preview.get_map_features(xmin, ymin, xmax, ymax, 2, scale, 'protomaps-XXXX')

self.assertEqual(len(landuse_geoms), 230, 'Should have 230 landuse geometries')
self.assertEqual(len(water_geoms), 9, 'Should have 9 water geometry')
self.assertEqual(len(roads_geoms), 44, 'Should have 44 road geometries')

0 comments on commit e315930

Please sign in to comment.