def scale_query_to_tile(self, dsquery, dstile): """Scales down query dataset to the tile dataset""" querysize = dsquery.RasterXSize tilesize = dstile.RasterXSize tilebands = dstile.RasterCount if self.options.resampling == 'average': for i in range(1, tilebands + 1): res = gdal.RegenerateOverview(dsquery.GetRasterBand(i), dstile.GetRasterBand(i), 'average') if res != 0: self.error("RegenerateOverview() failed") else: # Other algorithms are implemented by gdal.ReprojectImage(). dsquery.SetGeoTransform((0.0, tilesize / float(querysize), 0.0, 0.0, 0.0, tilesize / float(querysize))) dstile.SetGeoTransform((0.0, 1.0, 0.0, 0.0, 0.0, 1.0)) res = gdal.ReprojectImage(dsquery, dstile, None, None, self.resampling) if res != 0: self.error("ReprojectImage() failed on %s, error %d" % (tilefilename, res))
def resample(gdal_raster, epsg_code, output_filename, method='mode', x_res=None, y_res=None, \ output_width=None, output_height=None, output_format="GTiff", output_datatype=gdal.GDT_Float32, \ creation_options=["COMPRESS=LZW"]): """ a wrapper around a couple of other boilerplate gdal functions to have a one-off way of resampling a dataset """ input_extent = raster_bbox( gdal_raster.GetGeoTransform(), size=[gdal_raster.RasterXSize, gdal_raster.RasterYSize]) out = generate_raster(input_extent, epsg_code=3338, output_filename=output_filename, x_res=x_res, y_res=y_res, output_width=output_width, output_height=output_height, output_format=output_format, output_datatype=output_datatype, creation_options=creation_options) gdal.RegenerateOverview(gdal_raster.GetRasterBand(1), out.GetRasterBand(1), method) out.GetRasterBand(1).ComputeStatistics(0) out.FlushCache() return out
def resample_average(path, dsquery, dstile): for i in range(1, dstile.RasterCount + 1): res = gdal.RegenerateOverview(dsquery.GetRasterBand(i), dstile.GetRasterBand(i), "average") if res != 0: raise ImageOutputException( "RegenerateOverview() failed with error %d" % res) write_method(path, dstile)
def _gdal_resample_raster(self, out_raster, factor): """ Resample raster using GDAL utility :param factor: :return: """ with GdalOpen(self.raster_file) as source_ds: dst_ds = self._gdal_drv.Create(out_raster, self.x_size * factor, self.y_size * factor, 1, source_ds.GetRasterBand(1).DataType) resample_geo_transform = (self.x_origin, self.res / factor, 0, self.y_origin, 0, -self.res / factor) dst_ds.SetGeoTransform(resample_geo_transform) dst_ds.SetProjection(wkt_from(self.crs)) gdal.RegenerateOverview(source_ds.GetRasterBand(1), dst_ds.GetRasterBand(1), 'mode')
def scale_query_to_tile(self, dsquery, dstile, tilefilename=''): """Scales down query dataset to the tile dataset""" querysize = dsquery.RasterXSize tilesize = dstile.RasterXSize tilebands = dstile.RasterCount # Function: gdal.RegenerateOverview() for i in range(1, tilebands + 1): # Black border around NODATA #if i != 4: # dsquery.GetRasterBand(i).SetNoDataValue(0) res = gdal.RegenerateOverview(dsquery.GetRasterBand(i), dstile.GetRasterBand(i), 'average') if res != 0: self.error("RegenerateOverview() failed on %s, error %d" % (tilefilename, res))
def scaleTile(dsquery, dstile, resampling, tile=''): querysize = dsquery.RasterXSize tile_size = dstile.RasterXSize tilebands = dstile.RasterCount if resampling == 'average': # Function: gdal.RegenerateOverview() for i in range(1, tilebands + 1): # Black border around NODATA res = gdal.RegenerateOverview(dsquery.GetRasterBand(i), dstile.GetRasterBand(i), 'average') if res != 0: QgsMessageLog.logMessage( "RegenerateOverview() failed on %s, error %d" % (tile, res), CATEGORY, Qgis.Info) elif resampling == 'antialias' and numpy_available: # Scaling by PIL (Python Imaging Library) - improved Lanczos array = numpy.zeros((querysize, querysize, tilebands), numpy.uint8) for i in range(tilebands): array[:, :, i] = gdalarray.BandReadAsArray(dsquery.GetRasterBand(i + 1), 0, 0, querysize, querysize) im = Image.fromarray(array, 'RGBA') # Always four bands im1 = im.resize((tile_size, tile_size), Image.ANTIALIAS) if os.path.exists(tile): im0 = Image.open(tile) im1 = Image.composite(im1, im0, im1) im1.save(tile, 'PNG') else: if resampling == 'near': gdal_resampling = gdal.GRA_NearestNeighbour elif resampling == 'bilinear': gdal_resampling = gdal.GRA_Bilinear elif resampling == 'cubic': gdal_resampling = gdal.GRA_Cubic elif resampling == 'cubicspline': gdal_resampling = gdal.GRA_CubicSpline elif resampling == 'lanczos': gdal_resampling = gdal.GRA_Lanczos elif resampling == 'mode': gdal_resampling = gdal.GRA_Mode elif resampling == 'max': gdal_resampling = gdal.GRA_Max elif resampling == 'min': gdal_resampling = gdal.GRA_Min elif resampling == 'med': gdal_resampling = gdal.GRA_Med elif resampling == 'q1': gdal_resampling = gdal.GRA_Q1 elif resampling == 'q3': gdal_resampling = gdal.GRA_Q3 # Other algorithms are implemented by gdal.ReprojectImage(). dsquery.SetGeoTransform((0.0, tile_size / float(querysize), 0.0, 0.0, 0.0, tile_size / float(querysize))) dstile.SetGeoTransform((0.0, 1.0, 0.0, 0.0, 0.0, 1.0)) res = gdal.ReprojectImage(dsquery, dstile, None, None, gdal_resampling) if res != 0: QgsMessageLog.logMessage( "ReprojectImage() failed on %s, error %d" % (tile, res), CATEGORY, Qgis.Info)