def _bands_from_cache(self, indexes=None): """ Caches reprojected source data for multiple usage. """ band_indexes = _get_band_indexes(self, indexes) for band_index in band_indexes: if not band_index in self._np_band_cache: if len(self._get_band_paths(band_index)) == 0: band = masked_array( zeros(self.shape, dtype=self.dtype), mask=True ) else: # create VRT for granules sorted by SRID and combine outputs # to one band: srid_bands = () for paths in self._get_band_paths(band_index).values(): temp_vrt = NamedTemporaryFile() raster_file = temp_vrt.name build_vrt = "gdalbuildvrt %s %s > /dev/null" %( raster_file, ' '.join(paths) ) try: os.system(build_vrt) except: raise IOError("build temporary VRT failed") srid_bands += (read_raster_window( raster_file, self.tile, indexes=1, pixelbuffer=self.pixelbuffer, resampling=self.resampling ).next(), ) band = masked_array( zeros(self.shape, dtype=self.dtype), mask=True ) for srid_band in srid_bands: band = masked_array( data=np.where(band.mask, srid_band, band), mask=np.where(band.mask, srid_band.mask, band.mask) ) self._np_band_cache[band_index] = band yield self._np_band_cache[band_index]
def _bands_from_cache(inp_handler, indexes=None): """ Caches reprojected source data for multiple usage. """ band_indexes = _get_band_indexes(inp_handler, indexes) if isinstance(inp_handler, RasterProcessTile): tile_paths = inp_handler._get_src_tile_paths() temp_vrt = NamedTemporaryFile() raster_file = temp_vrt.name build_vrt = "gdalbuildvrt %s %s > /dev/null" %( raster_file, ' '.join(tile_paths) ) try: os.system(build_vrt) except: raise IOError("build temporary VRT failed") elif isinstance(inp_handler, RasterFileTile): raster_file = inp_handler.input_file for band_index in band_indexes: if not band_index in inp_handler._np_band_cache: if isinstance(inp_handler, RasterProcessTile) and \ len(tile_paths) == 0: band = masked_array( zeros(inp_handler.shape, dtype=inp_handler.dtype), mask=True ) else: band = read_raster_window( raster_file, inp_handler.tile, indexes=band_index, pixelbuffer=inp_handler.pixelbuffer, resampling=inp_handler.resampling ).next() inp_handler._np_band_cache[band_index] = band yield inp_handler._np_band_cache[band_index]