def misc_9(): old_val = gdal.GetCacheMax() gdal.SetCacheMax(3000000000) ret_val = gdal.GetCacheMax() gdal.SetCacheMax(old_val) if ret_val != 3000000000: gdaltest.post_reason('did not get expected value') print(ret_val) return 'fail' return 'success'
def basic_test_7_internal(): try: ds = gdal.Open('non_existing_ds', gdal.GA_ReadOnly) gdaltest.post_reason('opening should have thrown an exception') return 'fail' except: # Special case: we should still be able to get the error message # until we call a new GDAL function if gdal.GetLastErrorMsg( ) != '`non_existing_ds\' does not exist in the file system,\nand is not recognised as a supported dataset name.\n': gdaltest.post_reason('did not get expected error message') return 'fail' if gdal.GetLastErrorType() == 0: gdaltest.post_reason('did not get expected error type') return 'fail' # Should issue an implicit CPLErrorReset() gdal.GetCacheMax() if gdal.GetLastErrorType() != 0: gdaltest.post_reason('got unexpected error type') return 'fail' return 'success'
def mask_23(): if gdaltest.have_ng == 0: return 'skip' drv = gdal.GetDriverByName('GTiff') md = drv.GetMetadata() if md['DMD_CREATIONOPTIONLIST'].find('JPEG') == -1: return 'skip' src_ds = drv.Create('tmp/mask_23_src.tif', 3000, 2000, 3, options=['TILED=YES', 'SPARSE_OK=YES']) src_ds.CreateMaskBand(gdal.GMF_PER_DATASET) gdal.SetConfigOption('GDAL_TIFF_INTERNAL_MASK', 'YES') old_val = gdal.GetCacheMax() gdal.SetCacheMax(15000000) gdal.ErrorReset() ds = drv.CreateCopy('tmp/mask_23_dst.tif', src_ds, options=['TILED=YES', 'COMPRESS=JPEG']) gdal.SetConfigOption('GDAL_TIFF_INTERNAL_MASK', 'NO') gdal.SetCacheMax(old_val) ds = None error_msg = gdal.GetLastErrorMsg() src_ds = None drv.Delete('tmp/mask_23_src.tif') drv.Delete('tmp/mask_23_dst.tif') # 'ERROR 1: TIFFRewriteDirectory:Error fetching directory count' was triggered before if error_msg != '': return 'fail' return 'success'
def jpeg_18(): import struct height = 1024 width = 1024 src_ds = gdal.GetDriverByName('GTiff').Create('/vsimem/jpeg_18.tif', width, height, 1) for i in range(height): data = struct.pack('B' * 1, int(i / (height / 256))) src_ds.WriteRaster(0, i, width, 1, data, 1, 1) ds = gdal.GetDriverByName('JPEG').CreateCopy('/vsimem/jpeg_18.jpg', src_ds, options=['QUALITY=99']) src_ds = None gdal.Unlink('/vsimem/jpeg_18.tif') oldSize = gdal.GetCacheMax() gdal.SetCacheMax(0) line0 = ds.GetRasterBand(1).ReadRaster(0, 0, width, 1) data = struct.unpack('B' * width, line0) if abs(data[0] - 0) > 10: return 'fail' line1023 = ds.GetRasterBand(1).ReadRaster(0, height - 1, width, 1) data = struct.unpack('B' * width, line1023) if abs(data[0] - 255) > 10: return 'fail' line0_ovr1 = ds.GetRasterBand(1).GetOverview(1).ReadRaster( 0, 0, width / 4, 1) data = struct.unpack('B' * (width / 4), line0_ovr1) if abs(data[0] - 0) > 10: return 'fail' line1023_bis = ds.GetRasterBand(1).ReadRaster(0, height - 1, width, 1) if line1023_bis == line0 or line1023 != line1023_bis: gdaltest.post_reason('fail') return 'fail' line0_bis = ds.GetRasterBand(1).ReadRaster(0, 0, width, 1) if line0 != line0_bis: gdaltest.post_reason('fail') return 'fail' line255_ovr1 = ds.GetRasterBand(1).GetOverview(1).ReadRaster( 0, height / 4 - 1, width / 4, 1) data = struct.unpack('B' * (width / 4), line255_ovr1) if abs(data[0] - 255) > 10: return 'fail' line0_bis = ds.GetRasterBand(1).ReadRaster(0, 0, width, 1) if line0 != line0_bis: gdaltest.post_reason('fail') return 'fail' line0_ovr1_bis = ds.GetRasterBand(1).GetOverview(1).ReadRaster( 0, 0, width / 4, 1) if line0_ovr1 != line0_ovr1_bis: gdaltest.post_reason('fail') return 'fail' line255_ovr1_bis = ds.GetRasterBand(1).GetOverview(1).ReadRaster( 0, height / 4 - 1, width / 4, 1) if line255_ovr1 != line255_ovr1_bis: gdaltest.post_reason('fail') return 'fail' gdal.SetCacheMax(oldSize) ds = None gdal.Unlink('/vsimem/jpeg_18.jpg') return 'success'
def _create_thumbnail(red_file, green_file, blue_file, output_path, x_constraint=None, nodata=-999, work_dir=None, overwrite=True): """ Create JPEG thumbnail image using individual R, G, B images. This method comes from the old ULA codebase. :param red_file: red band data file :param green_file: green band data file :param blue_file: blue band data file :param output_path: thumbnail file to write to. :param x_constraint: thumbnail width (if not full resolution) :param nodata: null/fill data value :param work_dir: temp/work directory to use. :param overwrite: overwrite existing thumbnail? Thumbnail height is adjusted automatically to match the aspect ratio of the input images. """ nodata = int(nodata) # GDAL calls need absolute paths. thumbnail_path = pathlib.Path(output_path).absolute() if thumbnail_path.exists() and not overwrite: _LOG.warning('File already exists. Skipping creation of %s', thumbnail_path) return None, None, None # thumbnail_image = os.path.abspath(thumbnail_image) out_directory = str(thumbnail_path.parent) work_dir = os.path.abspath(work_dir) if work_dir else tempfile.mkdtemp(prefix='.thumb-tmp', dir=out_directory) try: # working files file_to = os.path.join(work_dir, 'rgb.vrt') warp_to_file = os.path.join(work_dir, 'rgb-warped.vrt') outtif = os.path.join(work_dir, 'thumbnail.tif') # Build the RGB Virtual Raster at full resolution run_command( [ "gdalbuildvrt", "-overwrite", "-separate", file_to, str(red_file), str(green_file), str(blue_file) ], work_dir ) assert os.path.exists(file_to), "VRT must exist" # Determine the pixel scaling to get the correct width thumbnail vrt = gdal.Open(file_to) intransform = vrt.GetGeoTransform() inpixelx = intransform[1] # inpixely = intransform[5] inrows = vrt.RasterYSize incols = vrt.RasterXSize # If a specific resolution is asked for. if x_constraint: outresx = inpixelx * incols / x_constraint _LOG.info('Input pixel res %r, output pixel res %r', inpixelx, outresx) outrows = int(math.ceil((float(inrows) / float(incols)) * x_constraint)) run_command([ "gdalwarp", "--config", "GDAL_CACHEMAX", str(GDAL_CACHE_MAX_MB), "-of", "VRT", "-tr", str(outresx), str(outresx), "-r", "near", "-overwrite", file_to, warp_to_file ], work_dir) else: # Otherwise use a full resolution browse image. outrows = inrows x_constraint = incols warp_to_file = file_to outresx = inpixelx _LOG.debug('Current GDAL cache max %rMB. Setting to %rMB', gdal.GetCacheMax() / 1024 / 1024, GDAL_CACHE_MAX_MB) gdal.SetCacheMax(GDAL_CACHE_MAX_MB * 1024 * 1024) # Open VRT file to array vrt = gdal.Open(warp_to_file) driver = gdal.GetDriverByName("GTiff") outdataset = driver.Create(outtif, x_constraint, outrows, 3, gdalconst.GDT_Byte) # Loop through bands and apply Scale and Offset for band_number in (1, 2, 3): band = vrt.GetRasterBand(band_number) scale, offset = _calculate_scale_offset(nodata, band) # Apply gain and offset outdataset.GetRasterBand(band_number).WriteArray( (numpy.ma.masked_less_equal(band.ReadAsArray(), nodata) * scale) + offset ) _LOG.debug('Scale %r, offset %r', scale, offset) # Must close datasets to flush to disk. # noinspection PyUnusedLocal outdataset = None # noinspection PyUnusedLocal vrt = None # GDAL Create doesn't support JPEG so we need to make a copy of the GeoTIFF run_command( [ "gdal_translate", "--config", "GDAL_CACHEMAX", str(GDAL_CACHE_MAX_MB), "-of", "JPEG", outtif, str(thumbnail_path) ], work_dir) _LOG.debug('Cleaning work files') finally: # Clean up work files if os.path.exists(work_dir): shutil.rmtree(work_dir) # Newer versions of GDAL create aux files due to the histogram. Clean them up. for f in (red_file, blue_file, green_file): f = pathlib.Path(f) aux_file = f.with_name(f.name + '.aux.xml') if aux_file.exists(): _LOG.info('Cleaning aux: %s', aux_file) os.remove(str(aux_file.absolute())) return x_constraint, outrows, outresx