def get_source_and_dest_rect(self, dataset, subsets):
        size_x, size_y = dataset.RasterXSize, dataset.RasterYSize
        image_rect = Rect(0, 0, size_x, size_y)

        if not subsets:
            subset_rect = image_rect

        # pixel subset
        elif subsets.srid is None:  # means "imageCRS"
            minx, miny, maxx, maxy = subsets.xy_bbox

            minx = int(minx) if minx is not None else image_rect.offset_x
            miny = int(miny) if miny is not None else image_rect.offset_y
            maxx = int(maxx) if maxx is not None else image_rect.upper_x
            maxy = int(maxy) if maxy is not None else image_rect.upper_y

            subset_rect = Rect(minx, miny, maxx - minx + 1, maxy - miny + 1)

        # subset in geographical coordinates
        else:
            vrt = VRTBuilder(*image_rect.size)
            vrt.copy_gcps(dataset)

            options = reftools.suggest_transformer(dataset)

            subset_rect = reftools.rect_from_subset(vrt.dataset, subsets.srid, *subsets.xy_bbox, **options)

        # check whether or not the subsets intersect with the image
        if not image_rect.intersects(subset_rect):
            raise RenderException("Subset outside coverage extent.", "subset")

        src_rect = subset_rect  # & image_rect # TODO: why no intersection??
        dst_rect = src_rect - subset_rect.offset

        return src_rect, dst_rect
Exemplo n.º 2
0
 def as_rect(self):
     """Cast to a `Rect` object. (Available only for the 2D bounding-box)."""
     if self.dimension != 2:
         raise RuntimeError(
             "Only 2D bounding-box can be cast to a rectangle object!"
         )
     return Rect(self[0][0], self[0][1], None, None, self[1][0], self[1][1])
    def get_source_image_rect(self, dataset, subsets):
        size_x, size_y = dataset.RasterXSize, dataset.RasterYSize
        image_rect = Rect(0, 0, size_x, size_y)

        if not subsets:
            subset_rect = image_rect

        # pixel subset
        elif subsets.xy_srid is None: # means "imageCRS"
            minx, miny, maxx, maxy = subsets.xy_bbox

            minx = minx if minx is not None else image_rect.offset_x
            miny = miny if miny is not None else image_rect.offset_y
            maxx = maxx if maxx is not None else image_rect.upper_x
            maxy = maxy if maxy is not None else image_rect.upper_y

            subset_rect = Rect(minx, miny, maxx-minx, maxy-miny)

        else:
            vrt = VRTBuilder(size_x, size_y)
            vrt.copy_gcps(dataset)

            minx, miny, maxx, maxy = subsets.xy_bbox

            # subset in geographical coordinates
            subset_rect = reftools.rect_from_subset(
                vrt.dataset, subsets.xy_srid, minx, miny, maxx, maxy
            )

        # check whether or not the subsets intersect with the image
        if not image_rect.intersects(subset_rect):
            raise Exception("Subset outside coverage extent.") # TODO: correct exception

        # in case the input and output rects are the same, return None to 
        # indicate this
        #if image_rect == subset_rect:
        #    return None

        return image_rect & subset_rect
Exemplo n.º 4
0
    def get_source_and_dest_rect(self, dataset, subsets):
        size_x, size_y = dataset.RasterXSize, dataset.RasterYSize
        image_rect = Rect(0, 0, size_x, size_y)

        if not subsets:
            subset_rect = image_rect

        # pixel subset
        elif subsets.srid is None:  # means "imageCRS"
            minx, miny, maxx, maxy = subsets.xy_bbox

            minx = int(minx) if minx is not None else image_rect.offset_x
            miny = int(miny) if miny is not None else image_rect.offset_y
            maxx = int(maxx) if maxx is not None else image_rect.upper_x
            maxy = int(maxy) if maxy is not None else image_rect.upper_y

            subset_rect = Rect(minx, miny, maxx - minx + 1, maxy - miny + 1)

        # subset in geographical coordinates
        else:
            vrt = VRTBuilder(*image_rect.size)
            vrt.copy_gcps(dataset)

            options = reftools.suggest_transformer(dataset)

            subset_rect = reftools.rect_from_subset(vrt.dataset, subsets.srid,
                                                    *subsets.xy_bbox,
                                                    **options)

        # check whether or not the subsets intersect with the image
        if not image_rect.intersects(subset_rect):
            raise RenderException("Subset outside coverage extent.", "subset")

        src_rect = subset_rect  #& image_rect # TODO: why no intersection??
        dst_rect = src_rect - subset_rect.offset

        return src_rect, dst_rect
Exemplo n.º 5
0
def rect_from_subset(path_or_ds,
                     srid,
                     minx,
                     miny,
                     maxx,
                     maxy,
                     method=METHOD_GCP,
                     order=0):
    """ Returns the smallest area of an image for the given spatial subset.

    :param path_or_ds: a :class:`GDAL Dataset <eoxserver.contrib.gdal.Dataset>`
                       or a path to such
    :param srid: the SRID the ``minx``, ``miny``, ``maxx`` and ``maxy`` are
                 expressed in
    :param minx: the minimum X subset coordinate
    :param miny: the minimum Y subset coordinate
    :param maxx: the maximum X subset coordinate
    :param maxy: the maximum Y subset coordinate
    :param method: either of :const:`METHOD_GCP`, :const:`METHOD_TPS` or
                   :const:`METHOD_TPS_LSQ`.
    :param order: the order of the function; see :func:`get_footprint_wkt` for
                  reference
    :returns: a :class:`Rect <eoxserver.core.util.rect.Rect>` portraing the
              subset in image coordinates
    """

    #import pdb; pdb.set_trace()
    ds = path_or_ds

    x_size = ds.RasterXSize
    y_size = ds.RasterYSize

    transformer = _create_referenceable_grid_transformer(ds, method, order)

    gcp_srs = osr.SpatialReference(ds.GetGCPProjection())

    subset_srs = osr.SpatialReference()
    subset_srs.ImportFromEPSG(srid)

    coord_array_type = (C.c_double * 4)
    x = coord_array_type()
    y = coord_array_type()
    z = coord_array_type()

    success = (C.c_int * 4)()

    x[1] = float(x_size)
    y[1] = 0.0

    x[2] = float(x_size)
    y[2] = float(y_size)

    x[3] = 0.0
    y[3] = float(y_size)

    GDALUseTransformer(transformer, False, 4, x, y, z, success)

    dist = min((max(x) - min(x)) / (x_size / 100),
               (max(y) - min(y)) / (y_size / 100))

    x[0] = x[3] = minx
    x[1] = x[2] = maxx
    y[0] = y[1] = miny
    y[2] = y[3] = maxy

    ct = CoordinateTransformation(subset_srs, gcp_srs)

    OCTTransform(ct, 4, x, y, z)

    num_x = int(math.ceil((max(x) - min(x)) / dist))
    num_y = int(math.ceil((max(y) - min(y)) / dist))

    x_step = (maxx - minx) / num_x
    y_step = (maxy - miny) / num_y

    num_points = 4 + 2 * num_x + 2 * num_y

    coord_array_type = (C.c_double * num_points)
    x = coord_array_type()
    y = coord_array_type()
    z = coord_array_type()
    success = (C.c_int * num_points)()

    x[0] = minx
    y[0] = miny

    for i in xrange(1, num_x + 1):
        x[i] = minx + i * x_step
        y[i] = miny

    x[num_x + 1] = maxx
    y[num_x + 1] = miny

    for i in xrange(1, num_y + 1):
        x[num_x + 1 + i] = maxx
        y[num_x + 1 + i] = miny + i * y_step

    x[num_x + num_y + 2] = maxx
    y[num_x + num_y + 2] = maxy

    for i in xrange(1, num_x + 1):
        x[num_x + num_y + 2 + i] = maxx - i * x_step
        y[num_x + num_y + 2 + i] = maxy

    x[num_x * 2 + num_y + 3] = minx
    y[num_x * 2 + num_y + 3] = maxy

    for i in xrange(1, num_y + 1):
        x[num_x * 2 + num_y + 3 + i] = minx
        y[num_x * 2 + num_y + 3 + i] = maxy - i * y_step

    OCTTransform(ct, num_points, x, y, z)
    GDALUseTransformer(transformer, True, num_points, x, y, z, success)

    minx = int(math.floor(min(x)))
    miny = int(math.floor(min(y)))
    size_x = int(math.ceil(max(x) - minx) + 1)
    size_y = int(math.ceil(max(y) - miny) + 1)

    return Rect(minx, miny, size_x, size_y)