def rasterio_5(): ds = gdal.Open('data/byte.tif') 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' import sys # This should only fail on a 32bit build try: maxsize = sys.maxint except: 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,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_geos_unioncascaded(): if not ogrtest.have_geos(): return 'skip' g1 = ogr.CreateGeometryFromWkt( 'MULTIPOLYGON(((0 0,0 1,1 1,1 0,0 0)),((0.5 0.5,0.5 1.5,1.5 1.5,1.5 0.5,0.5 0.5)))' ) gdal.ErrorReset() cascadedunion = g1.UnionCascaded() g1.Destroy() if cascadedunion is None: msg = gdal.GetLastErrorMsg() if msg.find('GEOS >=') != -1: return 'skip' if cascadedunion.ExportToWkt( ) != 'POLYGON ((0 0,0 1,0.5 1.0,0.5 1.5,1.5 1.5,1.5 0.5,1.0 0.5,1 0,0 0))': print('Got: ', cascadedunion.ExportToWkt()) return 'fail' cascadedunion.Destroy() return 'success'
def __init__(self, source, target=None): """ Initialize coordinate transform. source -- source osr.SpatialReference coordinate system. target -- destination osr.SpatialReference coordinate system. """ # # NOTE: A special requirement of the Atlantis Peppers system is # that it needs to be able to instantiate a coordinate tranform # by just passing in a tuple with two SpatialReference objects. We # assume this is the case if target is None. self._o = None if target is None: target = source[1] source = source[0] gdal.ErrorReset() self._o = _gdal.OCTNewCoordinateTransformation(source._o, target._o) if self._o is None or self._o == 'NULL': if len(gdal.GetLastErrorMsg()) > 0: raise ValueError, gdal.GetLastErrorMsg() else: raise ValueError, 'Failed to create coordinate transformation.'
def wms_4(): if gdaltest.wms_drv is None or gdaltest.wms_ds is None: return 'skip' if not gdaltest.wms_srv1_ok: return 'skip' gdal.SetConfigOption('CPL_ACCUM_ERROR_MSG', 'ON') gdal.PushErrorHandler('CPLQuietErrorHandler') cs = gdaltest.wms_ds.GetRasterBand(1).Checksum(0, 0, 100, 100) gdal.PopErrorHandler() gdal.SetConfigOption('CPL_ACCUM_ERROR_MSG', 'OFF') msg = gdal.GetLastErrorMsg() gdal.ErrorReset() if msg is not None and msg.find( 'Service denied due to system overload') != -1: print(msg) return 'skip' if cs != 57182: gdaltest.post_reason('Wrong checksum: ' + str(cs)) return 'fail' return 'success'
def rasterio_6(): ds = gdal.GetDriverByName('MEM').Create('', 2, 2) for obj in [ds, ds.GetRasterBand(1)]: try: obj.WriteRaster(0,0,2,2,None) gdaltest.post_reason('expected exception') return 'fail' except: pass gdal.ErrorReset() gdal.PushErrorHandler('CPLQuietErrorHandler') obj.WriteRaster(0,0,2,2,' ') gdal.PopErrorHandler() error_msg = gdal.GetLastErrorMsg() if error_msg.find('Buffer too small') == -1: gdaltest.post_reason('did not get expected error msg (1)') print(error_msg) return 'fail' gdal.ErrorReset() gdal.PushErrorHandler('CPLQuietErrorHandler') obj.WriteRaster(-1,0,1,1,' ') gdal.PopErrorHandler() error_msg = gdal.GetLastErrorMsg() if error_msg.find('Access window out of range in RasterIO()') == -1: gdaltest.post_reason('did not get expected error msg (2)') print(error_msg) return 'fail' gdal.ErrorReset() gdal.PushErrorHandler('CPLQuietErrorHandler') obj.WriteRaster(0,0,0,1,' ') gdal.PopErrorHandler() error_msg = gdal.GetLastErrorMsg() 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 jpeg_12(): ds = gdal.Open('/vsizip/data/byte_jpg.zip') if ds is None: return 'fail' gdal.ErrorReset() ds.GetRasterBand(1).Checksum() if gdal.GetLastErrorMsg() != '': return 'fail' gdal.ErrorReset() ds.GetRasterBand(1).GetMaskBand().Checksum() if gdal.GetLastErrorMsg() != '': return 'fail' ds = None return 'success'
def jpeg_17(): gdal.ErrorReset() gdal.PushErrorHandler('CPLQuietErrorHandler') ds = gdal.Open('data/bogus.jpg') gdal.PopErrorHandler() if ds is not None or gdal.GetLastErrorMsg() == '': gdaltest.post_reason('fail') return 'fail' gdal.ErrorReset() ds = gdal.Open('data/byte_corrupted.jpg') gdal.PushErrorHandler('CPLQuietErrorHandler') ds.GetRasterBand(1).Checksum() gdal.PopErrorHandler() if gdal.GetLastErrorMsg() == '': gdaltest.post_reason('fail') return 'fail' return 'success'
def ogr_sql_30(): gdal.ErrorReset() sql_lyr = gdaltest.ds.ExecuteSQL('select min(eas_id), count(*) from poly') feat = sql_lyr.GetNextFeature() val_count = feat.GetField(1) gdaltest.ds.ReleaseResultSet(sql_lyr) if gdal.GetLastErrorMsg() != '': return 'fail' if val_count == 10: return 'success' else: return 'fail'
def ogr_sql_31(): gdal.ErrorReset() sql_lyr = gdaltest.ds.ExecuteSQL( 'select min(eas_id) from poly where area = 0') feat = sql_lyr.GetNextFeature() val = feat.GetField(0) gdaltest.ds.ReleaseResultSet(sql_lyr) if gdal.GetLastErrorMsg() != '': return 'fail' if val is None: return 'success' else: return 'fail'
def osr_proj4_13(): proj4strlist = [ '', #None, 'foo', '+a=5', '+proj=foo', '+proj=longlat +a=5', '+proj=longlat +ellps=wgs72 +towgs84=3' ] for proj4str in proj4strlist: srs = osr.SpatialReference() gdal.ErrorReset() if srs.ImportFromProj4(proj4str) == 0 and gdal.GetLastErrorMsg() == '': return 'fail' return 'success'
def __init__(self,shapefile,fieldnames=[], fields={},srs_wkt=None,update=False): ''' Open the shapefile for writing or appending. @type shapefile: C{gdal.Dataset} @param shapefile: Dataset object @type fields: C{list} @param fields: L{Fields order list} @type fields: C{dict} @param fields: L{Fields dict<formats.fields>} @type srs_wkt: C{str} @param srs_wkt: Spatial reference system WKT @type update: C{boolean} @param update: Update or overwrite existing shapefile @note: Field names can only be <= 10 characters long. Longer names will be silently truncated. This may result in non-unique column names, which will definitely cause problems later. Field names can not contain spaces or special characters, except underscores. Starting with version 1.7, the OGR Shapefile driver tries to generate unique field names. Successive duplicate field names, including those created by truncation to 10 characters, will be truncated to 8 characters and appended with a serial number from 1 to 99. @see: U{http://www.gdal.org/ogr/drv_shapefile.html} ''' gdal.ErrorReset() ogr.UseExceptions() self.driver = ogr.GetDriverByName('ESRI Shapefile') self.srs=osr.SpatialReference() self.filename=shapefile self.srs_wkt=srs_wkt self.fieldsnames=[]#Truncated fields names self.shpfieldsnames=[] self.fields={} self.shpfields={} if fieldnames == None:fieldnames=sorted(self.fields.keys()) for fieldname in fieldnames: if fieldname[0:10] not in self.fieldsnames: self.fieldsnames.append(fieldname[0:10]) self.fields[fieldname[0:10]]=fields[fieldname] #print fieldname[0:10],':',self.fields[fieldname[0:10]] try: if update and os.path.exists(shapefile): print 'EDIT shape mode' self.shape=self.__openshapefile__() else: print 'CREATE shape mode' self.shape=self.__createshapefile__() except Exception, err: self.__error__(err)
def mask_23(): if gdaltest.have_ng == 0: return 'skip' drv = gdal.GetDriverByName('GTiff') md = drv.GetMetadata() if md['DMD_CREATIONOPTIONLIST'].find('JPEG') == -1: return 'skip' src_ds = drv.Create('tmp/mask_23_src.tif', 3000, 2000, 3, options=['TILED=YES', 'SPARSE_OK=YES']) src_ds.CreateMaskBand(gdal.GMF_PER_DATASET) gdal.SetConfigOption('GDAL_TIFF_INTERNAL_MASK', 'YES') old_val = gdal.GetCacheMax() gdal.SetCacheMax(15000000) gdal.ErrorReset() ds = drv.CreateCopy('tmp/mask_23_dst.tif', src_ds, options=['TILED=YES', 'COMPRESS=JPEG']) gdal.SetConfigOption('GDAL_TIFF_INTERNAL_MASK', 'NO') gdal.SetCacheMax(old_val) ds = None error_msg = gdal.GetLastErrorMsg() src_ds = None drv.Delete('tmp/mask_23_src.tif') drv.Delete('tmp/mask_23_dst.tif') # 'ERROR 1: TIFFRewriteDirectory:Error fetching directory count' was triggered before if error_msg != '': return 'fail' return 'success'
def ogr_geos_simplifypreservetopology_linestring(): if not ogrtest.have_geos(): return 'skip' g1 = ogr.CreateGeometryFromWkt( 'LINESTRING(0 0,1 0,10 0)' ) gdal.ErrorReset() simplify = g1.SimplifyPreserveTopology(5) g1.Destroy() if simplify is None: msg = gdal.GetLastErrorMsg() if msg.find('GEOS >=') != -1: return 'skip' if simplify.ExportToWkt() != 'LINESTRING (0 0,10 0)': print('Got: ', simplify.ExportToWkt()) return 'fail' simplify.Destroy() return 'success'
def ogr_wfs_geoserver(): if gdaltest.wfs_drv is None: return 'skip' if not gdaltest.have_gml_reader: return 'skip' if gdaltest.gdalurlopen( 'http://demo.opengeo.org/geoserver/wfs?TYPENAME=za:za_points&SERVICE=WFS&VERSION=1.1.0&REQUEST=DescribeFeatureType' ) is None: print('cannot open URL') gdaltest.geoserver_wfs = False return 'skip' gdaltest.geoserver_wfs = True ds = ogr.Open( 'WFS:http://demo.opengeo.org/geoserver/wfs?TYPENAME=za:za_points') if ds is None: gdaltest.post_reason('did not managed to open WFS datastore') return 'fail' if ds.GetLayerCount() != 1: gdaltest.post_reason('did not get expected layer count') print(ds.GetLayerCount()) return 'fail' lyr = ds.GetLayer(0) if lyr.GetName() != 'za:za_points': gdaltest.post_reason('did not get expected layer name') print(lyr.GetName()) return 'fail' sr = lyr.GetSpatialRef() sr2 = osr.SpatialReference() sr2.ImportFromEPSG(4326) if not sr.IsSame(sr2): gdaltest.post_reason('did not get expected SRS') print(sr) return 'fail' feat_count = lyr.GetFeatureCount() if feat_count < 14000: if gdal.GetLastErrorMsg().find('The connection attempt failed') != -1: print('server probably in a broken state') # Disable it for wfs-t test gdaltest.geoserver_wfs = False return 'skip' gdaltest.post_reason('did not get expected feature count') print(feat_count) return 'fail' if not lyr.TestCapability(ogr.OLCFastFeatureCount): gdaltest.post_reason('did not get OLCFastFeatureCount') return 'fail' ds = ogr.Open( 'WFS:http://demo.opengeo.org/geoserver/wfs?TYPENAME=tiger:poi&MAXFEATURES=10&VERSION=1.1.0' ) if ds is None: print('server perhaps overloaded') return 'skip' lyr = ds.GetLayer(0) gdal.ErrorReset() feat = lyr.GetNextFeature() # This error message is generally the sign of a server in a broken state if feat is None and gdal.GetLastErrorMsg( ).find('<ows:ExceptionText>org.geoserver.platform.ServiceException') != -1: print('server probably in a broken state') # Disable it for wfs-t test gdaltest.geoserver_wfs = False return 'skip' geom = feat.GetGeometryRef() geom_wkt = geom.ExportToWkt() if feat.GetField('NAME') != 'museam' or \ ogrtest.check_feature_geometry(feat,'POINT (-74.0104611 40.70758763)', max_error = 0.000001 ) != 0: gdaltest.post_reason('did not get expected feature (1)') feat.DumpReadable() return 'fail' # Same with VERSION=1.0.0 ds = ogr.Open( 'WFS:http://demo.opengeo.org/geoserver/wfs?TYPENAME=tiger:poi&MAXFEATURES=10&VERSION=1.0.0' ) if ds is None: print('server perhaps overloaded') return 'skip' lyr = ds.GetLayer(0) feat = lyr.GetNextFeature() geom = feat.GetGeometryRef() geom_wkt = geom.ExportToWkt() if feat.GetField('NAME') != 'museam' or \ ogrtest.check_feature_geometry(feat,'POINT (-74.0104611 40.70758763)', max_error = 0.000001 ) != 0: gdaltest.post_reason('did not get expected feature (2)') feat.DumpReadable() return 'fail' # Test attribute filter ds = ogr.Open( "WFS:http://demo.opengeo.org/geoserver/wfs?TYPENAME=tiger:poi") if ds is None: print('server perhaps overloaded') return 'skip' lyr = ds.GetLayer(0) lyr.SetAttributeFilter( "MAINPAGE is not null and NAME >= 'a' and NAME LIKE 'mu%%eam'") feat_count = lyr.GetFeatureCount() if feat_count != 1: gdaltest.post_reason( 'did not get expected feature count after SetAttributeFilter (1)') print(feat_count) return 'fail' feat = lyr.GetNextFeature() if feat.GetField('gml_id') != 'poi.1': gdaltest.post_reason('did not get expected feature (3)') feat.DumpReadable() return 'fail' if False: # This GeoServer version doesn't understand <GmlObjectId> lyr.SetAttributeFilter("gml_id = 'poi.1'") feat_count = lyr.GetFeatureCount() if feat_count != 1: gdaltest.post_reason( 'did not get expected feature count after SetAttributeFilter (2)' ) print(feat_count) return 'fail' feat = lyr.GetNextFeature() if feat.GetField('gml_id') != 'poi.1': gdaltest.post_reason('did not get expected feature (4)') feat.DumpReadable() return 'fail' return 'success'
def __del__(self): '''Shutdown and release the lock on the shapefile''' try: gdal.ErrorReset() self.shape.Release() except:pass
def ogr_osm_10(): if ogrtest.osm_drv is None: return 'skip' # Non existing file ds = ogr.Open('/nonexisting/foo.osm') if ds is not None: gdaltest.post_reason('fail') return 'fail' # Empty .osm file f = gdal.VSIFOpenL('/vsimem/foo.osm', 'wb') gdal.VSIFCloseL(f) ds = ogr.Open('/vsimem/foo.osm') if ds is not None: gdaltest.post_reason('fail') return 'fail' gdal.Unlink('/vsimem/foo.osm') # Empty .pbf file f = gdal.VSIFOpenL('/vsimem/foo.pbf', 'wb') gdal.VSIFCloseL(f) ds = ogr.Open('/vsimem/foo.pbf') if ds is not None: gdaltest.post_reason('fail') return 'fail' gdal.Unlink('/vsimem/foo.pbf') if ogrtest.osm_drv_parse_osm: # Invalid .osm file f = gdal.VSIFOpenL('/vsimem/foo.osm', 'wb') data = "<osm>" gdal.VSIFWriteL(data, 1, len(data), f) gdal.VSIFCloseL(f) ds = ogr.Open('/vsimem/foo.osm') lyr = ds.GetLayer(0) gdal.ErrorReset() gdal.PushErrorHandler('CPLQuietErrorHandler') feat = lyr.GetNextFeature() gdal.PopErrorHandler() if gdal.GetLastErrorMsg() == '': gdaltest.post_reason('fail') return 'fail' ds = None gdal.Unlink('/vsimem/foo.osm') # Invalid .pbf file f = gdal.VSIFOpenL('/vsimem/foo.pbf', 'wb') data = "OSMHeader\n" gdal.VSIFWriteL(data, 1, len(data), f) gdal.VSIFCloseL(f) ds = ogr.Open('/vsimem/foo.pbf') lyr = ds.GetLayer(0) gdal.ErrorReset() gdal.PushErrorHandler('CPLQuietErrorHandler') feat = lyr.GetNextFeature() gdal.PopErrorHandler() if gdal.GetLastErrorMsg() == '': gdaltest.post_reason('fail') return 'fail' ds = None gdal.Unlink('/vsimem/foo.pbf') # Test million laugh pattern if ogrtest.osm_drv_parse_osm: gdal.PushErrorHandler('CPLQuietErrorHandler') ds = ogr.Open('data/billionlaugh.osm') gdal.PopErrorHandler() if ds is not None: gdaltest.post_reason('fail') return 'fail' return 'success'
def vsizip_1(): # We can keep the handle open during all the ZIP writing hZIP = gdal.VSIFOpenL("/vsizip/vsimem/test.zip", "wb") if hZIP is None: gdaltest.post_reason('fail 1') return 'fail' # One way to create a directory f = gdal.VSIFOpenL("/vsizip/vsimem/test.zip/subdir2/", "wb") if f is None: gdaltest.post_reason('fail 2') return 'fail' 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") if f2 is None: gdaltest.post_reason('fail 3') return 'fail' 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() if gdal.GetLastErrorMsg() != 'Cannot read a zip file being written': gdaltest.post_reason('expected error') print(gdal.GetLastErrorMsg()) return 'fail' if f is not None: gdaltest.post_reason('should not have been successful 1') return 'fail' # Create 2nd file f3 = gdal.VSIFOpenL("/vsizip/vsimem/test.zip/subdir3/efghi", "wb") if f3 is None: gdaltest.post_reason('fail 4') return 'fail' 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() if gdal.GetLastErrorMsg() != 'Cannot create that_wont_work while another file is being written in the .zip': gdaltest.post_reason('expected error') print(gdal.GetLastErrorMsg()) return 'fail' if f4 is not None: gdaltest.post_reason('should not have been successful 2') return 'fail' gdal.VSIFCloseL(f3) # Now we can close the main handle gdal.VSIFCloseL(hZIP) f = gdal.VSIFOpenL("/vsizip/vsimem/test.zip/subdir3/abcd", "rb") if f is None: gdaltest.post_reason('fail 5') return 'fail' data = gdal.VSIFReadL(1, 4, f) gdal.VSIFCloseL(f) gdal.Unlink("/vsimem/test.zip") if data.decode('ASCII') != 'abcd': print(data) return 'fail' return 'success'
def vsizip_2(): fmain = gdal.VSIFOpenL("/vsizip/vsimem/test2.zip/foo.bar", "wb") if fmain is None: gdaltest.post_reason('fail 1') return 'fail' gdal.VSIFWriteL("12345", 1, 5, fmain) gdal.VSIFCloseL(fmain) content = gdal.ReadDir("/vsizip/vsimem/test2.zip") if content != ['foo.bar']: gdaltest.post_reason('bad content 1') print(content) return 'fail' # Now append a second file fmain = gdal.VSIFOpenL("/vsizip/vsimem/test2.zip/bar.baz", "wb") if fmain is None: gdaltest.post_reason('fail 2') return 'fail' gdal.VSIFWriteL("67890", 1, 5, fmain) gdal.ErrorReset() gdal.PushErrorHandler('CPLQuietErrorHandler') content = gdal.ReadDir("/vsizip/vsimem/test2.zip") gdal.PopErrorHandler() if gdal.GetLastErrorMsg() != 'Cannot read a zip file being written': gdaltest.post_reason('expected error') print(gdal.GetLastErrorMsg()) return 'fail' if content != None: gdaltest.post_reason('bad content 2') print(content) return 'fail' gdal.VSIFCloseL(fmain) content = gdal.ReadDir("/vsizip/vsimem/test2.zip") if content != ['foo.bar', 'bar.baz']: gdaltest.post_reason('bad content 3') print(content) return 'fail' fmain = gdal.VSIFOpenL("/vsizip/vsimem/test2.zip/foo.bar", "rb") if fmain is None: gdaltest.post_reason('fail 3') return 'fail' data = gdal.VSIFReadL(1, 5, fmain) gdal.VSIFCloseL(fmain) if data.decode('ASCII') != '12345': print(data) return 'fail' fmain = gdal.VSIFOpenL("/vsizip/vsimem/test2.zip/bar.baz", "rb") if fmain is None: gdaltest.post_reason('fail 4') return 'fail' data = gdal.VSIFReadL(1, 5, fmain) gdal.VSIFCloseL(fmain) if data.decode('ASCII') != '67890': print(data) return 'fail' gdal.Unlink("/vsimem/test2.zip") return 'success'
def pam_11(): # Create a read-only directory try: os.chmod('tmpdirreadonly', stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR) shutil.rmtree('tmpdirreadonly') except: pass os.mkdir('tmpdirreadonly') shutil.copy('data/byte.tif', 'tmpdirreadonly/byte.tif') # FIXME: how do we create a read-only dir on windows ? # The following has no effect os.chmod('tmpdirreadonly', stat.S_IRUSR | stat.S_IXUSR) # Test that the directory is really read-only try: f = open('tmpdirreadonly/test', 'w') if f is not None: f.close() return 'skip' except: pass # Compute statistics --> the saving as .aux.xml should fail ds = gdal.Open('tmpdirreadonly/byte.tif') stats = ds.GetRasterBand(1).ComputeStatistics(False) if stats[0] != 74: gdaltest.post_reason('did not get expected minimum') return 'fail' gdal.ErrorReset() ds = None error_msg = gdal.GetLastErrorMsg() if error_msg.find('Unable to save auxilary information') != 0: gdaltest.post_reason('warning was expected at that point') return 'fail' # Check that we actually have no saved statistics ds = gdal.Open('tmpdirreadonly/byte.tif') stats = ds.GetRasterBand(1).GetStatistics(False, False) if stats[3] != -1: gdaltest.post_reason('did not expected to have stats at that point') return 'fail' ds = None # This must be run as an external process so we can override GDAL_PAM_PROXY_DIR # at the beginning of the process import test_py_scripts ret = test_py_scripts.run_py_script_as_external_script('.', 'pamproxydb', '-test1') #print(ret) if ret.find('success') == -1: gdaltest.post_reason('pamproxydb.py -test1 failed') print(ret) return 'fail' # Test loading an existing proxydb ret = test_py_scripts.run_py_script_as_external_script('.', 'pamproxydb', '-test2') #print(ret) if ret.find('success') == -1: gdaltest.post_reason('pamproxydb.py -test2 failed') print(ret) return 'fail' return 'success'
def __error__(self, err): gdalerr=gdal.GetLastErrorMsg();gdal.ErrorReset() self.__del__() errmsg = str(err) if gdalerr:errmsg += '\n%s' % gdalerr raise err.__class__, errmsg
def rasterlite_2(): if gdaltest.rasterlite_drv is None: return 'skip' # Test if SQLite3 supports rtrees try: os.remove('tmp/testrtree.sqlite') except: pass ds2 = ogr.GetDriverByName('SQLite').CreateDataSource( 'tmp/testrtree.sqlite') gdal.ErrorReset() ds2.ExecuteSQL( 'CREATE VIRTUAL TABLE testrtree USING rtree(id,minX,maxX,minY,maxY)') ds2.Destroy() try: os.remove('tmp/testrtree.sqlite') except: pass if gdal.GetLastErrorMsg().find('rtree') != -1: gdaltest.rasterlite_drv = None gdaltest.post_reason( 'Please upgrade your sqlite3 library to be able to read Rasterlite DBs (needs rtree support)!' ) return 'skip' gdal.ErrorReset() ds = gdal.Open('data/rasterlite.sqlite') if ds is None: if gdal.GetLastErrorMsg().find('unsupported file format') != -1: gdaltest.rasterlite_drv = None gdaltest.post_reason( 'Please upgrade your sqlite3 library to be able to read Rasterlite DBs!' ) return 'skip' else: return 'fail' if ds.RasterCount != 3: gdaltest.post_reason('expected 3 bands') return 'fail' if ds.GetRasterBand(1).GetOverviewCount() != 0: gdaltest.post_reason('did not expect overview') return 'fail' cs = ds.GetRasterBand(1).Checksum() expected_cs = 11746 if cs != expected_cs: gdaltest.post_reason( 'for band 1, cs = %d, different from expected_cs = %d' % (cs, expected_cs)) return 'fail' cs = ds.GetRasterBand(2).Checksum() expected_cs = 19843 if cs != expected_cs: gdaltest.post_reason( 'for band 2, cs = %d, different from expected_cs = %d' % (cs, expected_cs)) return 'fail' cs = ds.GetRasterBand(3).Checksum() expected_cs = 48911 if cs != expected_cs: gdaltest.post_reason( 'for band 3, cs = %d, different from expected_cs = %d' % (cs, expected_cs)) return 'fail' if ds.GetProjectionRef().find('WGS_1984') == -1: gdaltest.post_reason('projection_ref = %s' % ds.GetProjectionRef()) return 'fail' gt = ds.GetGeoTransform() expected_gt = (-180.0, 360. / ds.RasterXSize, 0.0, 90.0, 0.0, -180. / ds.RasterYSize) for i in range(6): if abs(gt[i] - expected_gt[i]) > 1e-15: print(gt) print(expected_gt) return 'fail' ds = None return 'success'
def ogr_geom_linestring_limits(): geom = ogr.CreateGeometryFromWkt('LINESTRING EMPTY') if geom.Length() != 0: return 'fail' gdal.ErrorReset() gdal.PushErrorHandler('CPLQuietErrorHandler') geom.GetPoint(-1) gdal.PopErrorHandler() if gdal.GetLastErrorType() == 0: return 'fail' gdal.ErrorReset() gdal.PushErrorHandler('CPLQuietErrorHandler') geom.GetPoint(0) gdal.PopErrorHandler() if gdal.GetLastErrorType() == 0: return 'fail' gdal.ErrorReset() gdal.PushErrorHandler('CPLQuietErrorHandler') geom.GetPoint_2D(-1) gdal.PopErrorHandler() if gdal.GetLastErrorType() == 0: return 'fail' gdal.ErrorReset() gdal.PushErrorHandler('CPLQuietErrorHandler') geom.GetPoint_2D(0) gdal.PopErrorHandler() if gdal.GetLastErrorType() == 0: return 'fail' gdal.ErrorReset() gdal.PushErrorHandler('CPLQuietErrorHandler') geom.SetPoint(-1, 5, 6, 7) gdal.PopErrorHandler() if gdal.GetLastErrorType() == 0: return 'fail' gdal.ErrorReset() gdal.PushErrorHandler('CPLQuietErrorHandler') geom.SetPoint_2D(-1, 5, 6) gdal.PopErrorHandler() if gdal.GetLastErrorType() == 0: return 'fail' if False: gdal.ErrorReset() gdal.PushErrorHandler('CPLQuietErrorHandler') geom.SetPoint(2147000000, 5, 6, 7) gdal.PopErrorHandler() if gdal.GetLastErrorType() == 0: return 'fail' gdal.ErrorReset() gdal.PushErrorHandler('CPLQuietErrorHandler') geom.SetPoint_2D(2147000000, 5, 6) gdal.PopErrorHandler() if gdal.GetLastErrorType() == 0: return 'fail' geom = ogr.CreateGeometryFromWkt('LINESTRING(0 0)') if geom.Length() != 0: return 'fail' geom = ogr.CreateGeometryFromWkt('LINESTRING(0 0, 1 0)') if geom.Length() != 1: return 'fail' return 'success'
def ogr_sql_28(): ds = ogr.GetDriverByName("Memory").CreateDataSource("my_ds") lyr = ds.CreateLayer("my_layer") 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 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 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(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", ] 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 ds2 = None return 'success'