def plmosaic_21(): if gdaltest.plmosaic_drv is None: return 'skip' gdal.SetConfigOption('PL_URL', '/vsimem/root') ds = gdal.OpenEx('PLMosaic:', gdal.OF_RASTER, open_options=['API_KEY=foo', 'MOSAIC=my_mosaic', 'CACHE_PATH=', 'USE_TILES=YES']) gdal.SetConfigOption('PL_URL', None) gdal.ErrorReset() gdal.PushErrorHandler() ds.ReadRaster(256, 512, 1, 1) gdal.PopErrorHandler() if gdal.GetLastErrorMsg() == '': gdaltest.post_reason('fail') return 'fail' gdal.ErrorReset() gdal.PushErrorHandler() ds.GetRasterBand(1).ReadRaster(256, 512, 1, 1) gdal.PopErrorHandler() if gdal.GetLastErrorMsg() == '': gdaltest.post_reason('fail') return 'fail' gdal.ErrorReset() gdal.PushErrorHandler() ds.GetRasterBand(1).ReadBlock(1, 2) gdal.PopErrorHandler() if gdal.GetLastErrorMsg() == '': gdaltest.post_reason('fail') return 'fail' gdal.FileFromMemBuffer('/vsimem/root/?name__is=mosaic_uint16', """{ "mosaics": [{ "id": "mosaic_uint16", "name": "mosaic_uint16", "coordinate_system": "EPSG:3857", "datatype": "uint16", "grid": { "quad_size": 4096, "resolution": 4.77731426716 }, "first_acquired": "first_date", "last_acquired": "last_date", "_links" : { "tiles" : "/vsimem/root/mosaic_uint16/tiles{0-3}/{z}/{x}/{y}.png" }, "quad_download": true }] }""") # Should emit a warning gdal.ErrorReset() gdal.PushErrorHandler() gdal.SetConfigOption('PL_URL', '/vsimem/root') ds = gdal.OpenEx('PLMosaic:', gdal.OF_RASTER, open_options=['API_KEY=foo', 'MOSAIC=mosaic_uint16', 'CACHE_PATH=', 'USE_TILES=YES']) gdal.SetConfigOption('PL_URL', None) gdal.PopErrorHandler() if gdal.GetLastErrorMsg().find('Cannot use tile API for full resolution data on non Byte mosaic') < 0: gdaltest.post_reason('fail') print(gdal.GetLastErrorMsg()) return 'fail' gdal.FileFromMemBuffer('/vsimem/root/?name__is=mosaic_without_tiles', """{ "mosaics": [{ "id": "mosaic_without_tiles", "name": "mosaic_without_tiles", "coordinate_system": "EPSG:3857", "datatype": "byte", "grid": { "quad_size": 4096, "resolution": 4.77731426716 }, "first_acquired": "first_date", "last_acquired": "last_date", "quad_download": true }] }""") # Should emit a warning gdal.ErrorReset() gdal.PushErrorHandler() gdal.SetConfigOption('PL_URL', '/vsimem/root') ds = gdal.OpenEx('PLMosaic:', gdal.OF_RASTER, open_options=['API_KEY=foo', 'MOSAIC=mosaic_without_tiles', 'CACHE_PATH=', 'USE_TILES=YES']) gdal.SetConfigOption('PL_URL', None) gdal.PopErrorHandler() if gdal.GetLastErrorMsg().find('Cannot find tile definition, so use_tiles will be ignored') < 0: gdaltest.post_reason('fail') print(gdal.GetLastErrorMsg()) return 'fail' return 'success'
def merge_img(out_file, *args, is_verbose=0, is_quiet=0, separate=1, frmt=None, ): global verbose, quiet verbose = is_verbose quiet = is_quiet names = [] ulx = None psize_x = None copy_pct = 0 nodata = None a_nodata = None create_options = [] pre_init = [] band_type = None createonly = 0 bTargetAlignedPixels = False start_time = time.time() names.extend(args) if frmt is None: frmt = GetOutputDriverFor(out_file) Driver = gdal.GetDriverByName(frmt) if Driver is None: logger.error('Format driver %s not found, pick a supported driver.' % frmt) exit(1) DriverMD = Driver.GetMetadata() if 'DCAP_CREATE' not in DriverMD: logger.error('Format driver %s does not support creation and piecewise writing.\nPlease select a format that does, such as GTiff (the default) or HFA (Erdas Imagine).' % frmt) exit(1) # Collect information on all the source files. file_infos = names_to_fileinfos(names) if ulx is None: ulx = file_infos[0].ulx uly = file_infos[0].uly lrx = file_infos[0].lrx lry = file_infos[0].lry for fi in file_infos: ulx = min(ulx, fi.ulx) uly = max(uly, fi.uly) lrx = max(lrx, fi.lrx) lry = min(lry, fi.lry) if psize_x is None: psize_x = file_infos[0].geotransform[1] psize_y = file_infos[0].geotransform[5] if band_type is None: band_type = file_infos[0].band_type # Try opening as an existing file. gdal.PushErrorHandler('CPLQuietErrorHandler') t_fh = gdal.Open(out_file, gdal.GA_Update) gdal.PopErrorHandler() # Create output file if it does not already exist. if t_fh is None: if bTargetAlignedPixels: ulx = math.floor(ulx / psize_x) * psize_x lrx = math.ceil(lrx / psize_x) * psize_x lry = math.floor(lry / -psize_y) * -psize_y uly = math.ceil(uly / -psize_y) * -psize_y geotransform = [ulx, psize_x, 0, uly, 0, psize_y] xsize = int((lrx - ulx) / geotransform[1] + 0.5) ysize = int((lry - uly) / geotransform[5] + 0.5) if separate != 0: bands = 0 for fi in file_infos: bands = bands + fi.bands else: bands = file_infos[0].bands t_fh = Driver.Create(out_file, xsize, ysize, bands, band_type, create_options) if t_fh is None: logger.error('Creation failed, terminating gdal_merge.') exit(1) t_fh.SetGeoTransform(geotransform) t_fh.SetProjection(file_infos[0].projection) if copy_pct: t_fh.GetRasterBand(1).SetRasterColorTable(file_infos[0].ct) else: if separate != 0: bands = 0 for fi in file_infos: bands = bands + fi.bands if t_fh.RasterCount < bands: logger.error('Existing output file has less bands than the input files. You should delete it before. Terminating gdal_merge.') exit(1) else: bands = min(file_infos[0].bands, t_fh.RasterCount) # Do we need to set nodata value ? if a_nodata is not None: for i in range(t_fh.RasterCount): t_fh.GetRasterBand(i + 1).SetNoDataValue(a_nodata) # Do we need to pre-initialize the whole mosaic file to some value? if pre_init is not None: if t_fh.RasterCount <= len(pre_init): for i in range(t_fh.RasterCount): t_fh.GetRasterBand(i + 1).Fill(pre_init[i]) elif len(pre_init) == 1: for i in range(t_fh.RasterCount): t_fh.GetRasterBand(i + 1).Fill(pre_init[0]) # Copy data from source files into output file. t_band = 1 if quiet == 0 and verbose == 0: progress(0.0) fi_processed = 0 for fi in file_infos: if createonly != 0: continue if verbose != 0: print("") print("Processing file %5d of %5d, %6.3f%% completed in %d minutes." % (fi_processed + 1, len(file_infos), fi_processed * 100.0 / len(file_infos), int(round((time.time() - start_time) / 60.0)))) fi.report() if separate == 0: for band in range(1, bands + 1): fi.copy_into(t_fh, band, band, nodata) else: for band in range(1, fi.bands + 1): fi.copy_into(t_fh, band, t_band, nodata) t_band = t_band + 1 fi_processed = fi_processed + 1 if quiet == 0 and verbose == 0: progress(fi_processed / float(len(file_infos))) # Force file to be closed. t_fh = None
def misc_12(): if int(gdal.VersionInfo('VERSION_NUM')) < 1900: gdaltest.post_reason('would crash') return 'skip' import test_cli_utilities gdal_translate_path = test_cli_utilities.get_gdal_translate_path() for i in range(gdal.GetDriverCount()): drv = gdal.GetDriver(i) md = drv.GetMetadata() if ('DCAP_CREATECOPY' in md or 'DCAP_CREATE' in md) and 'DCAP_RASTER' in md: nbands = 1 if drv.ShortName == 'WEBP' or drv.ShortName == 'ADRG': nbands = 3 datatype = gdal.GDT_Byte if drv.ShortName == 'BT' or drv.ShortName == 'BLX': datatype = gdal.GDT_Int16 elif drv.ShortName == 'GTX' or drv.ShortName == 'NTv2' or drv.ShortName == 'Leveller' : datatype = gdal.GDT_Float32 size = 1201 if drv.ShortName == 'BLX': size = 128 src_ds = gdal.GetDriverByName('GTiff').Create('/vsimem/misc_12_src.tif', size, size, nbands, datatype) set_gt = (2,1.0/size,0,49,0,-1.0/size) src_ds.SetGeoTransform(set_gt) src_ds.SetProjection('GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["degree",0.01745329251994328]]') # Test to detect crashes gdal.PushErrorHandler('CPLQuietErrorHandler') ds = drv.CreateCopy('/nonexistingpath' + get_filename(drv, ''), src_ds) gdal.PopErrorHandler() if ds is None and gdal.GetLastErrorMsg() == '': gdaltest.post_reason('failure') print('CreateCopy() into non existing dir fails without error message for driver %s' % drv.ShortName) return 'fail' ds = None if gdal_translate_path is not None: # Test to detect memleaks ds = gdal.GetDriverByName('VRT').CreateCopy('tmp/misc_12.vrt', src_ds) (out, err) = gdaltest.runexternal_out_and_err(gdal_translate_path + ' -of ' + drv.ShortName + ' tmp/misc_12.vrt /nonexistingpath/' + get_filename(drv, ''), check_memleak = False) del ds gdal.Unlink('tmp/misc_12.vrt') # If DEBUG_VSIMALLOC_STATS is defined, this is an easy way # to catch some memory leaks if out.find('VSIMalloc + VSICalloc - VSIFree') != -1 and \ out.find('VSIMalloc + VSICalloc - VSIFree : 0') == -1: if drv.ShortName == 'Rasterlite' and out.find('VSIMalloc + VSICalloc - VSIFree : 1') != -1: pass else: print('memleak detected for driver %s' % drv.ShortName) src_ds = None gdal.Unlink('/vsimem/misc_12_src.tif') return 'success'
def validate(ds, check_tiled=True): """Check if a file is a (Geo)TIFF with cloud optimized compatible structure. Args: ds: GDAL Dataset for the file to inspect. check_tiled: Set to False to ignore missing tiling. Returns: A tuple, whose first element is an array of error messages (empty if there is no error), and the second element, a dictionary with the structure of the GeoTIFF file. Raises: ValidateCloudOptimizedGeoTIFFException: Unable to open the file or the file is not a Tiff. """ if int(gdal.VersionInfo('VERSION_NUM')) < 2020000: raise ValidateCloudOptimizedGeoTIFFException( 'GDAL 2.2 or above required') unicode_type = type(''.encode('utf-8').decode('utf-8')) if isinstance(ds, (str, unicode_type)): gdal.PushErrorHandler() ds = gdal.Open(ds) gdal.PopErrorHandler() if ds is None: raise ValidateCloudOptimizedGeoTIFFException( 'Invalid file : %s' % gdal.GetLastErrorMsg()) if ds.GetDriver().ShortName != 'GTiff': raise ValidateCloudOptimizedGeoTIFFException( 'The file is not a GeoTIFF') details = {} errors = [] filename = ds.GetDescription() main_band = ds.GetRasterBand(1) ovr_count = main_band.GetOverviewCount() filelist = ds.GetFileList() if filelist is not None and filename + '.ovr' in filelist: errors += [ 'Overviews found in external .ovr file. They should be internal'] if main_band.XSize >= 512 or main_band.YSize >= 512: if check_tiled: block_size = main_band.GetBlockSize() if block_size[0] == main_band.XSize and block_size[0] > 1024: errors += [ 'The file is greater than 512xH or Wx512, but is not tiled'] if ovr_count == 0: errors += [ 'The file is greater than 512xH or Wx512, but has no overviews'] ifd_offset = int(main_band.GetMetadataItem('IFD_OFFSET', 'TIFF')) ifd_offsets = [ifd_offset] if ifd_offset not in (8, 16): errors += [ 'The offset of the main IFD should be 8 for ClassicTIFF ' 'or 16 for BigTIFF. It is %d instead' % ifd_offsets[0]] details['ifd_offsets'] = {} details['ifd_offsets']['main'] = ifd_offset for i in range(ovr_count): # Check that overviews are by descending sizes ovr_band = ds.GetRasterBand(1).GetOverview(i) if i == 0: if (ovr_band.XSize > main_band.XSize or ovr_band.YSize > main_band.YSize): errors += [ 'First overview has larger dimension than main band'] else: prev_ovr_band = ds.GetRasterBand(1).GetOverview(i - 1) if (ovr_band.XSize > prev_ovr_band.XSize or ovr_band.YSize > prev_ovr_band.YSize): errors += [ 'Overview of index %d has larger dimension than ' 'overview of index %d' % (i, i - 1)] if check_tiled: block_size = ovr_band.GetBlockSize() if block_size[0] == ovr_band.XSize and block_size[0] > 1024: errors += [ 'Overview of index %d is not tiled' % i] # Check that the IFD of descending overviews are sorted by increasing # offsets ifd_offset = int(ovr_band.GetMetadataItem('IFD_OFFSET', 'TIFF')) ifd_offsets.append(ifd_offset) details['ifd_offsets']['overview_%d' % i] = ifd_offset if ifd_offsets[-1] < ifd_offsets[-2]: if i == 0: errors += [ 'The offset of the IFD for overview of index %d is %d, ' 'whereas it should be greater than the one of the main ' 'image, which is at byte %d' % (i, ifd_offsets[-1], ifd_offsets[-2])] else: errors += [ 'The offset of the IFD for overview of index %d is %d, ' 'whereas it should be greater than the one of index %d, ' 'which is at byte %d' % (i, ifd_offsets[-1], i - 1, ifd_offsets[-2])] # Check that the imagery starts by the smallest overview and ends with # the main resolution dataset block_offset = main_band.GetMetadataItem('BLOCK_OFFSET_0_0', 'TIFF') if not block_offset: errors += ['Missing BLOCK_OFFSET_0_0'] data_offset = int(block_offset) if block_offset else None data_offsets = [data_offset] details['data_offsets'] = {} details['data_offsets']['main'] = data_offset for i in range(ovr_count): ovr_band = ds.GetRasterBand(1).GetOverview(i) data_offset = int(ovr_band.GetMetadataItem('BLOCK_OFFSET_0_0', 'TIFF')) data_offsets.append(data_offset) details['data_offsets']['overview_%d' % i] = data_offset if data_offsets[-1] < ifd_offsets[-1]: if ovr_count > 0: errors += [ 'The offset of the first block of the smallest overview ' 'should be after its IFD'] else: errors += [ 'The offset of the first block of the image should ' 'be after its IFD'] for i in range(len(data_offsets) - 2, 0, -1): if data_offsets[i] < data_offsets[i + 1]: errors += [ 'The offset of the first block of overview of index %d should ' 'be after the one of the overview of index %d' % (i - 1, i)] if len(data_offsets) >= 2 and data_offsets[0] < data_offsets[1]: errors += [ 'The offset of the first block of the main resolution image' 'should be after the one of the overview of index %d' % (ovr_count - 1)] return errors, details
def mem_2(): gdal.PushErrorHandler('CPLQuietErrorHandler') ds = gdal.Open('MEM:::') gdal.PopErrorHandler() if ds is not None: gdaltest.post_reason('opening MEM dataset should have failed.') return 'fail' try: import ctypes except: return 'skip' for libname in ['msvcrt', 'libc.so.6']: try: crt = ctypes.CDLL(libname) except: crt = None if crt is not None: break if crt is None: return 'skip' malloc = crt.malloc malloc.argtypes = [ctypes.c_size_t] malloc.restype = ctypes.c_void_p free = crt.free free.argtypes = [ctypes.c_void_p] free.restype = None # allocate band data array. width = 50 height = 3 p = malloc(width * height * 4) if p is None: return 'skip' float_p = ctypes.cast(p, ctypes.POINTER(ctypes.c_float)) # build ds name. dsnames = [ 'MEM:::DATAPOINTER=0x%X,PIXELS=%d,LINES=%d,BANDS=1,DATATYPE=Float32,PIXELOFFSET=4,LINEOFFSET=%d,BANDOFFSET=0' % (p, width, height, width * 4), 'MEM:::DATAPOINTER=0x%X,PIXELS=%d,LINES=%d,DATATYPE=Float32' % (p, width, height) ] for dsname in dsnames: for i in range(width * height): float_p[i] = 5.0 ds = gdal.Open(dsname) if ds is None: gdaltest.post_reason('opening MEM dataset failed.') free(p) return 'fail' chksum = ds.GetRasterBand(1).Checksum() if chksum != 750: gdaltest.post_reason('checksum failed.') print(chksum) free(p) return 'fail' ds.GetRasterBand(1).Fill(100.0) ds.FlushCache() if float_p[0] != 100.0: print(float_p[0]) gdaltest.post_reason('fill seems to have failed.') free(p) return 'fail' ds = None free(p) return 'success'
def ogr_csw_vsimem_csw_output_schema_csw(): if gdaltest.csw_drv is None: return 'skip' ds = gdal.OpenEx('CSW:/vsimem/csw_endpoint', open_options=['OUTPUT_SCHEMA=CSW']) lyr = ds.GetLayer(0) gdal.FileFromMemBuffer( """/vsimem/csw_endpoint&POSTFIELDS=<?xml version="1.0" encoding="UTF-8"?><csw:GetRecords resultType="results" service="CSW" version="2.0.2" outputSchema="http://www.opengis.net/cat/csw/2.0.2" startPosition="1" maxRecords="500" xmlns:csw="http://www.opengis.net/cat/csw/2.0.2" xmlns:gml="http://www.opengis.net/gml" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dct="http://purl.org/dc/terms/" xmlns:ogc="http://www.opengis.net/ogc" xmlns:ows="http://www.opengis.net/ows" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opengis.net/cat/csw/2.0.2 http://schemas.opengis.net/csw/2.0.2/CSW-discovery.xsd"><csw:Query typeNames="csw:Record"><csw:ElementSetName>full</csw:ElementSetName></csw:Query></csw:GetRecords>""", """<invalid_xml """) lyr.ResetReading() gdal.PushErrorHandler() f = lyr.GetNextFeature() gdal.PopErrorHandler() if f is not None: gdaltest.post_reason('fail') return 'fail' gdal.FileFromMemBuffer( """/vsimem/csw_endpoint&POSTFIELDS=<?xml version="1.0" encoding="UTF-8"?><csw:GetRecords resultType="results" service="CSW" version="2.0.2" outputSchema="http://www.opengis.net/cat/csw/2.0.2" startPosition="1" maxRecords="500" xmlns:csw="http://www.opengis.net/cat/csw/2.0.2" xmlns:gml="http://www.opengis.net/gml" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dct="http://purl.org/dc/terms/" xmlns:ogc="http://www.opengis.net/ogc" xmlns:ows="http://www.opengis.net/ows" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opengis.net/cat/csw/2.0.2 http://schemas.opengis.net/csw/2.0.2/CSW-discovery.xsd"><csw:Query typeNames="csw:Record"><csw:ElementSetName>full</csw:ElementSetName></csw:Query></csw:GetRecords>""", """<csw:GetRecordsResponse/>""") lyr.ResetReading() gdal.PushErrorHandler() f = lyr.GetNextFeature() gdal.PopErrorHandler() if f is not None: gdaltest.post_reason('fail') return 'fail' gdal.FileFromMemBuffer( """/vsimem/csw_endpoint&POSTFIELDS=<?xml version="1.0" encoding="UTF-8"?><csw:GetRecords resultType="results" service="CSW" version="2.0.2" outputSchema="http://www.opengis.net/cat/csw/2.0.2" startPosition="1" maxRecords="500" xmlns:csw="http://www.opengis.net/cat/csw/2.0.2" xmlns:gml="http://www.opengis.net/gml" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dct="http://purl.org/dc/terms/" xmlns:ogc="http://www.opengis.net/ogc" xmlns:ows="http://www.opengis.net/ows" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opengis.net/cat/csw/2.0.2 http://schemas.opengis.net/csw/2.0.2/CSW-discovery.xsd"><csw:Query typeNames="csw:Record"><csw:ElementSetName>full</csw:ElementSetName></csw:Query></csw:GetRecords>""", """<?xml version="1.0" encoding="UTF-8" standalone="no"?> <csw:GetRecordsResponse xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dct="http://purl.org/dc/terms/" xmlns:ows="http://www.opengis.net/ows" xmlns:csw="http://www.opengis.net/cat/csw/2.0.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0.2" xsi:schemaLocation="http://www.opengis.net/cat/csw/2.0.2 http://schemas.opengis.net/csw/2.0.2/CSW-discovery.xsd"> <csw:SearchStatus timestamp="2015-04-27T00:46:35Z"/> <csw:SearchResults nextRecord="0" numberOfRecordsMatched="1" numberOfRecordsReturned="1" recordSchema="http://www.isotc211.org/2005/gmd" elementSet="full"> <csw:Record> <!-- lots of missing stuff --> <ows:BoundingBox crs="urn:x-ogc:def:crs:EPSG:6.11:4326" dimensions="2"> <ows:LowerCorner>-90 -180</ows:LowerCorner> <ows:UpperCorner>90 180</ows:UpperCorner> </ows:BoundingBox> <!-- lots of missing stuff --> </csw:Record> </csw:SearchResults> </csw:GetRecordsResponse> """) lyr.ResetReading() f = lyr.GetNextFeature() if f['raw_xml'].find('<csw:Record') != 0 or \ f['boundingbox'].ExportToWkt() != 'POLYGON ((-180 -90,-180 90,180 90,180 -90,-180 -90))': gdaltest.post_reason('fail') f.DumpReadable() return 'fail' return 'success'
def resample(in_raster, out_raster=None, reference_raster=None, reference_band_number=1, target_size=None, output_format='MEM', method='nearest', quiet=True): ''' Resample an input raster to match a reference raster or a target size. The target size is in the same unit as the input raster. If the unit is wgs84 - beware that the target should be in degrees. Args: in_raster (URL or GDAL.DataFrame): The raster to clip. **kwargs: out_raster (URL): The name of the output raster. Only used when output format is not memory. reference_raster (URL or GDAL.DataFrame): A reference raster from where to clip the extent of the in_raster. reference_band_number (Integer): The number of the band in the reference to use as a target. target_size (List): The target pixel size of the destination. e.g. [10, 10] for 10m resolution if the map unit is meters. output_format (String): Output of calculation. MEM is default, if out_raster is specified but MEM is selected, GTiff is used as output_format. method (String): The method to use for resampling. By default nearest pixel is used. Supports bilinear, cubic and so on. For more details look at the GDAL reference. quiet (Bool): Do not show the progressbars for warping. Returns: If the output format is memory outpus a GDAL dataframe containing the resampled raster. Otherwise a raster is created and the return value is the URL string pointing to the created raster. ''' if target_size is None and reference_raster is None: raise ValueError('Either target_size or a reference must be provided.') if out_raster is not None and output_format == 'MEM': output_format = 'GTiff' if out_raster is None and output_format != 'MEM': raise AttributeError("Either a reference raster or a cutline must be provided.") if out_raster is None: out_raster = 'ignored' if isinstance(in_raster, gdal.Dataset): inputDataframe = in_raster else: inputDataframe = gdal.Open(in_raster) # Throw error if GDAL cannot open the raster if inputDataframe is None: raise AttributeError(f"Unable to parse the input raster: {in_raster}") driver = gdal.GetDriverByName(output_format) inputTransform = inputDataframe.GetGeoTransform() inputPixelWidth = inputTransform[1] inputPixelHeight = inputTransform[5] inputProjection = inputDataframe.GetProjection() inputBandCount = inputDataframe.RasterCount inputBand = inputDataframe.GetRasterBand(1) inputDatatype = inputBand.DataType inputNodataValue = inputBand.GetNoDataValue() if output_format == 'MEM': options = [] else: if datatype_is_float(inputDatatype) is True: predictor = 3 else: predictor = 2 options = ['COMPRESS=DEFLATE', f'PREDICTOR={predictor}', 'NUM_THREADS=ALL_CPUS'] # Test if the same size is requested. if target_size is not None: if abs(inputPixelWidth) == abs(target_size[0]) and abs(inputPixelHeight) == abs(target_size[1]): copy = copy_dataframe(inputDataframe, out_raster, output_format) copy.FlushCache() if output_format == 'MEM': return copy else: return os.path.abspath(out_raster) if reference_raster is not None: if isinstance(reference_raster, gdal.Dataset): referenceDataframe = reference_raster else: referenceDataframe = gdal.Open(reference_raster) # Throw error if GDAL cannot open the raster if referenceDataframe is None: raise AttributeError(f"Unable to parse the reference raster: {reference_raster}") referenceTransform = referenceDataframe.GetGeoTransform() referenceProjection = referenceDataframe.GetProjection() referenceXSize = referenceDataframe.RasterXSize referenceYSize = referenceDataframe.RasterYSize referencePixelWidth = referenceTransform[1] referencePixelHeight = referenceTransform[5] referenceBand = referenceDataframe.GetRasterBand(reference_band_number) referenceDatatype = referenceBand.DataType # Test if the reference size and the input size are the same if abs(inputPixelWidth) == abs(referencePixelWidth) and abs(inputPixelHeight) == abs(referencePixelHeight): copy = copy_dataframe(inputDataframe, out_raster, output_format) copy.FlushCache() if output_format == 'MEM': return copy else: return os.path.abspath(out_raster) else: destination = driver.Create(out_raster, referenceXSize, referenceYSize, inputBandCount, referenceDatatype, options) destination.SetProjection(referenceProjection) destination.SetGeoTransform(referenceTransform) gdal.PushErrorHandler('CPLQuietErrorHandler') progressbar = progress_callback_quiet if quiet is False: progressbar = create_progress_callback(1, 'resampling') try: warpSuccess = gdal.Warp( destination, in_raster, format=output_format, multithread=True, targetAlignedPixels=True, xRes=referenceTransform[1], yRes=referenceTransform[5], srcSRS=inputProjection, dstSRS=referenceProjection, srcNodata=inputNodataValue, dstNodata=inputNodataValue, warpOptions=options, resampleAlg=translate_resample_method(method), callback=progressbar, ) except: raise RuntimeError("Error while Warping.") from None gdal.PopErrorHandler() # Check if warped was successfull. if warpSuccess == 0: # GDAL returns 0 for warnings. print('Warping completed with warnings. Check your result.') elif warpSuccess is None: # GDAL returns None for errors. raise RuntimeError("Warping completed unsuccesfully.") from None if output_format == 'MEM': return destination else: return os.path.abspath(out_raster) else: inputXSize = inputDataframe.RasterXSize inputYSize = inputDataframe.RasterYSize xRatio = inputPixelWidth / target_size[0] yRatio = inputPixelHeight / target_size[1] xPixels = abs(round(xRatio * inputXSize)) yPixels = abs(round(yRatio * inputYSize)) destination = driver.Create(out_raster, xPixels, yPixels, inputBandCount, inputDatatype, options) destination.SetProjection(inputProjection) destination.SetGeoTransform([ inputTransform[0], target_size[0], inputTransform[2], inputTransform[3], inputTransform[4], -target_size[1], ]) if inputNodataValue is not None: destination.SetNoDataValue(inputNodataValue) gdal.PushErrorHandler('CPLQuietErrorHandler') progressbar = progress_callback_quiet if quiet is False: progressbar = create_progress_callback(1, 'resampling') try: warpSuccess = gdal.Warp( destination, in_raster, format=output_format, multithread=True, targetAlignedPixels=True, xRes=target_size[0], yRes=target_size[1], srcSRS=inputProjection, dstSRS=inputProjection, srcNodata=inputNodataValue, dstNodata=inputNodataValue, warpOptions=options, resampleAlg=translate_resample_method(method), callback=progressbar, ) except: raise RuntimeError("Error while Warping.") from None gdal.PopErrorHandler() # Check if warped was successfull. if warpSuccess == 0: # GDAL returns 0 for warnings. print('Warping completed with warnings. Check your result.') elif warpSuccess is None: # GDAL returns None for errors. raise RuntimeError("Warping completed unsuccesfully.") from None if output_format == 'MEM': return destination else: return os.path.abspath(out_raster)
def basic_test_11(): ds = gdal.OpenEx('data/byte.tif') if ds is None: gdaltest.post_reason('fail') return 'fail' ds = gdal.OpenEx('data/byte.tif', gdal.OF_RASTER) if ds is None: gdaltest.post_reason('fail') return 'fail' ds = gdal.OpenEx('data/byte.tif', gdal.OF_VECTOR) if ds is not None: gdaltest.post_reason('fail') return 'fail' ds = gdal.OpenEx('data/byte.tif', gdal.OF_RASTER | gdal.OF_VECTOR) if ds is None: gdaltest.post_reason('fail') return 'fail' ds = gdal.OpenEx('data/byte.tif', gdal.OF_ALL) if ds is None: gdaltest.post_reason('fail') return 'fail' ds = gdal.OpenEx('data/byte.tif', gdal.OF_UPDATE) if ds is None: gdaltest.post_reason('fail') return 'fail' ds = gdal.OpenEx( 'data/byte.tif', gdal.OF_RASTER | gdal.OF_VECTOR | gdal.OF_UPDATE | gdal.OF_VERBOSE_ERROR) if ds is None: gdaltest.post_reason('fail') return 'fail' ds = gdal.OpenEx('data/byte.tif', allowed_drivers=[]) if ds is None: gdaltest.post_reason('fail') return 'fail' ds = gdal.OpenEx('data/byte.tif', allowed_drivers=['GTiff']) if ds is None: gdaltest.post_reason('fail') return 'fail' ds = gdal.OpenEx('data/byte.tif', allowed_drivers=['PNG']) if ds is not None: gdaltest.post_reason('fail') return 'fail' ds = gdal.OpenEx('data/byte.tif', open_options=['FOO']) if ds is None: gdaltest.post_reason('fail') return 'fail' ar_ds = [gdal.OpenEx('data/byte.tif', gdal.OF_SHARED) for i in range(1024)] if ar_ds[1023] is None: gdaltest.post_reason('fail') return 'fail' ar_ds = None ds = gdal.OpenEx('../ogr/data/poly.shp', gdal.OF_RASTER) if ds is not None: gdaltest.post_reason('fail') return 'fail' ds = gdal.OpenEx('../ogr/data/poly.shp', gdal.OF_VECTOR) if ds is None: gdaltest.post_reason('fail') return 'fail' if ds.GetLayerCount() != 1: gdaltest.post_reason('fail') return 'fail' if ds.GetLayer(0) is None: gdaltest.post_reason('fail') return 'fail' ds.GetLayer(0).GetMetadata() ds = gdal.OpenEx('../ogr/data/poly.shp', allowed_drivers=['ESRI Shapefile']) if ds is None: gdaltest.post_reason('fail') return 'fail' ds = gdal.OpenEx('../ogr/data/poly.shp', gdal.OF_RASTER | gdal.OF_VECTOR) if ds is None: gdaltest.post_reason('fail') return 'fail' ds = gdal.OpenEx('non existing') if ds is not None or gdal.GetLastErrorMsg() != '': gdaltest.post_reason('fail') return 'fail' gdal.PushErrorHandler('CPLQuietErrorHandler') ds = gdal.OpenEx('non existing', gdal.OF_VERBOSE_ERROR) gdal.PopErrorHandler() if ds is not None or gdal.GetLastErrorMsg() == '': gdaltest.post_reason('fail') return 'fail' return 'success'
def wrapper(*args, **kwargs): osgeo_gdal.PushErrorHandler('CPLQuietErrorHandler') v = f(*args, **kwargs) osgeo_gdal.PopErrorHandler() return v
def test_ogr_feature_nullable_validate(): # No fields feat_def = ogr.FeatureDefn('test') f = ogr.Feature(feat_def) assert f.Validate() == 1 # Field with default parameter and empty feature feat_def = ogr.FeatureDefn('test') field_def = ogr.FieldDefn('field_string', ogr.OFTString) assert field_def.IsNullable() == 1 assert feat_def.GetGeomFieldDefn(0).IsNullable() == 1 feat_def.AddFieldDefn(field_def) f = ogr.Feature(feat_def) assert f.Validate() == 1 # Field with not NULL constraint and empty feature feat_def = ogr.FeatureDefn('test') field_def = ogr.FieldDefn('field_string', ogr.OFTString) field_def.SetNullable(0) assert field_def.IsNullable() == 0 feat_def.AddFieldDefn(field_def) f = ogr.Feature(feat_def) gdal.PushErrorHandler() ret = f.Validate() gdal.PopErrorHandler() assert ret == 0 # Field with not NULL constraint and non-empty feature feat_def = ogr.FeatureDefn('test') field_def = ogr.FieldDefn('field_string', ogr.OFTString) field_def.SetNullable(0) feat_def.AddFieldDefn(field_def) f = ogr.Feature(feat_def) f.SetField(0, 'foo') assert f.Validate() == 1 # Field with width constraint that is met feat_def = ogr.FeatureDefn('test') field_def = ogr.FieldDefn('field_string', ogr.OFTString) field_def.SetWidth(3) feat_def.AddFieldDefn(field_def) f = ogr.Feature(feat_def) f.SetField(0, 'foo') assert f.Validate() == 1 # Field with width constraint that is not met feat_def = ogr.FeatureDefn('test') field_def = ogr.FieldDefn('field_string', ogr.OFTString) field_def.SetWidth(2) feat_def.AddFieldDefn(field_def) f = ogr.Feature(feat_def) f.SetField(0, 'foo') gdal.PushErrorHandler() ret = f.Validate() gdal.PopErrorHandler() assert ret == 0 # Geometry field with not-null geometry constraint that is met feat_def = ogr.FeatureDefn('test') feat_def.SetGeomType(ogr.wkbNone) gfield_def = ogr.GeomFieldDefn('test', ogr.wkbUnknown) gfield_def.SetNullable(0) assert gfield_def.IsNullable() == 0 feat_def.AddGeomFieldDefn(gfield_def) f = ogr.Feature(feat_def) f.SetGeometry(ogr.CreateGeometryFromWkt('POINT(0 1)')) assert f.Validate() == 1 # Geometry field with not-null geometry constraint that is not met feat_def = ogr.FeatureDefn('test') feat_def.SetGeomType(ogr.wkbNone) gfield_def = ogr.GeomFieldDefn('test', ogr.wkbPoint) gfield_def.SetNullable(0) feat_def.AddGeomFieldDefn(gfield_def) f = ogr.Feature(feat_def) gdal.PushErrorHandler() ret = f.Validate() gdal.PopErrorHandler() assert ret == 0 # Geometry field with Point geometry type that is met feat_def = ogr.FeatureDefn('test') feat_def.SetGeomType(ogr.wkbPoint) f = ogr.Feature(feat_def) f.SetGeometry(ogr.CreateGeometryFromWkt('POINT(0 1)')) assert f.Validate() == 1 # Geometry field with LineString geometry type that is not met feat_def = ogr.FeatureDefn('test') feat_def.SetGeomType(ogr.wkbLineString) f = ogr.Feature(feat_def) f.SetGeometry(ogr.CreateGeometryFromWkt('POINT(0 1)')) gdal.PushErrorHandler() ret = f.Validate() gdal.PopErrorHandler() assert ret == 0
def test_ogr_feature_default(): feat_def = ogr.FeatureDefn('test') field_def = ogr.FieldDefn('field_string', ogr.OFTString) assert field_def.GetDefault() is None assert not field_def.IsDefaultDriverSpecific() field_def.SetDefault("(some_expr)") assert field_def.GetDefault() == "(some_expr)" assert field_def.IsDefaultDriverSpecific() field_def.SetDefault("'a") assert field_def.GetDefault() == "'a" gdal.PushErrorHandler() field_def.SetDefault("'a''") gdal.PopErrorHandler() assert field_def.GetDefault() is None gdal.PushErrorHandler() field_def.SetDefault("'a'b'") gdal.PopErrorHandler() assert field_def.GetDefault() is None field_def.SetDefault("'a''b'''") assert field_def.GetDefault() == "'a''b'''" assert not field_def.IsDefaultDriverSpecific() feat_def.AddFieldDefn(field_def) field_def = ogr.FieldDefn('field_datetime', ogr.OFTDateTime) field_def.SetDefault("CURRENT_TIMESTAMP") feat_def.AddFieldDefn(field_def) field_def = ogr.FieldDefn('field_datetime2', ogr.OFTDateTime) field_def.SetDefault("'2015/06/30 12:34:56'") feat_def.AddFieldDefn(field_def) field_def = ogr.FieldDefn('field_int', ogr.OFTInteger) field_def.SetDefault('123') feat_def.AddFieldDefn(field_def) field_def = ogr.FieldDefn('field_nodefault', ogr.OFTInteger) feat_def.AddFieldDefn(field_def) f = ogr.Feature(feat_def) f.FillUnsetWithDefault() if f.GetField('field_string') != 'a\'b\'' or \ not f.IsFieldSet('field_datetime') or \ f.GetField('field_datetime2') != '2015/06/30 12:34:56+00' or \ f.GetField('field_int') != 123 or \ f.IsFieldSet('field_nodefault'): f.DumpReadable() pytest.fail() f = ogr.Feature(feat_def) f.SetField('field_string', 'b') f.FillUnsetWithDefault() if f.GetField('field_string') != 'b': f.DumpReadable() pytest.fail()
def test_ogr_carto_vsimem(): if ogrtest.carto_drv is None: pytest.skip() ogrtest.carto_api_key_ori = gdal.GetConfigOption('CARTO_API_KEY') gdal.SetConfigOption('CARTO_API_URL', '/vsimem/carto') gdal.SetConfigOption('CPL_CURL_ENABLE_VSIMEM', 'YES') gdal.FileFromMemBuffer( '/vsimem/carto&POSTFIELDS=q=SELECT postgis_version() LIMIT 500 OFFSET 0&api_key=foo', """{"rows":[{"postgis_version":"2.1 USE_GEOS=1 USE_PROJ=1 USE_STATS=1"}],"time":0.001,"fields":{"postgis_version":{"type":"string"}},"total_rows":1}""" ) gdal.PushErrorHandler() ds = ogr.Open('CARTO:foo') gdal.PopErrorHandler() assert ds is None gdal.FileFromMemBuffer( '/vsimem/carto&POSTFIELDS=q=SELECT current_schema() LIMIT 500 OFFSET 0', """Content-Type: text/html\r Error""") gdal.PushErrorHandler() ds = ogr.Open('CARTO:foo') gdal.PopErrorHandler() assert ds is None and gdal.GetLastErrorMsg().find('HTML error page') >= 0 gdal.FileFromMemBuffer( '/vsimem/carto&POSTFIELDS=q=SELECT current_schema() LIMIT 500 OFFSET 0', """""") ds = ogr.Open('CARTO:foo') assert ds is None, gdal.GetLastErrorMsg() gdal.FileFromMemBuffer( '/vsimem/carto&POSTFIELDS=q=SELECT current_schema() LIMIT 500 OFFSET 0', """{""") gdal.PushErrorHandler() ds = ogr.Open('CARTO:foo') gdal.PopErrorHandler() assert ds is None and gdal.GetLastErrorMsg().find( 'JSON parsing error') >= 0 gdal.FileFromMemBuffer( '/vsimem/carto&POSTFIELDS=q=SELECT current_schema() LIMIT 500 OFFSET 0', """ "not_expected_json" """) ds = ogr.Open('CARTO:foo') assert ds is None, gdal.GetLastErrorMsg() gdal.FileFromMemBuffer( '/vsimem/carto&POSTFIELDS=q=SELECT current_schema() LIMIT 500 OFFSET 0', """{ "error" : [ "bla"] }""") gdal.PushErrorHandler() ds = ogr.Open('CARTO:foo') gdal.PopErrorHandler() assert ds is None and gdal.GetLastErrorMsg().find( 'Error returned by server : bla') >= 0 gdal.FileFromMemBuffer( '/vsimem/carto&POSTFIELDS=q=SELECT current_schema() LIMIT 500 OFFSET 0', """{ "fields" : null } """) ds = ogr.Open('CARTO:foo') assert ds is None, gdal.GetLastErrorMsg() gdal.FileFromMemBuffer( '/vsimem/carto&POSTFIELDS=q=SELECT current_schema() LIMIT 500 OFFSET 0', """{ "fields" : "invalid" } """) ds = ogr.Open('CARTO:foo') assert ds is None, gdal.GetLastErrorMsg() gdal.FileFromMemBuffer( '/vsimem/carto&POSTFIELDS=q=SELECT current_schema() LIMIT 500 OFFSET 0', """{ "fields" : {} } """) ds = ogr.Open('CARTO:foo') assert ds is None, gdal.GetLastErrorMsg() gdal.FileFromMemBuffer( '/vsimem/carto&POSTFIELDS=q=SELECT current_schema() LIMIT 500 OFFSET 0', """{ "fields" : { "foo": "invalid" } } """) ds = ogr.Open('CARTO:foo') assert ds is None, gdal.GetLastErrorMsg() gdal.FileFromMemBuffer( '/vsimem/carto&POSTFIELDS=q=SELECT current_schema() LIMIT 500 OFFSET 0', """{ "fields" : { "foo": {} } } """) ds = ogr.Open('CARTO:foo') assert ds is None, gdal.GetLastErrorMsg() gdal.FileFromMemBuffer( '/vsimem/carto&POSTFIELDS=q=SELECT current_schema() LIMIT 500 OFFSET 0', """{ "fields" : { "foo": { "type" : null } } } """) ds = ogr.Open('CARTO:foo') assert ds is None, gdal.GetLastErrorMsg() gdal.FileFromMemBuffer( '/vsimem/carto&POSTFIELDS=q=SELECT current_schema() LIMIT 500 OFFSET 0', """{ "fields" : { "foo": { "type" : {} } } } """) ds = ogr.Open('CARTO:foo') assert ds is None, gdal.GetLastErrorMsg() gdal.FileFromMemBuffer( '/vsimem/carto&POSTFIELDS=q=SELECT current_schema() LIMIT 500 OFFSET 0', """{ "fields" : { "foo": { "type" : "string" } } } """) ds = ogr.Open('CARTO:foo') assert ds is None, gdal.GetLastErrorMsg() gdal.FileFromMemBuffer( '/vsimem/carto&POSTFIELDS=q=SELECT current_schema() LIMIT 500 OFFSET 0', """{"rows":[ {"field1": "foo", "field2": "bar"} ],"fields":{"field1":{"type":"string"}, "field2":{"type":"string"}}}""" ) ds = ogr.Open('CARTO:foo') assert ds is None, gdal.GetLastErrorMsg() gdal.FileFromMemBuffer( '/vsimem/carto&POSTFIELDS=q=SELECT current_schema() LIMIT 500 OFFSET 0', """{"rows":[],"fields":{"current_schema":{"type":"string"}}}""") gdal.PushErrorHandler() ds = ogr.Open('CARTO:foo') gdal.PopErrorHandler() assert ds is None, gdal.GetLastErrorMsg() gdal.FileFromMemBuffer( '/vsimem/carto&POSTFIELDS=q=SELECT current_schema() LIMIT 500 OFFSET 0', """{"rows":[{"current_schema":"public"}],"fields":{"current_schema":{"type":"unknown(19)"}}}""" ) gdal.PushErrorHandler() ds = ogr.Open('CARTO:foo') gdal.PopErrorHandler() assert ds is None, gdal.GetLastErrorMsg() gdal.FileFromMemBuffer( '/vsimem/carto&POSTFIELDS=q=SELECT CDB_UserTables() LIMIT 500 OFFSET 0', """{"rows":[{"cdb_usertables":"table1"}],"fields":{"cdb_usertables":{"type":"string"}}}""" ) ds = ogr.Open('CARTO:foo') assert ds is not None and ds.GetLayerCount() == 1, gdal.GetLastErrorMsg() gdal.PushErrorHandler() lyr_defn = ds.GetLayer(0).GetLayerDefn() gdal.PopErrorHandler() assert lyr_defn.GetFieldCount() == 0 # Empty layer gdal.FileFromMemBuffer( '/vsimem/carto&POSTFIELDS=q=SELECT * FROM "table1" LIMIT 0', """{"rows":[],"fields":{}}""") ds = ogr.Open('CARTO:foo') lyr = ds.GetLayer(0) lyr_defn = lyr.GetLayerDefn() assert lyr_defn.GetFieldCount() == 0 gdal.FileFromMemBuffer( '/vsimem/carto&POSTFIELDS=q=SELECT * FROM "table1" LIMIT 500 OFFSET 0', """{"rows":[{}],"fields":{}}}""") f = lyr.GetNextFeature() if f.GetFID() != 0: f.DumpReadable() pytest.fail() # Layer without geometry or primary key gdal.FileFromMemBuffer( '/vsimem/carto&POSTFIELDS=q=SELECT * FROM "table1" LIMIT 0', """{"rows":[],"fields":{"strfield":{"type":"string"}, "realfield":{"type":"number"}, "boolfield":{"type":"boolean"}, "datefield":{"type":"date"}}}""" ) ds = ogr.Open('CARTO:foo') lyr = ds.GetLayer(0) lyr_defn = lyr.GetLayerDefn() assert lyr_defn.GetFieldCount() == 4 assert (lyr_defn.GetFieldDefn(0).GetName() == 'strfield' and \ lyr_defn.GetFieldDefn(0).GetType() == ogr.OFTString) assert (lyr_defn.GetFieldDefn(1).GetName() == 'realfield' and \ lyr_defn.GetFieldDefn(1).GetType() == ogr.OFTReal) assert (lyr_defn.GetFieldDefn(2).GetName() == 'boolfield' and \ lyr_defn.GetFieldDefn(2).GetType() == ogr.OFTInteger and \ lyr_defn.GetFieldDefn(2).GetSubType() == ogr.OFSTBoolean) assert (lyr_defn.GetFieldDefn(3).GetName() == 'datefield' and \ lyr_defn.GetFieldDefn(3).GetType() == ogr.OFTDateTime) gdal.FileFromMemBuffer( '/vsimem/carto&POSTFIELDS=q=SELECT "strfield", "realfield", "boolfield", "datefield" FROM "table1" LIMIT 500 OFFSET 0', """{"rows":[{ "strfield": "foo", "realfield": 1.23, "boolfield": true, "datefield": "2015-04-24T12:34:56.123Z" }],"fields":{"strfield":{"type":"string"}, "realfield":{"type":"number"}, "boolfield":{"type":"boolean"}, "datefield":{"type":"date"}}}""" ) f = lyr.GetNextFeature() if f['strfield'] != 'foo' or f['realfield'] != 1.23 or f['boolfield'] != 1 or \ f['datefield'] != '2015/04/24 12:34:56.123+00': f.DumpReadable() pytest.fail() gdal.SetConfigOption('CARTO_API_KEY', 'foo') gdal.FileFromMemBuffer( '/vsimem/carto&POSTFIELDS=q=SELECT current_schema() LIMIT 500 OFFSET 0&api_key=foo', """{"rows":[{"current_schema":"public"}],"fields":{"current_schema":{"type":"unknown(19)"}}}""" ) gdal.FileFromMemBuffer( '/vsimem/carto&POSTFIELDS=q=SELECT CDB_UserTables() LIMIT 500 OFFSET 0&api_key=foo', """{"rows":[{"cdb_usertables":"table1"}],"fields":{"cdb_usertables":{"type":"string"}}}""" ) ds = ogr.Open('CARTO:foo') gdal.PushErrorHandler() lyr_defn = ds.GetLayer(0).GetLayerDefn() gdal.PopErrorHandler() assert lyr_defn.GetFieldCount() == 0 get_full_details_fields_url = """/vsimem/carto&POSTFIELDS=q=SELECT a.attname, t.typname, a.attlen, format_type(a.atttypid,a.atttypmod), a.attnum, a.attnotnull, i.indisprimary, pg_get_expr(def.adbin, c.oid) AS defaultexpr, postgis_typmod_dims(a.atttypmod) dim, postgis_typmod_srid(a.atttypmod) srid, postgis_typmod_type(a.atttypmod)::text geomtyp, srtext FROM pg_class c JOIN pg_attribute a ON a.attnum > 0 AND a.attrelid = c.oid AND c.relname = 'table1' JOIN pg_type t ON a.atttypid = t.oid JOIN pg_namespace n ON c.relnamespace=n.oid AND n.nspname= 'public' LEFT JOIN pg_index i ON c.oid = i.indrelid AND i.indisprimary = 't' AND a.attnum = ANY(i.indkey) LEFT JOIN pg_attrdef def ON def.adrelid = c.oid AND def.adnum = a.attnum LEFT JOIN spatial_ref_sys srs ON srs.srid = postgis_typmod_srid(a.atttypmod) ORDER BY a.attnum LIMIT 500 OFFSET 0&api_key=foo""" gdal.FileFromMemBuffer(get_full_details_fields_url, '') ds = ogr.Open('CARTO:foo') gdal.PushErrorHandler() lyr_defn = ds.GetLayer(0).GetLayerDefn() gdal.PopErrorHandler() assert lyr_defn.GetFieldCount() == 0 gdal.FileFromMemBuffer( get_full_details_fields_url, """{"rows":[{"attname":"foo"}], "fields":{"attname":{"type":"string"}}}""" ) ds = ogr.Open('CARTO:foo') lyr = ds.GetLayer(0) gdal.PushErrorHandler() lyr_defn = lyr.GetLayerDefn() gdal.PopErrorHandler() assert lyr_defn.GetFieldCount() == 1 gdal.PushErrorHandler() f = lyr.GetFeature(0) gdal.PopErrorHandler() assert f is None gdal.FileFromMemBuffer( get_full_details_fields_url, """{"rows":[{"attname":"strfield", "typname":"varchar", "attnotnull": true, "defaultexpr": "def_value"}, {"attname":"intfield", "typname":"int4"}, {"attname":"doublefield", "typname":"float"}, {"attname":"boolfield", "typname":"bool"}, {"attname":"datetimefield", "typname":"timestamp"}, {"attname":"cartodb_id","typname":"int4","indisprimary":true}, {"attname":"created_at","typname":"date"}, {"attname":"updated_at","typname":"date"}, {"attname":"my_geom","typname":"geometry","dim":3,"srid":4326,"geomtyp":"Point", "srtext":"GEOGCS[\\"WGS 84\\",DATUM[\\"WGS_1984\\",SPHEROID[\\"WGS 84\\",6378137,298.257223563,AUTHORITY[\\"EPSG\\",\\"7030\\"]],AUTHORITY[\\"EPSG\\",\\"6326\\"]],PRIMEM[\\"Greenwich\\",0,AUTHORITY[\\"EPSG\\",\\"8901\\"]],UNIT[\\"degree\\",0.0174532925199433,AUTHORITY[\\"EPSG\\",\\"9122\\"]],AUTHORITY[\\"EPSG\\",\\"4326\\"]]"}, {"attname":"the_geom_webmercator","typname":"geometry"}], "fields":{"attname":{"type":"string"}, "typname":{"type":"string"}, "attlen":{"type":"number"}, "format_type":{"type":"string"}, "attnum":{"type":"number"}, "attnotnull":{"type":"boolean"}, "indisprimary":{"type":"boolean"}, "defaultexpr":{"type":"string"}, "dim":{"type":"number"}, "srid":{"type":"number"}, "geomtyp":{"type":"string"}, "srtext":{"type":"string"}}}""") ds = ogr.Open('CARTO:foo') lyr = ds.GetLayer(0) lyr_defn = lyr.GetLayerDefn() assert lyr_defn.GetFieldCount() == 5 assert (lyr_defn.GetFieldDefn(0).GetName() == 'strfield' and \ lyr_defn.GetFieldDefn(0).GetType() == ogr.OFTString and not \ lyr_defn.GetFieldDefn(0).IsNullable() and \ lyr_defn.GetFieldDefn(0).GetDefault() == 'def_value') assert lyr_defn.GetGeomFieldCount() == 1 assert lyr_defn.GetGeomFieldDefn(0).GetName() == 'my_geom' assert lyr_defn.GetGeomFieldDefn(0).GetType() == ogr.wkbPoint25D assert lyr_defn.GetGeomFieldDefn(0).GetSpatialRef().ExportToWkt().find( '4326') >= 0 gdal.PushErrorHandler() fc = lyr.GetFeatureCount() gdal.PopErrorHandler() assert fc == 0 gdal.FileFromMemBuffer( """/vsimem/carto&POSTFIELDS=q=SELECT COUNT(*) FROM "table1"&api_key=foo""", """{}""") gdal.PushErrorHandler() fc = lyr.GetFeatureCount() gdal.PopErrorHandler() assert fc == 0 gdal.FileFromMemBuffer( """/vsimem/carto&POSTFIELDS=q=SELECT COUNT(*) FROM "table1"&api_key=foo""", """{"rows":[{"foo":1}], "fields":{"foo":{"type":"number"}}}""") gdal.PushErrorHandler() fc = lyr.GetFeatureCount() gdal.PopErrorHandler() assert fc == 0 gdal.FileFromMemBuffer( """/vsimem/carto&POSTFIELDS=q=SELECT COUNT(*) FROM "table1"&api_key=foo""", """{"rows":[{"count":9876543210}], "fields":{"count":{"type":"number"}}}""") assert lyr.GetFeatureCount() == 9876543210 gdal.PushErrorHandler() extent = lyr.GetExtent() gdal.PopErrorHandler() assert extent == (0, 0, 0, 0) gdal.FileFromMemBuffer( """/vsimem/carto&POSTFIELDS=q=SELECT ST_Extent("my_geom") FROM "table1"&api_key=foo""", """{"rows":[{"foo":1}], "fields":{"foo":{"type":"number"}}}""") gdal.PushErrorHandler() extent = lyr.GetExtent() gdal.PopErrorHandler() assert extent == (0, 0, 0, 0) gdal.FileFromMemBuffer( """/vsimem/carto&POSTFIELDS=q=SELECT ST_Extent("my_geom") FROM "table1"&api_key=foo""", """{"rows":[{"st_extent":""}], "fields":{"st_extent":{"type":"string"}}}""") gdal.ErrorReset() gdal.PushErrorHandler() lyr.GetExtent() gdal.PopErrorHandler() assert gdal.GetLastErrorMsg() != '' gdal.FileFromMemBuffer( """/vsimem/carto&POSTFIELDS=q=SELECT ST_Extent("my_geom") FROM "table1"&api_key=foo""", """{"rows":[{"st_extent":"("}], "fields":{"st_extent":{"type":"string"}}}""") gdal.ErrorReset() gdal.PushErrorHandler() lyr.GetExtent() gdal.PopErrorHandler() assert gdal.GetLastErrorMsg() != '' gdal.FileFromMemBuffer( """/vsimem/carto&POSTFIELDS=q=SELECT ST_Extent("my_geom") FROM "table1"&api_key=foo""", """{"rows":[{"st_extent":"BOX()"}], "fields":{"st_extent":{"type":"string"}}}""") gdal.ErrorReset() gdal.PushErrorHandler() lyr.GetExtent() gdal.PopErrorHandler() assert gdal.GetLastErrorMsg() != '' gdal.FileFromMemBuffer( """/vsimem/carto&POSTFIELDS=q=SELECT ST_Extent("my_geom") FROM "table1"&api_key=foo""", """{"rows":[{"st_extent":"BOX(0,1,2,3)"}], "fields":{"st_extent":{"type":"string"}}}""") assert lyr.GetExtent() == (0.0, 2.0, 1.0, 3.0) gdal.PushErrorHandler() f = lyr.GetFeature(0) gdal.PopErrorHandler() assert f is None gdal.FileFromMemBuffer( """/vsimem/carto&POSTFIELDS=q=SELECT "cartodb_id", "my_geom", "strfield", "intfield", "doublefield", "boolfield", "datetimefield" FROM "table1" WHERE "cartodb_id" = 0&api_key=foo""", """""") gdal.PushErrorHandler() f = lyr.GetFeature(0) gdal.PopErrorHandler() assert f is None gdal.FileFromMemBuffer( """/vsimem/carto&POSTFIELDS=q=SELECT "cartodb_id", "my_geom", "strfield", "intfield", "doublefield", "boolfield", "datetimefield" FROM "table1" WHERE "cartodb_id" = 0&api_key=foo""", """{"rows":[{"st_extent":"BOX(0,1,2,3)"}], "fields":{"st_extent":{"type":"string"}}}""") f = lyr.GetFeature(0) assert f.GetFID() == -1 and f.IsFieldNull(0) gdal.FileFromMemBuffer( """/vsimem/carto&POSTFIELDS=q=SELECT "cartodb_id", "my_geom", "strfield", "intfield", "doublefield", "boolfield", "datetimefield" FROM "table1" WHERE "cartodb_id" = 0&api_key=foo""", """{"rows":[{"cartodb_id":0}], "fields":{"cartodb_id":{"type":"numeric"}}}""") f = lyr.GetFeature(0) assert f.GetFID() == 0 lyr.ResetReading() gdal.PushErrorHandler() f = lyr.GetNextFeature() gdal.PopErrorHandler() assert f is None gdal.FileFromMemBuffer( """/vsimem/carto&POSTFIELDS=q=SELECT "cartodb_id", "my_geom", "strfield", "intfield", "doublefield", "boolfield", "datetimefield" FROM "table1" WHERE "cartodb_id" >= 0 ORDER BY "cartodb_id" ASC LIMIT 500&api_key=foo""", """{"rows":[{"cartodb_id":0}], "fields":{"cartodb_id":{"type":"numeric"}}}""") lyr.ResetReading() f = lyr.GetNextFeature() assert f.GetFID() == 0 gdal.PushErrorHandler() f = lyr.GetNextFeature() gdal.PopErrorHandler() assert f is None gdal.SetConfigOption('CARTO_PAGE_SIZE', '2') gdal.FileFromMemBuffer( """/vsimem/carto&POSTFIELDS=q=SELECT "cartodb_id", "my_geom", "strfield", "intfield", "doublefield", "boolfield", "datetimefield" FROM "table1" WHERE "cartodb_id" >= 0 ORDER BY "cartodb_id" ASC LIMIT 2&api_key=foo""", """{"rows":[{"cartodb_id":0},{"cartodb_id":10}], "fields":{"cartodb_id":{"type":"numeric"}}}""") lyr.ResetReading() f = lyr.GetNextFeature() assert f.GetFID() == 0 f = lyr.GetNextFeature() assert f.GetFID() == 10 gdal.FileFromMemBuffer( """/vsimem/carto&POSTFIELDS=q=SELECT "cartodb_id", "my_geom", "strfield", "intfield", "doublefield", "boolfield", "datetimefield" FROM "table1" WHERE "cartodb_id" >= 11 ORDER BY "cartodb_id" ASC LIMIT 2&api_key=foo""", """{"rows":[{"cartodb_id":12}], "fields":{"cartodb_id":{"type":"numeric"}}}""") f = lyr.GetNextFeature() assert f.GetFID() == 12 gdal.ErrorReset() f = lyr.GetNextFeature() assert f is None and gdal.GetLastErrorMsg() == '' lyr.SetAttributeFilter('strfield is NULL') gdal.PushErrorHandler() fc = lyr.GetFeatureCount() gdal.PopErrorHandler() assert fc == 0 gdal.FileFromMemBuffer( """/vsimem/carto&POSTFIELDS=q=SELECT "cartodb_id", "my_geom", "strfield", "intfield", "doublefield", "boolfield", "datetimefield" FROM "table1" WHERE (strfield is NULL) AND "cartodb_id" >= 0 ORDER BY "cartodb_id" ASC LIMIT 2&api_key=foo""", """{"rows":[{"cartodb_id":0}], "fields":{"cartodb_id":{"type":"numeric"}}}""") lyr.ResetReading() f = lyr.GetNextFeature() assert f is not None gdal.FileFromMemBuffer( """/vsimem/carto&POSTFIELDS=q=SELECT "cartodb_id", "my_geom", "strfield", "intfield", "doublefield", "boolfield", "datetimefield" FROM "table1" WHERE (strfield is NULL) AND "cartodb_id" >= 1 ORDER BY "cartodb_id" ASC LIMIT 2&api_key=foo""", """{"rows":[], "fields":{"cartodb_id":{"type":"numeric"}}}""") gdal.ErrorReset() f = lyr.GetNextFeature() assert f is None and gdal.GetLastErrorMsg() == '' gdal.FileFromMemBuffer( """/vsimem/carto&POSTFIELDS=q=SELECT COUNT(*) FROM "table1" WHERE (strfield is NULL)&api_key=foo""", """{"rows":[{"count":9876543210}], "fields":{"count":{"type":"number"}}}""") assert lyr.GetFeatureCount() == 9876543210 lyr.SetSpatialFilterRect(-180, -90, 180, 90) gdal.PushErrorHandler() fc = lyr.GetFeatureCount() gdal.PopErrorHandler() assert fc == 0 gdal.FileFromMemBuffer( """/vsimem/carto&POSTFIELDS=q=SELECT "cartodb_id", "my_geom", "strfield", "intfield", "doublefield", "boolfield", "datetimefield" FROM "table1" WHERE ("my_geom" %26%26 'BOX3D(-180 -90, 180 90)'::box3d) AND (strfield is NULL) AND "cartodb_id" >= 0 ORDER BY "cartodb_id" ASC LIMIT 2&api_key=foo""", """{"rows":[{"cartodb_id":20, "my_geom": "010100000000000000000000400000000000804840" }], "fields":{"cartodb_id":{"type":"numeric"}, "my_geom":{"type":"string"}}}""" ) lyr.ResetReading() f = lyr.GetNextFeature() assert f is not None and f.GetGeometryRef().ExportToWkt() == 'POINT (2 49)' gdal.PushErrorHandler() fc = lyr.GetFeatureCount() gdal.PopErrorHandler() assert fc == 1 gdal.FileFromMemBuffer( """/vsimem/carto&POSTFIELDS=q=SELECT COUNT(*) FROM "table1" WHERE ("my_geom" %26%26 'BOX3D(-180 -90, 180 90)'::box3d) AND (strfield is NULL)&api_key=foo""", """{"rows":[{"count":9876543210}], "fields":{"count":{"type":"number"}}}""") assert lyr.GetFeatureCount() == 9876543210 # Not permitted in read-only mode f = ogr.Feature(lyr.GetLayerDefn()) gdal.PushErrorHandler() ds.CreateLayer('foo') ds.DeleteLayer(0) lyr.CreateFeature(f) lyr.SetFeature(f) lyr.DeleteFeature(0) lyr.CreateField(ogr.FieldDefn('foo')) lyr.DeleteField(0) gdal.PopErrorHandler() ds = None gdal.FileFromMemBuffer( """/vsimem/carto&POSTFIELDS=q=DROP FUNCTION IF EXISTS ogr_table_metadata(TEXT,TEXT); CREATE OR REPLACE FUNCTION ogr_table_metadata(schema_name TEXT, table_name TEXT) RETURNS TABLE (attname TEXT, typname TEXT, attlen INT, format_type TEXT, attnum INT, attnotnull BOOLEAN, indisprimary BOOLEAN, defaultexpr TEXT, dim INT, srid INT, geomtyp TEXT, srtext TEXT) AS $$ SELECT a.attname::text, t.typname::text, a.attlen::int, format_type(a.atttypid,a.atttypmod)::text, a.attnum::int, a.attnotnull::boolean, i.indisprimary::boolean, pg_get_expr(def.adbin, c.oid)::text AS defaultexpr, (CASE WHEN t.typname = 'geometry' THEN postgis_typmod_dims(a.atttypmod) ELSE NULL END)::int dim, (CASE WHEN t.typname = 'geometry' THEN postgis_typmod_srid(a.atttypmod) ELSE NULL END)::int srid, (CASE WHEN t.typname = 'geometry' THEN postgis_typmod_type(a.atttypmod) ELSE NULL END)::text geomtyp, srtext FROM pg_class c JOIN pg_attribute a ON a.attnum > 0 AND a.attrelid = c.oid AND c.relname = $2 AND c.relname IN (SELECT CDB_UserTables())JOIN pg_type t ON a.atttypid = t.oid JOIN pg_namespace n ON c.relnamespace=n.oid AND n.nspname = $1 LEFT JOIN pg_index i ON c.oid = i.indrelid AND i.indisprimary = 't' AND a.attnum = ANY(i.indkey) LEFT JOIN pg_attrdef def ON def.adrelid = c.oid AND def.adnum = a.attnum LEFT JOIN spatial_ref_sys srs ON srs.srid = postgis_typmod_srid(a.atttypmod) ORDER BY a.attnum $$ LANGUAGE SQL&api_key=foo""", """""" "") gdal.SetConfigOption('CARTO_PAGE_SIZE', None) ds = ogr.Open('CARTO:foo', update=1) lyr = ds.CreateLayer('MY_LAYER') gdal.ErrorReset() gdal.PushErrorHandler() lyr.GetNextFeature() gdal.PopErrorHandler() assert gdal.GetLastErrorMsg() != '' gdal.FileFromMemBuffer( """/vsimem/carto&POSTFIELDS=q=SELECT cdb_cartodbfytable('my_layer')&api_key=foo""", """{"rows":[], "fields":{}}""") ds = None gdal.Unlink( """/vsimem/carto&POSTFIELDS=q=SELECT cdb_cartodbfytable('my_layer')&api_key=foo""" ) ds = gdal.OpenEx('CARTO:foo', gdal.OF_VECTOR | gdal.OF_UPDATE, open_options=['COPY_MODE=NO']) gdal.SetConfigOption('CARTO_MAX_CHUNK_SIZE', '0') sr = osr.SpatialReference() sr.ImportFromEPSG(4326) lyr = ds.CreateLayer('MY_LAYER', srs=sr) fld_defn = ogr.FieldDefn('STRFIELD', ogr.OFTString) fld_defn.SetNullable(0) fld_defn.SetDefault("'DEFAULT VAL'") fld_defn.SetWidth(20) lyr.CreateField(fld_defn) gdal.FileFromMemBuffer( """/vsimem/carto&POSTFIELDS=q=CREATE TABLE "my_layer" ( cartodb_id SERIAL,the_geom GEOMETRY(GEOMETRY, 4326),"strfield" VARCHAR NOT NULL DEFAULT 'DEFAULT VAL',PRIMARY KEY (cartodb_id) );DROP SEQUENCE IF EXISTS "my_layer_cartodb_id_seq" CASCADE;CREATE SEQUENCE "my_layer_cartodb_id_seq" START 1;ALTER SEQUENCE "my_layer_cartodb_id_seq" OWNED BY "my_layer".cartodb_id;ALTER TABLE "my_layer" ALTER COLUMN cartodb_id SET DEFAULT nextval('"my_layer_cartodb_id_seq"')&api_key=foo""", """{"rows":[], "fields":{}}""") f = ogr.Feature(lyr.GetLayerDefn()) gdal.PushErrorHandler() ret = lyr.CreateFeature(f) gdal.PopErrorHandler() assert ret != 0 f = None fld_defn = ogr.FieldDefn('INTFIELD', ogr.OFTInteger) # No server answer with gdaltest.error_handler(): ret = lyr.CreateField(fld_defn) assert ret != 0 gdal.FileFromMemBuffer( """/vsimem/carto&POSTFIELDS=q=ALTER TABLE "my_layer" ADD COLUMN "intfield" INTEGER&api_key=foo""", """{"rows":[], "fields":{}}""") assert lyr.CreateField(fld_defn) == 0 fld_defn = ogr.FieldDefn('boolfield', ogr.OFTInteger) fld_defn.SetSubType(ogr.OFSTBoolean) gdal.FileFromMemBuffer( """/vsimem/carto&POSTFIELDS=q=ALTER TABLE "my_layer" ADD COLUMN "boolfield" BOOLEAN&api_key=foo""", """{"rows":[], "fields":{}}""") assert lyr.CreateField(fld_defn) == 0 # Invalid field with gdaltest.error_handler(): assert lyr.DeleteField(-1) != 0 # No server answer with gdaltest.error_handler(): assert lyr.DeleteField(0) != 0 gdal.FileFromMemBuffer( """/vsimem/carto&POSTFIELDS=q=ALTER TABLE "my_layer" DROP COLUMN "boolfield"&api_key=foo""", """{"rows":[], "fields":{}}""") fld_pos = lyr.GetLayerDefn().GetFieldIndex(fld_defn.GetName()) assert lyr.DeleteField(fld_pos) == 0 gdal.FileFromMemBuffer( """/vsimem/carto&POSTFIELDS=q=ALTER TABLE "my_layer" ADD COLUMN "boolfield" BOOLEAN&api_key=foo""", """{"rows":[], "fields":{}}""") assert lyr.CreateField(fld_defn) == 0 f = ogr.Feature(lyr.GetLayerDefn()) gdal.PushErrorHandler() ret = lyr.CreateFeature(f) gdal.PopErrorHandler() assert ret != 0 f = ogr.Feature(lyr.GetLayerDefn()) f.SetField('strfield', 'foo') f.SetField('intfield', 1) f.SetField('boolfield', 1) f.SetGeometry(ogr.CreateGeometryFromWkt('POINT(2 49)')) gdal.FileFromMemBuffer( """/vsimem/carto&POSTFIELDS=q=INSERT INTO "my_layer" ("strfield", "intfield", "boolfield", "the_geom") VALUES ('foo', 1, 't', '0101000020E610000000000000000000400000000000804840') RETURNING "cartodb_id"&api_key=foo""", """{"rows":[ {"cartodb_id": 1} ], "fields":{"cartodb_id":{"type":"integer"}}}""") ret = lyr.CreateFeature(f) assert ret == 0 and f.GetFID() == 1 f.SetFID(-1) gdal.PushErrorHandler() ret = lyr.SetFeature(f) gdal.PopErrorHandler() assert ret != 0 f.SetFID(3) gdal.PushErrorHandler() ret = lyr.SetFeature(f) gdal.PopErrorHandler() assert ret != 0 gdal.FileFromMemBuffer( """/vsimem/carto&POSTFIELDS=q=UPDATE "my_layer" SET "strfield" = 'foo', "intfield" = 1, "boolfield" = 't', "the_geom" = '0101000020E610000000000000000000400000000000804840' WHERE "cartodb_id" = 3&api_key=foo""", """{"total_rows": 0}""") ret = lyr.SetFeature(f) assert ret == ogr.OGRERR_NON_EXISTING_FEATURE gdal.FileFromMemBuffer( """/vsimem/carto&POSTFIELDS=q=UPDATE "my_layer" SET "strfield" = 'foo', "intfield" = 1, "boolfield" = 't', "the_geom" = '0101000020E610000000000000000000400000000000804840' WHERE "cartodb_id" = 3&api_key=foo""", """{"total_rows": 1}""") ret = lyr.SetFeature(f) assert ret == 0 f = ogr.Feature(lyr.GetLayerDefn()) gdal.PushErrorHandler() ret = lyr.CreateFeature(f) gdal.PopErrorHandler() assert ret != 0 gdal.FileFromMemBuffer( """/vsimem/carto&POSTFIELDS=q=INSERT INTO "my_layer" DEFAULT VALUES RETURNING "cartodb_id"&api_key=foo""", """{"rows":[ {"cartodb_id": 4} ], "fields":{"cartodb_id":{"type":"integer"}}}""") ret = lyr.CreateFeature(f) assert ret == 0 and f.GetFID() == 4 gdal.PushErrorHandler() ret = lyr.DeleteFeature(0) gdal.PopErrorHandler() assert ret != 0 gdal.FileFromMemBuffer( """/vsimem/carto&POSTFIELDS=q=DELETE FROM "my_layer" WHERE "cartodb_id" = 0&api_key=foo""", """{"total_rows": 0}""") ret = lyr.DeleteFeature(0) assert ret == ogr.OGRERR_NON_EXISTING_FEATURE gdal.FileFromMemBuffer( """/vsimem/carto&POSTFIELDS=q=DELETE FROM "my_layer" WHERE "cartodb_id" = 0&api_key=foo""", """{"total_rows": 1}""") ret = lyr.DeleteFeature(0) assert ret == 0 gdal.FileFromMemBuffer( """/vsimem/carto&POSTFIELDS=q=SELECT cdb_cartodbfytable('my_layer')&api_key=foo""", """{"rows":[], "fields":{}}""") ds = None gdal.SetConfigOption('CARTO_MAX_CHUNK_SIZE', None) ds = gdal.OpenEx('CARTO:foo', gdal.OF_VECTOR | gdal.OF_UPDATE, open_options=['COPY_MODE=NO']) lyr = ds.GetLayer(0) gdal.FileFromMemBuffer( """/vsimem/carto&POSTFIELDS=q=SELECT pg_catalog.pg_get_serial_sequence('table1', 'cartodb_id') AS seq_name&api_key=foo""", """{"rows":[{"seq_name":"table1_cartodb_id_seq0"}],"fields":{"seq_name":{"type":"string"}}}""" ) gdal.FileFromMemBuffer( """/vsimem/carto&POSTFIELDS=q=SELECT nextval('table1_cartodb_id_seq0') AS nextid&api_key=foo""", """{"rows":[{"nextid":11}],"fields":{"nextid":{"type":"number"}}}""") f = ogr.Feature(lyr.GetLayerDefn()) f.SetField('strfield', 'foo') ret = lyr.CreateFeature(f) assert ret == 0 and f.GetFID() == 11 f = ogr.Feature(lyr.GetLayerDefn()) with gdaltest.tempfile( """/vsimem/carto&POSTFIELDS=q=BEGIN;INSERT INTO "table1" ("strfield", "cartodb_id") VALUES ('foo', 11);INSERT INTO "table1" DEFAULT VALUES;COMMIT;&api_key=foo""", """{"rows":[], "fields":{}}"""): ret = lyr.CreateFeature(f) ds = None if ret != 0 or f.GetFID() != 12: f.DumpReadable() pytest.fail() ds = gdal.OpenEx('CARTO:foo', gdal.OF_VECTOR | gdal.OF_UPDATE, open_options=['COPY_MODE=NO']) lyr = ds.GetLayer(0) f = ogr.Feature(lyr.GetLayerDefn()) f.SetFieldNull('strfield') with gdaltest.tempfile( """/vsimem/carto&POSTFIELDS=q=BEGIN;INSERT INTO "table1" ("strfield", "cartodb_id") VALUES (NULL, 11);COMMIT;&api_key=foo""", """{"rows":[], "fields":{}}"""): ret = lyr.CreateFeature(f) ds = None if ret != 0 or f.GetFID() != 11: f.DumpReadable() pytest.fail() # Now remove default value to strfield gdal.FileFromMemBuffer( get_full_details_fields_url, """{"rows":[{"attname":"strfield", "typname":"varchar"}, {"attname":"intfield", "typname":"int4"}, {"attname":"doublefield", "typname":"float"}, {"attname":"boolfield", "typname":"bool"}, {"attname":"datetimefield", "typname":"timestamp"}, {"attname":"cartodb_id","typname":"int4","indisprimary":true}, {"attname":"created_at","typname":"date"}, {"attname":"updated_at","typname":"date"}, {"attname":"my_geom","typname":"geometry","dim":3,"srid":4326,"geomtyp":"Point", "srtext":"GEOGCS[\\"WGS 84\\",DATUM[\\"WGS_1984\\",SPHEROID[\\"WGS 84\\",6378137,298.257223563,AUTHORITY[\\"EPSG\\",\\"7030\\"]],AUTHORITY[\\"EPSG\\",\\"6326\\"]],PRIMEM[\\"Greenwich\\",0,AUTHORITY[\\"EPSG\\",\\"8901\\"]],UNIT[\\"degree\\",0.0174532925199433,AUTHORITY[\\"EPSG\\",\\"9122\\"]],AUTHORITY[\\"EPSG\\",\\"4326\\"]]"}, {"attname":"the_geom_webmercator","typname":"geometry"}], "fields":{"attname":{"type":"string"}, "typname":{"type":"string"}, "attlen":{"type":"number"}, "format_type":{"type":"string"}, "attnum":{"type":"number"}, "attnotnull":{"type":"boolean"}, "indisprimary":{"type":"boolean"}, "defaultexpr":{"type":"string"}, "dim":{"type":"number"}, "srid":{"type":"number"}, "geomtyp":{"type":"string"}, "srtext":{"type":"string"}}}""") ds = gdal.OpenEx('CARTO:foo', gdal.OF_VECTOR | gdal.OF_UPDATE, open_options=['COPY_MODE=NO']) lyr = ds.GetLayer(0) f = ogr.Feature(lyr.GetLayerDefn()) f.SetField('strfield', 'foo') with gdaltest.tempfile( """/vsimem/carto&POSTFIELDS=q=SELECT nextval('table1_cartodb_id_seq') AS nextid&api_key=foo""", """{"rows":[{"nextid":11}],"fields":{"nextid":{"type":"number"}}}""" ): ret = lyr.CreateFeature(f) assert ret == 0 and f.GetFID() == 11 f = ogr.Feature(lyr.GetLayerDefn()) f.SetField('strfield', 'bar') ret = lyr.CreateFeature(f) assert ret == 0 and f.GetFID() == 12 f = ogr.Feature(lyr.GetLayerDefn()) f.SetField('strfield', 'baz') ret = lyr.CreateFeature(f) assert ret == 0 and f.GetFID() == 13 gdal.ErrorReset() with gdaltest.tempfile( """/vsimem/carto&POSTFIELDS=q=BEGIN;INSERT INTO "table1" ("strfield", "cartodb_id") VALUES ('foo', 11);INSERT INTO "table1" ("strfield", "intfield", "doublefield", "boolfield", "datetimefield", "my_geom") VALUES ('bar', NULL, NULL, NULL, NULL, NULL), ('baz', NULL, NULL, NULL, NULL, NULL);COMMIT;&api_key=foo""", """{"rows":[], "fields":{}}"""): ds = None assert gdal.GetLastErrorMsg() == '' ds = gdal.OpenEx('CARTO:foo', gdal.OF_VECTOR | gdal.OF_UPDATE, open_options=['COPY_MODE=NO']) gdal.PushErrorHandler() lyr = ds.CreateLayer('table1') gdal.PopErrorHandler() assert lyr is None with gdaltest.tempfile( """/vsimem/carto&POSTFIELDS=q=DROP TABLE "table1"&api_key=foo""", """{"rows":[], "fields":{}}"""): lyr = ds.CreateLayer('table1', geom_type=ogr.wkbPolygon, options=['OVERWRITE=YES', 'CARTODBFY=NO']) f = ogr.Feature(lyr.GetLayerDefn()) f.SetGeometry(ogr.CreateGeometryFromWkt('POLYGON((0 0,0 1,1 0,0 0))')) with gdaltest.tempfile( """/vsimem/carto&POSTFIELDS=q=CREATE TABLE "table1" ( cartodb_id SERIAL,the_geom Geometry(MULTIPOLYGON,0),PRIMARY KEY (cartodb_id) );DROP SEQUENCE IF EXISTS "table1_cartodb_id_seq" CASCADE;CREATE SEQUENCE "table1_cartodb_id_seq" START 1;ALTER SEQUENCE "table1_cartodb_id_seq" OWNED BY "table1".cartodb_id;ALTER TABLE "table1" ALTER COLUMN cartodb_id SET DEFAULT nextval('"table1_cartodb_id_seq"')&api_key=foo""", """{"rows":[], "fields":{}}"""): assert lyr.CreateFeature(f) == 0 gdal.ErrorReset() with gdaltest.tempfile( """/vsimem/carto&POSTFIELDS=q=BEGIN;INSERT INTO "table1" ("the_geom") VALUES ('0106000020E61000000100000001030000000100000004000000000000000000000000000000000000000000000000000000000000000000F03F000000000000F03F000000000000000000000000000000000000000000000000');COMMIT;&api_key=foo""", """{"rows":[], "fields":{}}"""): ds = None assert gdal.GetLastErrorMsg() == '' ds = gdal.OpenEx('CARTO:foo', gdal.OF_VECTOR | gdal.OF_UPDATE, open_options=['COPY_MODE=NO']) with gdaltest.tempfile( """/vsimem/carto&POSTFIELDS=q=DROP TABLE "table1"&api_key=foo""", """{"rows":[], "fields":{}}"""): lyr = ds.CreateLayer('table1', geom_type=ogr.wkbPolygon, options=['OVERWRITE=YES', 'CARTODBFY=NO']) f = ogr.Feature(lyr.GetLayerDefn()) f.SetFID(100) with gdaltest.tempfile( """/vsimem/carto&POSTFIELDS=q=CREATE TABLE "table1" ( cartodb_id SERIAL,the_geom Geometry(MULTIPOLYGON,0),PRIMARY KEY (cartodb_id) );DROP SEQUENCE IF EXISTS "table1_cartodb_id_seq" CASCADE;CREATE SEQUENCE "table1_cartodb_id_seq" START 1;ALTER SEQUENCE "table1_cartodb_id_seq" OWNED BY "table1".cartodb_id;ALTER TABLE "table1" ALTER COLUMN cartodb_id SET DEFAULT nextval('"table1_cartodb_id_seq"')&api_key=foo""", """{"rows":[], "fields":{}}"""): with gdaltest.tempfile( """/vsimem/carto&POSTFIELDS=q=BEGIN;INSERT INTO "table1" ("cartodb_id") VALUES (100);COMMIT;&api_key=foo""", """{"rows":[], "fields":{}}"""): assert lyr.CreateFeature(f) == 0 assert f.GetFID() == 100 ds = None ds = ogr.Open('CARTO:foo', update=1) gdal.PushErrorHandler() ret = ds.DeleteLayer(0) gdal.PopErrorHandler() assert ret != 0 gdal.ErrorReset() ds = gdal.OpenEx('CARTO:foo', gdal.OF_VECTOR | gdal.OF_UPDATE, open_options=['COPY_MODE=YES']) assert ds is not None lyr = ds.GetLayerByName('table1') assert lyr is not None with gdaltest.tempfile( """/vsimem/carto/copyfrom?q=COPY%20"table1"%20("strfield","my_geom","cartodb_id")%20FROM%20STDIN%20WITH%20(FORMAT%20text,%20ENCODING%20UTF8)&api_key=foo&POSTFIELDS=copytest\t0101000020E610000000000000000059400000000000005940\t11\n\\.\n""", """{}"""): with gdaltest.tempfile( """/vsimem/carto/copyfrom?q=COPY%20"table1"%20("intfield","my_geom")%20FROM%20STDIN%20WITH%20(FORMAT%20text,%20ENCODING%20UTF8)&api_key=foo&POSTFIELDS=12\t0101000020E610000000000000000059400000000000005940\n\\.\n""", """{}"""): f = ogr.Feature(lyr.GetLayerDefn()) f.SetField('strfield', 'copytest') f.SetGeometry(ogr.CreateGeometryFromWkt('POINT(100 100)')) assert lyr.CreateFeature(f) == 0 f = ogr.Feature(lyr.GetLayerDefn()) f.SetField('intfield', 12) f.SetGeometry(ogr.CreateGeometryFromWkt('POINT(100 100)')) assert lyr.CreateFeature(f) == 0 ds = None # force flush ds = ogr.Open('CARTO:foo', update=1) gdal.ErrorReset() with gdaltest.tempfile( """/vsimem/carto&POSTFIELDS=q=DROP TABLE "table1"&api_key=foo""", """{"rows":[], "fields":{}}"""): ds.ExecuteSQL('DELLAYER:table1') assert gdal.GetLastErrorMsg() == '' and ds.GetLayerByName('table1') is None with gdaltest.tempfile( '/vsimem/carto&POSTFIELDS=q=SELECT current_schema() LIMIT 500 OFFSET 0&api_key=foo', """{"rows":[{"current_schema":"my_schema"}],"fields":{"current_schema":{"type":"unknown(19)"}}}""" ): with gdaltest.tempfile( '/vsimem/carto&POSTFIELDS=q=SELECT CDB_UserTables() LIMIT 500 OFFSET 0&api_key=foo', """{"rows":[],"fields":{"cdb_usertables":{"type":"string"}}}""" ): with gdaltest.tempfile( """/vsimem/carto&POSTFIELDS=q=SELECT c.relname FROM pg_class c, pg_namespace n WHERE c.relkind in ('r', 'v') AND c.relname !~ '^pg_' AND c.relnamespace=n.oid AND n.nspname = 'my_schema' LIMIT 500 OFFSET 0&api_key=foo""", """{"rows":[{"relname": "a_layer"}],"fields":{"relname":{"type":"string"}}}""" ): ds = ogr.Open('CARTO:foo') assert ds.GetLayerByName('a_layer') is not None
def rasterio_9(): ds = gdal.Open('data/byte.tif') # Test RasterBand.ReadRaster, with Bilinear tab = [0, None] data = ds.GetRasterBand(1).ReadRaster( buf_type=gdal.GDT_Int16, buf_xsize=10, buf_ysize=10, resample_alg=gdal.GRIORA_Bilinear, callback=rasterio_9_progress_callback, callback_data=tab) if data is None: gdaltest.post_reason('failure') return 'fail' data_ar = struct.unpack('h' * 10 * 10, data) cs = rasterio_9_checksum(data, 10, 10, data_type=gdal.GDT_Int16) if cs != 1211: # checksum of gdal_translate data/byte.tif out.tif -outsize 10 10 -r BILINEAR gdaltest.post_reason('failure') print(cs) return 'fail' if abs(tab[0] - 1.0) > 1e-5: gdaltest.post_reason('failure') return 'fail' # Same but query with GDT_Float32. Check that we do not get floating-point # values, since the band type is Byte data = ds.GetRasterBand(1).ReadRaster(buf_type=gdal.GDT_Float32, buf_xsize=10, buf_ysize=10, resample_alg=gdal.GRIORA_Bilinear) data_float32_ar = struct.unpack('f' * 10 * 10, data) if data_ar != data_float32_ar: gdaltest.post_reason('failure') print(data_float32_ar) return 'fail' # Test RasterBand.ReadRaster, with Lanczos tab = [0, None] data = ds.GetRasterBand(1).ReadRaster( buf_xsize=10, buf_ysize=10, resample_alg=gdal.GRIORA_Lanczos, callback=rasterio_9_progress_callback, callback_data=tab) if data is None: gdaltest.post_reason('failure') return 'fail' cs = rasterio_9_checksum(data, 10, 10) if cs != 1154: # checksum of gdal_translate data/byte.tif out.tif -outsize 10 10 -r LANCZOS gdaltest.post_reason('failure') print(cs) return 'fail' if abs(tab[0] - 1.0) > 1e-5: gdaltest.post_reason('failure') return 'fail' # Test RasterBand.ReadRaster, with Bilinear and UInt16 data type src_ds_uint16 = gdal.Open('data/uint16.tif') tab = [0, None] data = src_ds_uint16.GetRasterBand(1).ReadRaster( buf_type=gdal.GDT_UInt16, buf_xsize=10, buf_ysize=10, resample_alg=gdal.GRIORA_Bilinear, callback=rasterio_9_progress_callback, callback_data=tab) if data is None: gdaltest.post_reason('failure') return 'fail' cs = rasterio_9_checksum(data, 10, 10, data_type=gdal.GDT_UInt16) if cs != 1211: # checksum of gdal_translate data/byte.tif out.tif -outsize 10 10 -r BILINEAR gdaltest.post_reason('failure') print(cs) return 'fail' if abs(tab[0] - 1.0) > 1e-5: gdaltest.post_reason('failure') return 'fail' # Test RasterBand.ReadRaster, with Bilinear on Complex, thus using warp API tab = [0, None] complex_ds = gdal.GetDriverByName('MEM').Create('', 20, 20, 1, gdal.GDT_CInt16) complex_ds.GetRasterBand(1).WriteRaster(0, 0, 20, 20, ds.GetRasterBand(1).ReadRaster(), buf_type=gdal.GDT_Byte) data = complex_ds.GetRasterBand(1).ReadRaster( buf_xsize=10, buf_ysize=10, resample_alg=gdal.GRIORA_Bilinear, callback=rasterio_9_progress_callback, callback_data=tab) if data is None: gdaltest.post_reason('failure') return 'fail' cs = rasterio_9_checksum(data, 10, 10, data_type=gdal.GDT_CInt16) if cs != 1211: # checksum of gdal_translate data/byte.tif out.tif -outsize 10 10 -r BILINEAR gdaltest.post_reason('failure') print(cs) return 'fail' if abs(tab[0] - 1.0) > 1e-5: gdaltest.post_reason('failure') return 'fail' # Test interruption tab = [0, 0.5] gdal.PushErrorHandler('CPLQuietErrorHandler') data = ds.GetRasterBand(1).ReadRaster( buf_xsize=10, buf_ysize=10, resample_alg=gdal.GRIORA_Bilinear, callback=rasterio_9_progress_callback, callback_data=tab) gdal.PopErrorHandler() if data is not None: gdaltest.post_reason('failure') return 'fail' if tab[0] < 0.50: gdaltest.post_reason('failure') return 'fail' # Test RasterBand.ReadRaster, with Gauss, and downsampling tab = [0, None] data = ds.GetRasterBand(1).ReadRaster( buf_xsize=10, buf_ysize=10, resample_alg=gdal.GRIORA_Gauss, callback=rasterio_9_progress_callback, callback_data=tab) if data is None: gdaltest.post_reason('failure') return 'fail' cs = rasterio_9_checksum(data, 10, 10) if cs != 1089: # checksum of gdal_translate data/byte.tif out.tif -outsize 10 10 -r GAUSS gdaltest.post_reason('failure') print(cs) return 'fail' if abs(tab[0] - 1.0) > 1e-5: gdaltest.post_reason('failure') return 'fail' # Test RasterBand.ReadRaster, with Cubic, and downsampling tab = [0, None] data = ds.GetRasterBand(1).ReadRaster( buf_xsize=10, buf_ysize=10, resample_alg=gdal.GRIORA_Cubic, callback=rasterio_9_progress_callback, callback_data=tab) if data is None: gdaltest.post_reason('failure') return 'fail' cs = rasterio_9_checksum(data, 10, 10) if cs != 1059: # checksum of gdal_translate data/byte.tif out.tif -outsize 10 10 -r CUBIC gdaltest.post_reason('failure') print(cs) return 'fail' if abs(tab[0] - 1.0) > 1e-5: gdaltest.post_reason('failure') return 'fail' # Test RasterBand.ReadRaster, with Cubic, and downsampling with >=8x8 source samples used for a dest sample data = ds.GetRasterBand(1).ReadRaster(buf_xsize=5, buf_ysize=5, resample_alg=gdal.GRIORA_Cubic) if data is None: gdaltest.post_reason('failure') return 'fail' cs = rasterio_9_checksum(data, 5, 5) if cs != 214: # checksum of gdal_translate data/byte.tif out.tif -outsize 5 5 -r CUBIC gdaltest.post_reason('failure') print(cs) return 'fail' # Same with UInt16 data = src_ds_uint16.GetRasterBand(1).ReadRaster( buf_xsize=5, buf_ysize=5, resample_alg=gdal.GRIORA_Cubic) if data is None: gdaltest.post_reason('failure') return 'fail' cs = rasterio_9_checksum(data, 5, 5, data_type=gdal.GDT_UInt16) if cs != 214: # checksum of gdal_translate data/byte.tif out.tif -outsize 5 5 -r CUBIC gdaltest.post_reason('failure') print(cs) return 'fail' # Test RasterBand.ReadRaster, with Cubic and supersampling tab = [0, None] data = ds.GetRasterBand(1).ReadRaster( buf_xsize=40, buf_ysize=40, resample_alg=gdal.GRIORA_Cubic, callback=rasterio_9_progress_callback, callback_data=tab) if data is None: gdaltest.post_reason('failure') return 'fail' cs = rasterio_9_checksum(data, 40, 40) if cs != 19556: # checksum of gdal_translate data/byte.tif out.tif -outsize 40 40 -r CUBIC gdaltest.post_reason('failure') print(cs) return 'fail' if abs(tab[0] - 1.0) > 1e-5: gdaltest.post_reason('failure') return 'fail' # Test Dataset.ReadRaster, with Cubic and supersampling tab = [0, None] data = ds.ReadRaster(buf_xsize=40, buf_ysize=40, resample_alg=gdal.GRIORA_CubicSpline, callback=rasterio_9_progress_callback, callback_data=tab) if data is None: gdaltest.post_reason('failure') return 'fail' cs = rasterio_9_checksum(data, 40, 40) if cs != 19041: # checksum of gdal_translate data/byte.tif out.tif -outsize 40 40 -r CUBICSPLINE gdaltest.post_reason('failure') print(cs) return 'fail' if abs(tab[0] - 1.0) > 1e-5: gdaltest.post_reason('failure') return 'fail' # Test Dataset.ReadRaster on a multi band file, with INTERLEAVE=PIXEL ds = gdal.Open('data/rgbsmall_cmyk.tif') tab = [0, None] data = ds.ReadRaster(buf_xsize=25, buf_ysize=25, resample_alg=gdal.GRIORA_Cubic, callback=rasterio_9_progress_callback, callback_data=tab) if data is None: gdaltest.post_reason('failure') return 'fail' cs = rasterio_9_checksum(data[0:25 * 25], 25, 25) if cs != 5975: # checksum of gdal_translate data/rgbsmall_cmyk.tif out.tif -outsize 25 25 -r CUBIC gdaltest.post_reason('failure') print(cs) return 'fail' cs = rasterio_9_checksum(data[25 * 25:2 * 25 * 25], 25, 25) if cs != 6248: # checksum of gdal_translate data/rgbsmall_cmyk.tif out.tif -outsize 25 25 -r CUBIC gdaltest.post_reason('failure') print(cs) return 'fail' if abs(tab[0] - 1.0) > 1e-5: gdaltest.post_reason('failure') return 'fail' ds = None # Test Band.ReadRaster on a RGBA with parts fully opaque, and fully transparent and with huge upscaling ds = gdal.Open('data/stefan_full_rgba.png') tab = [0, None] data = ds.GetRasterBand(1).ReadRaster( buf_xsize=162 * 16, buf_ysize=150 * 16, resample_alg=gdal.GRIORA_Cubic, callback=rasterio_9_progress_callback, callback_data=tab) if data is None: gdaltest.post_reason('failure') return 'fail' cs = rasterio_9_checksum(data, 162 * 16, 150 * 16) if cs != 30836: # checksum of gdal_translate data/stefan_full_rgba.png out.tif -outsize 1600% 1600% -r CUBIC gdaltest.post_reason('failure') print(cs) return 'fail' if abs(tab[0] - 1.0) > 1e-5: gdaltest.post_reason('failure') return 'fail' return 'success'
def rasterio_5(): ds = gdal.Open('data/byte.tif') for obj in [ds, ds.GetRasterBand(1)]: obj.ReadRaster(0, 0, -2000000000, 1, 1, 1) obj.ReadRaster(0, 0, 1, -2000000000, 1, 1) for band_number in [-1, 0, 2]: gdal.ErrorReset() gdal.PushErrorHandler('CPLQuietErrorHandler') res = ds.ReadRaster(0, 0, 1, 1, band_list=[band_number]) gdal.PopErrorHandler() error_msg = gdal.GetLastErrorMsg() if res is not None: gdaltest.post_reason('expected None') return 'fail' if error_msg.find('this band does not exist on dataset') == -1: gdaltest.post_reason('did not get expected error msg') print(error_msg) return 'fail' res = ds.ReadRaster(0, 0, 1, 1, band_list=[1, 1]) if res is None: gdaltest.post_reason('expected non None') return 'fail' for obj in [ds, ds.GetRasterBand(1)]: gdal.ErrorReset() gdal.PushErrorHandler('CPLQuietErrorHandler') res = obj.ReadRaster(0, 0, 21, 21) gdal.PopErrorHandler() error_msg = gdal.GetLastErrorMsg() if res is not None: gdaltest.post_reason('expected None') return 'fail' if error_msg.find('Access window out of range in RasterIO()') == -1: gdaltest.post_reason('did not get expected error msg (1)') print(error_msg) return 'fail' # This should only fail on a 32bit build try: maxsize = sys.maxint except AttributeError: maxsize = sys.maxsize # On win64, maxsize == 2147483647 and ReadRaster() # fails because of out of memory condition, not # because of integer overflow. I'm not sure on how # to detect win64 better. if maxsize == 2147483647 and sys.platform != 'win32': gdal.ErrorReset() gdal.PushErrorHandler('CPLQuietErrorHandler') res = obj.ReadRaster(0, 0, 1, 1, 1000000, 1000000) gdal.PopErrorHandler() error_msg = gdal.GetLastErrorMsg() if res is not None: gdaltest.post_reason('expected None') return 'fail' if error_msg.find('Integer overflow') == -1: gdaltest.post_reason('did not get expected error msg (2)') print(error_msg) return 'fail' gdal.ErrorReset() gdal.PushErrorHandler('CPLQuietErrorHandler') res = obj.ReadRaster(0, 0, 0, 1) gdal.PopErrorHandler() error_msg = gdal.GetLastErrorMsg() if res is not None: gdaltest.post_reason('expected None') return 'fail' if error_msg.find('Illegal values for buffer size') == -1: gdaltest.post_reason('did not get expected error msg (3)') print(error_msg) return 'fail' ds = None return 'success'
def ogr_pgdump_8(): ds = ogr.GetDriverByName('PGDump').CreateDataSource('/vsimem/ogr_pgdump_8.sql', options = [ 'LINEFORMAT=LF' ] ) lyr = ds.CreateLayer('test', geom_type = ogr.wkbNone, options = ['FID=myfid']) lyr.CreateField(ogr.FieldDefn('str', ogr.OFTString)) gdal.PushErrorHandler() ret = lyr.CreateField(ogr.FieldDefn('myfid', ogr.OFTString)) gdal.PopErrorHandler() if ret == 0: gdaltest.post_reason('fail') return 'fail' ret = lyr.CreateField(ogr.FieldDefn('myfid', ogr.OFTInteger)) if ret != 0: gdaltest.post_reason('fail') return 'fail' lyr.CreateField(ogr.FieldDefn('str2', ogr.OFTString)) feat = ogr.Feature(lyr.GetLayerDefn()) feat.SetField('str', 'first string') feat.SetField('myfid', 10) feat.SetField('str2', 'second string') gdal.SetConfigOption( 'PG_USE_COPY', 'YES' ) ret = lyr.CreateFeature(feat) gdal.SetConfigOption( 'PG_USE_COPY', None ) if ret != 0: gdaltest.post_reason('fail') return 'fail' if feat.GetFID() != 10: gdaltest.post_reason('fail') return 'fail' feat = ogr.Feature(lyr.GetLayerDefn()) feat.SetField('str2', 'second string') gdal.SetConfigOption( 'PG_USE_COPY', 'YES' ) ret = lyr.CreateFeature(feat) gdal.SetConfigOption( 'PG_USE_COPY', None ) if ret != 0: gdaltest.post_reason('fail') return 'fail' if feat.GetFID() < 0: gdaltest.post_reason('fail') feat.DumpReadable() return 'fail' if feat.GetField('myfid') != feat.GetFID(): gdaltest.post_reason('fail') feat.DumpReadable() return 'fail' #feat.SetField('str', 'foo') #ret = lyr.SetFeature(feat) #if ret != 0: # gdaltest.post_reason('fail') # return 'fail' feat = ogr.Feature(lyr.GetLayerDefn()) feat.SetFID(1) feat.SetField('myfid', 10) gdal.PushErrorHandler() gdal.SetConfigOption( 'PG_USE_COPY', 'YES' ) ret = lyr.CreateFeature(feat) gdal.SetConfigOption( 'PG_USE_COPY', None ) gdal.PopErrorHandler() if ret == 0: gdaltest.post_reason('fail') return 'fail' #gdal.PushErrorHandler() #ret = lyr.SetFeature(feat) #gdal.PopErrorHandler() #if ret == 0: # gdaltest.post_reason('fail') # return 'fail' #feat.UnsetField('myfid') #gdal.PushErrorHandler() #ret = lyr.SetFeature(feat) #gdal.PopErrorHandler() #if ret == 0: # gdaltest.post_reason('fail') # return 'fail' feat = ogr.Feature(lyr.GetLayerDefn()) feat.SetField('str', 'first string') feat.SetField('myfid', 12) feat.SetField('str2', 'second string') gdal.SetConfigOption( 'PG_USE_COPY', 'YES' ) ret = lyr.CreateFeature(feat) gdal.SetConfigOption( 'PG_USE_COPY', None ) if ret != 0: gdaltest.post_reason('fail') return 'fail' if feat.GetFID() != 12: gdaltest.post_reason('fail') return 'fail' ds = None f = gdal.VSIFOpenL('/vsimem/ogr_pgdump_8.sql', 'rb') sql = gdal.VSIFReadL(1, 10000, f).decode('ascii') gdal.VSIFCloseL(f) gdal.Unlink('/vsimem/ogr_pgdump_8.sql') if sql.find("""CREATE TABLE "public"."test" ( "myfid" SERIAL, CONSTRAINT "test_pk" PRIMARY KEY ("myfid") )""") < 0 or \ sql.find("""ALTER TABLE "public"."test" ADD COLUMN "myfid" """) >= 0 or \ sql.find("""10\tfirst string\tsecond string""") == -1 or \ sql.find("""INSERT INTO "public"."test" ("str2") VALUES ('second string');""") == -1 or \ sql.find("""12\tfirst string\tsecond string""") == -1: print(sql) return 'fail' return 'success'
def test_ogr_rfc28_44(): ds = ogr.GetDriverByName('Memory').CreateDataSource('') lyr = ds.CreateLayer('lyr.withpoint') fld_defn = ogr.FieldDefn('field.withpoint', ogr.OFTInteger) lyr.CreateField(fld_defn) fld_defn = ogr.FieldDefn('foo', ogr.OFTInteger) lyr.CreateField(fld_defn) feat = ogr.Feature(lyr.GetLayerDefn()) feat.SetField(0, -1) lyr.CreateFeature(feat) feat = ogr.Feature(lyr.GetLayerDefn()) feat.SetField(0, 1) feat.SetField(1, 2) lyr.CreateFeature(feat) gdal.ErrorReset() lyr = ds.ExecuteSQL( "SELECT * FROM \"lyr.withpoint\" WHERE \"field.withpoint\" = 1") assert gdal.GetLastErrorMsg() == '' f = lyr.GetNextFeature() assert f is not None gdaltest.ds.ReleaseResultSet(lyr) gdal.ErrorReset() lyr = ds.ExecuteSQL( "SELECT \"lyr.withpoint\".\"field.withpoint\", \"field.withpoint\" FROM \"lyr.withpoint\" WHERE \"lyr.withpoint\".\"field.withpoint\" = 1" ) assert gdal.GetLastErrorMsg() == '' f = lyr.GetNextFeature() assert f is not None gdaltest.ds.ReleaseResultSet(lyr) # Test our tolerance against lack of necessary quoting gdal.ErrorReset() gdal.PushErrorHandler() lyr = ds.ExecuteSQL( "SELECT * FROM \"lyr.withpoint\" WHERE field.withpoint = 1") gdal.PopErrorHandler() assert gdal.GetLastErrorMsg( ) == 'Passed field name field.withpoint should have been surrounded by double quotes. Accepted since there is no ambiguity...' f = lyr.GetNextFeature() assert f is not None gdaltest.ds.ReleaseResultSet(lyr) # Againg, but in a situation where there IS ambiguity lyr = ds.CreateLayer('field') fld_defn = ogr.FieldDefn('id', ogr.OFTInteger) lyr.CreateField(fld_defn) gdal.ErrorReset() gdal.PushErrorHandler() lyr = ds.ExecuteSQL( "SELECT * FROM \"lyr.withpoint\" JOIN field ON \"lyr.withpoint\".foo = field.id WHERE field.withpoint = 1" ) gdal.PopErrorHandler() assert gdal.GetLastErrorMsg( ) == '"field"."withpoint" not recognised as an available field.' assert lyr is None # Test our tolerance against unnecessary quoting gdal.ErrorReset() gdal.PushErrorHandler() lyr = ds.ExecuteSQL( "SELECT * FROM \"lyr.withpoint\" f WHERE \"f.foo\" = 2") gdal.PopErrorHandler() assert gdal.GetLastErrorMsg( ) == 'Passed field name f.foo should NOT have been surrounded by double quotes. Accepted since there is no ambiguity...' f = lyr.GetNextFeature() assert f is not None gdaltest.ds.ReleaseResultSet(lyr)
def ogr_csw_vsimem_csw_minimal_instance(): if gdaltest.csw_drv is None: return 'skip' gdal.SetConfigOption('CPL_CURL_ENABLE_VSIMEM', 'YES') # Invalid response, but enough for use gdal.FileFromMemBuffer( '/vsimem/csw_endpoint?SERVICE=CSW&REQUEST=GetCapabilities', """ <Capabilities version="2.0.2"/> """) ds = ogr.Open('CSW:/vsimem/csw_endpoint') if ds is None: gdaltest.post_reason('fail') return 'fail' ds.TestCapability('foo') if ds.GetLayerCount() != 1: gdaltest.post_reason('fail') return 'fail' if ds.GetLayer(-1) is not None or ds.GetLayer(1) is not None: gdaltest.post_reason('fail') return 'fail' lyr = ds.GetLayer(0) lyr.TestCapability('foo') gdal.PushErrorHandler() f = lyr.GetNextFeature() gdal.PopErrorHandler() if f is not None: gdaltest.post_reason('fail') return 'fail' gdal.PushErrorHandler() fc = lyr.GetFeatureCount() gdal.PopErrorHandler() if fc != 0: gdaltest.post_reason('fail') return 'fail' gdal.FileFromMemBuffer( """/vsimem/csw_endpoint&POSTFIELDS=<?xml version="1.0" encoding="UTF-8"?><csw:GetRecords resultType="results" service="CSW" version="2.0.2" startPosition="1" maxRecords="500" xmlns:csw="http://www.opengis.net/cat/csw/2.0.2" xmlns:gml="http://www.opengis.net/gml" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dct="http://purl.org/dc/terms/" xmlns:ogc="http://www.opengis.net/ogc" xmlns:ows="http://www.opengis.net/ows" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opengis.net/cat/csw/2.0.2 http://schemas.opengis.net/csw/2.0.2/CSW-discovery.xsd"><csw:Query typeNames="csw:Record"><csw:ElementSetName>full</csw:ElementSetName></csw:Query></csw:GetRecords>""", """""") lyr.ResetReading() gdal.PushErrorHandler() f = lyr.GetNextFeature() gdal.PopErrorHandler() if f is not None or gdal.GetLastErrorMsg().find( 'Empty content returned by server') < 0: gdaltest.post_reason('fail') print(gdal.GetLastErrorMsg()) return 'fail' gdal.FileFromMemBuffer( """/vsimem/csw_endpoint&POSTFIELDS=<?xml version="1.0" encoding="UTF-8"?><csw:GetRecords resultType="results" service="CSW" version="2.0.2" startPosition="1" maxRecords="500" xmlns:csw="http://www.opengis.net/cat/csw/2.0.2" xmlns:gml="http://www.opengis.net/gml" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dct="http://purl.org/dc/terms/" xmlns:ogc="http://www.opengis.net/ogc" xmlns:ows="http://www.opengis.net/ows" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opengis.net/cat/csw/2.0.2 http://schemas.opengis.net/csw/2.0.2/CSW-discovery.xsd"><csw:Query typeNames="csw:Record"><csw:ElementSetName>full</csw:ElementSetName></csw:Query></csw:GetRecords>""", """<invalid_xml""") lyr.ResetReading() gdal.PushErrorHandler() f = lyr.GetNextFeature() gdal.PopErrorHandler() if f is not None or gdal.GetLastErrorMsg().find('Error: cannot parse') < 0: gdaltest.post_reason('fail') print(gdal.GetLastErrorMsg()) return 'fail' gdal.FileFromMemBuffer( """/vsimem/csw_endpoint&POSTFIELDS=<?xml version="1.0" encoding="UTF-8"?><csw:GetRecords resultType="results" service="CSW" version="2.0.2" startPosition="1" maxRecords="500" xmlns:csw="http://www.opengis.net/cat/csw/2.0.2" xmlns:gml="http://www.opengis.net/gml" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dct="http://purl.org/dc/terms/" xmlns:ogc="http://www.opengis.net/ogc" xmlns:ows="http://www.opengis.net/ows" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opengis.net/cat/csw/2.0.2 http://schemas.opengis.net/csw/2.0.2/CSW-discovery.xsd"><csw:Query typeNames="csw:Record"><csw:ElementSetName>full</csw:ElementSetName></csw:Query></csw:GetRecords>""", """<dummy_xml/>""") lyr.ResetReading() gdal.PushErrorHandler() f = lyr.GetNextFeature() gdal.PopErrorHandler() if f is not None or gdal.GetLastErrorMsg().find('Error: cannot parse') < 0: gdaltest.post_reason('fail') print(gdal.GetLastErrorMsg()) return 'fail' gdal.FileFromMemBuffer( """/vsimem/csw_endpoint&POSTFIELDS=<?xml version="1.0" encoding="UTF-8"?><csw:GetRecords resultType="results" service="CSW" version="2.0.2" startPosition="1" maxRecords="500" xmlns:csw="http://www.opengis.net/cat/csw/2.0.2" xmlns:gml="http://www.opengis.net/gml" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dct="http://purl.org/dc/terms/" xmlns:ogc="http://www.opengis.net/ogc" xmlns:ows="http://www.opengis.net/ows" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opengis.net/cat/csw/2.0.2 http://schemas.opengis.net/csw/2.0.2/CSW-discovery.xsd"><csw:Query typeNames="csw:Record"><csw:ElementSetName>full</csw:ElementSetName></csw:Query></csw:GetRecords>""", """<ServiceExceptionReport/>""") lyr.ResetReading() gdal.PushErrorHandler() f = lyr.GetNextFeature() gdal.PopErrorHandler() if f is not None or gdal.GetLastErrorMsg().find( 'Error returned by server') < 0: gdaltest.post_reason('fail') print(gdal.GetLastErrorMsg()) return 'fail' gdal.FileFromMemBuffer( """/vsimem/csw_endpoint&POSTFIELDS=<?xml version="1.0" encoding="UTF-8"?><csw:GetRecords resultType="results" service="CSW" version="2.0.2" startPosition="1" maxRecords="500" xmlns:csw="http://www.opengis.net/cat/csw/2.0.2" xmlns:gml="http://www.opengis.net/gml" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dct="http://purl.org/dc/terms/" xmlns:ogc="http://www.opengis.net/ogc" xmlns:ows="http://www.opengis.net/ows" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opengis.net/cat/csw/2.0.2 http://schemas.opengis.net/csw/2.0.2/CSW-discovery.xsd"><csw:Query typeNames="csw:Record"><csw:ElementSetName>full</csw:ElementSetName></csw:Query></csw:GetRecords>""", """<?xml version="1.0" encoding="UTF-8" standalone="no"?> <csw:GetRecordsResponse xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dct="http://purl.org/dc/terms/" xmlns:ows="http://www.opengis.net/ows" xmlns:csw="http://www.opengis.net/cat/csw/2.0.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0.2" xsi:schemaLocation="http://www.opengis.net/cat/csw/2.0.2 http://schemas.opengis.net/csw/2.0.2/CSW-discovery.xsd"> <csw:SearchStatus timestamp="2015-04-27T00:46:35Z"/> <csw:SearchResults nextRecord="3" numberOfRecordsMatched="2" numberOfRecordsReturned="3" recordSchema="http://www.opengis.net/cat/csw/2.0.2" elementSet="full"> <csw:Record> <dc:identifier>an_identifier</dc:identifier> <dc:identifier>another_identifier</dc:identifier> <dc:title>a_title</dc:title> <dc:type>dataset</dc:type> <dc:subject>a_subject</dc:subject> <dc:subject>another_subject</dc:subject> <dct:references scheme="None">http://foo/</dct:references> <dct:references scheme="None">http://bar/</dct:references> <dct:modified>2015-04-27</dct:modified> <dct:abstract>an_abstract</dct:abstract> <dc:date>2015-04-27</dc:date> <dc:language>eng</dc:language> <dc:format>a_format</dc:format> <dc:format>another_format</dc:format> <ows:BoundingBox crs="urn:x-ogc:def:crs:EPSG:6.11:4326" dimensions="2"> <ows:LowerCorner>-90 -180</ows:LowerCorner> <ows:UpperCorner>90 180</ows:UpperCorner> </ows:BoundingBox> </csw:Record> <csw:Record> </csw:Record> </csw:SearchResults> </csw:GetRecordsResponse> """) lyr.ResetReading() f = lyr.GetNextFeature() if f is None: gdaltest.post_reason('fail') return 'fail' if f['identifier'] != 'an_identifier' or f['other_identifiers'] != ['another_identifier'] or \ f['subject'] != 'a_subject' or f['other_subjects'] != ['another_subject'] or \ f['references'] != 'http://foo/' or f['other_references'] != ['http://bar/'] or \ f['format'] != 'a_format' or f['other_formats'] != ['another_format'] or \ f['boundingbox'].ExportToWkt() != 'POLYGON ((-180 -90,-180 90,180 90,180 -90,-180 -90))': gdaltest.post_reason('fail') f.DumpReadable() return 'fail' f = lyr.GetNextFeature() if f is None: gdaltest.post_reason('fail') return 'fail' gdal.PushErrorHandler() f = lyr.GetNextFeature() gdal.PopErrorHandler() if f is not None: gdaltest.post_reason('fail') return 'fail' gdal.PushErrorHandler() fc = lyr.GetFeatureCount() gdal.PopErrorHandler() if fc != 2: gdaltest.post_reason('fail') return 'fail' lyr.ResetReading() f = lyr.GetNextFeature() if f is None: gdaltest.post_reason('fail') return 'fail' gdal.FileFromMemBuffer( """/vsimem/csw_endpoint&POSTFIELDS=<?xml version="1.0" encoding="UTF-8"?><csw:GetRecords resultType="results" service="CSW" version="2.0.2" startPosition="3" maxRecords="500" xmlns:csw="http://www.opengis.net/cat/csw/2.0.2" xmlns:gml="http://www.opengis.net/gml" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dct="http://purl.org/dc/terms/" xmlns:ogc="http://www.opengis.net/ogc" xmlns:ows="http://www.opengis.net/ows" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opengis.net/cat/csw/2.0.2 http://schemas.opengis.net/csw/2.0.2/CSW-discovery.xsd"><csw:Query typeNames="csw:Record"><csw:ElementSetName>full</csw:ElementSetName></csw:Query></csw:GetRecords>""", """<?xml version="1.0" encoding="UTF-8" standalone="no"?> <csw:GetRecordsResponse xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dct="http://purl.org/dc/terms/" xmlns:ows="http://www.opengis.net/ows" xmlns:csw="http://www.opengis.net/cat/csw/2.0.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0.2" xsi:schemaLocation="http://www.opengis.net/cat/csw/2.0.2 http://schemas.opengis.net/csw/2.0.2/CSW-discovery.xsd"> <csw:SearchStatus timestamp="2015-04-27T00:46:35Z"/> <csw:SearchResults nextRecord="0" numberOfRecordsMatched="3" numberOfRecordsReturned="1" recordSchema="http://www.opengis.net/cat/csw/2.0.2" elementSet="full"> <csw:Record> </csw:Record> </csw:SearchResults> </csw:GetRecordsResponse> """) f = lyr.GetNextFeature() if f is None: gdaltest.post_reason('fail') return 'fail' f = lyr.GetNextFeature() if f is None: gdaltest.post_reason('fail') return 'fail' gdal.FileFromMemBuffer( """/vsimem/csw_endpoint&POSTFIELDS=<?xml version="1.0" encoding="UTF-8"?><csw:GetRecords resultType="results" service="CSW" version="2.0.2" startPosition="4" maxRecords="500" xmlns:csw="http://www.opengis.net/cat/csw/2.0.2" xmlns:gml="http://www.opengis.net/gml" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dct="http://purl.org/dc/terms/" xmlns:ogc="http://www.opengis.net/ogc" xmlns:ows="http://www.opengis.net/ows" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opengis.net/cat/csw/2.0.2 http://schemas.opengis.net/csw/2.0.2/CSW-discovery.xsd"><csw:Query typeNames="csw:Record"><csw:ElementSetName>full</csw:ElementSetName></csw:Query></csw:GetRecords>""", """<?xml version="1.0" encoding="UTF-8" standalone="no"?> <csw:GetRecordsResponse xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dct="http://purl.org/dc/terms/" xmlns:ows="http://www.opengis.net/ows" xmlns:csw="http://www.opengis.net/cat/csw/2.0.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0.2" xsi:schemaLocation="http://www.opengis.net/cat/csw/2.0.2 http://schemas.opengis.net/csw/2.0.2/CSW-discovery.xsd"> <csw:SearchStatus timestamp="2015-04-27T00:46:35Z"/> <csw:SearchResults nextRecord="0" numberOfRecordsMatched="3" numberOfRecordsReturned="0" recordSchema="http://www.opengis.net/cat/csw/2.0.2" elementSet="full"> </csw:SearchResults> </csw:GetRecordsResponse> """) f = lyr.GetNextFeature() if f is not None: gdaltest.post_reason('fail') return 'fail' gdal.FileFromMemBuffer( """/vsimem/csw_endpoint&POSTFIELDS=<?xml version="1.0" encoding="UTF-8"?><csw:GetRecords resultType="hits" service="CSW" version="2.0.2" xmlns:csw="http://www.opengis.net/cat/csw/2.0.2" xmlns:gml="http://www.opengis.net/gml" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dct="http://purl.org/dc/terms/" xmlns:ogc="http://www.opengis.net/ogc" xmlns:ows="http://www.opengis.net/ows" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opengis.net/cat/csw/2.0.2 http://schemas.opengis.net/csw/2.0.2/CSW-discovery.xsd"><csw:Query typeNames="csw:Record"><csw:ElementSetName>full</csw:ElementSetName></csw:Query></csw:GetRecords>""", """""") gdal.PushErrorHandler() fc = lyr.GetFeatureCount() gdal.PopErrorHandler() if fc != 3: gdaltest.post_reason('fail') print(gdal.GetLastErrorMsg()) return 'fail' gdal.FileFromMemBuffer( """/vsimem/csw_endpoint&POSTFIELDS=<?xml version="1.0" encoding="UTF-8"?><csw:GetRecords resultType="hits" service="CSW" version="2.0.2" xmlns:csw="http://www.opengis.net/cat/csw/2.0.2" xmlns:gml="http://www.opengis.net/gml" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dct="http://purl.org/dc/terms/" xmlns:ogc="http://www.opengis.net/ogc" xmlns:ows="http://www.opengis.net/ows" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opengis.net/cat/csw/2.0.2 http://schemas.opengis.net/csw/2.0.2/CSW-discovery.xsd"><csw:Query typeNames="csw:Record"><csw:ElementSetName>full</csw:ElementSetName></csw:Query></csw:GetRecords>""", """<dummy_xml/>""") gdal.PushErrorHandler() fc = lyr.GetFeatureCount() gdal.PopErrorHandler() if fc != 3: gdaltest.post_reason('fail') print(gdal.GetLastErrorMsg()) return 'fail' gdal.FileFromMemBuffer( """/vsimem/csw_endpoint&POSTFIELDS=<?xml version="1.0" encoding="UTF-8"?><csw:GetRecords resultType="hits" service="CSW" version="2.0.2" xmlns:csw="http://www.opengis.net/cat/csw/2.0.2" xmlns:gml="http://www.opengis.net/gml" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dct="http://purl.org/dc/terms/" xmlns:ogc="http://www.opengis.net/ogc" xmlns:ows="http://www.opengis.net/ows" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opengis.net/cat/csw/2.0.2 http://schemas.opengis.net/csw/2.0.2/CSW-discovery.xsd"><csw:Query typeNames="csw:Record"><csw:ElementSetName>full</csw:ElementSetName></csw:Query></csw:GetRecords>""", """<invalid_xml>""") gdal.PushErrorHandler() fc = lyr.GetFeatureCount() gdal.PopErrorHandler() if fc != 3: gdaltest.post_reason('fail') print(gdal.GetLastErrorMsg()) return 'fail' gdal.FileFromMemBuffer( """/vsimem/csw_endpoint&POSTFIELDS=<?xml version="1.0" encoding="UTF-8"?><csw:GetRecords resultType="hits" service="CSW" version="2.0.2" xmlns:csw="http://www.opengis.net/cat/csw/2.0.2" xmlns:gml="http://www.opengis.net/gml" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dct="http://purl.org/dc/terms/" xmlns:ogc="http://www.opengis.net/ogc" xmlns:ows="http://www.opengis.net/ows" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opengis.net/cat/csw/2.0.2 http://schemas.opengis.net/csw/2.0.2/CSW-discovery.xsd"><csw:Query typeNames="csw:Record"><csw:ElementSetName>full</csw:ElementSetName></csw:Query></csw:GetRecords>""", """<ServiceExceptionReport/>""") gdal.PushErrorHandler() fc = lyr.GetFeatureCount() gdal.PopErrorHandler() if fc != 3: gdaltest.post_reason('fail') print(gdal.GetLastErrorMsg()) return 'fail' gdal.FileFromMemBuffer( """/vsimem/csw_endpoint&POSTFIELDS=<?xml version="1.0" encoding="UTF-8"?><csw:GetRecords resultType="hits" service="CSW" version="2.0.2" xmlns:csw="http://www.opengis.net/cat/csw/2.0.2" xmlns:gml="http://www.opengis.net/gml" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dct="http://purl.org/dc/terms/" xmlns:ogc="http://www.opengis.net/ogc" xmlns:ows="http://www.opengis.net/ows" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opengis.net/cat/csw/2.0.2 http://schemas.opengis.net/csw/2.0.2/CSW-discovery.xsd"><csw:Query typeNames="csw:Record"><csw:ElementSetName>full</csw:ElementSetName></csw:Query></csw:GetRecords>""", """<?xml version="1.0" encoding="UTF-8" standalone="no"?> <csw:GetRecordsResponse xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dct="http://purl.org/dc/terms/" xmlns:ows="http://www.opengis.net/ows" xmlns:csw="http://www.opengis.net/cat/csw/2.0.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0.2" xsi:schemaLocation="http://www.opengis.net/cat/csw/2.0.2 http://schemas.opengis.net/csw/2.0.2/CSW-discovery.xsd"> <csw:SearchStatus timestamp="2015-04-27T00:46:35Z"/> <csw:SearchResults nextRecord="0" numberOfRecordsMatched="200" numberOfRecordsReturned="0" recordSchema="http://www.opengis.net/cat/csw/2.0.2" elementSet="full"> </csw:SearchResults> </csw:GetRecordsResponse> """) fc = lyr.GetFeatureCount() if fc != 200: gdaltest.post_reason('fail') return 'fail' lyr.SetAttributeFilter("identifier = 'an_identifier'") lyr.SetSpatialFilterRect(-180, -90, 180, 90) gdal.FileFromMemBuffer( """/vsimem/csw_endpoint&POSTFIELDS=<?xml version="1.0" encoding="UTF-8"?><csw:GetRecords resultType="results" service="CSW" version="2.0.2" startPosition="1" maxRecords="500" xmlns:csw="http://www.opengis.net/cat/csw/2.0.2" xmlns:gml="http://www.opengis.net/gml" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dct="http://purl.org/dc/terms/" xmlns:ogc="http://www.opengis.net/ogc" xmlns:ows="http://www.opengis.net/ows" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opengis.net/cat/csw/2.0.2 http://schemas.opengis.net/csw/2.0.2/CSW-discovery.xsd"><csw:Query typeNames="csw:Record"><csw:ElementSetName>full</csw:ElementSetName><csw:Constraint version="1.1.0"><ogc:Filter><ogc:And><ogc:BBOX><ogc:PropertyName>ows:BoundingBox</ogc:PropertyName><gml:Envelope srsName="urn:ogc:def:crs:EPSG::4326"><gml:lowerCorner>-90 -180</gml:lowerCorner><gml:upperCorner>90 180</gml:upperCorner></gml:Envelope></ogc:BBOX><ogc:PropertyIsEqualTo><ogc:PropertyName>dc:identifier</ogc:PropertyName><ogc:Literal>an_identifier</ogc:Literal></ogc:PropertyIsEqualTo></ogc:And></ogc:Filter></csw:Constraint></csw:Query></csw:GetRecords>""", """<?xml version="1.0" encoding="UTF-8" standalone="no"?> <csw:GetRecordsResponse xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dct="http://purl.org/dc/terms/" xmlns:ows="http://www.opengis.net/ows" xmlns:csw="http://www.opengis.net/cat/csw/2.0.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0.2" xsi:schemaLocation="http://www.opengis.net/cat/csw/2.0.2 http://schemas.opengis.net/csw/2.0.2/CSW-discovery.xsd"> <csw:SearchStatus timestamp="2015-04-27T00:46:35Z"/> <csw:SearchResults nextRecord="0" numberOfRecordsMatched="3" numberOfRecordsReturned="1" recordSchema="http://www.opengis.net/cat/csw/2.0.2" elementSet="full"> <csw:Record> </csw:Record> </csw:SearchResults> </csw:GetRecordsResponse> """) f = lyr.GetNextFeature() if f is None: gdaltest.post_reason('fail') return 'fail' gdal.FileFromMemBuffer( """/vsimem/csw_endpoint&POSTFIELDS=<?xml version="1.0" encoding="UTF-8"?><csw:GetRecords resultType="hits" service="CSW" version="2.0.2" xmlns:csw="http://www.opengis.net/cat/csw/2.0.2" xmlns:gml="http://www.opengis.net/gml" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dct="http://purl.org/dc/terms/" xmlns:ogc="http://www.opengis.net/ogc" xmlns:ows="http://www.opengis.net/ows" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opengis.net/cat/csw/2.0.2 http://schemas.opengis.net/csw/2.0.2/CSW-discovery.xsd"><csw:Query typeNames="csw:Record"><csw:ElementSetName>full</csw:ElementSetName><csw:Constraint version="1.1.0"><ogc:Filter><ogc:And><ogc:BBOX><ogc:PropertyName>ows:BoundingBox</ogc:PropertyName><gml:Envelope srsName="urn:ogc:def:crs:EPSG::4326"><gml:lowerCorner>-90 -180</gml:lowerCorner><gml:upperCorner>90 180</gml:upperCorner></gml:Envelope></ogc:BBOX><ogc:PropertyIsEqualTo><ogc:PropertyName>dc:identifier</ogc:PropertyName><ogc:Literal>an_identifier</ogc:Literal></ogc:PropertyIsEqualTo></ogc:And></ogc:Filter></csw:Constraint></csw:Query></csw:GetRecords>""", """<?xml version="1.0" encoding="UTF-8" standalone="no"?> <csw:GetRecordsResponse xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dct="http://purl.org/dc/terms/" xmlns:ows="http://www.opengis.net/ows" xmlns:csw="http://www.opengis.net/cat/csw/2.0.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0.2" xsi:schemaLocation="http://www.opengis.net/cat/csw/2.0.2 http://schemas.opengis.net/csw/2.0.2/CSW-discovery.xsd"> <csw:SearchStatus timestamp="2015-04-27T00:46:35Z"/> <csw:SearchResults nextRecord="0" numberOfRecordsMatched="300" numberOfRecordsReturned="0" recordSchema="http://www.opengis.net/cat/csw/2.0.2" elementSet="full"> </csw:SearchResults> </csw:GetRecordsResponse> """) fc = lyr.GetFeatureCount() if fc != 300: gdaltest.post_reason('fail') return 'fail' lyr.SetAttributeFilter( "identifier = 'an_identifier' AND " + "references = 'http://foo/' AND " + "anytext LIKE '%%foo%%' AND " + "other_identifiers = '' AND " + "other_subjects = '' AND " + "other_formats = '' AND " + "other_references = '' AND " + "ST_Intersects(boundingbox, ST_MakeEnvelope(2,49,2,49,4326))") lyr.SetAttributeFilter(None) lyr.SetSpatialFilter(None) return 'success'
def test_vsizip_1(): # We can keep the handle open during all the ZIP writing hZIP = gdal.VSIFOpenL("/vsizip/vsimem/test.zip", "wb") assert hZIP is not None, 'fail 1' # One way to create a directory f = gdal.VSIFOpenL("/vsizip/vsimem/test.zip/subdir2/", "wb") assert f is not None, 'fail 2' gdal.VSIFCloseL(f) # A more natural one gdal.Mkdir("/vsizip/vsimem/test.zip/subdir1", 0) # Create 1st file f2 = gdal.VSIFOpenL("/vsizip/vsimem/test.zip/subdir3/abcd", "wb") assert f2 is not None, 'fail 3' gdal.VSIFWriteL("abcd", 1, 4, f2) gdal.VSIFCloseL(f2) # Test that we cannot read a zip file being written gdal.ErrorReset() gdal.PushErrorHandler('CPLQuietErrorHandler') f = gdal.VSIFOpenL("/vsizip/vsimem/test.zip/subdir3/abcd", "rb") gdal.PopErrorHandler() assert gdal.GetLastErrorMsg() == 'Cannot read a zip file being written', \ 'expected error' assert f is None, 'should not have been successful 1' # Create 2nd file f3 = gdal.VSIFOpenL("/vsizip/vsimem/test.zip/subdir3/efghi", "wb") assert f3 is not None, 'fail 4' gdal.VSIFWriteL("efghi", 1, 5, f3) # Try creating a 3d file gdal.ErrorReset() gdal.PushErrorHandler('CPLQuietErrorHandler') f4 = gdal.VSIFOpenL("/vsizip/vsimem/test.zip/that_wont_work", "wb") gdal.PopErrorHandler() assert gdal.GetLastErrorMsg() == 'Cannot create that_wont_work while another file is being written in the .zip', \ 'expected error' assert f4 is None, 'should not have been successful 2' gdal.VSIFCloseL(f3) # Now we can close the main handle gdal.VSIFCloseL(hZIP) # ERROR 6: Support only 1 file in archive file /vsimem/test.zip when no explicit in-archive filename is specified gdal.ErrorReset() with gdaltest.error_handler(): f = gdal.VSIFOpenL('/vsizip/vsimem/test.zip', 'rb') if f is not None: gdal.VSIFCloseL(f) assert gdal.GetLastErrorMsg() != '', 'expected error' f = gdal.VSIFOpenL("/vsizip/vsimem/test.zip/subdir3/abcd", "rb") assert f is not None, 'fail 5' data = gdal.VSIFReadL(1, 4, f) gdal.VSIFCloseL(f) assert data.decode('ASCII') == 'abcd' # Test alternate uri syntax gdal.Rename("/vsimem/test.zip", "/vsimem/test.xxx") f = gdal.VSIFOpenL("/vsizip/{/vsimem/test.xxx}/subdir3/abcd", "rb") assert f is not None data = gdal.VSIFReadL(1, 4, f) gdal.VSIFCloseL(f) assert data.decode('ASCII') == 'abcd' # With a trailing slash f = gdal.VSIFOpenL("/vsizip/{/vsimem/test.xxx}/subdir3/abcd/", "rb") assert f is not None gdal.VSIFCloseL(f) # Test ReadDir() assert len(gdal.ReadDir("/vsizip/{/vsimem/test.xxx}")) == 3 # Unbalanced curls f = gdal.VSIFOpenL("/vsizip/{/vsimem/test.xxx", "rb") assert f is None # Non existing mainfile f = gdal.VSIFOpenL("/vsizip/{/vsimem/test.xxx}/bla", "rb") assert f is None # Non existing subfile f = gdal.VSIFOpenL("/vsizip/{/vsimem/test.zzz}/bla", "rb") assert f is None # Wrong syntax f = gdal.VSIFOpenL("/vsizip/{/vsimem/test.xxx}.aux.xml", "rb") assert f is None # Test nested { { } } hZIP = gdal.VSIFOpenL("/vsizip/{/vsimem/zipinzip.yyy}", "wb") assert hZIP is not None, 'fail 1' f = gdal.VSIFOpenL("/vsizip/{/vsimem/zipinzip.yyy}/test.xxx", "wb") f_src = gdal.VSIFOpenL("/vsimem/test.xxx", "rb") data = gdal.VSIFReadL(1, 10000, f_src) gdal.VSIFCloseL(f_src) gdal.VSIFWriteL(data, 1, len(data), f) gdal.VSIFCloseL(f) gdal.VSIFCloseL(hZIP) f = gdal.VSIFOpenL( "/vsizip/{/vsizip/{/vsimem/zipinzip.yyy}/test.xxx}/subdir3/abcd/", "rb") assert f is not None data = gdal.VSIFReadL(1, 4, f) gdal.VSIFCloseL(f) assert data.decode('ASCII') == 'abcd' gdal.Unlink("/vsimem/test.xxx") gdal.Unlink("/vsimem/zipinzip.yyy") # Test VSIStatL on a non existing file assert gdal.VSIStatL('/vsizip//vsimem/foo.zip') is None # Test ReadDir on a non existing file assert gdal.ReadDir('/vsizip//vsimem/foo.zip') is None
def main(argv=None): bComputeMinMax = False bShowGCPs = True bShowMetadata = True bShowRAT = True bStats = False bApproxStats = True bShowColorTable = True bComputeChecksum = False bReportHistograms = False pszFilename = None papszExtraMDDomains = [] pszProjection = None hTransform = None bShowFileList = True # Must process GDAL_SKIP before GDALAllRegister(), but we can't call # GDALGeneralCmdLineProcessor before it needs the drivers to be registered # for the --format or --formats options #for( i = 1; i < argc; i++ ) #{ # if EQUAL(argv[i],"--config") and i + 2 < argc and EQUAL(argv[i + 1], "GDAL_SKIP"): # { # CPLSetConfigOption( argv[i+1], argv[i+2] ); # # i += 2; # } #} # #GDALAllRegister(); if argv is None: argv = sys.argv argv = gdal.GeneralCmdLineProcessor(argv) if argv is None: return 1 nArgc = len(argv) # -------------------------------------------------------------------- # Parse arguments. # -------------------------------------------------------------------- i = 1 while i < nArgc: if EQUAL(argv[i], "--utility_version"): print("%s is running against GDAL %s" % (argv[0], gdal.VersionInfo("RELEASE_NAME"))) return 0 elif EQUAL(argv[i], "-mm"): bComputeMinMax = True elif EQUAL(argv[i], "-hist"): bReportHistograms = True elif EQUAL(argv[i], "-stats"): bStats = True bApproxStats = False elif EQUAL(argv[i], "-approx_stats"): bStats = True bApproxStats = True elif EQUAL(argv[i], "-checksum"): bComputeChecksum = True elif EQUAL(argv[i], "-nogcp"): bShowGCPs = False elif EQUAL(argv[i], "-nomd"): bShowMetadata = False elif EQUAL(argv[i], "-norat"): bShowRAT = False elif EQUAL(argv[i], "-noct"): bShowColorTable = False elif EQUAL(argv[i], "-mdd") and i < nArgc - 1: i = i + 1 papszExtraMDDomains.append(argv[i]) elif EQUAL(argv[i], "-nofl"): bShowFileList = False elif argv[i][0] == '-': return Usage() elif pszFilename is None: pszFilename = argv[i] else: return Usage() i = i + 1 if pszFilename is None: return Usage() # -------------------------------------------------------------------- # Open dataset. # -------------------------------------------------------------------- hDataset = gdal.Open(pszFilename, gdal.GA_ReadOnly) if hDataset is None: print("gdalinfo failed - unable to open '%s'." % pszFilename) return 1 # -------------------------------------------------------------------- # Report general info. # -------------------------------------------------------------------- hDriver = hDataset.GetDriver() print( "Driver: %s/%s" % ( \ hDriver.ShortName, \ hDriver.LongName )) papszFileList = hDataset.GetFileList() if papszFileList is None or len(papszFileList) == 0: print("Files: none associated") else: print("Files: %s" % papszFileList[0]) if bShowFileList: for i in range(1, len(papszFileList)): print(" %s" % papszFileList[i]) print("Size is %d, %d" % (hDataset.RasterXSize, hDataset.RasterYSize)) # -------------------------------------------------------------------- # Report projection. # -------------------------------------------------------------------- pszProjection = hDataset.GetProjectionRef() if pszProjection is not None: hSRS = osr.SpatialReference() if hSRS.ImportFromWkt(pszProjection) == gdal.CE_None: pszPrettyWkt = hSRS.ExportToPrettyWkt(False) print("Coordinate System is:\n%s" % pszPrettyWkt) else: print("Coordinate System is `%s'" % pszProjection) # -------------------------------------------------------------------- # Report Geotransform. # -------------------------------------------------------------------- adfGeoTransform = hDataset.GetGeoTransform(can_return_null=True) if adfGeoTransform is not None: if adfGeoTransform[2] == 0.0 and adfGeoTransform[4] == 0.0: print( "Origin = (%.15f,%.15f)" % ( \ adfGeoTransform[0], adfGeoTransform[3] )) print( "Pixel Size = (%.15f,%.15f)" % ( \ adfGeoTransform[1], adfGeoTransform[5] )) else: print( "GeoTransform =\n" \ " %.16g, %.16g, %.16g\n" \ " %.16g, %.16g, %.16g" % ( \ adfGeoTransform[0], \ adfGeoTransform[1], \ adfGeoTransform[2], \ adfGeoTransform[3], \ adfGeoTransform[4], \ adfGeoTransform[5] )) # -------------------------------------------------------------------- # Report GCPs. # -------------------------------------------------------------------- if bShowGCPs and hDataset.GetGCPCount() > 0: pszProjection = hDataset.GetGCPProjection() if pszProjection is not None: hSRS = osr.SpatialReference() if hSRS.ImportFromWkt(pszProjection) == gdal.CE_None: pszPrettyWkt = hSRS.ExportToPrettyWkt(False) print("GCP Projection = \n%s" % pszPrettyWkt) else: print( "GCP Projection = %s" % \ pszProjection ) gcps = hDataset.GetGCPs() i = 0 for gcp in gcps: print( "GCP[%3d]: Id=%s, Info=%s\n" \ " (%.15g,%.15g) -> (%.15g,%.15g,%.15g)" % ( \ i, gcp.Id, gcp.Info, \ gcp.GCPPixel, gcp.GCPLine, \ gcp.GCPX, gcp.GCPY, gcp.GCPZ )) i = i + 1 # -------------------------------------------------------------------- # Report metadata. # -------------------------------------------------------------------- if bShowMetadata: papszMetadata = hDataset.GetMetadata_List() else: papszMetadata = None if bShowMetadata and papszMetadata is not None and len(papszMetadata) > 0: print("Metadata:") for metadata in papszMetadata: print(" %s" % metadata) if bShowMetadata: for extra_domain in papszExtraMDDomains: papszMetadata = hDataset.GetMetadata_List(extra_domain) if papszMetadata is not None and len(papszMetadata) > 0: print("Metadata (%s):" % extra_domain) for metadata in papszMetadata: print(" %s" % metadata) # -------------------------------------------------------------------- # Report "IMAGE_STRUCTURE" metadata. # -------------------------------------------------------------------- if bShowMetadata: papszMetadata = hDataset.GetMetadata_List("IMAGE_STRUCTURE") else: papszMetadata = None if bShowMetadata and papszMetadata is not None and len(papszMetadata) > 0: print("Image Structure Metadata:") for metadata in papszMetadata: print(" %s" % metadata) # -------------------------------------------------------------------- # Report subdatasets. # -------------------------------------------------------------------- papszMetadata = hDataset.GetMetadata_List("SUBDATASETS") if papszMetadata is not None and len(papszMetadata) > 0: print("Subdatasets:") for metadata in papszMetadata: print(" %s" % metadata) # -------------------------------------------------------------------- # Report geolocation. # -------------------------------------------------------------------- if bShowMetadata: papszMetadata = hDataset.GetMetadata_List("GEOLOCATION") else: papszMetadata = None if bShowMetadata and papszMetadata is not None and len(papszMetadata) > 0: print("Geolocation:") for metadata in papszMetadata: print(" %s" % metadata) # -------------------------------------------------------------------- # Report RPCs # -------------------------------------------------------------------- if bShowMetadata: papszMetadata = hDataset.GetMetadata_List("RPC") else: papszMetadata = None if bShowMetadata and papszMetadata is not None and len(papszMetadata) > 0: print("RPC Metadata:") for metadata in papszMetadata: print(" %s" % metadata) # -------------------------------------------------------------------- # Setup projected to lat/long transform if appropriate. # -------------------------------------------------------------------- if pszProjection is not None and len(pszProjection) > 0: hProj = osr.SpatialReference(pszProjection) if hProj is not None: hLatLong = hProj.CloneGeogCS() if hLatLong is not None: gdal.PushErrorHandler('CPLQuietErrorHandler') hTransform = osr.CoordinateTransformation(hProj, hLatLong) gdal.PopErrorHandler() if gdal.GetLastErrorMsg().find( 'Unable to load PROJ.4 library') != -1: hTransform = None # -------------------------------------------------------------------- # Report corners. # -------------------------------------------------------------------- print("Corner Coordinates:") GDALInfoReportCorner( hDataset, hTransform, "Upper Left", \ 0.0, 0.0 ) GDALInfoReportCorner( hDataset, hTransform, "Lower Left", \ 0.0, hDataset.RasterYSize) GDALInfoReportCorner( hDataset, hTransform, "Upper Right", \ hDataset.RasterXSize, 0.0 ) GDALInfoReportCorner( hDataset, hTransform, "Lower Right", \ hDataset.RasterXSize, \ hDataset.RasterYSize ) GDALInfoReportCorner( hDataset, hTransform, "Center", \ hDataset.RasterXSize/2.0, \ hDataset.RasterYSize/2.0 ) # ==================================================================== # Loop over bands. # ==================================================================== for iBand in range(hDataset.RasterCount): hBand = hDataset.GetRasterBand(iBand + 1) #if( bSample ) #{ # float afSample[10000]; # int nCount; # # nCount = GDALGetRandomRasterSample( hBand, 10000, afSample ); # print( "Got %d samples.\n", nCount ); #} (nBlockXSize, nBlockYSize) = hBand.GetBlockSize() print( "Band %d Block=%dx%d Type=%s, ColorInterp=%s" % ( iBand+1, \ nBlockXSize, nBlockYSize, \ gdal.GetDataTypeName(hBand.DataType), \ gdal.GetColorInterpretationName( \ hBand.GetRasterColorInterpretation()) )) if hBand.GetDescription() is not None \ and len(hBand.GetDescription()) > 0 : print(" Description = %s" % hBand.GetDescription()) dfMin = hBand.GetMinimum() dfMax = hBand.GetMaximum() if dfMin is not None or dfMax is not None or bComputeMinMax: line = " " if dfMin is not None: line = line + ("Min=%.3f " % dfMin) if dfMax is not None: line = line + ("Max=%.3f " % dfMax) if bComputeMinMax: gdal.ErrorReset() adfCMinMax = hBand.ComputeRasterMinMax(False) if gdal.GetLastErrorType() == gdal.CE_None: line = line + ( " Computed Min/Max=%.3f,%.3f" % ( \ adfCMinMax[0], adfCMinMax[1] )) print(line) stats = hBand.GetStatistics(bApproxStats, bStats) # Dirty hack to recognize if stats are valid. If invalid, the returned # stddev is negative if stats[3] >= 0.0: print( " Minimum=%.3f, Maximum=%.3f, Mean=%.3f, StdDev=%.3f" % ( \ stats[0], stats[1], stats[2], stats[3] )) if bReportHistograms: hist = hBand.GetDefaultHistogram(force=True, callback=gdal.TermProgress) if hist is not None: dfMin = hist[0] dfMax = hist[1] nBucketCount = hist[2] panHistogram = hist[3] print( " %d buckets from %g to %g:" % ( \ nBucketCount, dfMin, dfMax )) line = ' ' for bucket in panHistogram: line = line + ("%d " % bucket) print(line) if bComputeChecksum: print(" Checksum=%d" % hBand.Checksum()) dfNoData = hBand.GetNoDataValue() if dfNoData is not None: if dfNoData != dfNoData: print(" NoData Value=nan") else: print(" NoData Value=%.18g" % dfNoData) if hBand.GetOverviewCount() > 0: line = " Overviews: " for iOverview in range(hBand.GetOverviewCount()): if iOverview != 0: line = line + ", " hOverview = hBand.GetOverview(iOverview) if hOverview is not None: line = line + ("%dx%d" % (hOverview.XSize, hOverview.YSize)) pszResampling = \ hOverview.GetMetadataItem( "RESAMPLING", "" ) if pszResampling is not None \ and len(pszResampling) >= 12 \ and EQUAL(pszResampling[0:12],"AVERAGE_BIT2"): line = line + "*" else: line = line + "(null)" print(line) if bComputeChecksum: line = " Overviews checksum: " for iOverview in range(hBand.GetOverviewCount()): if iOverview != 0: line = line + ", " hOverview = hBand.GetOverview(iOverview) if hOverview is not None: line = line + ("%d" % hOverview.Checksum()) else: line = line + "(null)" print(line) if hBand.HasArbitraryOverviews(): print(" Overviews: arbitrary") nMaskFlags = hBand.GetMaskFlags() if (nMaskFlags & (gdal.GMF_NODATA | gdal.GMF_ALL_VALID)) == 0: hMaskBand = hBand.GetMaskBand() line = " Mask Flags: " if (nMaskFlags & gdal.GMF_PER_DATASET) != 0: line = line + "PER_DATASET " if (nMaskFlags & gdal.GMF_ALPHA) != 0: line = line + "ALPHA " if (nMaskFlags & gdal.GMF_NODATA) != 0: line = line + "NODATA " if (nMaskFlags & gdal.GMF_ALL_VALID) != 0: line = line + "ALL_VALID " print(line) if hMaskBand is not None and \ hMaskBand.GetOverviewCount() > 0: line = " Overviews of mask band: " for iOverview in range(hMaskBand.GetOverviewCount()): if iOverview != 0: line = line + ", " hOverview = hMaskBand.GetOverview(iOverview) if hOverview is not None: line = line + ("%d" % hOverview.Checksum()) else: line = line + "(null)" if len(hBand.GetUnitType()) > 0: print(" Unit Type: %s" % hBand.GetUnitType()) papszCategories = hBand.GetRasterCategoryNames() if papszCategories is not None: print(" Categories:") i = 0 for category in papszCategories: print(" %3d: %s" % (i, category)) i = i + 1 if hBand.GetScale() != 1.0 or hBand.GetOffset() != 0.0: print( " Offset: %.15g, Scale:%.15g" % \ ( hBand.GetOffset(), hBand.GetScale())) if bShowMetadata: papszMetadata = hBand.GetMetadata_List() else: papszMetadata = None if bShowMetadata and papszMetadata is not None and len( papszMetadata) > 0: print(" Metadata:") for metadata in papszMetadata: print(" %s" % metadata) if bShowMetadata: papszMetadata = hBand.GetMetadata_List("IMAGE_STRUCTURE") else: papszMetadata = None if bShowMetadata and papszMetadata is not None and len( papszMetadata) > 0: print(" Image Structure Metadata:") for metadata in papszMetadata: print(" %s" % metadata) hTable = hBand.GetRasterColorTable() if hBand.GetRasterColorInterpretation() == gdal.GCI_PaletteIndex \ and hTable is not None: print( " Color Table (%s with %d entries)" % (\ gdal.GetPaletteInterpretationName( \ hTable.GetPaletteInterpretation( )), \ hTable.GetCount() )) if bShowColorTable: for i in range(hTable.GetCount()): sEntry = hTable.GetColorEntry(i) print( " %3d: %d,%d,%d,%d" % ( \ i, \ sEntry[0],\ sEntry[1],\ sEntry[2],\ sEntry[3] )) if bShowRAT: pass #hRAT = hBand.GetDefaultRAT() #GDALRATDumpReadable( hRAT, None ); return 0
def main(argv=None): print('Now in main!') bComputeMinMax = False bSample = False bShowGCPs = True bShowMetadata = False bShowRAT = False debug = False bStats = False bApproxStats = True bShowColorTable = True bComputeChecksum = False bReportHistograms = False pszFilename = None papszExtraMDDomains = [] pszProjection = None hTransform = None bShowFileList = True dst_img = None dst_lbl = None bands = 1 #/* Must process GDAL_SKIP before GDALAllRegister(), but we can't call */ #/* GDALGeneralCmdLineProcessor before it needs the drivers to be registered */ #/* for the --format or --formats options */ #for( i = 1; i < argc; i++ ) #{ # if EQUAL(argv[i],"--config") and i + 2 < argc and EQUAL(argv[i + 1], "GDAL_SKIP"): # { # CPLSetConfigOption( argv[i+1], argv[i+2] ); # # i += 2; # } #} # #GDALAllRegister(); if argv is None: argv = sys.argv argv = gdal.GeneralCmdLineProcessor(argv) if argv is None: return 1 nArgc = len(argv) #/* -------------------------------------------------------------------- */ #/* Parse arguments. */ #/* -------------------------------------------------------------------- */ i = 1 while i < nArgc: if EQUAL(argv[i], "--utility_version"): print("%s is running against GDAL %s" % (argv[0], gdal.VersionInfo("RELEASE_NAME"))) return 0 elif EQUAL(argv[i], "-debug"): debug = True elif EQUAL(argv[i], "-mm"): bComputeMinMax = True elif EQUAL(argv[i], "-hist"): bReportHistograms = True elif EQUAL(argv[i], "-stats"): bStats = True bApproxStats = False elif EQUAL(argv[i], "-approx_stats"): bStats = True bApproxStats = True elif EQUAL(argv[i], "-sample"): bSample = True elif EQUAL(argv[i], "-checksum"): bComputeChecksum = True elif EQUAL(argv[i], "-nogcp"): bShowGCPs = False elif EQUAL(argv[i], "-nomd"): bShowMetadata = False elif EQUAL(argv[i], "-norat"): bShowRAT = False elif EQUAL(argv[i], "-noct"): bShowColorTable = False elif EQUAL(argv[i], "-mdd") and i < nArgc - 1: i = i + 1 papszExtraMDDomains.append(argv[i]) elif EQUAL(argv[i], "-nofl"): bShowFileList = False elif argv[i][0] == '-': return Usage(argv[0]) elif pszFilename is None: pszFilename = argv[i] elif dst_img is None: dst_img = argv[i] else: return Usage(argv[0]) i = i + 1 if pszFilename is None: return Usage(argv[0]) if dst_img is None: return Usage(argv[0]) #/* -------------------------------------------------------------------- */ #/* Open dataset. */ #/* -------------------------------------------------------------------- */ hDataset = gdal.Open(pszFilename, gdal.GA_ReadOnly) if hDataset is None: print("gdalinfo failed - unable to open '%s'." % pszFilename) sys.exit(1) # Open the output file. if dst_img is not None: dst_lbl = dst_img.replace("IMG", "LBL") dst_lbl = dst_lbl.replace("img", "lbl") if (EQUAL(dst_lbl, dst_img)): print( 'Extension must be .IMG or .img - unable to run using filename: %s' % pszFilename) sys.exit(1) else: f = open(dst_lbl, 'wt') # else: # f = sys.stdout # dst_img = "out.img" #/* -------------------------------------------------------------------- */ #/* Report general info. */ #/* -------------------------------------------------------------------- */ hDriver = hDataset.GetDriver() if debug: print( "Driver: %s/%s" % ( \ hDriver.ShortName, \ hDriver.LongName )) papszFileList = hDataset.GetFileList() if papszFileList is None or len(papszFileList) == 0: print("Files: none associated") else: if debug: print("Files: %s" % papszFileList[0]) if bShowFileList: for i in range(1, len(papszFileList)): print(" %s" % papszFileList[i]) if debug: print("Size is %d, %d" % (hDataset.RasterXSize, hDataset.RasterYSize)) #/* -------------------------------------------------------------------- */ #/* Report projection. */ #/* -------------------------------------------------------------------- */ pszProjection = hDataset.GetProjectionRef() if pszProjection is not None: hSRS = osr.SpatialReference() if hSRS.ImportFromWkt(pszProjection) == gdal.CE_None: pszPrettyWkt = hSRS.ExportToPrettyWkt(False) #print( "Coordinate System is:\n%s" % pszPrettyWkt ) mapProjection = "None" #Extract projection information target = hSRS.GetAttrValue("DATUM", 0).replace("D_", "").replace("_2000", "") semiMajor = hSRS.GetSemiMajor() / 1000.0 semiMinor = hSRS.GetSemiMinor() / 1000.0 if (pszProjection[0:6] == "GEOGCS"): mapProjection = "SIMPLE_CYLINDRICAL" centLat = 0 centLon = 0 if (pszProjection[0:6] == "PROJCS"): mapProjection = hSRS.GetAttrValue("PROJECTION", 0) if EQUAL(mapProjection, "Equirectangular"): centLat = hSRS.GetProjParm('standard_parallel_1') centLon = hSRS.GetProjParm('central_meridian') if EQUAL(mapProjection, "Polar_Stereographic"): centLat = hSRS.GetProjParm('latitude_of_origin') centLon = hSRS.GetProjParm('central_meridian') if EQUAL(mapProjection, "Stereographic_South_Pole"): centLat = hSRS.GetProjParm('latitude_of_origin') centLon = hSRS.GetProjParm('central_meridian') if EQUAL(mapProjection, "Stereographic_North_Pole"): centLat = hSRS.GetProjParm('latitude_of_origin') centLon = hSRS.GetProjParm('central_meridian') if debug: print("Coordinate System is:\n%s" % pszPrettyWkt) else: print("Warning - Can't parse this type of projection\n") print("Coordinate System is `%s'" % pszProjection) sys.exit(1) else: print("Warning - No Coordinate System defined:\n") sys.exit(1) #/* -------------------------------------------------------------------- */ #/* Report Geotransform. */ #/* -------------------------------------------------------------------- */ adfGeoTransform = hDataset.GetGeoTransform(can_return_null=True) if adfGeoTransform is not None: if adfGeoTransform[2] == 0.0 and adfGeoTransform[4] == 0.0: if debug: print( "Origin = (%.15f,%.15f)" % ( \ adfGeoTransform[0], adfGeoTransform[3] )) print( "Pixel Size = (%.15f,%.15f)" % ( \ adfGeoTransform[1], adfGeoTransform[5] )) else: if debug: print( "GeoTransform =\n" \ " %.16g, %.16g, %.16g\n" \ " %.16g, %.16g, %.16g" % ( \ adfGeoTransform[0], \ adfGeoTransform[1], \ adfGeoTransform[2], \ adfGeoTransform[3], \ adfGeoTransform[4], \ adfGeoTransform[5] )) if (pszProjection[0:6] == "GEOGCS"): #convert degrees/pixel to km/pixel mapres = 1 / adfGeoTransform[1] kmres = adfGeoTransform[1] * (semiMajor * math.pi / 180.0) else: #convert m/pixel to pixel/degree mapres = 1 / (adfGeoTransform[1] / (semiMajor * 1000.0 * math.pi / 180.0)) kmres = adfGeoTransform[1] / 1000.0 #/* -------------------------------------------------------------------- */ #/* Report GCPs. */ #/* -------------------------------------------------------------------- */ if bShowGCPs and hDataset.GetGCPCount() > 0: pszProjection = hDataset.GetGCPProjection() if pszProjection is not None: hSRS = osr.SpatialReference() if hSRS.ImportFromWkt(pszProjection) == gdal.CE_None: pszPrettyWkt = hSRS.ExportToPrettyWkt(False) if debug: print("GCP Projection = \n%s" % pszPrettyWkt) else: if debug: print( "GCP Projection = %s" % \ pszProjection ) gcps = hDataset.GetGCPs() i = 0 for gcp in gcps: if debug: print( "GCP[%3d]: Id=%s, Info=%s\n" \ " (%.15g,%.15g) -> (%.15g,%.15g,%.15g)" % ( \ i, gcp.Id, gcp.Info, \ gcp.GCPPixel, gcp.GCPLine, \ gcp.GCPX, gcp.GCPY, gcp.GCPZ )) i = i + 1 #/* -------------------------------------------------------------------- */ #/* Report metadata. */ #/* -------------------------------------------------------------------- */ if debug: if bShowMetadata: papszMetadata = hDataset.GetMetadata_List() else: papszMetadata = None if bShowMetadata and papszMetadata is not None and len( papszMetadata) > 0: print("Metadata:") for metadata in papszMetadata: print(" %s" % metadata) if bShowMetadata: for extra_domain in papszExtraMDDomains: papszMetadata = hDataset.GetMetadata_List(extra_domain) if papszMetadata is not None and len(papszMetadata) > 0: print("Metadata (%s):" % extra_domain) for metadata in papszMetadata: print(" %s" % metadata) #/* -------------------------------------------------------------------- */ #/* Report "IMAGE_STRUCTURE" metadata. */ #/* -------------------------------------------------------------------- */ if bShowMetadata: papszMetadata = hDataset.GetMetadata_List("IMAGE_STRUCTURE") else: papszMetadata = None if bShowMetadata and papszMetadata is not None and len( papszMetadata) > 0: print("Image Structure Metadata:") for metadata in papszMetadata: print(" %s" % metadata) #/* -------------------------------------------------------------------- */ #/* Report subdatasets. */ #/* -------------------------------------------------------------------- */ papszMetadata = hDataset.GetMetadata_List("SUBDATASETS") if papszMetadata is not None and len(papszMetadata) > 0: print("Subdatasets:") for metadata in papszMetadata: print(" %s" % metadata) #/* -------------------------------------------------------------------- */ #/* Report geolocation. */ #/* -------------------------------------------------------------------- */ if bShowMetadata: papszMetadata = hDataset.GetMetadata_List("GEOLOCATION") else: papszMetadata = None if bShowMetadata and papszMetadata is not None and len( papszMetadata) > 0: print("Geolocation:") for metadata in papszMetadata: print(" %s" % metadata) #/* -------------------------------------------------------------------- */ #/* Report RPCs */ #/* -------------------------------------------------------------------- */ if bShowMetadata: papszMetadata = hDataset.GetMetadata_List("RPC") else: papszMetadata = None if bShowMetadata and papszMetadata is not None and len( papszMetadata) > 0: print("RPC Metadata:") for metadata in papszMetadata: print(" %s" % metadata) #/* -------------------------------------------------------------------- */ #/* Setup projected to lat/long transform if appropriate. */ #/* -------------------------------------------------------------------- */ if pszProjection is not None and len(pszProjection) > 0: hProj = osr.SpatialReference(pszProjection) if hProj is not None: hLatLong = hProj.CloneGeogCS() if hLatLong is not None: gdal.PushErrorHandler('CPLQuietErrorHandler') hTransform = osr.CoordinateTransformation(hProj, hLatLong) gdal.PopErrorHandler() if gdal.GetLastErrorMsg().find( 'Unable to load PROJ.4 library') != -1: hTransform = None #/* -------------------------------------------------------------------- */ #/* Report corners. */ #/* -------------------------------------------------------------------- */ if debug: print("Corner Coordinates:") GDALInfoReportCorner( hDataset, hTransform, "Upper Left", \ 0.0, 0.0 ) GDALInfoReportCorner( hDataset, hTransform, "Lower Left", \ 0.0, hDataset.RasterYSize) GDALInfoReportCorner( hDataset, hTransform, "Upper Right", \ hDataset.RasterXSize, 0.0 ) GDALInfoReportCorner( hDataset, hTransform, "Lower Right", \ hDataset.RasterXSize, \ hDataset.RasterYSize ) GDALInfoReportCorner( hDataset, hTransform, "Center", \ hDataset.RasterXSize/2.0, \ hDataset.RasterYSize/2.0 ) #Get bounds ulx = GDALGetLon(hDataset, hTransform, 0.0, 0.0) uly = GDALGetLat(hDataset, hTransform, 0.0, 0.0) lrx = GDALGetLon( hDataset, hTransform, hDataset.RasterXSize, \ hDataset.RasterYSize ) lry = GDALGetLat( hDataset, hTransform, hDataset.RasterXSize, \ hDataset.RasterYSize ) #/* ==================================================================== */ #/* Loop over bands. */ #/* ==================================================================== */ if debug: bands = hDataset.RasterCount for iBand in range(hDataset.RasterCount): hBand = hDataset.GetRasterBand(iBand + 1) #if( bSample ) #{ # float afSample[10000]; # int nCount; # # nCount = GDALGetRandomRasterSample( hBand, 10000, afSample ); # print( "Got %d samples.\n", nCount ); #} (nBlockXSize, nBlockYSize) = hBand.GetBlockSize() print( "Band %d Block=%dx%d Type=%s, ColorInterp=%s" % ( iBand+1, \ nBlockXSize, nBlockYSize, \ gdal.GetDataTypeName(hBand.DataType), \ gdal.GetColorInterpretationName( \ hBand.GetRasterColorInterpretation()) )) if hBand.GetDescription() is not None \ and len(hBand.GetDescription()) > 0 : print(" Description = %s" % hBand.GetDescription()) dfMin = hBand.GetMinimum() dfMax = hBand.GetMaximum() if dfMin is not None or dfMax is not None or bComputeMinMax: line = " " if dfMin is not None: line = line + ("Min=%.3f " % dfMin) if dfMax is not None: line = line + ("Max=%.3f " % dfMax) if bComputeMinMax: gdal.ErrorReset() adfCMinMax = hBand.ComputeRasterMinMax(False) if gdal.GetLastErrorType() == gdal.CE_None: line = line + ( " Computed Min/Max=%.3f,%.3f" % ( \ adfCMinMax[0], adfCMinMax[1] )) print(line) stats = hBand.GetStatistics(bApproxStats, bStats) # Dirty hack to recognize if stats are valid. If invalid, the returned # stddev is negative if stats[3] >= 0.0: print( " Minimum=%.3f, Maximum=%.3f, Mean=%.3f, StdDev=%.3f" % ( \ stats[0], stats[1], stats[2], stats[3] )) if bReportHistograms: hist = hBand.GetDefaultHistogram(force=True, callback=gdal.TermProgress) if hist is not None: dfMin = hist[0] dfMax = hist[1] nBucketCount = hist[2] panHistogram = hist[3] print( " %d buckets from %g to %g:" % ( \ nBucketCount, dfMin, dfMax )) line = ' ' for bucket in panHistogram: line = line + ("%d " % bucket) print(line) if bComputeChecksum: print(" Checksum=%d" % hBand.Checksum()) dfNoData = hBand.GetNoDataValue() if dfNoData is not None: if dfNoData != dfNoData: print(" NoData Value=nan") else: print(" NoData Value=%.18g" % dfNoData) if hBand.GetOverviewCount() > 0: line = " Overviews: " for iOverview in range(hBand.GetOverviewCount()): if iOverview != 0: line = line + ", " hOverview = hBand.GetOverview(iOverview) if hOverview is not None: line = line + ("%dx%d" % (hOverview.XSize, hOverview.YSize)) pszResampling = \ hOverview.GetMetadataItem( "RESAMPLING", "" ) if pszResampling is not None \ and len(pszResampling) >= 12 \ and EQUAL(pszResampling[0:12],"AVERAGE_BIT2"): line = line + "*" else: line = line + "(null)" print(line) if bComputeChecksum: line = " Overviews checksum: " for iOverview in range(hBand.GetOverviewCount()): if iOverview != 0: line = line + ", " hOverview = hBand.GetOverview(iOverview) if hOverview is not None: line = line + ("%d" % hOverview.Checksum()) else: line = line + "(null)" print(line) if hBand.HasArbitraryOverviews(): print(" Overviews: arbitrary") nMaskFlags = hBand.GetMaskFlags() if (nMaskFlags & (gdal.GMF_NODATA | gdal.GMF_ALL_VALID)) == 0: hMaskBand = hBand.GetMaskBand() line = " Mask Flags: " if (nMaskFlags & gdal.GMF_PER_DATASET) != 0: line = line + "PER_DATASET " if (nMaskFlags & gdal.GMF_ALPHA) != 0: line = line + "ALPHA " if (nMaskFlags & gdal.GMF_NODATA) != 0: line = line + "NODATA " if (nMaskFlags & gdal.GMF_ALL_VALID) != 0: line = line + "ALL_VALID " print(line) if hMaskBand is not None and \ hMaskBand.GetOverviewCount() > 0: line = " Overviews of mask band: " for iOverview in range(hMaskBand.GetOverviewCount()): if iOverview != 0: line = line + ", " hOverview = hMaskBand.GetOverview(iOverview) if hOverview is not None: line = line + ("%d" % hOverview.Checksum()) else: line = line + "(null)" if len(hBand.GetUnitType()) > 0: print(" Unit Type: %s" % hBand.GetUnitType()) papszCategories = hBand.GetRasterCategoryNames() if papszCategories is not None: print(" Categories:") i = 0 for category in papszCategories: print(" %3d: %s" % (i, category)) i = i + 1 if hBand.GetScale() != 1.0 or hBand.GetOffset() != 0.0: print( " Offset: %.15g, Scale:%.15g" % \ ( hBand.GetOffset(), hBand.GetScale())) if bShowMetadata: papszMetadata = hBand.GetMetadata_List() else: papszMetadata = None if bShowMetadata and papszMetadata is not None and len( papszMetadata) > 0: print(" Metadata:") for metadata in papszMetadata: print(" %s" % metadata) if bShowMetadata: papszMetadata = hBand.GetMetadata_List("IMAGE_STRUCTURE") else: papszMetadata = None if bShowMetadata and papszMetadata is not None and len( papszMetadata) > 0: print(" Image Structure Metadata:") for metadata in papszMetadata: print(" %s" % metadata) hTable = hBand.GetRasterColorTable() if hBand.GetRasterColorInterpretation() == gdal.GCI_PaletteIndex \ and hTable is not None: print( " Color Table (%s with %d entries)" % (\ gdal.GetPaletteInterpretationName( \ hTable.GetPaletteInterpretation( )), \ hTable.GetCount() )) if bShowColorTable: for i in range(hTable.GetCount()): sEntry = hTable.GetColorEntry(i) print( " %3d: %d,%d,%d,%d" % ( \ i, \ sEntry[0],\ sEntry[1],\ sEntry[2],\ sEntry[3] )) if bShowRAT: hRAT = hBand.GetDefaultRAT() #GDALRATDumpReadable( hRAT, None ); #/************************************************************************/ #/* WritePDSlabel() */ #/************************************************************************/ #def WritePDSlabel(outFile, DataSetID, pszFilename, sampleBits, lines, samples): instrList = pszFilename.split("_") hBand = hDataset.GetRasterBand(1) #get the datatype if EQUAL(gdal.GetDataTypeName(hBand.DataType), "Float32"): sample_bits = 32 sample_type = "PC_REAL" sample_mask = "2#11111111111111111111111111111111#" elif EQUAL(gdal.GetDataTypeName(hBand.DataType), "INT16"): sample_bits = 16 sample_type = "LSB_INTEGER" sample_mask = "2#1111111111111111#" elif EQUAL(gdal.GetDataTypeName(hBand.DataType), "UINT16"): sample_bits = 16 sample_type = "UNSIGNED_INTEGER" sample_mask = "2#1111111111111111#" elif EQUAL(gdal.GetDataTypeName(hBand.DataType), "Byte"): sample_bits = 8 sample_type = "UNSIGNED_INTEGER" sample_mask = "2#11111111#" else: print(" %s: Not supported pixel type" % gdal.GetDataTypeName(hBand.DataType)) sys.exit(1) f.write('PDS_VERSION_ID = PDS3\n') f.write('\n') f.write('/* The source image data definition. */\n') f.write('FILE_NAME = \"%s\"\n' % (dst_img)) f.write('RECORD_TYPE = FIXED_LENGTH\n') f.write('RECORD_BYTES = %d\n' % (hDataset.RasterYSize)) f.write('FILE_RECORDS = %d\n' % ((hDataset.RasterXSize * sample_bits / 8))) #f.write('LABEL_RECORDS = 1\n') f.write('^IMAGE = \"%s\"\n' % (dst_img)) f.write('\n') f.write('/* Identification Information */\n') f.write('DATA_SET_ID = "%s"\n' % pszFilename.split(".")[0]) f.write('DATA_SET_NAME = "%s"\n' % pszFilename.split(".")[0]) f.write( 'PRODUCER_INSTITUTION_NAME = "Lunar Mapping and Modeling Project"\n') f.write('PRODUCER_ID = "LMMP_TEAM"\n') f.write('PRODUCER_FULL_NAME = "LMMP TEAM"\n') f.write('PRODUCT_ID = "%s"\n' % pszFilename.split(".")[0]) if "_v" in pszFilename: f.write('PRODUCT_VERSION_ID = "%s.0"\n' % instrList[-1].split(".")[0].upper()) else: f.write('PRODUCT_VERSION_ID = "%s"\n' % "V1.0") f.write('PRODUCT_TYPE = "RDR"\n') f.write('INSTRUMENT_HOST_NAME = "%s"\n' % instrList[0]) f.write('INSTRUMENT_HOST_ID = "%s"\n' % instrList[0]) f.write('INSTRUMENT_NAME = "%s"\n' % instrList[1]) f.write('INSTRUMENT_ID = "%s"\n' % instrList[1]) f.write('TARGET_NAME = MOON\n') f.write('MISSION_PHASE_NAME = "POST MISSION"\n') f.write( 'RATIONALE_DESC = "Created at the request of NASA\'s Exploration\n' ) f.write( ' Systems Mission Directorate to support future\n' ) f.write(' human exploration"\n') f.write( 'SOFTWARE_NAME = "ISIS 3.2.1 | SOCET SET v5.5 (r) BAE Systems\n' ) f.write(' | GDAL 1.8"\n') f.write('\n') f.write('/* Time Parameters */\n') f.write('START_TIME = "N/A"\n') f.write('STOP_TIME = "N/A"\n') f.write('SPACECRAFT_CLOCK_START_COUNT = "N/A"\n') f.write('SPACECRAFT_CLOCK_STOP_COUNT = "N/A"\n') f.write('PRODUCT_CREATION_TIME = %s\n' % strftime("%Y-%m-%dT%H:%M:%S")) #2011-03-11T22:13:40 f.write('\n') f.write('OBJECT = IMAGE_MAP_PROJECTION\n') f.write(' ^DATA_SET_MAP_PROJECTION = "DSMAP.CAT"\n') f.write(' MAP_PROJECTION_TYPE = \"%s\"\n' % mapProjection) f.write(' PROJECTION_LATITUDE_TYPE = PLANETOCENTRIC\n') f.write(' A_AXIS_RADIUS = %.1f <KM>\n' % semiMajor) f.write(' B_AXIS_RADIUS = %.1f <KM>\n' % semiMajor) f.write(' C_AXIS_RADIUS = %.1f <KM>\n' % semiMinor) f.write(' COORDINATE_SYSTEM_NAME = PLANETOCENTRIC\n') f.write(' POSITIVE_LONGITUDE_DIRECTION = EAST\n') f.write(' KEYWORD_LATITUDE_TYPE = PLANETOCENTRIC\n') f.write( ' /* NOTE: CENTER_LATITUDE and CENTER_LONGITUDE describe the location */\n' ) f.write( ' /* of the center of projection, which is not necessarily equal to the */\n' ) f.write( ' /* location of the center point of the image. */\n' ) f.write(' CENTER_LATITUDE = %5.2f <DEG>\n' % centLat) f.write(' CENTER_LONGITUDE = %5.2f <DEG>\n' % centLon) f.write(' LINE_FIRST_PIXEL = 1\n') f.write(' LINE_LAST_PIXEL = %d\n' % hDataset.RasterYSize) f.write(' SAMPLE_FIRST_PIXEL = 1\n') f.write(' SAMPLE_LAST_PIXEL = %d\n' % hDataset.RasterXSize) f.write(' MAP_PROJECTION_ROTATION = 0.0 <DEG>\n') f.write(' MAP_RESOLUTION = %.4f <PIX/DEG>\n' % mapres) f.write(' MAP_SCALE = %.8f <KM/PIXEL>\n' % kmres) f.write(' MINIMUM_LATITUDE = %.8f <DEGREE>\n' % lry) f.write(' MAXIMUM_LATITUDE = %.8f <DEGREE>\n' % uly) f.write(' WESTERNMOST_LONGITUDE = %.8f <DEGREE>\n' % ulx) f.write(' EASTERNMOST_LONGITUDE = %.8f <DEGREE>\n' % lrx) f.write(' LINE_PROJECTION_OFFSET = %.1f\n' % ((ulx / kmres * 1000) - 0.5)) f.write(' SAMPLE_PROJECTION_OFFSET = %.1f\n' % ((uly / kmres * 1000) + 0.5)) f.write('END_OBJECT = IMAGE_MAP_PROJECTION\n') f.write('\n') f.write('OBJECT = IMAGE\n') f.write(' NAME = \"%s\"\n' % (pszFilename)) f.write( ' DESCRIPTION = "Export data set from LMMP portal.\n' ) f.write(' see filename for data type."\n') #f.write('\n') f.write(' LINES = %d\n' % hDataset.RasterYSize) f.write(' LINE_SAMPLES = %d\n' % hDataset.RasterXSize) f.write(' UNIT = METER\n') f.write(' OFFSET = %.10g\n' % (hBand.GetOffset())) f.write(' SCALING_FACTOR = %.10g\n' % (hBand.GetScale())) f.write(' SAMPLE_TYPE = %s\n' % (sample_type)) f.write(' SAMPLE_BITS = %d\n' % (sample_bits)) f.write(' SAMPLE_BIT_MASK = %s\n' % (sample_mask)) #f.write('\n') f.write(' BANDS = %d\n' % hDataset.RasterCount) #f.write('\n') f.write(' BAND_STORAGE_TYPE = BAND_SEQUENTIAL\n') if (sample_bits == 32): f.write(' CORE_NULL = 16#FF7FFFFB#\n') f.write(' CORE_LOW_REPR_SATURATION = 16#FF7FFFFC#\n') f.write(' CORE_LOW_INSTR_SATURATION = 16#FF7FFFFD#\n') f.write(' CORE_HIGH_REPR_SATURATION = 16#FF7FFFFF#\n') f.write(' CORE_HIGH_INSTR_SATURATION = 16#FF7FFFFE#\n') elif (sample_bits == 16): f.write(' CORE_NULL = -32768\n') f.write(' CORE_LOW_REPR_SATURATION = -32767\n') f.write(' CORE_LOW_INSTR_SATURATION = -32766\n') f.write(' CORE_HIGH_REPR_SATURATION = 32767\n') f.write(' CORE_HIGH_INSTR_SATURATION = 32768\n') else: #8bit f.write(' CORE_NULL = 0\n') f.write(' CORE_LOW_REPR_SATURATION = 0\n') f.write(' CORE_LOW_INSTR_SATURATION = 0\n') f.write(' CORE_HIGH_REPR_SATURATION = 255\n') f.write(' CORE_HIGH_INSTR_SATURATION = 255\n') f.write('END_OBJECT = IMAGE\n') f.write('END\n') f.close() ######################### #Export out raw image ######################### #Setup the output dataset print('Please wait, writing out raw image: %s' % dst_img) driver = gdal.GetDriverByName('ENVI') output = driver.CreateCopy(dst_img, hDataset, 1) print('Complete. PDS label also created: %s' % dst_lbl) return 0
def write_to_file(self, filename, sublayer=None): """Save vector data to file :param filename: filename with extension .shp or .gml :type filename: str :param sublayer: Optional parameter for writing a sublayer. Ignored unless we are writing to an sqlite file. :type sublayer: str :raises: WriteLayerError Note: Shp limitation, if attribute names are longer than 10 characters they will be truncated. This is due to limitations in the shp file driver and has to be done here since gdal v1.7 onwards has changed its handling of this issue: http://www.gdal.org/ogr/drv_shapefile.html **For this reason we recommend writing to spatialite.** """ # Check file format base_name, extension = os.path.splitext(filename) msg = ('Invalid file type for file %s. Only extensions ' 'sqlite, shp or gml allowed.' % filename) verify(extension in ['.sqlite', '.shp', '.gml'], msg) driver = DRIVER_MAP[extension] # FIXME (Ole): Tempory flagging of GML issue (ticket #18) if extension == '.gml': msg = ('OGR GML driver does not store geospatial reference.' 'This format is disabled for the time being. See ' 'https://github.com/AIFDR/riab/issues/18') raise WriteLayerError(msg) # Derive layer_name from filename (excluding preceding dirs) if sublayer is None or extension == '.shp': layer_name = os.path.split(base_name)[-1] else: layer_name = sublayer # Get vector data if self.is_polygon_data: geometry = self.get_geometry(as_geometry_objects=True) else: geometry = self.get_geometry() data = self.get_data() N = len(geometry) # Clear any previous file of this name (ogr does not overwrite) try: os.remove(filename) except OSError: pass # Create new file with one layer drv = ogr.GetDriverByName(driver) if drv is None: msg = 'OGR driver %s not available' % driver raise WriteLayerError(msg) ds = drv.CreateDataSource(filename) if ds is None: msg = 'Creation of output file %s failed' % filename raise WriteLayerError(msg) lyr = ds.CreateLayer(layer_name, self.projection.spatial_reference, self.geometry_type) if lyr is None: msg = 'Could not create layer %s' % layer_name raise WriteLayerError(msg) # Define attributes if any store_attributes = False fields = [] if data is not None: if len(data) > 0: try: fields = data[0].keys() except: msg = ('Input parameter "attributes" was specified ' 'but it does not contain list of dictionaries ' 'with field information as expected. The first ' 'element is %s' % data[0]) raise WriteLayerError(msg) else: # Establish OGR types for each element ogr_types = {} for name in fields: att = data[0][name] py_type = type(att) msg = ('Unknown type for storing vector ' 'data: %s, %s' % (name, str(py_type)[1:-1])) verify(py_type in TYPE_MAP, msg) ogr_types[name] = TYPE_MAP[py_type] else: # msg = ('Input parameter "data" was specified ' # 'but appears to be empty') # raise InaSAFEError(msg) pass # Create attribute fields in layer store_attributes = True for name in fields: fd = ogr.FieldDefn(name, ogr_types[name]) # FIXME (Ole): Trying to address issue #16 # But it doesn't work and # somehow changes the values of MMI in test # width = max(128, len(name)) # print name, width # fd.SetWidth(width) # Silent handling of warnings like # Warning 6: Normalized/laundered field name: # 'CONTENTS_LOSS_AUD' to 'CONTENTS_L' gdal.PushErrorHandler('CPLQuietErrorHandler') if lyr.CreateField(fd) != 0: msg = 'Could not create field %s' % name raise WriteLayerError(msg) # Restore error handler gdal.PopErrorHandler() # Store geometry geom = ogr.Geometry(self.geometry_type) layer_def = lyr.GetLayerDefn() for i in range(N): # Create new feature instance feature = ogr.Feature(layer_def) # Store geometry and check if self.is_point_data: x = float(geometry[i][0]) y = float(geometry[i][1]) geom.SetPoint_2D(0, x, y) elif self.is_line_data: geom = array_to_line(geometry[i], geometry_type=ogr.wkbLineString) elif self.is_polygon_data: # Create polygon geometry geom = ogr.Geometry(ogr.wkbPolygon) # Add outer ring linear_ring = array_to_line(geometry[i].outer_ring, geometry_type=ogr.wkbLinearRing) geom.AddGeometry(linear_ring) # Add inner rings if any for A in geometry[i].inner_rings: geom.AddGeometry( array_to_line(A, geometry_type=ogr.wkbLinearRing)) else: msg = 'Geometry type %s not implemented' % self.geometry_type raise WriteLayerError(msg) feature.SetGeometry(geom) G = feature.GetGeometryRef() if G is None: msg = 'Could not create GeometryRef for file %s' % filename raise WriteLayerError(msg) # Store attributes if store_attributes: for j, name in enumerate(fields): actual_field_name = layer_def.GetFieldDefn(j).GetNameRef() val = data[i][name] if type(val) == numpy.ndarray: # A singleton of type <type 'numpy.ndarray'> works # for gdal version 1.6 but fails for version 1.8 # in SetField with error: NotImplementedError: # Wrong number of arguments for overloaded function val = float(val) elif val is None: val = '' # We do this because there is NaN problem on windows # NaN value must be converted to _pseudo_in to solve the # problem. But, when InaSAFE read the file, it'll be # converted back to NaN value, so that NaN in InaSAFE is a # numpy.nan # please check https://github.com/AIFDR/inasafe/issues/269 # for more information if val != val: val = _pseudo_inf feature.SetField(actual_field_name, val) # Save this feature if lyr.CreateFeature(feature) != 0: msg = 'Failed to create feature %i in file %s' % (i, filename) raise WriteLayerError(msg) feature.Destroy() # Write keywords if any write_keywords(self.keywords, base_name + '.keywords')
def ogr_wkbwkt_test_broken_geom(): list_broken = [ 'POINT', 'POINT UNKNOWN', 'POINT(', 'POINT()', 'POINT(,)', 'POINT(EMPTY', 'POINT(A)', 'POINT(0)', 'POINT(A 0)', 'POINT(0 A)', 'POINT(0 1', 'POINT(0 1,', 'POINT((0 1))', 'POINT Z', 'POINT Z UNKNOWN', 'POINT Z(', 'POINT Z()', 'POINT Z(EMPTY)', 'POINT Z(A)', 'POINT Z(0 1', 'LINESTRING', 'LINESTRING UNKNOWN', 'LINESTRING(', 'LINESTRING()', 'LINESTRING(,)', 'LINESTRING(())', 'LINESTRING(EMPTY', 'LINESTRING(A)', 'LINESTRING(0 1,', 'LINESTRING(0 1,2 3', 'LINESTRING(0 1,,2 3)', 'LINESTRING((0 1,2 3))', 'LINESTRING Z', 'LINESTRING Z UNKNOWN', 'LINESTRING Z(', 'LINESTRING Z()', 'LINESTRING Z(EMPTY)', 'LINESTRING Z(A)', 'LINESTRING Z(0 1', 'LINESTRING Z(0 1,2 3', 'POLYGON', 'POLYGON UNKNOWN', 'POLYGON(', 'POLYGON()', 'POLYGON(,)', 'POLYGON(())', 'POLYGON(EMPTY', 'POLYGON(A)', 'POLYGON(0 1)', 'POLYGON(0 1,2 3', 'POLYGON((0 1,2 3', 'POLYGON((0 1,2 3,', 'POLYGON((0 1,2 3)', 'POLYGON((0 1,2 3),', 'POLYGON((0 1,2 3),EMPTY', 'POLYGON(((0 1,2 3)))', 'POLYGON Z', 'POLYGON Z UNKNOWN', 'POLYGON Z(', 'POLYGON Z()', 'POLYGON Z(EMPTY', 'POLYGON Z(A)', 'POLYGON Z(0 1', 'POLYGON Z(0 1,2 3', 'POLYGON Z((0 1,2 3', 'POLYGON Z((0 1,2 3)', 'POLYGON Z(((0 1,2 3)))', 'MULTIPOINT', 'MULTIPOINT UNKNOWN', 'MULTIPOINT(', 'MULTIPOINT()', 'MULTIPOINT(())', 'MULTIPOINT(EMPTY', 'MULTIPOINT(EMPTY,', 'MULTIPOINT(EMPTY,(0 1)', 'MULTIPOINT(A)', 'MULTIPOINT(0 1', 'MULTIPOINT(0 1,', 'MULTIPOINT(0 1,2 3', 'MULTIPOINT((0 1),,(2 3))', 'MULTIPOINT(0 1,EMPTY', 'MULTIPOINT((0 1),EMPTY', 'MULTIPOINT((0 1)', #'MULTIPOINT(0 1,2 3)', # This one is not SF compliant but supported for legacy 'MULTIPOINT((0 1),(2 3)', 'MULTIPOINT Z', 'MULTIPOINT Z UNKNOWN', 'MULTIPOINT Z(', 'MULTIPOINT Z()', 'MULTIPOINT Z(EMPTY', 'MULTIPOINT Z(A)', 'MULTIPOINT Z(0 1', 'MULTIPOINT Z((0 1)', 'MULTIPOINT Z(0 1,2 3)', 'MULTILINESTRING', 'MULTILINESTRING UNKNOWN', 'MULTILINESTRING(', 'MULTILINESTRING()', 'MULTILINESTRING(,)', 'MULTILINESTRING(())', 'MULTILINESTRING(EMPTY', 'MULTILINESTRING(EMPTY,', 'MULTILINESTRING(A)', 'MULTILINESTRING(0 1', 'MULTILINESTRING(0 1,', 'MULTILINESTRING(0 1,2 3)', 'MULTILINESTRING((0 1,2 3', 'MULTILINESTRING((0 1,2 3),)', 'MULTILINESTRING((0 1)', 'MULTILINESTRING((0 1),', 'MULTILINESTRING((0 1),EMPTY', 'MULTILINESTRING((0 1),(2 3)', 'MULTILINESTRING Z', 'MULTILINESTRING Z UNKNOWN', 'MULTILINESTRING Z(', 'MULTILINESTRING Z()', 'MULTILINESTRING Z(EMPTY', 'MULTILINESTRING Z(A)', 'MULTILINESTRING Z(0 1', 'MULTILINESTRING Z((0 1)', 'MULTILINESTRING Z((0 1),(2 3)', 'MULTIPOLYGON', 'MULTIPOLYGON UNKNOWN', 'MULTIPOLYGON(', 'MULTIPOLYGON()', 'MULTIPOLYGON(,)', 'MULTIPOLYGON(())', 'MULTIPOLYGON((()))', 'MULTIPOLYGON(EMPTY', 'MULTIPOLYGON(EMPTY,', 'MULTIPOLYGON(A)', 'MULTIPOLYGON(0 1', 'MULTIPOLYGON(0 1,', 'MULTIPOLYGON(0 1,2 3)', 'MULTIPOLYGON((0 1,2 3', 'MULTIPOLYGON((0 1,2 3),)', 'MULTIPOLYGON((0 1)', 'MULTIPOLYGON((0 1),', 'MULTIPOLYGON((0 1),EMPTY', 'MULTIPOLYGON((0 1),(2 3)', 'MULTIPOLYGON((0 1),(2 3))', 'MULTIPOLYGON(((0 1))', 'MULTIPOLYGON(((0 1)),', 'MULTIPOLYGON(((0 1)),,', 'MULTIPOLYGON(((0 1),(2 3))', 'MULTIPOLYGON(((0 1),EMPTY', 'MULTIPOLYGON(((0 1),EMPTY,', 'MULTIPOLYGON((((0 1)),)', 'MULTIPOLYGON Z', 'MULTIPOLYGON Z UNKNOWN', 'MULTIPOLYGON Z(', 'MULTIPOLYGON Z()', 'MULTIPOLYGON Z(EMPTY', 'MULTIPOLYGON Z(A)', 'MULTIPOLYGON Z(0 1', 'MULTIPOLYGON Z((0 1)', 'MULTIPOLYGON Z((0 1),(2 3)', 'GEOMETRYCOLLECTION', 'GEOMETRYCOLLECTION UNKNOWN', 'GEOMETRYCOLLECTION(', 'GEOMETRYCOLLECTION()', 'GEOMETRYCOLLECTION(,)', 'GEOMETRYCOLLECTION(())', 'GEOMETRYCOLLECTION(EMPTY', 'GEOMETRYCOLLECTION(EMPTY,', 'GEOMETRYCOLLECTION(A)', 'GEOMETRYCOLLECTION(POINT(0 1)', 'GEOMETRYCOLLECTION(POINT(0 1),', 'GEOMETRYCOLLECTION(POINT(0 1),)', 'GEOMETRYCOLLECTION(POINT(0 1),UNKNOWN)', 'GEOMETRYCOLLECTION Z', 'GEOMETRYCOLLECTION Z(', 'GEOMETRYCOLLECTION Z()', 'GEOMETRYCOLLECTION Z(EMPTY', 'GEOMETRYCOLLECTION Z(POINT(0 1)', 'COMPOUNDCURVE', 'COMPOUNDCURVE UNKNOWN', 'COMPOUNDCURVE(', 'COMPOUNDCURVE()', 'COMPOUNDCURVE(,)', 'COMPOUNDCURVE(())', 'COMPOUNDCURVE(EMPTY', 'COMPOUNDCURVE(EMPTY,', 'COMPOUNDCURVE(A)', 'COMPOUNDCURVE((0 1,2 3', 'COMPOUNDCURVE((0 1,2 3)', 'COMPOUNDCURVE((0 1,2 3)', 'COMPOUNDCURVE((0 1,2 3),', 'COMPOUNDCURVE((0 1,2 3),)', 'COMPOUNDCURVE((0 1,2 3),UNKNOWN)', 'COMPOUNDCURVE Z', 'COMPOUNDCURVE Z(', 'COMPOUNDCURVE Z()', 'COMPOUNDCURVE Z(EMPTY', 'COMPOUNDCURVE Z((0 1,2 3)', 'CURVEPOLYGON', 'CURVEPOLYGON UNKNOWN', 'CURVEPOLYGON(', 'CURVEPOLYGON()', 'CURVEPOLYGON(,)', 'CURVEPOLYGON(())', 'CURVEPOLYGON(EMPTY', 'CURVEPOLYGON(EMPTY,', 'CURVEPOLYGON(A)', 'CURVEPOLYGON((0 1,2 3', 'CURVEPOLYGON((0 1,2 3)', 'CURVEPOLYGON((0 1,2 3)', 'CURVEPOLYGON((0 1,2 3),', 'CURVEPOLYGON((0 1,2 3),)', 'CURVEPOLYGON((0 1,2 3),UNKNOWN)', 'CURVEPOLYGON Z', 'CURVEPOLYGON Z(', 'CURVEPOLYGON Z()', 'CURVEPOLYGON Z(EMPTY', 'CURVEPOLYGON Z((0 1,2 3)', ] for wkt in list_broken: gdal.PushErrorHandler('CPLQuietErrorHandler') geom = ogr.CreateGeometryFromWkt(wkt) gdal.PopErrorHandler() if geom is not None: gdaltest.post_reason('geom %s instantiated but not expected' % wkt) return 'fail' return 'success'
if mask is 'default': maskband = srcband.GetMaskBand() elif mask is 'none': maskband = None else: mask_ds = gdal.Open(mask) maskband = mask_ds.GetRasterBand(1) # ============================================================================= # Try opening the destination file as an existing file. # ============================================================================= try: gdal.PushErrorHandler('CPLQuietErrorHandler') dst_ds = ogr.Open(dst_filename, update=1) gdal.PopErrorHandler() except: dst_ds = None # ============================================================================= # Create output file. # ============================================================================= if dst_ds is None: drv = ogr.GetDriverByName(format) if not quiet_flag: print('Creating output %s of format %s.' % (dst_filename, format)) dst_ds = drv.CreateDataSource(dst_filename) # ============================================================================= # Find or create destination layer. # =============================================================================
def ogr_sql_46(): ds = ogr.GetDriverByName('Memory').CreateDataSource('test') lyr = ds.CreateLayer('test') lyr.CreateField(ogr.FieldDefn('id', ogr.OFTInteger)) lyr.CreateField(ogr.FieldDefn('from', ogr.OFTString)) feat = ogr.Feature(lyr.GetLayerDefn()) feat.SetField(0, 1) feat.SetField(1, "not_from") lyr.CreateFeature(feat) feat = ogr.Feature(lyr.GetLayerDefn()) feat.SetField(0, 3) feat.SetField(1, "from") lyr.CreateFeature(feat) feat = None sql_lyr = ds.ExecuteSQL("select id, 'id', \"id\" as id2, id as \"id3\", \"from\" from test where \"from\" = 'from'") feat = sql_lyr.GetNextFeature() if feat.GetField(0) != 3 or feat.GetField(1) != 'id' or feat.GetField(2) != 3 or feat.GetField(3) != 3 or feat.GetField(4) != 'from': gdaltest.post_reason('fail') feat.DumpReadable() return 'fail' feat = sql_lyr.GetNextFeature() if feat is not None: gdaltest.post_reason('fail') return 'fail' ds.ReleaseResultSet(sql_lyr) sql_lyr = ds.ExecuteSQL("select max(\"id\"), max(id), count(\"id\"), count(id) from \"test\"") feat = sql_lyr.GetNextFeature() if feat.GetField(0) != 3 or feat.GetField(1) != 3 or feat.GetField(2) != 2 or feat.GetField(3) != 2: gdaltest.post_reason('fail') feat.DumpReadable() return 'fail' ds.ReleaseResultSet(sql_lyr) # Not accepted gdal.PushErrorHandler() sql_lyr = ds.ExecuteSQL("select * from 'test'") gdal.PopErrorHandler() if sql_lyr is not None: gdaltest.post_reason('fail') return 'fail' # Not accepted gdal.PushErrorHandler() sql_lyr = ds.ExecuteSQL("select distinct 'id' from 'test'") gdal.PopErrorHandler() if sql_lyr is not None: gdaltest.post_reason('fail') return 'fail' # Not accepted gdal.PushErrorHandler() sql_lyr = ds.ExecuteSQL("select max('id') from 'test'") gdal.PopErrorHandler() if sql_lyr is not None: gdaltest.post_reason('fail') return 'fail' # Not accepted gdal.PushErrorHandler() sql_lyr = ds.ExecuteSQL("select id as 'id2' from 'test'") gdal.PopErrorHandler() if sql_lyr is not None: gdaltest.post_reason('fail') return 'fail' return 'success'
def main(argv=None): global verbose, quiet verbose = 0 quiet = 0 names = [] out_file = 'out.tif' frmt = None ulx = None psize_x = None separate = 0 copy_pct = 0 nodata = None a_nodata = None create_options = [] pre_init = [] band_type = None createonly = 0 bTargetAlignedPixels = False start_time = time.time() gdal.AllRegister() if argv is None: argv = sys.argv argv = gdal.GeneralCmdLineProcessor(argv) if argv is None: sys.exit(0) # Parse command line arguments. i = 1 while i < len(argv): arg = argv[i] if arg == '-o': i = i + 1 out_file = argv[i] elif arg == '-v': verbose = 1 elif arg == '-q' or arg == '-quiet': quiet = 1 elif arg == '-createonly': createonly = 1 elif arg == '-separate': separate = 1 elif arg == '-seperate': separate = 1 elif arg == '-pct': copy_pct = 1 elif arg == '-ot': i = i + 1 band_type = gdal.GetDataTypeByName(argv[i]) if band_type == gdal.GDT_Unknown: print('Unknown GDAL data type: %s' % argv[i]) sys.exit(1) elif arg == '-init': i = i + 1 str_pre_init = argv[i].split() for x in str_pre_init: pre_init.append(float(x)) elif arg == '-n': i = i + 1 nodata = float(argv[i]) elif arg == '-a_nodata': i = i + 1 a_nodata = float(argv[i]) elif arg == '-f' or arg == '-of': i = i + 1 frmt = argv[i] elif arg == '-co': i = i + 1 create_options.append(argv[i]) elif arg == '-ps': psize_x = float(argv[i + 1]) psize_y = -1 * abs(float(argv[i + 2])) i = i + 2 elif arg == '-tap': bTargetAlignedPixels = True elif arg == '-ul_lr': ulx = float(argv[i + 1]) uly = float(argv[i + 2]) lrx = float(argv[i + 3]) lry = float(argv[i + 4]) i = i + 4 elif arg[:1] == '-': print('Unrecognized command option: %s' % arg) Usage() sys.exit(1) else: names.append(arg) i = i + 1 if not names: print('No input files selected.') Usage() sys.exit(1) if frmt is None: frmt = GetOutputDriverFor(out_file) Driver = gdal.GetDriverByName(frmt) if Driver is None: print('Format driver %s not found, pick a supported driver.' % frmt) sys.exit(1) DriverMD = Driver.GetMetadata() if 'DCAP_CREATE' not in DriverMD: print('Format driver %s does not support creation and piecewise writing.\nPlease select a format that does, such as GTiff (the default) or HFA (Erdas Imagine).' % frmt) sys.exit(1) # Collect information on all the source files. file_infos = names_to_fileinfos(names) if ulx is None: ulx = file_infos[0].ulx uly = file_infos[0].uly lrx = file_infos[0].lrx lry = file_infos[0].lry for fi in file_infos: ulx = min(ulx, fi.ulx) uly = max(uly, fi.uly) lrx = max(lrx, fi.lrx) lry = min(lry, fi.lry) if psize_x is None: psize_x = file_infos[0].geotransform[1] psize_y = file_infos[0].geotransform[5] if band_type is None: band_type = file_infos[0].band_type # Try opening as an existing file. gdal.PushErrorHandler('CPLQuietErrorHandler') t_fh = gdal.Open(out_file, gdal.GA_Update) gdal.PopErrorHandler() # Create output file if it does not already exist. if t_fh is None: if bTargetAlignedPixels: ulx = math.floor(ulx / psize_x) * psize_x lrx = math.ceil(lrx / psize_x) * psize_x lry = math.floor(lry / -psize_y) * -psize_y uly = math.ceil(uly / -psize_y) * -psize_y geotransform = [ulx, psize_x, 0, uly, 0, psize_y] xsize = int((lrx - ulx) / geotransform[1] + 0.5) ysize = int((lry - uly) / geotransform[5] + 0.5) if separate != 0: bands = 0 for fi in file_infos: bands = bands + fi.bands else: bands = file_infos[0].bands t_fh = Driver.Create(out_file, xsize, ysize, bands, band_type, create_options) if t_fh is None: print('Creation failed, terminating gdal_merge.') sys.exit(1) t_fh.SetGeoTransform(geotransform) t_fh.SetProjection(file_infos[0].projection) if copy_pct: t_fh.GetRasterBand(1).SetRasterColorTable(file_infos[0].ct) else: if separate != 0: bands = 0 for fi in file_infos: bands = bands + fi.bands if t_fh.RasterCount < bands: print('Existing output file has less bands than the input files. You should delete it before. Terminating gdal_merge.') sys.exit(1) else: bands = min(file_infos[0].bands, t_fh.RasterCount) # Do we need to set nodata value ? if a_nodata is not None: for i in range(t_fh.RasterCount): t_fh.GetRasterBand(i + 1).SetNoDataValue(a_nodata) # Do we need to pre-initialize the whole mosaic file to some value? if pre_init is not None: if t_fh.RasterCount <= len(pre_init): for i in range(t_fh.RasterCount): t_fh.GetRasterBand(i + 1).Fill(pre_init[i]) elif len(pre_init) == 1: for i in range(t_fh.RasterCount): t_fh.GetRasterBand(i + 1).Fill(pre_init[0]) # Copy data from source files into output file. t_band = 1 if quiet == 0 and verbose == 0: progress(0.0) fi_processed = 0 for fi in file_infos: if createonly != 0: continue if verbose != 0: print("") print("Processing file %5d of %5d, %6.3f%% completed in %d minutes." % (fi_processed + 1, len(file_infos), fi_processed * 100.0 / len(file_infos), int(round((time.time() - start_time) / 60.0)))) fi.report() if separate == 0: for band in range(1, bands + 1): fi.copy_into(t_fh, band, band, nodata) else: for band in range(1, fi.bands + 1): fi.copy_into(t_fh, band, t_band, nodata) t_band = t_band + 1 fi_processed = fi_processed + 1 if quiet == 0 and verbose == 0: progress(fi_processed / float(len(file_infos))) # Force file to be closed. t_fh = None
def ogr_sql_28(): ds = ogr.GetDriverByName("Memory").CreateDataSource("my_ds") lyr = ds.CreateLayer("my_layer") lyr.GetLayerDefn().GetGeomFieldDefn(0).SetName('geom') # a bit border line but OK for Memory driver... field_defn = ogr.FieldDefn("strfield", ogr.OFTString) lyr.CreateField(field_defn) field_defn = ogr.FieldDefn("intfield", ogr.OFTInteger) lyr.CreateField(field_defn) lyr = ds.CreateLayer("my_layer2") field_defn = ogr.FieldDefn("strfield", ogr.OFTString) lyr.CreateField(field_defn) field_defn = ogr.FieldDefn("strfield2", ogr.OFTString) lyr.CreateField(field_defn) try: sql_lyr = ds.ExecuteSQL(None) gdaltest.post_reason('expected error on NULL query') return 'fail' except: pass queries = [ '', '1', '*', 'SELECT', "SELECT ' FROM my_layer", 'SELECT + FROM my_layer', 'SELECT (1 FROM my_layer', 'SELECT (1)) FROM my_layer', 'SELECT (1,) FROM my_layer', 'SELECT 1 + FROM my_layer', "SELECT 1 + 'a' FROM my_layer", 'SELECT 1 - FROM my_layer', 'SELECT 1 * FROM my_layer', 'SELECT 1 % FROM my_layer', 'SELECT *', 'SELECT * FROM', 'SELECT * FROM foo', 'SELECT FROM my_layer', 'SELECT FROM FROM my_layer', "SELECT ('strfield'", "SELECT 'strfield' +", "SELECT 'strfield' 'strfield'", "SELECT CONCAT('strfield')", 'SELECT foo(strfield) FROM my_layer', # Undefined function 'foo' used. 'SELECT strfield, FROM my_layer', 'SELECT strfield, foo FROM my_layer', 'SELECT strfield AS FROM my_layer', 'SELECT strfield AS 1 FROM my_layer', 'SELECT strfield AS strfield2 FROM', 'SELECT strfield + intfield FROM my_layer', 'SELECT CAST', 'SELECT CAST(', 'SELECT CAST(strfield', 'SELECT CAST(strfield AS', 'SELECT CAST(strfield AS foo', 'SELECT CAST(strfield AS foo)', 'SELECT CAST(strfield AS foo) FROM', 'SELECT CAST(strfield AS foo) FROM my_layer', 'SELECT CAST(strfield AS CHARACTER', 'SELECT CAST(strfield AS CHARACTER)', 'SELECT CAST(strfield AS CHARACTER) FROM', 'SELECT CAST(strfield AS CHARACTER) FROM foo', 'SELECT CAST(strfield AS CHARACTER(', 'SELECT CAST(strfield AS CHARACTER(2', 'SELECT CAST(strfield AS CHARACTER(2)', 'SELECT CAST(strfield AS CHARACTER(2))', 'SELECT CAST(strfield AS CHARACTER(2)) FROM', 'SELECT CAST(strfield AS CHARACTER(2)) FROM foo', 'SELECT CAST(strfield AS 1) FROM my_layer', 'SELECT * FROM my_layer WHERE', #'SELECT * FROM my_layer WHERE strfield', 'SELECT * FROM my_layer WHERE strfield = ', 'SELECT * FROM my_layer WHERE strfield = foo', "SELECT * FROM my_layer WHERE foo = 'a'", "SELECT * FROM my_layer WHERE strfield = 'a" "SELECT * FROM my_layer WHERE strfield = 'a' ORDER ", "SELECT * FROM my_layer WHERE strfield = 'a' ORDER BY", "SELECT * FROM my_layer WHERE strfield = 'a' ORDER BY foo", "SELECT * FROM my_layer WHERE strfield = 'a' ORDER BY strfield UNK", "SELECT * FROM my_layer ORDER BY geom", # Cannot use geometry field 'geom' in a ORDER BY clause "SELECT FOO(*) FROM my_layer", "SELECT FOO(*) AS bar FROM my_layer", "SELECT COUNT", "SELECT COUNT(", "SELECT COUNT() FROM my_layer", "SELECT COUNT(*", "SELECT COUNT(*)", "SELECT COUNT(*) FROM", "SELECT COUNT(*) AS foo FROM", "SELECT COUNT(* FROM my_layer", "SELECT COUNT(FOO intfield) FROM my_layer", "SELECT COUNT(DISTINCT intfield FROM my_layer", "SELECT COUNT(DISTINCT *) FROM my_layer", "SELECT FOO(DISTINCT intfield) FROM my_layer", "SELECT FOO(DISTINCT intfield) as foo FROM my_layer", "SELECT DISTINCT foo FROM my_layer", "SELECT DISTINCT foo AS 'id' 'id2' FROM", "SELECT DISTINCT foo AS id id2 FROM", "SELECT DISTINCT FROM my_layer", "SELECT DISTINCT strfield, COUNT(DISTINCT intfield) FROM my_layer", "SELECT MIN(intfield*2) FROM my_layer", "SELECT MIN(intfield,2) FROM my_layer", "SELECT MIN(foo) FROM my_layer", "SELECT MAX(foo) FROM my_layer", "SELECT SUM(foo) FROM my_layer", "SELECT AVG(foo) FROM my_layer", "SELECT MIN(strfield) FROM my_layer", "SELECT MAX(strfield) FROM my_layer", "SELECT SUM(strfield) FROM my_layer", "SELECT AVG(strfield) FROM my_layer", "SELECT AVG(intfield, intfield) FROM my_layer", "SELECT * FROM my_layer WHERE AVG(intfield) = 1" , "SELECT * FROM 'foo' foo" , "SELECT * FROM my_layer WHERE strfield =" , "SELECT * FROM my_layer WHERE strfield = foo" , "SELECT * FROM my_layer WHERE strfield = intfield" , "SELECT * FROM my_layer WHERE strfield = 1" , "SELECT * FROM my_layer WHERE strfield = '1' AND" , #"SELECT * FROM my_layer WHERE 1 AND 2" , "SELECT * FROM my_layer WHERE strfield LIKE" , "SELECT * FROM my_layer WHERE strfield LIKE 1" , "SELECT * FROM my_layer WHERE strfield IS" , "SELECT * FROM my_layer WHERE strfield IS NOT" , "SELECT * FROM my_layer WHERE strfield IS foo" , "SELECT * FROM my_layer WHERE strfield IS NOT foo" , "SELECT * FROM my_layer WHERE (strfield IS NOT NULL" , "SELECT * FROM my_layer WHERE strfield IN" , "SELECT * FROM my_layer WHERE strfield IN(" , "SELECT * FROM my_layer WHERE strfield IN()" , "SELECT * FROM my_layer WHERE strfield IN('a'" , "SELECT * FROM my_layer WHERE strfield IN('a'," , "SELECT * FROM my_layer WHERE strfield IN('a','b'" , "SELECT * FROM my_layer WHERE strfield IN('a','b'))" , "SELECT * FROM my_layer LEFT" , "SELECT * FROM my_layer LEFT JOIN" , "SELECT * FROM my_layer LEFT JOIN foo", "SELECT * FROM my_layer LEFT JOIN foo ON my_layer.strfield = my_layer2.strfield", "SELECT * FROM my_layer LEFT JOIN my_layer2 ON my_layer.strfield = foo.strfield", "SELECT * FROM my_layer LEFT JOIN my_layer2 ON my_layer.strfield = my_layer2.foo", #"SELECT * FROM my_layer LEFT JOIN my_layer2 ON my_layer.strfield != my_layer2.strfield", "SELECT *, my_layer2. FROM my_layer LEFT JOIN my_layer2 ON my_layer.strfield = my_layer2.strfield", "SELECT *, my_layer2.foo FROM my_layer LEFT JOIN my_layer2 ON my_layer.strfield = my_layer2.strfield", "SELECT * FROM my_layer UNION" , "SELECT * FROM my_layer UNION ALL" , "SELECT * FROM my_layer UNION ALL SELECT" , "SELECT * FROM my_layer UNION ALL SELECT *" , "SELECT * FROM my_layer UNION ALL SELECT * FROM" , ] for query in queries: gdal.ErrorReset() #print query gdal.PushErrorHandler('CPLQuietErrorHandler') sql_lyr = ds.ExecuteSQL(query) gdal.PopErrorHandler() if sql_lyr is not None: gdaltest.post_reason('expected None result on "%s"' % query) ds.ReleaseResultSet(sql_lyr) return 'fail' if gdal.GetLastErrorType() == 0: gdaltest.post_reason('expected error on "%s"' % query) return 'fail' ds = None return 'success'
def warp_30(): # Open source dataset src_ds = gdal.Open('../gcore/data/byte.tif') # Desfine target SRS dst_srs = osr.SpatialReference() dst_srs.ImportFromEPSG(4326) dst_wkt = dst_srs.ExportToWkt() error_threshold = 0.125 # error threshold --> use same value as in gdalwarp resampling = gdal.GRA_Bilinear # Call AutoCreateWarpedVRT() to fetch default values for target raster dimensions and geotransform tmp_ds = gdal.AutoCreateWarpedVRT( src_ds, \ None, # src_wkt : left to default value --> will use the one from source \ dst_wkt, \ resampling, \ error_threshold ) dst_xsize = tmp_ds.RasterXSize dst_ysize = tmp_ds.RasterYSize dst_gt = tmp_ds.GetGeoTransform() tmp_ds = None # Now create the true target dataset dst_ds = gdal.GetDriverByName('GTiff').Create('/vsimem/warp_30.tif', dst_xsize, dst_ysize, src_ds.RasterCount) dst_ds.SetProjection(dst_wkt) dst_ds.SetGeoTransform(dst_gt) # And run the reprojection cbk = warp_30_progress_callback cbk_user_data = None # value for last parameter of above warp_27_progress_callback gdal.PushErrorHandler('CPLQuietErrorHandler') ret = gdal.ReprojectImage( src_ds, \ dst_ds, \ None, # src_wkt : left to default value --> will use the one from source \ None, # dst_wkt : left to default value --> will use the one from destination \ resampling, \ 0, # WarpMemoryLimit : left to default value \ error_threshold, cbk, # Progress callback : could be left to None or unspecified for silent progress cbk_user_data) # Progress callback user data gdal.PopErrorHandler() if ret == 0: gdaltest.post_reason('failed') return 'fail' old_val = gdal.GetConfigOption('GDAL_NUM_THREADS') gdal.SetConfigOption('GDAL_NUM_THREADS', '2') gdal.PushErrorHandler('CPLQuietErrorHandler') ret = gdal.ReprojectImage( src_ds, \ dst_ds, \ None, # src_wkt : left to default value --> will use the one from source \ None, # dst_wkt : left to default value --> will use the one from destination \ resampling, \ 0, # WarpMemoryLimit : left to default value \ error_threshold, cbk, # Progress callback : could be left to None or unspecified for silent progress cbk_user_data) # Progress callback user data gdal.PopErrorHandler() gdal.SetConfigOption('GDAL_NUM_THREADS', old_val) if ret == 0: gdaltest.post_reason('failed') return 'fail' return 'success'
def test_ogr_oci_20(): if gdaltest.oci_ds is None: pytest.skip() lyr = gdaltest.oci_ds.CreateLayer( 'ogr_oci_20', geom_type=ogr.wkbPoint, options=['GEOMETRY_NULLABLE=NO', 'DIM=2']) assert lyr.GetLayerDefn().GetGeomFieldDefn(0).IsNullable() == 0 field_defn = ogr.FieldDefn('field_not_nullable', ogr.OFTString) field_defn.SetNullable(0) lyr.CreateField(field_defn) field_defn = ogr.FieldDefn('field_nullable', ogr.OFTString) lyr.CreateField(field_defn) f = ogr.Feature(lyr.GetLayerDefn()) f.SetField('field_not_nullable', 'not_null') f.SetGeometryDirectly(ogr.CreateGeometryFromWkt('POINT(0 1)')) ret = lyr.CreateFeature(f) f = None assert ret == 0 # Error case: missing geometry f = ogr.Feature(lyr.GetLayerDefn()) f.SetField('field_not_nullable', 'not_null') gdal.PushErrorHandler() ret = lyr.CreateFeature(f) gdal.PopErrorHandler() assert ret != 0 f = None # Error case: missing non-nullable field f = ogr.Feature(lyr.GetLayerDefn()) f.SetGeometryDirectly(ogr.CreateGeometryFromWkt('POINT(0 0)')) gdal.PushErrorHandler() ret = lyr.CreateFeature(f) gdal.PopErrorHandler() assert ret != 0 f = None lyr.SyncToDisk() # Test with nullable geometry lyr = gdaltest.oci_ds.CreateLayer('ogr_oci_20bis', geom_type=ogr.wkbPoint, options=['DIM=2']) assert lyr.GetLayerDefn().GetGeomFieldDefn(0).IsNullable() == 1 f = ogr.Feature(lyr.GetLayerDefn()) f.SetGeometryDirectly(ogr.CreateGeometryFromWkt('POINT(0 1)')) ret = lyr.CreateFeature(f) f = None assert ret == 0 f = ogr.Feature(lyr.GetLayerDefn()) ret = lyr.CreateFeature(f) f = None assert ret == 0 lyr.SyncToDisk() oci_ds2 = ogr.Open(os.environ['OCI_DSNAME']) lyr = oci_ds2.GetLayerByName('ogr_oci_20') assert lyr.GetLayerDefn().GetFieldDefn(lyr.GetLayerDefn().GetFieldIndex( 'field_not_nullable')).IsNullable() == 0 assert lyr.GetLayerDefn().GetFieldDefn( lyr.GetLayerDefn().GetFieldIndex('field_nullable')).IsNullable() == 1 assert lyr.GetLayerDefn().GetGeomFieldDefn(0).IsNullable() == 0 lyr = oci_ds2.GetLayerByName('ogr_oci_20bis') assert lyr.GetLayerDefn().GetGeomFieldDefn(0).IsNullable() == 1 feat = lyr.GetNextFeature() assert feat.GetGeometryRef() is not None feat = lyr.GetNextFeature() assert feat.GetGeometryRef() is None
def ogr_rfc41_2(): # Check implicit geometry field creation feature_defn = ogr.FeatureDefn() if feature_defn.GetGeomFieldCount() != 1: gdaltest.post_reason('fail') return 'fail' if feature_defn.GetGeomType() != ogr.wkbUnknown: gdaltest.post_reason('fail') return 'fail' # Test IsSame() if feature_defn.IsSame(feature_defn) != 1: gdaltest.post_reason('fail') return 'fail' other_feature_defn = ogr.FeatureDefn() if feature_defn.IsSame(other_feature_defn) != 1: gdaltest.post_reason('fail') return 'fail' other_feature_defn.GetGeomFieldDefn(0).SetSpatialRef( osr.SpatialReference()) if feature_defn.IsSame(other_feature_defn) != 0: gdaltest.post_reason('fail') return 'fail' feature_defn.GetGeomFieldDefn(0).SetSpatialRef(osr.SpatialReference()) if feature_defn.IsSame(other_feature_defn) != 1: gdaltest.post_reason('fail') return 'fail' other_feature_defn.GetGeomFieldDefn(0).SetSpatialRef(None) if feature_defn.IsSame(other_feature_defn) != 0: gdaltest.post_reason('fail') return 'fail' feature_defn = None feature_defn = ogr.FeatureDefn() # Check changing geometry type feature_defn.SetGeomType(ogr.wkbPoint) if feature_defn.GetGeomType() != ogr.wkbPoint: gdaltest.post_reason('fail') return 'fail' if feature_defn.GetGeomFieldDefn(0).GetType() != ogr.wkbPoint: gdaltest.post_reason('fail') return 'fail' # Check setting to wkbNone and implicitely destroying the field for i in range(2): feature_defn.SetGeomType(ogr.wkbNone) if feature_defn.GetGeomFieldCount() != 0: gdaltest.post_reason('fail') return 'fail' if feature_defn.GetGeomType() != ogr.wkbNone: gdaltest.post_reason('fail') return 'fail' # Recreate the field for t in [ogr.wkbPoint, ogr.wkbLineString]: feature_defn.SetGeomType(t) if feature_defn.GetGeomFieldCount() != 1: gdaltest.post_reason('fail') return 'fail' if feature_defn.GetGeomType() != t: gdaltest.post_reason('fail') return 'fail' if feature_defn.GetGeomFieldDefn(0).GetType() != t: gdaltest.post_reason('fail') return 'fail' # Test setting invalid value old_val = feature_defn.GetGeomType() gdal.PushErrorHandler('CPLQuietErrorHandler') feature_defn.SetGeomType(-3) gdal.PopErrorHandler() if feature_defn.GetGeomType() != old_val: gdaltest.post_reason('fail') return 'fail' # Test SetIgnored() / IsIgnored() if feature_defn.IsGeometryIgnored() != 0: gdaltest.post_reason('fail') return 'fail' if feature_defn.GetGeomFieldDefn(0).IsIgnored() != 0: gdaltest.post_reason('fail') return 'fail' feature_defn.SetGeometryIgnored(1) if feature_defn.IsGeometryIgnored() != 1: gdaltest.post_reason('fail') return 'fail' if feature_defn.GetGeomFieldDefn(0).IsIgnored() != 1: gdaltest.post_reason('fail') return 'fail' # Test wrong index values for GetGeomFieldDefn() for idx in [-1, 1]: gdal.PushErrorHandler('CPLQuietErrorHandler') ret = feature_defn.GetGeomFieldDefn(idx) gdal.PopErrorHandler() if ret != None: gdaltest.post_reason('fail') return 'fail' # Test GetGeomFieldIndex() if feature_defn.GetGeomFieldIndex("") != 0: gdaltest.post_reason('fail') return 'fail' if feature_defn.GetGeomFieldIndex("invalid") != -1: gdaltest.post_reason('fail') return 'fail' # Test AddGeomFieldDefn() gfld_defn = ogr.GeomFieldDefn('polygon_field', ogr.wkbPolygon) feature_defn.AddGeomFieldDefn(gfld_defn) if feature_defn.GetGeomFieldCount() != 2: gdaltest.post_reason('fail') return 'fail' if feature_defn.GetGeomFieldIndex("polygon_field") != 1: gdaltest.post_reason('fail') return 'fail' if feature_defn.GetGeomFieldDefn(1).GetName() != 'polygon_field': gdaltest.post_reason('fail') return 'fail' # Test DeleteGeomFieldDefn() : error cases if feature_defn.DeleteGeomFieldDefn(-1) == 0: gdaltest.post_reason('fail') return 'fail' if feature_defn.DeleteGeomFieldDefn(2) == 0: gdaltest.post_reason('fail') return 'fail' if feature_defn.GetGeomFieldCount() != 2: gdaltest.post_reason('fail') return 'fail' # Test DeleteGeomFieldDefn() : valid cases if feature_defn.DeleteGeomFieldDefn(0) != 0: gdaltest.post_reason('fail') return 'fail' if feature_defn.GetGeomFieldCount() != 1: gdaltest.post_reason('fail') return 'fail' if feature_defn.GetGeomFieldIndex("polygon_field") != 0: gdaltest.post_reason('fail') return 'fail' if feature_defn.DeleteGeomFieldDefn(0) != 0: gdaltest.post_reason('fail') return 'fail' if feature_defn.GetGeomFieldCount() != 0: gdaltest.post_reason('fail') return 'fail' if feature_defn.IsSame(feature_defn) != 1: gdaltest.post_reason('fail') return 'fail' if feature_defn.IsSame(ogr.FeatureDefn()) != 0: gdaltest.post_reason('fail') return 'fail' feature_defn = None return 'success'
def plmosaic_17(): if gdaltest.plmosaic_drv is None: return 'skip' gdal.SetConfigOption('PL_URL', '/vsimem/root') ds = gdal.OpenEx('PLMosaic:', gdal.OF_RASTER, open_options=['API_KEY=foo', 'MOSAIC=my_mosaic', 'CACHE_PATH=tmp']) gdal.SetConfigOption('PL_URL', None) if ds is None: gdaltest.post_reason('fail') return 'fail' if ds.GetMetadata() != {'LAST_ACQUIRED': 'last_date', 'NAME': 'my_mosaic', 'FIRST_ACQUIRED': 'first_date'}: gdaltest.post_reason('fail') print(ds.GetMetadata()) return 'fail' if ds.GetProjectionRef().find('3857') < 0: gdaltest.post_reason('fail') print(ds.GetProjectionRef()) return 'fail' if ds.RasterXSize != 8388608: gdaltest.post_reason('fail') print(ds.RasterXSize) return 'fail' if ds.RasterYSize != 8388608: gdaltest.post_reason('fail') print(ds.RasterYSize) return 'fail' got_gt = ds.GetGeoTransform() expected_gt = (-20037508.34, 4.7773142671600004, 0.0, 20037508.34, 0.0, -4.7773142671600004) for i in range(6): if abs(got_gt[i] - expected_gt[i]) > 1e-8: gdaltest.post_reason('fail') print(ds.GetGeoTransform()) return 'fail' if ds.GetMetadataItem('INTERLEAVE', 'IMAGE_STRUCTURE') != 'PIXEL': gdaltest.post_reason('fail') print(ds.GetMetadata('IMAGE_STRUCTURE')) print(ds.GetMetadataItem('INTERLEAVE', 'IMAGE_STRUCTURE')) return 'fail' if ds.GetRasterBand(1).GetOverviewCount() != 15: gdaltest.post_reason('fail') print(ds.GetRasterBand(1).GetOverviewCount()) return 'fail' if ds.GetRasterBand(1).GetOverview(-1) is not None: gdaltest.post_reason('fail') return 'fail' if ds.GetRasterBand(1).GetOverview(ds.GetRasterBand(1).GetOverviewCount()) is not None: gdaltest.post_reason('fail') return 'fail' if ds.GetRasterBand(1).GetOverview(0) is None: gdaltest.post_reason('fail') return 'fail' try: shutil.rmtree('tmp/plmosaic_cache') except OSError: pass for i in range(12): # Read at one nonexistent position. ds.GetRasterBand(1).ReadRaster(4096 * i, 0, 1, 1) if gdal.GetLastErrorMsg() != '': gdaltest.post_reason('fail') return 'fail' for i in range(11, -1, -1): # Again in the same quad, but in different block, to test cache ds.GetRasterBand(1).ReadRaster(4096 * i + 256, 0, 1, 1) if gdal.GetLastErrorMsg() != '': gdaltest.post_reason('fail') return 'fail' for i in range(12): # Again in the same quad, but in different block, to test cache ds.GetRasterBand(1).ReadRaster(4096 * i + 512, 256, 1, 1) if gdal.GetLastErrorMsg() != '': gdaltest.post_reason('fail') return 'fail' ds.FlushCache() # Invalid tile content gdal.FileFromMemBuffer('/vsimem/root/my_mosaic_id/quads/0-2047/full', 'garbage') gdal.PushErrorHandler() ds.GetRasterBand(1).ReadRaster(0, 0, 1, 1) gdal.PopErrorHandler() os.stat('tmp/plmosaic_cache/my_mosaic/my_mosaic_0-2047.tif') ds.FlushCache() shutil.rmtree('tmp/plmosaic_cache') # GeoTIFF but with wrong dimensions gdal.GetDriverByName('GTiff').Create('/vsimem/root/my_mosaic_id/quads/0-2047/full', 1, 1, 1) gdal.PushErrorHandler() ds.GetRasterBand(1).ReadRaster(0, 0, 1, 1) gdal.PopErrorHandler() os.stat('tmp/plmosaic_cache/my_mosaic/my_mosaic_0-2047.tif') ds.FlushCache() shutil.rmtree('tmp/plmosaic_cache') # Good GeoTIFF tmp_ds = gdal.GetDriverByName('GTiff').Create('/vsimem/root/my_mosaic_id/quads/0-2047/full', 4096, 4096, 4, options=['INTERLEAVE=BAND', 'SPARSE_OK=YES']) tmp_ds.GetRasterBand(1).Fill(255) tmp_ds = None val = ds.GetRasterBand(1).ReadRaster(0, 0, 1, 1) val = struct.unpack('B', val)[0] if val != 255: gdaltest.post_reason('fail') print(val) return 'fail' os.stat('tmp/plmosaic_cache/my_mosaic/my_mosaic_0-2047.tif') ds.FlushCache() # Read again from file cache. # We change the file behind the scene (but not changing its size) # to demonstrate that the cached tile is still use tmp_ds = gdal.GetDriverByName('GTiff').Create('/vsimem/root/my_mosaic_id/quads/0-2047/full', 4096, 4096, 4, options=['INTERLEAVE=BAND', 'SPARSE_OK=YES']) tmp_ds.GetRasterBand(1).Fill(1) tmp_ds = None val = ds.GetRasterBand(1).ReadRaster(0, 0, 1, 1) val = struct.unpack('B', val)[0] if val != 255: gdaltest.post_reason('fail') print(val) return 'fail' ds = None # Read again from file cache, but with TRUST_CACHE=YES # delete the full GeoTIFF before gdal.Unlink('/vsimem/root/my_mosaic_id/quads/0-2047/full') gdal.SetConfigOption('PL_URL', '/vsimem/root') ds = gdal.OpenEx('PLMosaic:API_KEY=foo,MOSAIC=my_mosaic,CACHE_PATH=tmp,TRUST_CACHE=YES', gdal.OF_RASTER) gdal.SetConfigOption('PL_URL', None) val = ds.GetRasterBand(1).ReadRaster(0, 0, 1, 1) val = struct.unpack('B', val)[0] if val != 255: gdaltest.post_reason('fail') print(val) return 'fail' ds = None # Read again from file cache but the metatile has changed in between gdal.SetConfigOption('PL_URL', '/vsimem/root') ds = gdal.OpenEx('PLMosaic:', gdal.OF_RASTER, open_options=['API_KEY=foo', 'MOSAIC=my_mosaic', 'CACHE_PATH=tmp']) gdal.SetConfigOption('PL_URL', None) tmp_ds = gdal.GetDriverByName('GTiff').Create('/vsimem/root/my_mosaic_id/quads/0-2047/full', 4096, 4096, 4, options=['INTERLEAVE=BAND', 'SPARSE_OK=YES']) tmp_ds.SetMetadataItem('foo', 'bar') tmp_ds.GetRasterBand(1).Fill(254) tmp_ds = None val = ds.ReadRaster(0, 0, 1, 1) val = struct.unpack('B' * 4, val) if val != (254, 0, 0, 0): gdaltest.post_reason('fail') print(val) return 'fail' return 'success'