def way_full(way_id, zoom=16): url = 'https://api.openstreetmap.org/api/0.6/way/%d/full' % (way_id,) headers = {'user-agent': 'mktest.py/0.0.1 (https://github.com/tilezen)'} r = requests.get(url, headers=headers) root = ET.fromstring(r.content) assert root.tag == 'osm' nodes = {} nds = [] tags = {} for child in root: if child.tag == 'node': node_id = int(child.attrib['id']) nodes[node_id] = child elif child.tag == 'way': assert way_id == int(child.attrib['id']) for wchild in child: if wchild.tag == 'nd': nds.append(int(wchild.attrib['ref'])) elif wchild.tag == 'tag': k = wchild.attrib['k'] v = wchild.attrib['v'] tags[k] = v assert nds first_node = nodes[nds[0]] lat = float(first_node.attrib['lat']) lon = float(first_node.attrib['lon']) x, y = tile.deg2num(lat, lon, zoom) tags['source'] = 'openstreetmap.org' return Coordinate(zoom=zoom, column=x, row=y), tags
def _overpass_element(layer_name, query_fn, args): import json result = query_fn(args.query) pos = result.position x, y = tile.deg2num(pos[0], pos[1], args.zoom) coord = Coordinate(zoom=args.zoom, column=x, row=y) expect = json.loads(args.expect) if args.expect else None if expect: name = '_'.join(_make_ident(v) for v in expect.values()) else: name = 'FIXME' name = name + '_' + result.element_type() tags = result.tags tags['source'] = 'openstreetmap.org' params = dict( name=name, z=args.zoom, x=coord.column, y=coord.row, elt_id=result.element_id, tags=tags, expect=expect, layer_name=layer_name, geom_fn_name=result.geom_fn_name(), geom_fn_arg=result.geom_fn_arg(), elt_type=result.element_type(), ) output = _render_template('overpass_test', params) print output.encode('utf-8')
def node_test(args): import json position, node_tags = node(args.node_id, args.zoom) x, y = tile.deg2num(position[1], position[0], args.zoom) coord = Coordinate(zoom=args.zoom, column=x, row=y) expect = json.loads(args.expect) if args.expect else None if expect: name = '_'.join(_make_ident(v) for v in expect.values()) else: name = 'FIXME' args = dict( name=name, z=args.zoom, x=coord.column, y=coord.row, position=position, node_id=args.node_id, node_tags=node_tags, expect=expect, ) output = _render_template('node_test', args) print output.encode('utf-8')
def test_united_kingdom(self): # in the absence of data joined from NE, we should fall back to a # default based on the country that the label point is in. import dsl from tilequeue.tile import deg2num lon, lat = (-3.2765753, 54.7023545) z = 5 x, y = deg2num(lat, lon, z) self.generate_fixtures( dsl.is_in('GB', z, x, y), # https://www.openstreetmap.org/node/838090640 dsl.point( 838090640, (lon, lat), { 'name': u'United Kingdom', 'place': u'country', 'population': u'61792000', 'source': u'openstreetmap.org', 'wikidata': u'Q145', 'wikipedia': u'de:United Kingdom', # LOL, de: }), ) self.assert_has_feature(z, x, y, 'places', { 'id': 838090640, 'min_zoom': 1.7, 'max_zoom': 6.7, })
def test_ne_min_zoom_should_override_default(self): import dsl from tilequeue.tile import deg2num lon, lat = (-3.2765753, 54.7023545) z = 5 x, y = deg2num(lat, lon, z) self.generate_fixtures( dsl.is_in('GB', z, x, y), # https://www.openstreetmap.org/node/838090640 dsl.point( 838090640, (lon, lat), { 'name': u'United Kingdom', 'place': u'country', 'population': u'61792000', 'source': u'openstreetmap.org', 'wikidata': u'Q145', 'wikipedia': u'de:United Kingdom', # LOL, de: # NE joins should override defaults from location '__ne_min_zoom': 0, '__ne_max_zoom': 16, }), ) self.assert_has_feature(z, x, y, 'places', { 'id': 838090640, 'min_zoom': 0, 'max_zoom': 16, })
def test_united_kingdom(self): # in the absence of data joined from NE, we should fall back to a # default based on the country that the label point is in. import dsl from tilequeue.tile import deg2num lon, lat = (-3.2765753, 54.7023545) z = 6 x, y = deg2num(lat, lon, z) self.generate_fixtures( dsl.is_in('GB', z, x, y), # https://www.openstreetmap.org/node/838090640 dsl.point(838090640, (lon, lat), { 'name': u'United Kingdom', 'place': u'country', 'population': u'61792000', 'source': u'openstreetmap.org', 'wikidata': u'Q145', 'wikipedia': u'de:United Kingdom', # LOL, de: }), ) self.assert_has_feature( z, x, y, 'places', { 'id': 838090640, 'min_zoom': 6, 'max_zoom': 6.7, })
def test_ne_min_zoom_should_override_default(self): import dsl from tilequeue.tile import deg2num lon, lat = (-3.2765753, 54.7023545) z = 5 x, y = deg2num(lat, lon, z) self.generate_fixtures( dsl.is_in('GB', z, x, y), # https://www.openstreetmap.org/node/838090640 dsl.point(838090640, (lon, lat), { 'name': u'United Kingdom', 'place': u'country', 'population': u'61792000', 'source': u'openstreetmap.org', 'wikidata': u'Q145', 'wikipedia': u'de:United Kingdom', # LOL, de: # NE joins should override defaults from location '__ne_min_zoom': 0, '__ne_max_zoom': 16, }), ) self.assert_has_feature( z, x, y, 'places', { 'id': 838090640, 'min_zoom': 0, 'max_zoom': 16, })
def test_uk_should_not_show_up_zoom_7(self): # shouldn't be in the zoom 0 tile because max_zoom < 7 from tilequeue.tile import deg2num zoom = 7 x, y = deg2num(self.lat, self.lon, zoom) self.assert_no_matching_feature(zoom, x, y, 'places', {'id': 838090640})
def test_uk_should_not_show_up_zoom_7(self): # shouldn't be in the zoom 7 tile because max_zoom < 7 from tilequeue.tile import deg2num zoom = 7 x, y = deg2num(self.lat, self.lon, zoom) self.assert_no_matching_feature( zoom, x, y, 'places', {'id': 838090640})
def test_uk_should_not_show_up_zoom_0_to_1(self): from tilequeue.tile import deg2num # shouldn't be in the zoom 0 or zoom 1 tiles because min_zoom >= 1.5 for zoom in xrange(0, 1): x, y = deg2num(self.lat, self.lon, zoom) self.assert_no_matching_feature( zoom, x, y, 'places', {'id': 838090640})
def test_uk_should_not_show_up_zoom_0_to_1(self): from tilequeue.tile import deg2num # shouldn't be in the zoom 0 or zoom 1 tiles because min_zoom >= 1.5 for zoom in xrange(0, 1): x, y = deg2num(self.lat, self.lon, zoom) self.assert_no_matching_feature(zoom, x, y, 'places', {'id': 838090640})
def test_uk_should_show_up_zooms_1_to_6(self): from tilequeue.tile import deg2num # should show up in zooms within the range 1-6 for zoom in xrange(1, 6): x, y = deg2num(self.lat, self.lon, zoom) self.assert_has_feature(zoom, x, y, 'places', { 'id': 838090640, 'min_zoom': 1.7, 'max_zoom': 6.7, })
def test_uk_should_show_up_zooms_2_to_6(self): from tilequeue.tile import deg2num # should show up in zooms within the range 2-6 for zoom in xrange(2, 6): x, y = deg2num(self.lat, self.lon, zoom) self.assert_has_feature( zoom, x, y, 'places', { 'id': 838090640, 'min_zoom': 2.0, 'max_zoom': 6.7, })
def naturalearth_test(args): import json where = compile(args.where, '<command line arguments>', 'eval') feature = None with _ne_features_from_zip(args.zip) as features: for shape, props, fid in features: if eval(where, {}, props): feature = (shape, props, fid) break if not feature: raise RuntimeError("Unable to find item in NE zip %r matching %r" % (args.zip, args.where)) expect = json.loads(args.expect) if args.expect else None if expect: name = '_'.join(_make_ident(v) for v in expect.values()) else: name = 'FIXME' if shape.geom_type in ('Point', 'Multipoint'): geom_func = 'tile_centre_shape' lon, lat = shape.coords[0] else: raise RuntimeError("Haven't implemented NE shape type %r yet." % (shape.geom_type,)) x, y = tile.deg2num(lat, lon, args.zoom) coord = Coordinate(zoom=args.zoom, column=x, row=y) props['source'] = 'naturalearthdata.com' args = dict( name=name, z=args.zoom, x=coord.column, y=coord.row, geom_func=geom_func, ne_id=args.ne_id, iso_code=args.is_in, props=props, expect=expect, layer_name=args.layer_name, ) output = _render_template('naturalearth_test', args) print output.encode('utf-8')
def naturalearth_test(args): import json from shapely.ops import transform from tilequeue.tile import reproject_lnglat_to_mercator where = compile(args.where, '<command line arguments>', 'eval') feature = None with _ne_features_from_zip(args.zip) as features: for shape, props, fid in features: if eval(where, {}, props): feature = (shape, props, fid) break if not feature: raise RuntimeError("Unable to find item in NE zip %r matching %r" % (args.zip, args.where)) expect = json.loads(args.expect) if args.expect else None if expect: name = '_'.join(_make_ident(v) for v in expect.values()) else: name = 'FIXME' if shape.geom_type in ('Point', 'Multipoint'): geom_func = 'tile_centre_shape' geom_extra_args = '' lon, lat = shape.coords[0] elif shape.geom_type in ('Polygon', 'MultiPolygon'): shape_merc = transform(reproject_lnglat_to_mercator, shape) geom_func = 'box_area' geom_extra_args = ', %f' % (shape_merc.area, ) lon, lat = shape.representative_point().coords[0] else: raise RuntimeError("Haven't implemented NE shape type %r yet." % (shape.geom_type, )) # check that lon & lat are within expected range. helps to catch shape # files which have been projected to mercator. assert -90 <= lat <= 90 assert -180 <= lon <= 180 x, y = tile.deg2num(lat, lon, args.zoom) coord = Coordinate(zoom=args.zoom, column=x, row=y) props['source'] = 'naturalearthdata.com' args = dict( name=name, z=args.zoom, x=coord.column, y=coord.row, geom_func=geom_func, ne_id=args.ne_id, iso_code=args.is_in, props=props, expect=expect, layer_name=args.layer_name, geom_extra_args=geom_extra_args, ) output = _render_template('naturalearth_test', args) print output.encode('utf-8')