def init_grid(self): connection = self.layer.metadata.get("connection") data = self.layer.metadata.get("data") if connection and data: logger.info("generating from postgis query") return vector(self.layer, self.bbox, self.levels, connection, data) else: logger.info("generating all tiles") return grid(self.layer, self.bbox, self.levels)
def test_dump_load(self): service = tilecache_service() raster = service.layers.get("valid-raster") in_tiles = [_ for _ in grid(raster, bbox=[655000,194000,672500,206000], levels=(19,20))] f = StringIO() dump(raster, in_tiles, f) f.seek(0) out_layername, out_tiles = load(f, service) f.close() assert out_layername == raster.name assert out_tiles == in_tiles
def vector(tcLayer, bounds, levels, connection, data): # http://www.gdal.org/ogr/classOGRDataSource.html#a6acc228db6513784a56ce12334a8c33 pad = pack('x') tiles = {} ogrBounds = polygon(bounds) ds = ogr.Open("PG:%s"%connection.strip('"')) if ds is None: raise Exception("PGconnectdb failed: '%s'"%connection) assert ds is not None for sql in re.split('"\W*"', data): layer = ds.ExecuteSQL("SELECT " + sql.strip('" '), ogrBounds) if layer is not None: layer.ResetReading() for geometry in (f.GetGeometryRef() for f in layer): minx, maxx, miny, maxy = geometry.GetEnvelope() # dont generate all tile for the whole feature if it is larger than the requested bbox logger.debug('feature bbox: ' + str(minx) + ' ' + str(miny) + ' ' + str(maxx) + ' ' + str(maxy)) nminx = max(min(minx,maxx),min(bounds[0],bounds[2])) nminy = max(min(miny,maxy),min(bounds[1],bounds[3])) nmaxx = min(max(minx,maxx),max(bounds[0],bounds[2])) nmaxy = min(max(miny,maxy),max(bounds[1],bounds[3])) logger.debug('final bbox: ' + str(nminx) + ' ' + str(nminy) + ' ' + str(nmaxx) + ' ' + str(nmaxy)) for x, y, z in grid(tcLayer, (nminx, nminy, nmaxx, nmaxy), levels, use_buffer=True): tile = pack('3i', x, y, z) if tile not in tiles: metatile = MetaTile(tcLayer, x, y, z) tminx, tminy, tmaxx, tmaxy = metatile.bounds() shift = tcLayer.yshift(z) if hasattr(tcLayer, 'yshift') else 0.0 tminy -= shift tmaxy -= shift if geometry.Intersect(polygon((tminx, tminy, tmaxx, tmaxy))): tiles[tile] = pad yield (x, y, z) ds.ReleaseResultSet(layer) ds.Destroy()