Ejemplo n.º 1
0
 def get_valid_versions(self, group_by=None, **kwargs):
     if "layers" not in kwargs:
         return []
     else:
         p = Pyramid.objects(name=layers[0]).first()
         qs = Tile.objects(pyramid=p)
         if "filter" in kwargs:
             qs = qs.filter(**kwargs["filter"])
         return qs.distinct("version")
Ejemplo n.º 2
0
 def get_valid_times(self, **kwargs):
     if "layers" not in kwargs:
         return []
     else:
         p = Pyramid.objects(name=layers[0]).first()
         qs = Tile.objects(pyramid=p)
         if "filter" in kwargs:
             qs = qs.filter(**kwargs["filter"])
         return qs.distinct("time")
Ejemplo n.º 3
0
    def get_2d_dataset(
        self,
        layers,
        srs,
        bbox,
        width,
        height,
        styles=None,
        bgcolor=None,
        transparent=False,
        time=None,
        elevation=None,
        v=None,
        filter=None,
        **kwargs
    ):
        pyramid = Pyramid.objects(name=layers[0]).first()
        minx, miny, maxx, maxy = bbox

        # see if we need to transform the dataset
        default_srid = Pyramid.objects(name=layers[0]).first().srs

        s_srs = djgdal.SpatialReference(default_srid.decode("ascii"))

        print srs
        if not srs:
            t_srs = djgdal.SpatialReference(default_srid.decode("ascii"))
        else:
            t_srs = djgdal.SpatialReference(srs)

        # print t_srs, s_srs

        s_mins = Point(minx, miny, srid=t_srs.wkt)
        s_maxs = Point(maxx, maxy, srid=t_srs.wkt)
        s_mins.transform(s_srs.srid)
        s_maxs.transform(s_srs.srid)

        # figure out the pixel size.  if we're only after one pixel, like for GetFeatureInfo,
        # it's likely that pxsize will be 0.  In that case, set the pixel size to be the
        # smallest available.
        tminx, tminy, tmaxx, tmaxy = bbox
        minx = s_mins.x
        maxx = s_maxs.x
        miny = s_mins.y
        maxy = s_maxs.y
        pxsize = (maxx - minx) / width

        # case of only looking for one pixel
        if pxsize == 0:
            pxsize = pyramid.pxsize_at_levels[-1]
            maxx = maxx + pxsize
            maxy = maxy + pxsize

        # start with the furthest zoomed out tiles and work our way in, searching for the
        # nearest level to the query
        l = 0
        p = 999999999
        for level, px in enumerate(pyramid.pxsize_at_levels):
            if abs(px - pxsize) < p:
                l = level
                p = abs(px - pxsize)

        # open the index for that level, filter it by our bounding box.
        dataset = ogr.Open(pyramid.indices[l].encode("ascii"))
        index = dataset.GetLayer(0)
        index.ResetReading()
        index.SetSpatialFilterRect(minx, miny, maxx, maxy)
        f = index.GetNextFeature()

        # retrieve tiles
        tiles = []
        while f:
            Tile.objects.ensure_index("tile_name")
            Tile.objects.ensure_index("pyramid", "tile_name")
            t = Tile.objects(pyramid=pyramid, level=l, tile_name=f.location).first()
            fl = gdal.FileFromMemBuffer("/vsimem/" + t.tile_name, t.data)
            tiles.append(gdal.Open("/vsimem/" + t.tile_name))
            f = index.GetNextFeature()

        del index  # close the index

        # create our output dataset and stitch tiles into it by projecting them in.
        output = gdal.GetDriverByName("MEM").Create("TEMP", width, height, pyramid.raster_count, pyramid.data_type, [])
        output.SetGeoTransform([tminx, (tmaxx - tminx) / width, 0, tmaxy, 0, (tminy - tmaxy) / height])
        output.SetProjection(t_srs.wkt)

        # print output.GetGeoTransform()

        for tile in tiles:
            # print tile.GetGeoTransform()
            # print tile.GetProjection()
            # print output.GetProjection()
            gdal.ReprojectImage(tile, output, None, None, gdal.GRA_Bilinear)

        return output