Beispiel #1
0
 def test_simple_tile_z0(self):
     """ Test creating a tile, adding a single layer and a single point """
     req = renderer.Request(0, 0, 0)
     vtile = renderer.VectorTile(req)
     #Test creating a layer
     layerName = "points"
     layer = vtile.add_layer(name=layerName)
     #test adding a point feature
     lat, lng = 38, -121
     attr = {"hello": "world"}
     x, y = renderer.lonlat2merc(lng, lat)
     assert vtile.add_point(layer, x, y, attr)
     assert len(layer.features) == 1
     feature = layer.features[0]
     key_id = feature.tags[0]
     value_id = feature.tags[1]
     key = str(layer.keys[key_id])
     assert key == "hello"
     value = layer.values[value_id]
     assert value.HasField('string_value')
     assert value.string_value == "world"
     #dump the layer to GeoJSON and make sure the output matches the input
     j_obj = vtile.to_geojson(layer_names=True, lonlat=True)
     assert isinstance(j_obj, dict)
     self.assertEqual(j_obj["type"], "FeatureCollection")
     self.assertEqual(len(j_obj["features"]), 1)
     self.assertEqual(j_obj["features"][0]["geometry"]["type"], "Point")
     self.assertEqual(len(j_obj["features"][0]["geometry"]["coordinates"]),
                      2)
     self.assertAlmostEqual(
         j_obj["features"][0]["geometry"]["coordinates"][0], lng, 0)
     self.assertAlmostEqual(
         j_obj["features"][0]["geometry"]["coordinates"][1], lat, 0)
     self.assertEqual(j_obj["features"][0]["properties"]["layer"],
                      layerName)
     self.assertEqual(j_obj["features"][0]["properties"]["hello"], "world")
     #now dump tile to protocol buffer message
     pbf = vtile.to_message()
     assert len(pbf) > 0
     tile = vector_tile_pb2.tile()
     tile.ParseFromString(pbf)
     vtile2 = renderer.VectorTile(req, tile)
     j_obj_deserialized = vtile2.to_geojson(layer_names=True, lonlat=True)
     self.assertDictEqual(j_obj, j_obj_deserialized)
Beispiel #2
0
 def test_simple_tile_z0(self):
     """ Test creating a tile, adding a single layer and a single point """
     req = renderer.Request(0,0,0)
     vtile = renderer.VectorTile(req)
     #Test creating a layer
     layerName = "points"
     layer = vtile.add_layer(name=layerName)
     #test adding a point feature
     lat, lng = 38, -121
     attr = {"hello":"world"}
     x,y = renderer.lonlat2merc(lng,lat)
     assert vtile.add_point(layer,x,y,attr)
     assert len(layer.features) == 1
     feature = layer.features[0]
     key_id = feature.tags[0]
     value_id = feature.tags[1]
     key = str(layer.keys[key_id])
     assert key == "hello"
     value = layer.values[value_id]
     assert value.HasField('string_value')
     assert value.string_value == "world"
     #dump the layer to GeoJSON and make sure the output matches the input
     j_obj = vtile.to_geojson(layer_names=True, lonlat=True)
     assert isinstance(j_obj, dict)
     self.assertEqual(j_obj["type"], "FeatureCollection")
     self.assertEqual(len(j_obj["features"]), 1)
     self.assertEqual(j_obj["features"][0]["geometry"]["type"], "Point")
     self.assertEqual(len(j_obj["features"][0]["geometry"]["coordinates"]), 2)
     self.assertAlmostEqual(j_obj["features"][0]["geometry"]["coordinates"][0], lng, 0)
     self.assertAlmostEqual(j_obj["features"][0]["geometry"]["coordinates"][1], lat, 0)
     self.assertEqual(j_obj["features"][0]["properties"]["layer"], layerName)
     self.assertEqual(j_obj["features"][0]["properties"]["hello"], "world")
     #now dump tile to protocol buffer message
     pbf = vtile.to_message()
     assert len(pbf) > 0
     tile = vector_tile_pb2.tile()
     tile.ParseFromString(pbf)
     vtile2 = renderer.VectorTile(req, tile)
     j_obj_deserialized = vtile2.to_geojson(layer_names=True, lonlat=True)
     self.assertDictEqual(j_obj, j_obj_deserialized)
Beispiel #3
0
if __name__ == "__main__":
    sys.stderr = codecs.getwriter('utf8')(sys.stderr)
    usage = "usage: %prog "
    parser = OptionParser(usage=usage,
        description="Output information in a Mapnik vector tile.")
    parser.add_option("-v", "--verbose", action="store_true",
                      dest="verbose", default=False)
    (options, args) = parser.parse_args()
    
    if len(args) != 1:
        stderr("No file name")
        sys.exit(0)

    filename = args[0]
    with open(filename, "rb") as f:
        tile = vector_tile_pb2.tile()
        raw = f.read()
        try:
            decoded = raw.decode('zlib')
        except:
            decoded = raw

        tile.ParseFromString(decoded)

        SEG_END    = 0
        SEG_MOVETO = 1
        SEG_LINETO = 2
        SEG_CLOSE = (0x40 | 0x0f)

        if options.verbose:
            # print out each layer and feature's raw data.
Beispiel #4
0
def tile(layers):
    pbt = vector_tile_pb2.tile()
    pbt.layers.extend(list(layers))
    return pbt
Beispiel #5
0
def tile(layers):
    pbt = vector_tile_pb2.tile()
    pbt.layers.extend(list(layers))
    return pbt
Beispiel #6
0
        zoom = int(components[0])
        x = int(components[1])
        y = int(components[2])
    elif options.z and options.x and options.y:
        zoom = options.z
        x = options.x
        y = options.y
    else:
        stderr("Error: no tile address, use -x -y and -z, or -t z/x/y")
        sys.exit(0)

    filename = args[0]
    if options.verbose:
        stderr("opening %s as tile %d/%d/%d" % (filename, zoom, x, y))
    with open(filename, "rb") as f:
        tile = vector_tile_pb2.tile()
        raw = f.read()
        try:
            decoded = raw.decode('zlib')
            if options.verbose:
                stderr("decoded zlib length:%d" % len(decoded))
        except:
            decoded = raw

        tile.ParseFromString(decoded)
        req = renderer.Request(x,y,zoom)
        vtile = renderer.VectorTile(req, tile)
        vtile.layer = tile.layer

        if options.layer:
            for layer in tile.layers: