forked from mapbox/vector-tile-py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tile-info.py
executable file
·81 lines (68 loc) · 2.64 KB
/
tile-info.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
import sys
import json
from vector_tile import renderer
from vector_tile import vector_tile_pb2
from optparse import OptionParser
def stderr(*objs):
print(*objs, file=sys.stderr)
if __name__ == "__main__" :
usage = "usage: %prog "
parser = OptionParser(usage=usage,
description="Convert a Mapnik vector tile to GeoJSON")
parser.add_option("-z", type="int", dest="z")
parser.add_option("-x", type="int", dest="x")
parser.add_option("-y", type="int", dest="y")
parser.add_option("-t", type="string", dest="tile_address", default=None)
parser.add_option("-l", "--layer", dest="layer", default=None)
parser.add_option("-v", "--verbose", action="store_true", dest="verbose")
(options, args) = parser.parse_args()
if len(args) != 1:
stderr("No file name")
sys.exit(0)
if options.tile_address is not None:
components = options.tile_address.split("/")
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:
if layer.name == options.layer:
print(vtile.to_geojson(layer=layer, lonlat=True, layer_names=True))
break
else:
print(json.dumps(vtile.to_geojson(lonlat=True, layer_names=True), indent=4))
if options.verbose:
for layer in tile.layers:
if options.verbose:
stderr("Layer: " + layer.name)
stderr(" Version: %d" % layer.version)
stderr(" Extent: %d" % layer.extent)
stderr(" %d Features" % len(layer.features))
stderr(" %d Keys" % len(layer.keys))
stderr(" %d Values" % len(layer.values))