def test_adding_duplicate_points(self): req = renderer.Request(0, 0, 0) vtile = renderer.VectorTile(req) vtile.add_point(0, 0, {}) vtile.add_point(0, 0, {}) j_obj = json.loads(vtile.to_geojson()) self.assertEqual(len(j_obj['features']), 1)
def test_vtile_z0(self): req = renderer.Request(0, 0, 0) vtile = renderer.VectorTile(req) x, y = -8526703.378081053, 4740318.745473632 vtile.add_point(x, y, {}) j_obj = json.loads(vtile.to_geojson()) feature = j_obj['features'][0] self.assertEqual(feature['type'], 'Feature') self.assertEqual(feature['geometry']['type'], 'Point') coords = feature['geometry']['coordinates'] self.assertAlmostEqual(coords[0], x, -4) self.assertAlmostEqual(coords[1], y, -4)
def test_vtile_z22_higher_precision(self): merc = renderer.SphericalMercator() x, y = -8526703.378081053, 4740318.745473632 xyz_bounds = merc.xyz([x, y, x, y], 22) req = renderer.Request(xyz_bounds[0], xyz_bounds[1], 22) vtile = renderer.VectorTile(req, 512) vtile.add_point(x, y, {}) j_obj = json.loads(vtile.to_geojson()) feature = j_obj['features'][0] coords = feature['geometry']['coordinates'] self.assertAlmostEqual(coords[0], x, 4) self.assertAlmostEqual(coords[1], y, 4)
def test_ctrans(self): req = renderer.Request(0, 0, 0) x, y = renderer.lonlat2merc(-180, -85) ctrans = renderer.CoordTransform(req) px, py = ctrans.forward(x, y) self.assertAlmostEqual(px, 0.0) self.assertAlmostEqual(py, 255.5806938147701) px2, py2 = ctrans.forward(-20037508.34, -20037508.34) self.assertAlmostEqual(px2, 0.0) self.assertAlmostEqual(py2, 256.0) px3, py3 = ctrans.forward(-20037508.34 / 2, -20037508.34 / 2) self.assertAlmostEqual(px2, 0.0) self.assertAlmostEqual(py2, 256.0)
def test_vtile_attributes(self): req = renderer.Request(0, 0, 0) vtile = renderer.VectorTile(req) attr = { "name": "DC", "integer": 10, "bigint": sys.maxint, "nbigint": -1 * sys.maxint, "float": 1.5, "bigfloat": float(sys.maxint), "unistr": u"élan", "bool": True, "bool2": False } vtile.add_point(0, 0, attr) j_obj = json.loads(vtile.to_geojson()) self.assertEqual(j_obj['type'], "FeatureCollection") self.assertEqual(len(j_obj['features']), 1) feature = j_obj['features'][0] self.assertDictEqual(feature['properties'], attr)
def test_vtile_z20(self): merc = renderer.SphericalMercator() x, y = -8526703.378081053, 4740318.745473632 xyz_bounds = merc.xyz([x, y, x, y], 20) req = renderer.Request(xyz_bounds[0], xyz_bounds[1], 20) vtile = renderer.VectorTile(req) vtile.add_point(x, y, {"name": "DC", "integer": 10, "float": 1.5}) j_obj = json.loads(vtile.to_geojson()) self.assertEqual(j_obj['type'], "FeatureCollection") self.assertEqual(len(j_obj['features']), 1) feature = j_obj['features'][0] self.assertDictEqual(feature['properties'], { "integer": 10, "float": 1.5, "name": "DC" }) self.assertEqual(feature['type'], 'Feature') self.assertEqual(feature['geometry']['type'], 'Point') coords = feature['geometry']['coordinates'] self.assertAlmostEqual(coords[0], x, 2) self.assertAlmostEqual(coords[1], y, 2)
def test_request(self): req = renderer.Request(0, 0, 0) self.assertAlmostEqual(req.get_width(), 40075016.68557849) self.assertAlmostEqual(req.get_height(), 40075016.68557849)
# -*- coding: utf-8 -*- import sys import json # put `./lib` dir on path sys.path.append("./python") import renderer if __name__ == "__main__": # create a single tile at 0/0/0.png like tile.osm.org/0/0/0.png zoom = 0 x = 0 y = 0 # request object holds a Tile XYZ and internally holds mercator extent req = renderer.Request(x, y, zoom) # create a vector tile, given a tile request vtile = renderer.VectorTile(req) # for a given point representing a spot in NYC lat = 40.70512 lng = -74.01226 # and some attributes attr = {"hello": "world"} # convert to mercator coords x, y = renderer.lonlat2merc(lng, lat) # add this point and attributes to the tile vtile.add_point(x, y, attr) # print the protobuf as geojson just for debugging # NOTE: coordinate rounding is by design and print 'GeoJSON representation of tile (purely for debugging):' print vtile.to_geojson()