Exemplo n.º 1
0
def tiles(layersObj, tcLayer, bbox=None, levels=None, dataProjectionString=None, tilesProjectionString=None):
    dataProj = None
    if (dataProjectionString != None):
        print("Data projection: %s"%dataProjectionString)
        dataProj = mapscript.projectionObj(dataProjectionString)

    tilesProj = None
    if (tilesProjectionString != None):
        print("Tiles projection: %s"%tilesProjectionString)
        tilesProj = mapscript.projectionObj(tilesProjectionString)
    elif (tcLayer.srs != None and tcLayer.srs != ""):
        print("Tiles projection: %s"%tcLayer.srs)
        tilesProj = mapscript.projectionObj("+init=" + tcLayer.srs.lower())

    """ yield all non empty tiles indexes (x, y, z) """
    pad = pack('x')
    done = {}
    for layerObj in layersObj:
        if (dataProj == None and layerObj.map.getProjection() != ""):
            print("Data projection: %s"%layerObj.map.getProjection())
            dataProj = mapscript.projectionObj(layerObj.map.getProjection())

        for shapeObj in shapes(layerObj, projectArray(bbox, tilesProj, dataProj)): # reprojet bbox tiles -> data
            for x, y, z in tcLayer.range(projectRect(shapeObj.bounds, dataProj, tilesProj), levels):   # first filter using shapes bbox, reprojet bounds data -> tiles
                key = pack('3i', x, y, z)
                if key not in done:
                    tile = MetaTile(tcLayer, x, y, z)
                    # FIXME: handle metaSize
                    rect = mapscript.rectObj(*tile.bufferbounds())
                    if intersects(shapeObj, projectRect(rect, tilesProj, dataProj)):    # second filter using shapes geometry, reprojet bufferbounds tiles -> data
                        done[key] = pad
                        yield layerObj, shapeObj, x, y, z
Exemplo n.º 2
0
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()