def test_one(self): tilestore = MBTilesTileStore(sqlite3.connect(':memory:'), content_type='image/png') self.assertEqual(len(tilestore), 0) tilestream = [Tile(TileCoord(1, 0, 0), data='data'), None, Tile(TileCoord(1, 0, 1), error=True)] tilestream = tilestore.put(tilestream) tiles = list(tilestream) self.assertEqual(len(tilestore), 2) self.assertEqual(len(tiles), 2) self.assertEqual(tiles[0].tilecoord, TileCoord(1, 0, 0)) self.assertEqual(tiles[0].data, 'data') self.assertEqual(tiles[1].tilecoord, TileCoord(1, 0, 1)) self.assertEqual(tiles[1].error, True) self.assertTrue(Tile(TileCoord(1, 0, 0)) in tilestore) self.assertTrue(Tile(TileCoord(1, 0, 1)) in tilestore) tilestream = [Tile(TileCoord(1, 0, 0)), Tile(TileCoord(1, 0, 1))] tilestream = tilestore.get(tilestream) consume(tilestream, None) self.assertEqual(tilestore.get_cheap_bounding_pyramid(), BoundingPyramid({1: (Bounds(0, 1), Bounds(0, 2))})) self.assertEqual(len(tilestore), 2) tiles = list(tilestore.list()) self.assertEqual(len(tiles), 2) tiles = sorted(tilestore.get_all()) self.assertEqual(len(tiles), 2) self.assertEqual(tiles[0].tilecoord, TileCoord(1, 0, 0)) self.assertEqual(str(tiles[0].data), 'data') self.assertEqual(tiles[1].tilecoord, TileCoord(1, 0, 1)) self.assertEqual(tiles[1].data, None) tilestream = [Tile(TileCoord(1, 0, 0))] tilestream = tilestore.delete(tilestream) consume(tilestream, None) self.assertEqual(len(tilestore), 1) tiles = list(tilestore.get_all()) self.assertEqual(len(tiles), 1) self.assertFalse(Tile(TileCoord(1, 0, 0)) in tilestore) self.assertTrue(Tile(TileCoord(1, 0, 1)) in tilestore)
def main(argv): # Create our RenderingTheWorld tile store that will manage the queue and subdivision. # We pass it the function that decides whether a tile should be subdivided, and an initial tile. rendering_the_world_tilestore = RenderingTheWorldTileStore(subdivide, seeds=(Tile(TileCoord(0, 0, 0)),)) # Start the tilestream by getting a list of all tiles to be generated. tilestream = rendering_the_world_tilestore.list() tilestream = imap(Logger(logger, logging.INFO, 'get %(tilecoord)s'), tilestream) # Create the tile store that will generate our tiles, in this case it's a demo WMTS server at OpenGeo. # Getting tiles from this store will either return the tile as a PNG file, or set an error on the tile if there are no features in this tile. generate_tilestore = WMTSTileStore( url='http://v2.suite.opengeo.org/geoserver/gwc/service/wmts/', layer='medford:buildings', style='_null', format='image/png', tile_matrix_set='EPSG:900913', tile_matrix=lambda z: 'EPSG:900913:%d' % (z,)) tilestream = generate_tilestore.get(tilestream) tilestream = imap(Logger(logger, logging.INFO, 'got %(tilecoord)s, error=%(error)s'), tilestream) # Put the tile back into the RenderingTheWorld tile store. This check whether the tile should be subdivided, and, if so, adds the tile's children to the list of tiles to be generated. tilestream = rendering_the_world_tilestore.put(tilestream) # Get rid of tiles that returned an error (i.e. where there was no data). tilestream = imap(DropErrors(), tilestream) # Store the generated tiles in the output tile store, in our case a local MBTiles file. output_tilestore = MBTilesTileStore(sqlite3.connect('medford_buildings.mbtiles')) tilestream = output_tilestore.put(tilestream) tilestream = imap(Logger(logger, logging.INFO, 'saved %(tilecoord)s'), tilestream) # Go! consume(tilestream, None)
def test_one(self): tilestore = DictTileStore() self.assertEqual(len(tilestore), 0) tilestream = [Tile(TileCoord(1, 0, 0), data='data'), None, Tile(TileCoord(1, 0, 1), error=True)] tilestream = tilestore.put(tilestream) tiles = list(tilestream) self.assertEqual(len(tiles), 2) self.assertEqual(tiles[0].tilecoord, TileCoord(1, 0, 0)) self.assertEqual(tiles[0].data, 'data') self.assertEqual(tiles[1].tilecoord, TileCoord(1, 0, 1)) self.assertEqual(tiles[1].error, True) self.assertTrue(Tile(TileCoord(1, 0, 0)) in tilestore) self.assertTrue(Tile(TileCoord(1, 0, 1)) in tilestore) tilestream = [Tile(TileCoord(1, 0, 0)), Tile(TileCoord(1, 0, 1))] tilestream = tilestore.get(tilestream) consume(tilestream, None) tiles = list(tilestore.get_all()) self.assertEqual(len(tiles), 2) self.assertEqual(tiles[0].tilecoord, TileCoord(1, 0, 0)) self.assertEqual(tiles[0].data, 'data') self.assertEqual(tiles[1].tilecoord, TileCoord(1, 0, 1)) self.assertEqual(tiles[1].error, True) tilestream = [Tile(TileCoord(1, 0, 0))] tilestream = tilestore.delete(tilestream) consume(tilestream, None) tiles = list(tilestore.get_all()) self.assertEqual(len(tiles), 1) self.assertFalse(Tile(TileCoord(1, 0, 0)) in tilestore) self.assertTrue(Tile(TileCoord(1, 0, 1)) in tilestore)
def test_one(self): tilestore = DictTileStore() self.assertEqual(len(tilestore), 0) tilestream = [ Tile(TileCoord(1, 0, 0), data='data'), None, Tile(TileCoord(1, 0, 1), error=True) ] tilestream = tilestore.put(tilestream) tiles = list(tilestream) self.assertEqual(len(tiles), 2) self.assertEqual(tiles[0].tilecoord, TileCoord(1, 0, 0)) self.assertEqual(tiles[0].data, 'data') self.assertEqual(tiles[1].tilecoord, TileCoord(1, 0, 1)) self.assertEqual(tiles[1].error, True) self.assertTrue(Tile(TileCoord(1, 0, 0)) in tilestore) self.assertTrue(Tile(TileCoord(1, 0, 1)) in tilestore) tilestream = [Tile(TileCoord(1, 0, 0)), Tile(TileCoord(1, 0, 1))] tilestream = tilestore.get(tilestream) consume(tilestream, None) tiles = list(tilestore.get_all()) self.assertEqual(len(tiles), 2) self.assertEqual(tiles[0].tilecoord, TileCoord(1, 0, 0)) self.assertEqual(tiles[0].data, 'data') self.assertEqual(tiles[1].tilecoord, TileCoord(1, 0, 1)) self.assertEqual(tiles[1].error, True) tilestream = [Tile(TileCoord(1, 0, 0))] tilestream = tilestore.delete(tilestream) consume(tilestream, None) tiles = list(tilestore.get_all()) self.assertEqual(len(tiles), 1) self.assertFalse(Tile(TileCoord(1, 0, 0)) in tilestore) self.assertTrue(Tile(TileCoord(1, 0, 1)) in tilestore)
def main(): # Create our RenderingTheWorld tile store that will manage the queue and subdivision. # We pass it the function that decides whether a tile should be subdivided, and an initial tile. rendering_the_world_tilestore = RenderingTheWorldTileStore(subdivide, seeds=(Tile(TileCoord(0, 0, 0)),)) # Start the tilestream by getting a list of all tiles to be generated. tilestream = rendering_the_world_tilestore.list() tilestream = imap(Logger(logger, logging.INFO, 'get %(tilecoord)s'), tilestream) # Create the tile store that will generate our tiles, in this case it's a demo WMTS server at OpenGeo. # Getting tiles from this store will either return the tile as a PNG file, or set an error on the tile # if there are no features in this tile. generate_tilestore = WMTSTileStore( url='http://v2.suite.opengeo.org/geoserver/gwc/service/wmts/', layer='medford:buildings', style='_null', format='image/png', tile_matrix_set='EPSG:900913', tile_matrix=lambda z: 'EPSG:900913:{0:d}'.format(z)) tilestream = generate_tilestore.get(tilestream) tilestream = imap(Logger(logger, logging.INFO, 'got %(tilecoord)s, error=%(error)s'), tilestream) # Put the tile back into the RenderingTheWorld tile store. This check whether the tile should be # subdivided, and, if so, adds the tile's children to the list of tiles to be generated. tilestream = rendering_the_world_tilestore.put(tilestream) # Get rid of tiles that returned an error (i.e. where there was no data). tilestream = imap(DropErrors(), tilestream) # Store the generated tiles in the output tile store, in our case a local MBTiles file. output_tilestore = MBTilesTileStore(sqlite3.connect('medford_buildings.mbtiles')) tilestream = output_tilestore.put(tilestream) tilestream = imap(Logger(logger, logging.INFO, 'saved %(tilecoord)s'), tilestream) # Go! consume(tilestream, None)
def consume(self, test=None): if test is None: test = self.options.test start = datetime.now() consume(self.tilestream, test) self.duration = datetime.now() - start for ca in self.close_actions: ca()
def consume(self, test=None, force=False): assert self.tilestream is not None if hasattr(self.options, 'daemon') and self.options.daemon and \ not self.options.debug and not force: while True: try: self.consume(test, True) except KeyboardInterrupt: sys.exit() test = self.options.test if test is None else test start = datetime.now() consume(self.tilestream, test) self.duration = datetime.now() - start for ca in self._close_actions: ca()
def main(): # Create our input and output TileStores input_tilestore = TileStore.load('tiles.openstreetmap_org') output_tilestore = TileStore.load('local.mbtiles') # 1. Generate a list of tiles to download from a BoundingPyramid # 4/8/5 is the root tile, corresponding to Central Europe # +3/+1/+1 specifies up to zoom level 4 + 3 = 7 and an extent of one tile in the X and Y directions bounding_pyramid = BoundingPyramid.from_string('4/8/5:+3/+1/+1') bounding_pyramid_tilestore = BoundingPyramidTileStore(bounding_pyramid) tilestream = bounding_pyramid_tilestore.list() # 2. Filter out tiles that already downloaded tilestream = (tile for tile in tilestream if tile not in output_tilestore) # 3. Get the tile from openstreetmap.org tilestream = input_tilestore.get(tilestream) # 4. Save the tile to local.mbtiles tilestream = output_tilestore.put(tilestream) # 5. Log the fact that the tile was downloaded tilestream = imap(Logger(logger, logging.INFO, 'downloaded %(tilecoord)s'), tilestream) # Go! consume(tilestream, None)
def main(argv): # Create our input and output TileStores input_tilestore = TileStore.load('tiles.openstreetmap_org') output_tilestore = TileStore.load('local.mbtiles') # 1. Generate a list of tiles to download from a BoundingPyramid # 4/8/5 is the root tile, corresponding to Central Europe # +3/+1/+1 specifies up to zoom level 4 + 3 = 7 and an extent of one tile in the X and Y directions bounding_pyramid = BoundingPyramid.from_string('4/8/5:+3/+1/+1') bounding_pyramid_tilestore = BoundingPyramidTileStore(bounding_pyramid) tilestream = bounding_pyramid_tilestore.list() # 2. Filter out tiles that already downloaded tilestream = (tile for tile in tilestream if not tile in output_tilestore) # 3. Get the tile from openstreetmap.org tilestream = input_tilestore.get(tilestream) # 4. Save the tile to local.mbtiles tilestream = output_tilestore.put(tilestream) # 5. Log the fact that the tile was downloaded tilestream = imap(Logger(logger, logging.INFO, 'downloaded %(tilecoord)s'), tilestream) # Go! consume(tilestream, None)
def test_one(self): tilestore = MBTilesTileStore(sqlite3.connect(':memory:'), content_type='image/png') self.assertEqual(len(tilestore), 0) tilestream = [ Tile(TileCoord(1, 0, 0), data=b'data'), None, Tile(TileCoord(1, 0, 1), error=True) ] tilestream = tilestore.put(tilestream) tiles = list(tilestream) self.assertEqual(len(tilestore), 2) self.assertEqual(len(tiles), 2) self.assertEqual(tiles[0].tilecoord, TileCoord(1, 0, 0)) self.assertEqual(tiles[0].data, b'data') self.assertEqual(tiles[1].tilecoord, TileCoord(1, 0, 1)) self.assertEqual(tiles[1].error, True) self.assertTrue(Tile(TileCoord(1, 0, 0)) in tilestore) self.assertTrue(Tile(TileCoord(1, 0, 1)) in tilestore) tilestream = [Tile(TileCoord(1, 0, 0)), Tile(TileCoord(1, 0, 1))] tilestream = tilestore.get(tilestream) consume(tilestream, None) self.assertEqual(tilestore.get_cheap_bounding_pyramid(), BoundingPyramid({1: (Bounds(0, 1), Bounds(0, 2))})) self.assertEqual(len(tilestore), 2) tiles = list(tilestore.list()) self.assertEqual(len(tiles), 2) tiles = sorted(tilestore.get_all()) self.assertEqual(len(tiles), 2) self.assertEqual(tiles[0].tilecoord, TileCoord(1, 0, 0)) self.assertEqual(bytes(tiles[0].data), b'data') self.assertEqual(tiles[1].tilecoord, TileCoord(1, 0, 1)) self.assertEqual(tiles[1].data, None) tilestream = [Tile(TileCoord(1, 0, 0))] tilestream = tilestore.delete(tilestream) consume(tilestream, None) self.assertEqual(len(tilestore), 1) tiles = list(tilestore.get_all()) self.assertEqual(len(tiles), 1) self.assertFalse(Tile(TileCoord(1, 0, 0)) in tilestore) self.assertTrue(Tile(TileCoord(1, 0, 1)) in tilestore)
def consume(self, test=None, force=False): assert self.tilestream is not None if hasattr(self.options, 'daemon') and self.options.daemon and \ not self.options.debug and not force: while True: try: self.consume(test, True) except KeyboardInterrupt: sys.exit() except Exception as e: logger.error(e, exc_info=True) traceback.print_exc() time.sleep(1) test = self.options.test if test is None else test start = datetime.now() consume(self.tilestream, test) self.duration = datetime.now() - start for ca in self._close_actions: ca()
def _calculate_cost(gene, options): validate_calculate_cost(gene) nb_metatiles = {} nb_tiles = {} meta = gene.layer['meta'] if options.cost_algo == 'area': tile_size = gene.layer['grid_ref']['tile_size'] for zoom, resolution in enumerate(gene.layer['grid_ref']['resolutions']): if 'min_resolution_seed' in gene.layer and resolution < gene.layer['min_resolution_seed']: continue print "Calculate zoom %i." % zoom px_buffer = gene.layer['px_buffer'] + \ gene.layer['meta_buffer'] if meta else 0 m_buffer = px_buffer * resolution if meta: size = tile_size * gene.layer['meta_size'] * resolution meta_buffer = size * 0.7 + m_buffer meta_geom = gene.geoms[zoom].buffer(meta_buffer, 1) nb_metatiles[zoom] = int(round(meta_geom.area / size ** 2)) size = tile_size * resolution tile_buffer = size * 0.7 + m_buffer geom = gene.geoms[zoom].buffer(tile_buffer, 1) nb_tiles[zoom] = int(round(geom.area / size ** 2)) elif options.cost_algo == 'count': gene.init_tilecoords() gene.add_geom_filter() if meta: def count_metatile(tile): if tile: if tile.tilecoord.z in nb_metatiles: nb_metatiles[tile.tilecoord.z] += 1 else: nb_metatiles[tile.tilecoord.z] = 1 return tile gene.imap(count_metatile) class MetaTileSplitter(TileStore): def get(self, tiles): for metatile in tiles: for tilecoord in metatile.tilecoord: yield Tile(tilecoord) gene.tilestream = MetaTileSplitter().get(gene.tilestream) # Only keep tiles that intersect geometry gene.add_geom_filter() def count_tile(tile): if tile: if tile.tilecoord.z in nb_tiles: nb_tiles[tile.tilecoord.z] += 1 else: print "Calculate zoom %i." % tile.tilecoord.z nb_tiles[tile.tilecoord.z] = 1 return tile gene.imap(count_tile) consume(gene.tilestream, None) times = {} print for z in nb_metatiles: print "%i meta tiles in zoom %i." % (nb_metatiles[z], z) times[z] = gene.layer['cost']['metatile_generation_time'] * nb_metatiles[z] price = 0 all_size = 0 all_time = 0 all_tiles = 0 for z in nb_tiles: print print "%i tiles in zoom %i." % (nb_tiles[z], z) all_tiles += nb_tiles[z] if meta: time = times[z] + gene.layer['cost']['tile_generation_time'] * nb_tiles[z] else: time = gene.layer['cost']['tileonly_generation_time'] * nb_tiles[z] size = gene.layer['cost']['tile_size'] * nb_tiles[z] all_size += size all_time += time td = timedelta(milliseconds=time) print "Time to generate: %s [d h:mm:ss]" % (duration_format(td)) c = gene.config['cost']['s3']['put'] * nb_tiles[z] / 1000.0 price += c print 'S3 PUT: %0.2f [$]' % c if 'ec2' in gene.config: c = time * gene.config['cost']['ec2']['usage'] / (1000.0 * 3600) price += c print 'EC2 usage: %0.2f [$]' % c c = gene.config['cost']['esb']['io'] * time / (1000.0 * 2600 * 24 * 30) price += c print 'ESB usage: %0.2f [$]' % c if 'sqs' in gene.layer: if meta: nb_sqs = nb_metatiles[z] * 3 else: nb_sqs = nb_tiles[z] * 3 c = nb_sqs * gene.config['cost']['sqs']['request'] / 1000000.0 price += c print 'SQS usage: %0.2f [$]' % c print td = timedelta(milliseconds=all_time) print "Number of tiles: %i" % all_tiles print 'Generation time: %s [d h:mm:ss]' % (duration_format(td)) print 'Generation cost: %0.2f [$]' % price return (all_size, td, price, all_tiles)
def _calculate_cost(gene, options): validate_calculate_cost(gene) nb_metatiles = {} nb_tiles = {} meta = gene.layer['meta'] if options.cost_algo == 'area': tile_size = gene.layer['grid_ref']['tile_size'] for zoom, resolution in enumerate( gene.layer['grid_ref']['resolutions']): if 'min_resolution_seed' in gene.layer and resolution < gene.layer[ 'min_resolution_seed']: continue print "Calculate zoom %i." % zoom px_buffer = gene.layer['px_buffer'] + \ gene.layer['meta_buffer'] if meta else 0 m_buffer = px_buffer * resolution if meta: size = tile_size * gene.layer['meta_size'] * resolution meta_buffer = size * 0.7 + m_buffer meta_geom = gene.geoms[zoom].buffer(meta_buffer, 1) nb_metatiles[zoom] = int(round(meta_geom.area / size**2)) size = tile_size * resolution tile_buffer = size * 0.7 + m_buffer geom = gene.geoms[zoom].buffer(tile_buffer, 1) nb_tiles[zoom] = int(round(geom.area / size**2)) elif options.cost_algo == 'count': gene.init_tilecoords() gene.add_geom_filter() if meta: def count_metatile(tile): if tile: if tile.tilecoord.z in nb_metatiles: nb_metatiles[tile.tilecoord.z] += 1 else: nb_metatiles[tile.tilecoord.z] = 1 return tile gene.imap(count_metatile) class MetaTileSplitter(TileStore): def get(self, tiles): for metatile in tiles: for tilecoord in metatile.tilecoord: yield Tile(tilecoord) gene.tilestream = MetaTileSplitter().get(gene.tilestream) # Only keep tiles that intersect geometry gene.add_geom_filter() def count_tile(tile): if tile: if tile.tilecoord.z in nb_tiles: nb_tiles[tile.tilecoord.z] += 1 else: print "Calculate zoom %i." % tile.tilecoord.z nb_tiles[tile.tilecoord.z] = 1 return tile gene.imap(count_tile) consume(gene.tilestream, None) times = {} print for z in nb_metatiles: print "%i meta tiles in zoom %i." % (nb_metatiles[z], z) times[z] = gene.layer['cost'][ 'metatile_generation_time'] * nb_metatiles[z] price = 0 all_size = 0 all_time = 0 all_tiles = 0 for z in nb_tiles: print print "%i tiles in zoom %i." % (nb_tiles[z], z) all_tiles += nb_tiles[z] if meta: time = times[ z] + gene.layer['cost']['tile_generation_time'] * nb_tiles[z] else: time = gene.layer['cost']['tileonly_generation_time'] * nb_tiles[z] size = gene.layer['cost']['tile_size'] * nb_tiles[z] all_size += size all_time += time td = timedelta(milliseconds=time) print "Time to generate: %s [d h:mm:ss]" % (duration_format(td)) c = gene.config['cost']['s3']['put'] * nb_tiles[z] / 1000.0 price += c print 'S3 PUT: %0.2f [$]' % c if 'ec2' in gene.config: c = time * gene.config['cost']['ec2']['usage'] / (1000.0 * 3600) price += c print 'EC2 usage: %0.2f [$]' % c c = gene.config['cost']['esb']['io'] * time / (1000.0 * 2600 * 24 * 30) price += c print 'ESB usage: %0.2f [$]' % c if 'sqs' in gene.layer: if meta: nb_sqs = nb_metatiles[z] * 3 else: nb_sqs = nb_tiles[z] * 3 c = nb_sqs * gene.config['cost']['sqs']['request'] / 1000000.0 price += c print 'SQS usage: %0.2f [$]' % c print td = timedelta(milliseconds=all_time) print "Number of tiles: %i" % all_tiles print 'Generation time: %s [d h:mm:ss]' % (duration_format(td)) print 'Generation cost: %0.2f [$]' % price return (all_size, td, price, all_tiles)
def _calculate_cost(gene, layer, options): nb_metatiles = {} nb_tiles = {} meta = layer['meta'] if options.cost_algo == 'area': tile_size = layer['grid_ref']['tile_size'] for zoom, resolution in enumerate(layer['grid_ref']['resolutions']): if 'min_resolution_seed' in layer and resolution < layer['min_resolution_seed']: continue print("Calculate zoom {}.".format(zoom)) px_buffer = layer['px_buffer'] + \ layer['meta_buffer'] if meta else 0 m_buffer = px_buffer * resolution if meta: size = tile_size * layer['meta_size'] * resolution meta_buffer = size * 0.7 + m_buffer meta_geom = gene.geoms[zoom].buffer(meta_buffer, 1) nb_metatiles[zoom] = int(round(meta_geom.area / size ** 2)) size = tile_size * resolution tile_buffer = size * 0.7 + m_buffer geom = gene.geoms[zoom].buffer(tile_buffer, 1) nb_tiles[zoom] = int(round(geom.area / size ** 2)) elif options.cost_algo == 'count': gene.init_tilecoords(layer) gene.add_geom_filter(layer) if meta: def count_metatile(tile): if tile: if tile.tilecoord.z in nb_metatiles: nb_metatiles[tile.tilecoord.z] += 1 else: nb_metatiles[tile.tilecoord.z] = 1 return tile gene.imap(count_metatile) class MetaTileSplitter(TileStore): @staticmethod def get(tiles): for metatile in tiles: for tilecoord in metatile.tilecoord: yield Tile(tilecoord) gene.tilestream = MetaTileSplitter().get(gene.tilestream) # Only keep tiles that intersect geometry gene.add_geom_filter(layer) def count_tile(tile): if tile: if tile.tilecoord.z in nb_tiles: nb_tiles[tile.tilecoord.z] += 1 else: print("Calculate zoom {}.".format(tile.tilecoord.z)) nb_tiles[tile.tilecoord.z] = 1 return tile gene.imap(count_tile) consume(gene.tilestream, None) times = {} print('') for z in nb_metatiles: print("{} meta tiles in zoom {}.".format(nb_metatiles[z], z)) times[z] = layer['cost']['metatile_generation_time'] * nb_metatiles[z] price = 0 all_size = 0 all_time = 0 all_tiles = 0 for z in nb_tiles: print('') print("{} tiles in zoom {}.".format(nb_tiles[z], z)) all_tiles += nb_tiles[z] if meta: time = times[z] + layer['cost']['tile_generation_time'] * nb_tiles[z] else: time = layer['cost']['tileonly_generation_time'] * nb_tiles[z] size = layer['cost']['tile_size'] * nb_tiles[z] all_size += size all_time += time td = timedelta(milliseconds=time) print("Time to generate: {} [d h:mm:ss]".format((duration_format(td)))) c = gene.config['cost']['s3']['put'] * nb_tiles[z] / 1000.0 price += c print('S3 PUT: {0:0.2f} [$]'.format(c)) if 'sqs' in gene.config: if meta: nb_sqs = nb_metatiles[z] * 3 else: nb_sqs = nb_tiles[z] * 3 c = nb_sqs * gene.config['cost']['sqs']['request'] / 1000000.0 price += c print('SQS usage: {0:0.2f} [$]'.format(c)) print("") td = timedelta(milliseconds=all_time) print("Number of tiles: {}".format(all_tiles)) print('Generation time: {} [d h:mm:ss]'.format((duration_format(td)))) print('Generation cost: {0:0.2f} [$]'.format(price)) return all_size, td, price, all_tiles
def _calculate_cost(gene, options): nb_metatiles = {} nb_tiles = {} meta = gene.layer['meta'] if options.cost_algo == 'area': tile_size = gene.layer['grid_ref']['tile_size'] for zoom, resolution in enumerate(gene.layer['grid_ref']['resolutions']): if 'min_resolution_seed' in gene.layer and resolution < gene.layer['min_resolution_seed']: continue print("Calculate zoom {}.".format(zoom)) px_buffer = gene.layer['px_buffer'] + \ gene.layer['meta_buffer'] if meta else 0 m_buffer = px_buffer * resolution if meta: size = tile_size * gene.layer['meta_size'] * resolution meta_buffer = size * 0.7 + m_buffer meta_geom = gene.geoms[zoom].buffer(meta_buffer, 1) nb_metatiles[zoom] = int(round(meta_geom.area / size ** 2)) size = tile_size * resolution tile_buffer = size * 0.7 + m_buffer geom = gene.geoms[zoom].buffer(tile_buffer, 1) nb_tiles[zoom] = int(round(geom.area / size ** 2)) elif options.cost_algo == 'count': gene.init_tilecoords() gene.add_geom_filter() if meta: def count_metatile(tile): if tile: if tile.tilecoord.z in nb_metatiles: nb_metatiles[tile.tilecoord.z] += 1 else: nb_metatiles[tile.tilecoord.z] = 1 return tile gene.imap(count_metatile) class MetaTileSplitter(TileStore): @staticmethod def get(tiles): for metatile in tiles: for tilecoord in metatile.tilecoord: yield Tile(tilecoord) gene.tilestream = MetaTileSplitter().get(gene.tilestream) # Only keep tiles that intersect geometry gene.add_geom_filter() def count_tile(tile): if tile: if tile.tilecoord.z in nb_tiles: nb_tiles[tile.tilecoord.z] += 1 else: print("Calculate zoom {}.".format(tile.tilecoord.z)) nb_tiles[tile.tilecoord.z] = 1 return tile gene.imap(count_tile) consume(gene.tilestream, None) times = {} print('') for z in nb_metatiles: print("{} meta tiles in zoom {}.".format(nb_metatiles[z], z)) times[z] = gene.layer['cost']['metatile_generation_time'] * nb_metatiles[z] price = 0 all_size = 0 all_time = 0 all_tiles = 0 for z in nb_tiles: print('') print("{} tiles in zoom {}.".format(nb_tiles[z], z)) all_tiles += nb_tiles[z] if meta: time = times[z] + gene.layer['cost']['tile_generation_time'] * nb_tiles[z] else: time = gene.layer['cost']['tileonly_generation_time'] * nb_tiles[z] size = gene.layer['cost']['tile_size'] * nb_tiles[z] all_size += size all_time += time td = timedelta(milliseconds=time) print("Time to generate: {} [d h:mm:ss]".format((duration_format(td)))) c = gene.config['cost']['s3']['put'] * nb_tiles[z] / 1000.0 price += c print('S3 PUT: {0:0.2f} [$]'.format(c)) if 'sqs' in gene.config: if meta: nb_sqs = nb_metatiles[z] * 3 else: nb_sqs = nb_tiles[z] * 3 c = nb_sqs * gene.config['cost']['sqs']['request'] / 1000000.0 price += c print('SQS usage: {0:0.2f} [$]'.format(c)) print("") td = timedelta(milliseconds=all_time) print("Number of tiles: {}".format(all_tiles)) print('Generation time: {} [d h:mm:ss]'.format((duration_format(td)))) print('Generation cost: {0:0.2f} [$]'.format(price)) return (all_size, td, price, all_tiles)