Permalink
Browse files

Thinking I've nailed the mbtiles-to-directory use case

  • Loading branch information...
1 parent 6707d02 commit d78148b1b2abb011aa87b83c7125e422f12fe557 @migurski migurski committed Feb 17, 2012
Showing with 52 additions and 65 deletions.
  1. +52 −65 scripts/tilestache-seed.py
View
@@ -28,20 +28,28 @@
of lat/lon coordinates, e.g. "37.788 -122.349 37.833 -122.246". Output is a list
of tile paths as they are created.
+Example:
+
+ tilestache-seed.py -b 52.55 13.28 52.46 13.51 -c tilestache.cfg -l osm 11 12 13
+
+Protip: extract tiles from an MBTiles tileset to a directory like this:
+
+ tilestache-seed.py --from-mbtiles filename.mbtiles --output-directory dirname
+
Configuration, bbox, and layer options are required; see `%prog --help` for info.""")
defaults = dict(extension='png', padding=0, verbose=True, enable_retries=False, bbox=(37.777, -122.352, 37.839, -122.226))
parser.set_defaults(**defaults)
parser.add_option('-c', '--config', dest='config',
- help='Path to configuration file.')
+ help='Path to configuration file, typically required.')
parser.add_option('-l', '--layer', dest='layer',
- help='Layer name from configuration.')
+ help='Layer name from configuration, typically required.')
parser.add_option('-b', '--bbox', dest='bbox',
- help='Bounding box in floating point geographic coordinates: south west north east.',
+ help='Bounding box in floating point geographic coordinates: south west north east. Default value is %.3f, %.3f, %.3f, %.3f.' % defaults['bbox'],
type='float', nargs=4)
parser.add_option('-p', '--padding', dest='padding',
@@ -149,85 +157,62 @@ def tilesetCoordinates(filename):
from TileStache import parseConfigfile, getTile
from TileStache.Core import KnownUnknown
- from TileStache.Caches import Disk, Multi
+ from TileStache.Caches import Disk, Multi, Test
from TileStache import MBTiles
import TileStache
from ModestMaps.Core import Coordinate
from ModestMaps.Geo import Location
try:
- verbose = options.verbose
- extension = options.extension
- enable_retries = options.enable_retries
- progressfile = options.progressfile
- src_mbtiles = options.mbtiles_input
+ # determine if we have enough information to prep a config and layer
- # check for cache overrides
+ has_fake_destination = bool(options.outputdirectory or options.mbtiles_output)
+ has_fake_source = bool(options.mbtiles_input)
- if options.outputdirectory and options.mbtiles_output:
- cache1 = Disk(options.outputdirectory, dirs='portable', gzip=[])
- cache2 = MBTiles.Cache(options.mbtiles_output, extension, options.layer)
- fake_cache = Multi([cache1, cache2])
-
- elif options.outputdirectory:
- fake_cache = Disk(options.outputdirectory, dirs='portable', gzip=[])
+ if has_fake_destination and has_fake_source:
+ config = TileStache.Config.Configuration(Test(), '.')
- elif options.mbtiles_output:
- fake_cache = MBTiles.Cache(options.mbtiles_output, extension, options.layer)
-
- else:
- fake_cache = False
-
- # build configuration
+ metatile = TileStache.Core.Metatile()
+ projection = TileStache.Geography.SphericalMercator()
+ layer = TileStache.Core.Layer(config, projection, metatile)
+
+ config.layers[options.layer or 'tiles-layer'] = layer
- if options.config is None and not fake_cache:
- raise KnownUnknown('Missing required configuration (--config) parameter.')
-
elif options.config is None:
- config = TileStache.Config.Configuration(fake_cache, '.')
-
- elif fake_cache:
- config = parseConfigfile(options.config)
- config.cache = fake_cache
+ raise KnownUnknown('Missing required configuration (--config) parameter.')
+ elif options.layer is None:
+ raise KnownUnknown('Missing required layer (--layer) parameter.')
+
else:
config = parseConfigfile(options.config)
-
- # check for layer overrides
-
- if src_mbtiles:
- metatile = TileStache.Core.Metatile()
- projection = TileStache.Geography.SphericalMercator()
- layer = TileStache.Core.Layer(config, projection, metatile)
- if fake_cache:
- layer_name = options.layer or 'your-mbtiles'
- else:
- layer_name = options.layer
+ if options.layer not in config.layers:
+ raise KnownUnknown('"%s" is not a layer I know about. Here are some that I do know about: %s.' % (options.layer, ', '.join(sorted(config.layers.keys()))))
- config.layers[layer_name] = layer
-
- layer.provider = MBTiles.Provider(layer, src_mbtiles)
- n, t, v, d, format, b = MBTiles.tileset_info(src_mbtiles)
- extension = format or extension
+ layer = config.layers[options.layer]
+ layer.write_cache = True # Override to make seeding guaranteed useful.
- else:
- layer_name = options.layer
+ # override parts of the config and layer if needed
- print config.layers
+ if options.outputdirectory and options.mbtiles_output:
+ cache1 = Disk(options.outputdirectory, dirs='portable', gzip=[])
+ cache2 = MBTiles.Cache(options.mbtiles_output, extension, options.layer)
+ config.cache = Multi([cache1, cache2])
+
+ elif options.outputdirectory:
+ config.cache = Disk(options.outputdirectory, dirs='portable', gzip=[])
- # build layer
+ elif options.mbtiles_output:
+ config.cache = MBTiles.Cache(options.mbtiles_output, extension, options.layer)
- if layer_name not in config.layers:
- raise KnownUnknown('"%s" is not a layer I know about.' % layer_name)
+ extension = options.extension
- elif layer_name is None:
- raise KnownUnknown('Missing required layer (--layer) parameter.')
-
- else:
- layer = config.layers[layer_name]
- layer.write_cache = True # Override to make seeding guaranteed useful.
+ if options.mbtiles_input:
+ layer.provider = MBTiles.Provider(layer, options.mbtiles_input)
+ n, t, v, d, format, b = MBTiles.tileset_info(options.mbtiles_input)
+ extension = format or extension
# do the actual work
@@ -259,11 +244,13 @@ def tilesetCoordinates(filename):
if tile_list:
coordinates = listCoordinates(tile_list)
- elif src_mbtiles:
- coordinates = tilesetCoordinates(src_mbtiles)
+ elif options.mbtiles_input:
+ coordinates = tilesetCoordinates(options.mbtiles_input)
else:
coordinates = generateCoordinates(ul, lr, zooms, padding)
+ coordinates = list(coordinates)[:10]
+
for (offset, count, coord) in coordinates:
path = '%s/%d/%d/%d.%s' % (layer.name(), coord.zoom, coord.column, coord.row, extension)
@@ -275,7 +262,7 @@ def tilesetCoordinates(filename):
# Fetch a tile.
#
- attempts = enable_retries and 3 or 1
+ attempts = options.enable_retries and 3 or 1
rendered = False
while not rendered:
@@ -313,7 +300,7 @@ def tilesetCoordinates(filename):
if options.verbose:
print >> stderr, '%(tile)s (%(size)s)' % progress
- if progressfile:
- fp = open(progressfile, 'w')
+ if options.progressfile:
+ fp = open(options.progressfile, 'w')
json_dump(progress, fp)
fp.close()

0 comments on commit d78148b

Please sign in to comment.