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_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
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
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
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 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
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