def get_subset(src_ds, range_type, subset_rect, dst_rect, rangesubset=None):
        """ Get subset of the image (as GDAL dataset) matching the requsted
            pixel rectangle.
        """
        vrt = VRTBuilder(*subset_rect.size)

        input_bands = list(range_type)

        # list of band indices/names. defaults to all bands
        if rangesubset:
            subset_bands = rangesubset.get_band_indices(range_type, 1)
        else:
            subset_bands = xrange(1, len(range_type) + 1)

        for dst_index, src_index in enumerate(subset_bands, start=1):
            input_band = input_bands[src_index-1]
            vrt.add_band(input_band.data_type)
            vrt.add_simple_source(
                dst_index, src_ds, src_index, subset_rect, dst_rect
            )

        vrt.copy_metadata(src_ds)
        vrt.copy_gcps(src_ds, subset_rect)

        return vrt.dataset
    def get_source_dataset(self, coverage, data_items, range_type):
        if len(data_items) == 1:
            return gdal.OpenShared(abspath(connect(data_items[0])))
        else:
            vrt = VRTBuilder(
                coverage.size_x, coverage.size_y,
                vrt_filename=temp_vsimem_filename()
            )

            # sort in ascending order according to semantic
            data_items = sorted(data_items, key=(lambda d: d.semantic))

            gcps = []
            compound_index = 0
            for data_item in data_items:
                path = abspath(connect(data_item))

                # iterate over all bands of the data item
                for set_index, item_index in self._data_item_band_indices(data_item):
                    if set_index != compound_index + 1: 
                        raise ValueError
                    compound_index = set_index

                    band = range_type[set_index]
                    vrt.add_band(band.data_type)
                    vrt.add_simple_source(
                        set_index, path, item_index
                    )

            return vrt.dataset
    def perform_range_subset(self, src_ds, range_type, subset_bands, 
                             subset_rect):

        vrt = VRTBuilder(src_ds.RasterXSize, src_ds.RasterYSize)
        dst_rect = Rect(0, 0, src_ds.RasterXSize, src_ds.RasterYSize)

        input_bands = list(range_type)
        for index, subset_band in enumerate(subset_bands, start=1):
            if isinstance(subset_band, int):
                if subset_band > 0:
                    subset_band = len(range_type) + subset_band
            else:
                subset_band = index_of(input_bands,
                    lambda band: (
                        band.name == subset_band 
                        or band.identifier == subset_band
                    )
                )
                if subset_band is None:
                    raise ValueError("Invalid range subset.")

            # prepare and add a simple source for the band
            input_band = input_bands[subset_band]
            vrt.add_band(input_band.data_type)
            vrt.add_simple_source(
                index, src_ds, subset_band, subset_rect, dst_rect
            )

        vrt.copy_metadata(src_ds)
        vrt.copy_gcps(src_ds, subset_rect)

        return vrt.dataset
Exemple #4
0
    def perform_subset(self,
                       src_ds,
                       range_type,
                       subset_rect,
                       dst_rect,
                       rangesubset=None):
        vrt = VRTBuilder(*subset_rect.size)

        input_bands = list(range_type)

        # list of band indices/names. defaults to all bands
        if rangesubset:
            subset_bands = rangesubset.get_band_indices(range_type, 1)
        else:
            subset_bands = xrange(1, len(range_type) + 1)

        for dst_index, src_index in enumerate(subset_bands, start=1):
            input_band = input_bands[src_index - 1]
            vrt.add_band(input_band.data_type)
            vrt.add_simple_source(dst_index, src_ds, src_index, subset_rect,
                                  dst_rect)

        vrt.copy_metadata(src_ds)
        vrt.copy_gcps(src_ds, subset_rect)

        return vrt.dataset
Exemple #5
0
    def get_source_dataset(self, coverage, data_items, range_type):
        if len(data_items) == 1:
            return gdal.OpenShared(abspath(connect(data_items[0])))
        else:
            vrt = VRTBuilder(coverage.size_x,
                             coverage.size_y,
                             vrt_filename=temp_vsimem_filename())

            # sort in ascending order according to semantic
            data_items = sorted(data_items, key=(lambda d: d.semantic))

            gcps = []
            compound_index = 0
            for data_item in data_items:
                path = abspath(connect(data_item))

                # iterate over all bands of the data item
                for set_index, item_index in self._data_item_band_indices(
                        data_item):
                    if set_index != compound_index + 1:
                        raise ValueError
                    compound_index = set_index

                    band = range_type[set_index]
                    vrt.add_band(band.data_type)
                    vrt.add_simple_source(set_index, path, item_index)

            return vrt.dataset
    def perform_subset(self, src_ds, subset_rect):
        vrt = VRTBuilder(src_ds.RasterXSize, src_ds.RasterYSize)
        dst_rect = Rect(0, 0, src_ds.RasterXSize, src_ds.RasterYSize)

        for index in xrange(1, src_ds.RasterCount + 1):
            src_band = src_ds.GetRasterBand(index)
            vrt.add_band(src_band.DataType)
            vrt.add_simple_source(index, src_ds, index, subset_rect, dst_rect)

        vrt.copy_metadata(src_ds)
        vrt.copy_gcps(src_ds, subset_rect)

        return vrt.dataset
    def perform_subset(self, src_ds, range_type, subset_rect, dst_rect, rangesubset=None):
        vrt = VRTBuilder(*subset_rect.size)

        input_bands = list(range_type)

        # list of band indices/names. defaults to all bands
        if rangesubset:
            subset_bands = rangesubset.get_band_indices(range_type, 1)
        else:
            subset_bands = xrange(1, len(range_type) + 1)

        for dst_index, src_index in enumerate(subset_bands, start=1):
            input_band = input_bands[src_index - 1]
            vrt.add_band(input_band.data_type)
            vrt.add_simple_source(dst_index, src_ds, src_index, subset_rect, dst_rect)

        vrt.copy_metadata(src_ds)
        vrt.copy_gcps(src_ds, subset_rect)

        return vrt.dataset