def test_ogr_rfc41_2(): # Check implicit geometry field creation feature_defn = ogr.FeatureDefn() assert feature_defn.GetGeomFieldCount() == 1 assert feature_defn.GetGeomType() == ogr.wkbUnknown # Test IsSame() assert feature_defn.IsSame(feature_defn) == 1 other_feature_defn = ogr.FeatureDefn() assert feature_defn.IsSame(other_feature_defn) == 1 other_feature_defn.GetGeomFieldDefn(0).SetSpatialRef( osr.SpatialReference()) assert feature_defn.IsSame(other_feature_defn) == 0 feature_defn.GetGeomFieldDefn(0).SetSpatialRef(osr.SpatialReference()) assert feature_defn.IsSame(other_feature_defn) == 1 other_feature_defn.GetGeomFieldDefn(0).SetSpatialRef(None) assert feature_defn.IsSame(other_feature_defn) == 0 feature_defn = None feature_defn = ogr.FeatureDefn() # Check changing geometry type feature_defn.SetGeomType(ogr.wkbPoint) assert feature_defn.GetGeomType() == ogr.wkbPoint assert feature_defn.GetGeomFieldDefn(0).GetType() == ogr.wkbPoint # Check setting to wkbNone and implicitly destroying the field. for _ in range(2): feature_defn.SetGeomType(ogr.wkbNone) assert feature_defn.GetGeomFieldCount() == 0 assert feature_defn.GetGeomType() == ogr.wkbNone # Recreate the field for t in [ogr.wkbPoint, ogr.wkbLineString]: feature_defn.SetGeomType(t) assert feature_defn.GetGeomFieldCount() == 1 assert feature_defn.GetGeomType() == t assert feature_defn.GetGeomFieldDefn(0).GetType() == t # Test setting invalid value old_val = feature_defn.GetGeomType() gdal.PushErrorHandler('CPLQuietErrorHandler') feature_defn.SetGeomType(-3) gdal.PopErrorHandler() assert feature_defn.GetGeomType() == old_val # Test SetIgnored() / IsIgnored() assert feature_defn.IsGeometryIgnored() == 0 assert feature_defn.GetGeomFieldDefn(0).IsIgnored() == 0 feature_defn.SetGeometryIgnored(1) assert feature_defn.IsGeometryIgnored() == 1 assert feature_defn.GetGeomFieldDefn(0).IsIgnored() == 1 # Test wrong index values for GetGeomFieldDefn() for idx in [-1, 1]: gdal.PushErrorHandler('CPLQuietErrorHandler') ret = feature_defn.GetGeomFieldDefn(idx) gdal.PopErrorHandler() assert ret is None # Test GetGeomFieldIndex() assert feature_defn.GetGeomFieldIndex("") == 0 assert feature_defn.GetGeomFieldIndex("invalid") == -1 # Test AddGeomFieldDefn() gfld_defn = ogr.GeomFieldDefn('polygon_field', ogr.wkbPolygon) feature_defn.AddGeomFieldDefn(gfld_defn) assert feature_defn.GetGeomFieldCount() == 2 assert feature_defn.GetGeomFieldIndex("polygon_field") == 1 assert feature_defn.GetGeomFieldDefn(1).GetName() == 'polygon_field' # Test DeleteGeomFieldDefn() : error cases assert feature_defn.DeleteGeomFieldDefn(-1) != 0 assert feature_defn.DeleteGeomFieldDefn(2) != 0 assert feature_defn.GetGeomFieldCount() == 2 # Test DeleteGeomFieldDefn() : valid cases assert feature_defn.DeleteGeomFieldDefn(0) == 0 assert feature_defn.GetGeomFieldCount() == 1 assert feature_defn.GetGeomFieldIndex("polygon_field") == 0 assert feature_defn.DeleteGeomFieldDefn(0) == 0 assert feature_defn.GetGeomFieldCount() == 0 assert feature_defn.IsSame(feature_defn) == 1 assert feature_defn.IsSame(ogr.FeatureDefn()) == 0 feature_defn = None
def ogr_sql_44(): # Invalid parameters for sql in [ "SELECT hstore_get_value('a') FROM poly", "SELECT hstore_get_value(1, 1) FROM poly" ]: gdal.PushErrorHandler('CPLQuietErrorHandler') sql_lyr = gdaltest.ds.ExecuteSQL(sql) gdal.PopErrorHandler() if sql_lyr is not None: gdaltest.post_reason('fail') print(sql) return 'fail' # Invalid hstore syntax or empty result for sql in [ "SELECT hstore_get_value('a', null) FROM poly", "SELECT hstore_get_value(null, 'a') FROM poly", "SELECT hstore_get_value('a', 'a') FROM poly", "SELECT hstore_get_value('a=>b', 'c') FROM poly", "SELECT hstore_get_value('a=>', 'a') FROM poly", "SELECT hstore_get_value(' a => ', 'a') FROM poly", "SELECT hstore_get_value('a=>b,z,c=>d', 'c') FROM poly", "SELECT hstore_get_value('\"a', 'a') FROM poly", "SELECT hstore_get_value('\"a\"', 'a') FROM poly", "SELECT hstore_get_value('\"a\"=', 'a') FROM poly", "SELECT hstore_get_value('\"a\" =>', 'a') FROM poly", "SELECT hstore_get_value('\"a\" => ', 'a') FROM poly", "SELECT hstore_get_value('\"a\" => \"', 'a') FROM poly", "SELECT hstore_get_value('\"a\" => \"\" z', 'a') FROM poly" ]: sql_lyr = gdaltest.ds.ExecuteSQL(sql) f = sql_lyr.GetNextFeature() if f.IsFieldSetAndNotNull(0): gdaltest.post_reason('fail') print(sql) f.DumpReadable() return 'fail' gdaltest.ds.ReleaseResultSet(sql_lyr) # Valid hstore syntax for (sql, expected) in [ ("SELECT hstore_get_value('a=>b', 'a') FROM poly", 'b'), ("SELECT hstore_get_value(' a => b ', 'a') FROM poly", 'b'), ("SELECT hstore_get_value('\"a\"=>b', 'a') FROM poly", 'b'), ("SELECT hstore_get_value(' \"a\" =>b', 'a') FROM poly", 'b'), ("SELECT hstore_get_value('a=>\"b\"', 'a') FROM poly", 'b'), ("SELECT hstore_get_value('a=> \"b\" ', 'a') FROM poly", 'b'), ("SELECT hstore_get_value('\"a\"=>\"b\"', 'a') FROM poly", 'b'), ("SELECT hstore_get_value(' \"a\" => \"b\" ', 'a') FROM poly", 'b'), ("SELECT hstore_get_value(' \"a\\\"b\" => \"b\" ', 'a\"b') FROM poly", 'b') ]: sql_lyr = gdaltest.ds.ExecuteSQL(sql) f = sql_lyr.GetNextFeature() if f.GetField(0) != expected: gdaltest.post_reason('fail') print(sql) f.DumpReadable() return 'fail' gdaltest.ds.ReleaseResultSet(sql_lyr) return 'success'
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 main(argv): frmt = None options = [] quiet_flag = 0 src_filename = None src_band_n = 1 dst_filename = None dst_layername = None dst_fieldname = None dst_field = -1 mask = 'default' argv = gdal.GeneralCmdLineProcessor(argv) if argv is None: return 0 # Parse command line arguments. i = 1 while i < len(argv): arg = argv[i] if arg == '-f' or arg == '-of': i = i + 1 frmt = argv[i] elif arg == '-q' or arg == '-quiet': quiet_flag = 1 elif arg == '-8': options.append('8CONNECTED=8') elif arg == '-nomask': mask = 'none' elif arg == '-mask': i = i + 1 mask = argv[i] elif arg == '-b': i = i + 1 if argv[i].startswith('mask'): src_band_n = argv[i] else: src_band_n = int(argv[i]) elif src_filename is None: src_filename = argv[i] elif dst_filename is None: dst_filename = argv[i] elif dst_layername is None: dst_layername = argv[i] elif dst_fieldname is None: dst_fieldname = argv[i] else: return Usage() i = i + 1 if src_filename is None or dst_filename is None: return Usage() if frmt is None: frmt = GetOutputDriverFor(dst_filename, is_raster=False) if dst_layername is None: dst_layername = 'out' # ============================================================================= # Verify we have next gen bindings with the polygonize method. # ============================================================================= try: gdal.Polygonize except AttributeError: print('') print( 'gdal.Polygonize() not available. You are likely using "old gen"') print('bindings or an older version of the next gen bindings.') print('') return 1 # ============================================================================= # Open source file # ============================================================================= src_ds = gdal.Open(src_filename) if src_ds is None: print('Unable to open %s' % src_filename) return 1 if src_band_n == 'mask': srcband = src_ds.GetRasterBand(1).GetMaskBand() # Workaround the fact that most source bands have no dataset attached options.append('DATASET_FOR_GEOREF=' + src_filename) elif isinstance(src_band_n, str) and src_band_n.startswith('mask,'): srcband = src_ds.GetRasterBand(int( src_band_n[len('mask,'):])).GetMaskBand() # Workaround the fact that most source bands have no dataset attached options.append('DATASET_FOR_GEOREF=' + src_filename) else: srcband = src_ds.GetRasterBand(src_band_n) if mask == 'default': maskband = srcband.GetMaskBand() elif mask == '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(frmt) if not quiet_flag: print('Creating output %s of format %s.' % (dst_filename, frmt)) dst_ds = drv.CreateDataSource(dst_filename) # ============================================================================= # Find or create destination layer. # ============================================================================= try: dst_layer = dst_ds.GetLayerByName(dst_layername) except: dst_layer = None if dst_layer is None: srs = src_ds.GetSpatialRef() dst_layer = dst_ds.CreateLayer(dst_layername, geom_type=ogr.wkbPolygon, srs=srs) if dst_fieldname is None: dst_fieldname = 'DN' fd = ogr.FieldDefn(dst_fieldname, ogr.OFTInteger) dst_layer.CreateField(fd) dst_field = 0 else: if dst_fieldname is not None: dst_field = dst_layer.GetLayerDefn().GetFieldIndex(dst_fieldname) if dst_field < 0: print("Warning: cannot find field '%s' in layer '%s'" % (dst_fieldname, dst_layername)) # ============================================================================= # Invoke algorithm. # ============================================================================= if quiet_flag: prog_func = None else: prog_func = gdal.TermProgress_nocb result = gdal.Polygonize(srcband, maskband, dst_layer, dst_field, options, callback=prog_func) srcband = None src_ds = None dst_ds = None mask_ds = None return result
def test_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() assert ret != 0 ret = lyr.CreateField(ogr.FieldDefn('myfid', ogr.OFTInteger)) assert ret == 0 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) assert ret == 0 assert feat.GetFID() == 10 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) assert ret == 0 if feat.GetFID() < 0: feat.DumpReadable() pytest.fail() if feat.GetField('myfid') != feat.GetFID(): feat.DumpReadable() pytest.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() assert ret != 0 # 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) assert ret == 0 assert feat.GetFID() == 12 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') assert (not ("""CREATE TABLE "public"."test" ( "myfid" SERIAL, CONSTRAINT "test_pk" PRIMARY KEY ("myfid") )""" not in sql or \ """ALTER TABLE "public"."test" ADD COLUMN "myfid" """ in sql 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))
def test_ogr_csw_vsimem_csw_minimal_instance(): 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') assert ds is not None ds.TestCapability('foo') assert ds.GetLayerCount() == 1 assert ds.GetLayer(-1) is None and ds.GetLayer(1) is None lyr = ds.GetLayer(0) lyr.TestCapability('foo') gdal.PushErrorHandler() f = lyr.GetNextFeature() gdal.PopErrorHandler() assert f is None gdal.PushErrorHandler() fc = lyr.GetFeatureCount() gdal.PopErrorHandler() assert fc == 0 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() assert f is None and gdal.GetLastErrorMsg().find( 'Empty content returned by server') >= 0 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() assert f is None and gdal.GetLastErrorMsg().find( 'Error: cannot parse') >= 0 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() assert f is None and gdal.GetLastErrorMsg().find( 'Error: cannot parse') >= 0 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() assert f is None and gdal.GetLastErrorMsg().find( 'Error returned by server') >= 0 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() assert f is not None 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))': f.DumpReadable() pytest.fail() f = lyr.GetNextFeature() assert f is not None gdal.PushErrorHandler() f = lyr.GetNextFeature() gdal.PopErrorHandler() assert f is None gdal.PushErrorHandler() fc = lyr.GetFeatureCount() gdal.PopErrorHandler() assert fc == 2 lyr.ResetReading() f = lyr.GetNextFeature() assert f is not None 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() assert f is not None f = lyr.GetNextFeature() assert f is not None 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() assert f is None 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() assert fc == 3, gdal.GetLastErrorMsg() 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() assert fc == 3, gdal.GetLastErrorMsg() 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() assert fc == 3, gdal.GetLastErrorMsg() 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() assert fc == 3, gdal.GetLastErrorMsg() 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() assert fc == 200 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() assert f is not None 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() assert fc == 300 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)
def test_basic_test_11(): ds = gdal.OpenEx('data/byte.tif') assert ds is not None ds = gdal.OpenEx('data/byte.tif', gdal.OF_RASTER) assert ds is not None ds = gdal.OpenEx('data/byte.tif', gdal.OF_VECTOR) assert ds is None ds = gdal.OpenEx('data/byte.tif', gdal.OF_RASTER | gdal.OF_VECTOR) assert ds is not None ds = gdal.OpenEx('data/byte.tif', gdal.OF_ALL) assert ds is not None ds = gdal.OpenEx('data/byte.tif', gdal.OF_UPDATE) assert ds is not None ds = gdal.OpenEx('data/byte.tif', gdal.OF_RASTER | gdal.OF_VECTOR | gdal.OF_UPDATE | gdal.OF_VERBOSE_ERROR) assert ds is not None ds = gdal.OpenEx('data/byte.tif', allowed_drivers=[]) assert ds is not None ds = gdal.OpenEx('data/byte.tif', allowed_drivers=['GTiff']) assert ds is not None ds = gdal.OpenEx('data/byte.tif', allowed_drivers=['PNG']) assert ds is None with gdaltest.error_handler(): ds = gdal.OpenEx('data/byte.tif', open_options=['FOO']) assert ds is not None ar_ds = [gdal.OpenEx('data/byte.tif', gdal.OF_SHARED) for _ in range(1024)] assert ar_ds[1023] is not None ar_ds = None ds = gdal.OpenEx('../ogr/data/poly.shp', gdal.OF_RASTER) assert ds is None ds = gdal.OpenEx('../ogr/data/poly.shp', gdal.OF_VECTOR) assert ds is not None assert ds.GetLayerCount() == 1 assert ds.GetLayer(0) is not None ds.GetLayer(0).GetMetadata() ds = gdal.OpenEx('../ogr/data/poly.shp', allowed_drivers=['ESRI Shapefile']) assert ds is not None ds = gdal.OpenEx('../ogr/data/poly.shp', gdal.OF_RASTER | gdal.OF_VECTOR) assert ds is not None ds = gdal.OpenEx('non existing') assert ds is None and gdal.GetLastErrorMsg() == '' gdal.PushErrorHandler('CPLQuietErrorHandler') ds = gdal.OpenEx('non existing', gdal.OF_VERBOSE_ERROR) gdal.PopErrorHandler() assert ds is None and gdal.GetLastErrorMsg() != '' old_use_exceptions_status = gdal.GetUseExceptions() gdal.UseExceptions() got_exception = False try: ds = gdal.OpenEx('non existing') except RuntimeError: got_exception = True if old_use_exceptions_status == 0: gdal.DontUseExceptions() assert got_exception
def test_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) assert data is not None data_ar = struct.unpack('h' * 10 * 10, data) cs = rasterio_9_checksum(data, 10, 10, data_type=gdal.GDT_Int16) assert cs == 1211 assert abs(tab[0] - 1.0) <= 1e-5 # 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) assert data_ar == data_float32_ar # 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) assert data is not None cs = rasterio_9_checksum(data, 10, 10) assert cs == 1154 assert abs(tab[0] - 1.0) <= 1e-5 # 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) assert data is not None cs = rasterio_9_checksum(data, 10, 10, data_type=gdal.GDT_UInt16) assert cs == 1211 assert abs(tab[0] - 1.0) <= 1e-5 # 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) assert data is not None cs = rasterio_9_checksum(data, 10, 10, data_type=gdal.GDT_CInt16) assert cs == 1211 assert abs(tab[0] - 1.0) <= 1e-5 # 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() assert data is None assert tab[0] >= 0.50 # 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) assert data is not None cs = rasterio_9_checksum(data, 10, 10) assert cs == 1089 assert abs(tab[0] - 1.0) <= 1e-5 # 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) assert data is not None cs = rasterio_9_checksum(data, 10, 10) assert cs == 1059 assert abs(tab[0] - 1.0) <= 1e-5 # 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) assert data is not None cs = rasterio_9_checksum(data, 5, 5) assert cs == 214 # Same with UInt16 data = src_ds_uint16.GetRasterBand(1).ReadRaster(buf_xsize=5, buf_ysize=5, resample_alg=gdal.GRIORA_Cubic) assert data is not None cs = rasterio_9_checksum(data, 5, 5, data_type=gdal.GDT_UInt16) assert cs == 214 # 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) assert data is not None cs = rasterio_9_checksum(data, 40, 40) assert cs == 19556 assert abs(tab[0] - 1.0) <= 1e-5 # 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) assert data is not None cs = rasterio_9_checksum(data, 40, 40) assert cs == 19041 assert abs(tab[0] - 1.0) <= 1e-5 # 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) assert data is not None cs = rasterio_9_checksum(data[0:25 * 25], 25, 25) assert cs == 5975 cs = rasterio_9_checksum(data[25 * 25:2 * 25 * 25], 25, 25) assert cs == 6248 assert abs(tab[0] - 1.0) <= 1e-5 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) assert data is not None cs = rasterio_9_checksum(data, 162 * 16, 150 * 16) assert cs == 30836 assert abs(tab[0] - 1.0) <= 1e-5
def main_by_params(params=None): global verbose, quiet verbose = 0 quiet = 0 names = [] #名字 format = 'GTiff' #格式 out_file = 'out.tif' #输出文件名 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 gdal.AllRegister() if params is None: sys.exit( 0 ) # Parse params arguments. if "-o" in params: out_file = params["-o"] if "input_files" in params: names = params["input_files"] if "-v" in params: verbose = 1 if "-q" in params or "-quiet" in params: quiet = 1 if "-createonly" in params: createonly = 1 if '-separate' in params: separate = 1 if '-pct' in params: copy_pct = 1 if '-ot' in params: band_type = gdal.GetDataTypeByName( params["ot"] ) if band_type == gdal.GDT_Unknown: print('Unknown GDAL 2001 type: %s' % params["ot"]) sys.exit( 1 ) if '-init' in params: str_pre_init = params["init"] for x in str_pre_init: pre_init.append(float(x)) if '-n' in params: nodata = float(params["n"]) if '-a_nodata' in params: a_nodata = float(params["a_nodata"]) if '-of' in params: #格式 format = params["-of"] if '-co' in params: create_options.append( params["co"] ) if '-ps' in params: ps = params["ps"] psize_x = float(ps[0]) psize_y = -1 * abs(float(ps[1])) if '-tap' in params: bTargetAlignedPixels = True if '-ul_lr' in params: a = params["ul_lr"] ulx = float(a[0]) uly = float(a[1]) lrx = float(a[2]) lry = float(a[3]) if len(names) == 0: print('No input files selected.') Usage() sys.exit( 1 ) Driver = gdal.GetDriverByName(format) if Driver is None: print('Format driver %s not found, pick a supported driver.' % format) 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).' % format) 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 2001 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." \ % (fi_processed+1,len(file_infos), fi_processed * 100.0 / len(file_infos)) ) 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 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 test_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() assert res is None, 'expected None' assert error_msg.find('this band does not exist on dataset') != -1, \ 'did not get expected error msg' res = ds.ReadRaster(0, 0, 1, 1, band_list=[1, 1]) assert res is not None, 'expected non None' 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() assert res is None, 'expected None' assert error_msg.find('Access window out of range in RasterIO()') != -1, \ 'did not get expected error msg (1)' # 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() assert res is None, 'expected None' assert error_msg.find('Integer overflow') != -1, \ 'did not get expected error msg (2)' gdal.ErrorReset() gdal.PushErrorHandler('CPLQuietErrorHandler') res = obj.ReadRaster(0, 0, 0, 1) gdal.PopErrorHandler() error_msg = gdal.GetLastErrorMsg() assert res is None, 'expected None' assert error_msg.find('Illegal values for buffer size') != -1, \ 'did not get expected error msg (3)' ds = None
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'
import unittest from contextlib import contextmanager import os import uuid from osgeo import gdal from gdaljson import VRTDataset, VRTWarpedDataset import gdaljson_utils as utils gdal.UseExceptions() gdal.PushErrorHandler("CPLQuietErrorHandler") class VRTTestCases(unittest.TestCase): """ Testing equivalency between VRT made natively with gdal (gdal.Warp/gdal.Translate) and VRT made with gdaljson """ @staticmethod def read_vsimem(fn): """Read GDAL vsimem files""" vsifile = gdal.VSIFOpenL(fn, "r") gdal.VSIFSeekL(vsifile, 0, 2) vsileng = gdal.VSIFTellL(vsifile) gdal.VSIFSeekL(vsifile, 0, 0) return gdal.VSIFReadL(1, vsileng, vsifile) def setUp(self): self.warpedvrt = os.path.join(os.path.split(__file__)[0], "templates/warped.vrt")
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 write_to_file(self, filename, sublayer=None): """Save vector data to file Args: * filename: filename with extension .shp or .gml * sublayer: Optional string for writing a sublayer. Ignored unless we are writing to an sqlite file. 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 basename, 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 layername from filename (excluding preceding dirs) if sublayer is None or extension == '.shp': layername = os.path.split(basename)[-1] else: layername = 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(layername, self.projection.spatial_reference, self.geometry_type) if lyr is None: msg = 'Could not create layer %s' % layername 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 ogrtypes = {} 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) ogrtypes[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, ogrtypes[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 = array2line(geometry[i], geometry_type=ogr.wkbLineString) elif self.is_polygon_data: # Create polygon geometry geom = ogr.Geometry(ogr.wkbPolygon) # Add outer ring linear_ring = array2line(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( array2line(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, basename + '.keywords')
def test_ecrgtoc_4(): toc_xml = """<Table_of_Contents> <file_header file_status="new"> <file_name>TOC.xml</file_name> </file_header> <product product_title="ProductTitle"> <disc id="DiscId"> <frame_list number_of_frames="2"> <scale size="1:500 K"> <frame name="000000009s0013.lf2"> <frame_path>clfc\\2</frame_path> <frame_version>001</frame_version> <frame_chart_type>lf</frame_chart_type> <frame_zone>2</frame_zone> </frame> </scale> <scale size="1:1000 K"> <frame name="000000009s0013.lf2"> <frame_path>clfc\\2</frame_path> <frame_version>001</frame_version> <frame_chart_type>lf</frame_chart_type> <frame_zone>2</frame_zone> </frame> </scale> </frame_list> </disc> <disc id="DiscId2"> <frame_list number_of_frames="1"> <scale size="1:500 K"> <frame name="000000009t0013.lf2"> <frame_path>clfc\\2</frame_path> <frame_version>001</frame_version> <frame_chart_type>lf</frame_chart_type> <frame_zone>2</frame_zone> </frame> </scale> </frame_list> </disc> </product> <extension_list> <extension code="LF"> <chart_code>LF</chart_code> <chart_type>1:500 K (LFC Day)</chart_type> <chart_scale>1:500 K</chart_scale> <chart_description>LFC Day</chart_description> </extension> </extension_list> </Table_of_Contents>""" f = gdal.VSIFOpenL('/vsimem/TOC.xml', 'wb') gdal.VSIFWriteL(toc_xml, 1, len(toc_xml), f) gdal.VSIFCloseL(f) ds = gdal.Open('/vsimem/TOC.xml') assert ds is not None assert ds.RasterCount == 0, 'bad raster count' expected_gt = (-85.43147208121826, 0.00059486040609137061, 0.0, 37.241379310344833, 0.0, -0.00044985604606525913) gt = ds.GetGeoTransform() for i in range(6): assert abs(gt[i] - expected_gt[i] ) <= 1e-10, 'did not get expected geotransform' wkt = ds.GetProjectionRef() assert wkt.find('WGS 84') != -1, 'did not get expected SRS' filelist = ds.GetFileList() assert len(filelist) == 4, 'did not get expected filelist' subdatasets = ds.GetMetadata('SUBDATASETS') if len(subdatasets) != 6: print(filelist) pytest.fail('did not get expected subdatasets') ds = None ds = gdal.Open( 'ECRG_TOC_ENTRY:ProductTitle:DiscId:1_500_K:/vsimem/TOC.xml') assert ds is not None, 'did not get subdataset' ds = None ds = gdal.Open( 'ECRG_TOC_ENTRY:ProductTitle:DiscId:1_1000_K:/vsimem/TOC.xml') assert ds is not None, 'did not get subdataset' ds = None ds = gdal.Open( 'ECRG_TOC_ENTRY:ProductTitle:DiscId2:1_500_K:/vsimem/TOC.xml') assert ds is not None, 'did not get subdataset' ds = None gdal.PushErrorHandler('CPLQuietErrorHandler') ds = gdal.Open('ECRG_TOC_ENTRY:ProductTitle:DiscId:/vsimem/TOC.xml') gdal.PopErrorHandler() assert ds is None, 'should not have got subdataset' gdal.Unlink('/vsimem/TOC.xml')
def ogr_oci_20(): if gdaltest.oci_ds is None: return 'skip' lyr = gdaltest.oci_ds.CreateLayer( 'ogr_oci_20', geom_type=ogr.wkbPoint, options=['GEOMETRY_NULLABLE=NO', 'DIM=2']) if lyr.GetLayerDefn().GetGeomFieldDefn(0).IsNullable() != 0: gdaltest.post_reason('fail') return 'fail' 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 if ret != 0: gdaltest.post_reason('fail') return 'fail' # Error case: missing geometry f = ogr.Feature(lyr.GetLayerDefn()) f.SetField('field_not_nullable', 'not_null') gdal.PushErrorHandler() ret = lyr.CreateFeature(f) gdal.PopErrorHandler() if ret == 0: gdaltest.post_reason('fail') return 'fail' 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() if ret == 0: gdaltest.post_reason('fail') return 'fail' f = None lyr.SyncToDisk() # Test with nullable geometry lyr = gdaltest.oci_ds.CreateLayer('ogr_oci_20bis', geom_type=ogr.wkbPoint, options=['DIM=2']) if lyr.GetLayerDefn().GetGeomFieldDefn(0).IsNullable() != 1: gdaltest.post_reason('fail') return 'fail' f = ogr.Feature(lyr.GetLayerDefn()) f.SetGeometryDirectly(ogr.CreateGeometryFromWkt('POINT(0 1)')) ret = lyr.CreateFeature(f) f = None if ret != 0: gdaltest.post_reason('fail') return 'fail' f = ogr.Feature(lyr.GetLayerDefn()) ret = lyr.CreateFeature(f) f = None if ret != 0: gdaltest.post_reason('fail') return 'fail' lyr.SyncToDisk() oci_ds2 = ogr.Open(os.environ['OCI_DSNAME']) lyr = oci_ds2.GetLayerByName('ogr_oci_20') if lyr.GetLayerDefn().GetFieldDefn(lyr.GetLayerDefn().GetFieldIndex( 'field_not_nullable')).IsNullable() != 0: gdaltest.post_reason('fail') return 'fail' if lyr.GetLayerDefn().GetFieldDefn(lyr.GetLayerDefn().GetFieldIndex( 'field_nullable')).IsNullable() != 1: gdaltest.post_reason('fail') return 'fail' if lyr.GetLayerDefn().GetGeomFieldDefn(0).IsNullable() != 0: gdaltest.post_reason('fail') return 'fail' lyr = oci_ds2.GetLayerByName('ogr_oci_20bis') if lyr.GetLayerDefn().GetGeomFieldDefn(0).IsNullable() != 1: gdaltest.post_reason('fail') return 'fail' feat = lyr.GetNextFeature() if feat.GetGeometryRef() is None: gdaltest.post_reason('fail') return 'fail' feat = lyr.GetNextFeature() if feat.GetGeometryRef() is not None: gdaltest.post_reason('fail') return 'fail' return 'success'
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 test_ogr_csw_vsimem_fail_because_not_enabled(): gdal.PushErrorHandler() ds = ogr.Open('CSW:/vsimem/csw_endpoint') gdal.PopErrorHandler() assert ds is None
def 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") if gdal.GetLastErrorMsg() != '': gdaltest.post_reason('fail') return 'fail' f = lyr.GetNextFeature() if f is None: gdaltest.post_reason('fail') return 'fail' 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" ) if gdal.GetLastErrorMsg() != '': gdaltest.post_reason('fail') return 'fail' f = lyr.GetNextFeature() if f is None: gdaltest.post_reason('fail') return 'fail' 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() if gdal.GetLastErrorMsg( ) != 'Passed field name field.withpoint should have been surrounded by double quotes. Accepted since there is no ambiguity...': gdaltest.post_reason('fail') return 'fail' f = lyr.GetNextFeature() if f is None: gdaltest.post_reason('fail') return 'fail' 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() if gdal.GetLastErrorMsg( ) != '"field"."withpoint" not recognised as an available field.': gdaltest.post_reason('fail') return 'fail' if lyr is not None: gdaltest.post_reason('fail') return 'fail' # Test our tolerance against unnecessary quoting gdal.ErrorReset() gdal.PushErrorHandler() lyr = ds.ExecuteSQL( "SELECT * FROM \"lyr.withpoint\" f WHERE \"f.foo\" = 2") gdal.PopErrorHandler() if gdal.GetLastErrorMsg( ) != 'Passed field name f.foo should NOT have been surrounded by double quotes. Accepted since there is no ambiguity...': gdaltest.post_reason('fail') return 'fail' f = lyr.GetNextFeature() if f is None: gdaltest.post_reason('fail') return 'fail' gdaltest.ds.ReleaseResultSet(lyr) return 'success'
def updateVariable(self): dim_map = dict() self.dim_names = [] self.dim_values = dict() self.dim_values2 = dict() self.dim_def = dict() self.dim_band = dict() self.dim1Count = 0 self.dim2Count = 0 self.clear() uri = 'NETCDF:"%s":%s' % (self.ui.leFileName.text(), self.ui.cboVars.currentText()) if debug > 0: print('updateVariable ' + str(uri)) #look for extra dim definitions # NETCDF_DIM_EXTRA={time,tile} # NETCDF_DIM_tile_DEF={3,6} # NETCDF_DIM_tile_VALUES={1,2,3} # NETCDF_DIM_time_DEF={12,6} # NETCDF_DIM_time_VALUES={1,32,60,91,121,152,182,213,244,274,305,335} # open file and get basic info gdal.PushErrorHandler('CPLQuietErrorHandler') ds = gdal.Open(uri) gdal.PopErrorHandler() if ds is None: return wkt = ds.GetProjection() md = ds.GetMetadata() ds = None if md is None: return # update CRS selectors projectCrs = self.iface.mapCanvas().mapRenderer().destinationCrs() self.ui.cboCrs.setItemText( 1, self.tr("Project") + " (%s, %s)" % (projectCrs.description(), projectCrs.authid())) if not wkt or not self.layerCrs.createFromWkt(wkt): self.layerCrs = QgsCoordinateReferenceSystem() if debug > 0: print('wkt: ' + wkt + ' layer desc:' + self.layerCrs.description()) # if layer has valid crs, use that, if not use selected crs if self.layerCrs.description(): self.ui.cboCrs.setItemText( 0, self.tr("Layer") + " (%s, %s)" % (self.layerCrs.description(), self.layerCrs.authid())) self.ui.cboCrs.setCurrentIndex(0) else: self.ui.cboCrs.setItemText(0, self.tr("Layer (None)")) if self.selectedCrs.description(): self.ui.cboCrs.setItemText( 2, self.tr("Selected") + " (%s, %s)" % (self.selectedCrs.description(), self.selectedCrs.authid()) ) else: self.ui.cboCrs.setItemText(2, self.tr("Selected (None)")) self.ui.cboCrs.setCurrentIndex(2) ds = None # iterate over all md items looking for dim info for key in sorted(md.iterkeys()): if key.startswith('NETCDF_DIM_'): line = "%s=%s" % (key, md[key]) m = re.search('^(NETCDF_DIM_.+)={(.+)}', line) if m is not None: dim_map[m.group(1)] = m.group(2) if not 'NETCDF_DIM_EXTRA' in dim_map: self.warning() return tok = dim_map['NETCDF_DIM_EXTRA'] if tok is not None: for dim in tok.split(','): self.dim_names.append(dim) tok2 = dim_map.get('NETCDF_DIM_' + dim + '_VALUES') self.dim_values[dim] = [] if tok2 is not None: for s in tok2.split(','): self.dim_values[dim].append(num(s)) tok2 = dim_map.get('NETCDF_DIM_' + dim + '_DEF') self.dim_def[dim] = [] if tok2 is not None: for s in tok2.split(','): self.dim_def[dim].append(num(s)) # remove any dims which have only 1 element dim_names = self.dim_names self.dim_names = [] for dim in dim_names: if self.dim_def[dim][0] <= 1: del self.dim_values[dim] del self.dim_def[dim] else: self.dim_names.append(dim) # transform time dimensions - currently requires netcdftime from python-netcdf4 if has_netcdftime: for dim in dim_names: #dim+"#standard_name" in md and md[dim+"#standard_name"] == "time": if dim in self.dim_values: if dim + "#units" in md: timestr = md[dim + "#units"] units = timestr.split()[0].lower() if units in _units: try: dates = num2date(self.dim_values[dim], units=timestr) except ValueError: continue self.dim_values2[dim] = [] only_days = True for date in dates: val = date.isoformat( " " ) # equivalent to strftime("%Y-%m-%d %H:%M:%S") if not val.endswith(" 00:00:00"): only_days = False self.dim_values2[dim].append(val) if only_days: for i in range(0, len(self.dim_values2[dim])): self.dim_values2[dim][ i] = self.dim_values2[dim][i][0:10] if debug > 1: print(str(dim_map)) print(str(self.dim_names)) print(str(self.dim_def)) print(str(self.dim_values)) print(str(self.dim_values2)) # update UI self.ui.pbnDim1.setEnabled(False) self.ui.pbnDim2.setEnabled(False) if len(self.dim_names) > 0: dim = self.dim_names[0] self.ui.lblDim1.setText(dim) menu = MyMenu() action = QAction(self.tr('all/none'), menu) action.setCheckable(True) menu.addAction(action) for i in range(0, len(self.dim_values[dim])): self.dim2Count = self.dim2Count + 1 value = self.dim_values2[dim][ i] if dim in self.dim_values2 else self.dim_values[dim][i] action = QAction(str(value), menu) action.setCheckable(True) menu.addAction(action) self.ui.pbnDim1.setMenu(menu) QObject.connect(self.ui.pbnDim1.menu(), SIGNAL("triggered(QAction *)"), self.on_pbnDimx_triggered) # click first element of each dim if len(menu.actions()) > 1: menu.actions()[1].setChecked(True) self.ui.pbnDim1.setEnabled(True) if len(self.dim_names) > 1: dim = self.dim_names[1] self.ui.lblDim2.setText(dim) menu = MyMenu() action = QAction(self.tr('all/none'), menu) action.setCheckable(True) menu.addAction(action) for i in range(0, len(self.dim_values[dim])): self.dim2Count = self.dim2Count + 1 value = self.dim_values[dim][i] action = QAction(str(value), menu) action.setCheckable(True) menu.addAction(action) self.ui.pbnDim2.setMenu(menu) QObject.connect(self.ui.pbnDim2.menu(), SIGNAL("triggered(QAction *)"), self.on_pbnDimx_triggered) # click first element of each dim if len(menu.actions()) > 1: menu.actions()[1].setChecked(True) self.ui.pbnDim2.setEnabled(True) # make sure we found something, if not notify user if len(self.dim_names) == 0: self.warning2() self.updateURI() self.updateDims()
def test_mem_2(): gdal.PushErrorHandler('CPLQuietErrorHandler') ds = gdal.Open('MEM:::') gdal.PopErrorHandler() assert ds is None, 'opening MEM dataset should have failed.' try: import ctypes except ImportError: pytest.skip() for libname in ['msvcrt', 'libc.so.6']: try: crt = ctypes.CDLL(libname) except OSError: crt = None if crt is not None: break if crt is None: pytest.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: pytest.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: free(p) pytest.fail('opening MEM dataset failed.') chksum = ds.GetRasterBand(1).Checksum() if chksum != 750: print(chksum) free(p) pytest.fail('checksum failed.') ds.GetRasterBand(1).Fill(100.0) ds.FlushCache() if float_p[0] != 100.0: print(float_p[0]) free(p) pytest.fail('fill seems to have failed.') ds = None free(p)
def main(argv=None): global verbose, quiet verbose = 0 quiet = 0 names = [] format = 'GTiff' out_file = 'out.tif' 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': # for backward compatibility. i = i + 1 format = argv[i] elif arg == '-of': i = i + 1 format = 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('Unrecognised command option: %s' % arg) Usage() sys.exit(1) else: names.append(arg) i = i + 1 if len(names) == 0: print('No input files selected.') Usage() sys.exit(1) Driver = gdal.GetDriverByName(format) if Driver is None: print('Format driver %s not found, pick a supported driver.' % format) 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).' % format) 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_style_styletable(): style_table = ogr.StyleTable() style_table.AddStyle("style1_normal", 'SYMBOL(id:"http://style1_normal",c:#67452301)') gdal.PushErrorHandler('CPLQuietErrorHandler') ret = style_table.SaveStyleTable('/nonexistingdir/nonexistingfile') gdal.PopErrorHandler() if ret != 0: gdaltest.post_reason('failure') print(ret) return 'fail' if style_table.SaveStyleTable("/vsimem/out.txt") != 1: gdaltest.post_reason('failure') return 'fail' style_table = None style_table = ogr.StyleTable() gdal.PushErrorHandler('CPLQuietErrorHandler') ret = style_table.LoadStyleTable('/nonexistent') gdal.PopErrorHandler() if ret != 0: gdaltest.post_reason('failure') return 'fail' if style_table.LoadStyleTable('/vsimem/out.txt') != 1: gdaltest.post_reason('failure') return 'fail' gdal.Unlink('/vsimem/out.txt') gdal.PushErrorHandler('CPLQuietErrorHandler') ret = style_table.Find("non_existing_style") gdal.PopErrorHandler() if ret is not None: gdaltest.post_reason('failure') return 'fail' if style_table.Find("style1_normal") != 'SYMBOL(id:"http://style1_normal",c:#67452301)': gdaltest.post_reason('failure') return 'fail' style = style_table.GetNextStyle() if style != 'SYMBOL(id:"http://style1_normal",c:#67452301)': gdaltest.post_reason('failure') return 'fail' style_name = style_table.GetLastStyleName() if style_name != 'style1_normal': gdaltest.post_reason('failure') return 'fail' style = style_table.GetNextStyle() if style is not None: gdaltest.post_reason('failure') return 'fail' style_table.ResetStyleStringReading() style = style_table.GetNextStyle() if style is None: gdaltest.post_reason('failure') return 'fail' # GetStyleTable()/SetStyleTable() on data source ds = ogr.GetDriverByName('Memory').CreateDataSource('') if ds.GetStyleTable() is not None: gdaltest.post_reason('failure') return 'fail' ds.SetStyleTable(None) if ds.GetStyleTable() is not None: gdaltest.post_reason('failure') return 'fail' ds.SetStyleTable(style_table) style_table2 = ds.GetStyleTable() style = style_table2.GetNextStyle() if style != 'SYMBOL(id:"http://style1_normal",c:#67452301)': gdaltest.post_reason('failure') return 'fail' # GetStyleTable()/SetStyleTable() on layer lyr = ds.CreateLayer('foo') if lyr.GetStyleTable() is not None: gdaltest.post_reason('failure') return 'fail' lyr.SetStyleTable(None) if lyr.GetStyleTable() is not None: gdaltest.post_reason('failure') return 'fail' lyr.SetStyleTable(style_table) style_table2 = lyr.GetStyleTable() style = style_table2.GetNextStyle() if style != 'SYMBOL(id:"http://style1_normal",c:#67452301)': gdaltest.post_reason('failure') return 'fail' ds = None return 'success'
# # Author: Simran Sangha & David Bekaert # Copyright 2019, by the California Institute of Technology. ALL RIGHTS # RESERVED. United States Government Sponsorship acknowledged. # #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ import os import ogr import numpy as np from osgeo import gdal, ogr gdal.UseExceptions() #Suppress warnings gdal.PushErrorHandler('CPLQuietErrorHandler') def open_shapefile(fname, lyrind, ftind): ''' Open a existing shapefile and pass the coordinates back. ##SS => see commend on projection of the shapefile, expand the desciption of what the function would do based on this. ''' # import dependencies from shapely.wkt import loads # opening the file file_bbox = ogr.Open(fname) #If layer name provided
def ogr_cartodb_vsimem(): if ogrtest.cartodb_drv is None: return 'skip' ogrtest.cartodb_api_key_ori = gdal.GetConfigOption('CARTODB_API_KEY') gdal.SetConfigOption('CARTODB_API_URL', '/vsimem/cartodb') gdal.SetConfigOption('CPL_CURL_ENABLE_VSIMEM', 'YES') gdal.PushErrorHandler() ds = ogr.Open('CARTODB:foo') gdal.PopErrorHandler() if ds is not None: gdaltest.post_reason('fail') return 'fail' gdal.FileFromMemBuffer( '/vsimem/cartodb&POSTFIELDS=q=SELECT current_schema() LIMIT 500 OFFSET 0', """Content-Type: text/html\r Error""") gdal.PushErrorHandler() ds = ogr.Open('CARTODB:foo') gdal.PopErrorHandler() if ds is not None or gdal.GetLastErrorMsg().find('HTML error page') < 0: gdaltest.post_reason('fail') print(gdal.GetLastErrorMsg()) return 'fail' gdal.FileFromMemBuffer( '/vsimem/cartodb&POSTFIELDS=q=SELECT current_schema() LIMIT 500 OFFSET 0', """""") ds = ogr.Open('CARTODB:foo') if ds is not None: gdaltest.post_reason('fail') print(gdal.GetLastErrorMsg()) return 'fail' gdal.FileFromMemBuffer( '/vsimem/cartodb&POSTFIELDS=q=SELECT current_schema() LIMIT 500 OFFSET 0', """{""") gdal.PushErrorHandler() ds = ogr.Open('CARTODB:foo') gdal.PopErrorHandler() if ds is not None or gdal.GetLastErrorMsg().find('JSON parsing error') < 0: gdaltest.post_reason('fail') print(gdal.GetLastErrorMsg()) return 'fail' gdal.FileFromMemBuffer( '/vsimem/cartodb&POSTFIELDS=q=SELECT current_schema() LIMIT 500 OFFSET 0', """ "not_expected_json" """) ds = ogr.Open('CARTODB:foo') if ds is not None: gdaltest.post_reason('fail') print(gdal.GetLastErrorMsg()) return 'fail' gdal.FileFromMemBuffer( '/vsimem/cartodb&POSTFIELDS=q=SELECT current_schema() LIMIT 500 OFFSET 0', """{ "error" : [ "bla"] }""") gdal.PushErrorHandler() ds = ogr.Open('CARTODB:foo') gdal.PopErrorHandler() if ds is not None or gdal.GetLastErrorMsg().find( 'Error returned by server : bla') < 0: gdaltest.post_reason('fail') print(gdal.GetLastErrorMsg()) return 'fail' gdal.FileFromMemBuffer( '/vsimem/cartodb&POSTFIELDS=q=SELECT current_schema() LIMIT 500 OFFSET 0', """{ "fields" : null } """) ds = ogr.Open('CARTODB:foo') if ds is not None: gdaltest.post_reason('fail') print(gdal.GetLastErrorMsg()) return 'fail' gdal.FileFromMemBuffer( '/vsimem/cartodb&POSTFIELDS=q=SELECT current_schema() LIMIT 500 OFFSET 0', """{ "fields" : "invalid" } """) ds = ogr.Open('CARTODB:foo') if ds is not None: gdaltest.post_reason('fail') print(gdal.GetLastErrorMsg()) return 'fail' gdal.FileFromMemBuffer( '/vsimem/cartodb&POSTFIELDS=q=SELECT current_schema() LIMIT 500 OFFSET 0', """{ "fields" : {} } """) ds = ogr.Open('CARTODB:foo') if ds is not None: gdaltest.post_reason('fail') print(gdal.GetLastErrorMsg()) return 'fail' gdal.FileFromMemBuffer( '/vsimem/cartodb&POSTFIELDS=q=SELECT current_schema() LIMIT 500 OFFSET 0', """{ "fields" : { "foo": "invalid" } } """) ds = ogr.Open('CARTODB:foo') if ds is not None: gdaltest.post_reason('fail') print(gdal.GetLastErrorMsg()) return 'fail' gdal.FileFromMemBuffer( '/vsimem/cartodb&POSTFIELDS=q=SELECT current_schema() LIMIT 500 OFFSET 0', """{ "fields" : { "foo": {} } } """) ds = ogr.Open('CARTODB:foo') if ds is not None: gdaltest.post_reason('fail') print(gdal.GetLastErrorMsg()) return 'fail' gdal.FileFromMemBuffer( '/vsimem/cartodb&POSTFIELDS=q=SELECT current_schema() LIMIT 500 OFFSET 0', """{ "fields" : { "foo": { "type" : null } } } """) ds = ogr.Open('CARTODB:foo') if ds is not None: gdaltest.post_reason('fail') print(gdal.GetLastErrorMsg()) return 'fail' gdal.FileFromMemBuffer( '/vsimem/cartodb&POSTFIELDS=q=SELECT current_schema() LIMIT 500 OFFSET 0', """{ "fields" : { "foo": { "type" : {} } } } """) ds = ogr.Open('CARTODB:foo') if ds is not None: gdaltest.post_reason('fail') print(gdal.GetLastErrorMsg()) return 'fail' gdal.FileFromMemBuffer( '/vsimem/cartodb&POSTFIELDS=q=SELECT current_schema() LIMIT 500 OFFSET 0', """{ "fields" : { "foo": { "type" : "string" } } } """) ds = ogr.Open('CARTODB:foo') if ds is not None: gdaltest.post_reason('fail') print(gdal.GetLastErrorMsg()) return 'fail' gdal.FileFromMemBuffer( '/vsimem/cartodb&POSTFIELDS=q=SELECT current_schema() LIMIT 500 OFFSET 0', """{"rows":[ {"field1": "foo", "field2": "bar"} ],"fields":{"field1":{"type":"string"}, "field2":{"type":"string"}}}""" ) ds = ogr.Open('CARTODB:foo') if ds is not None: gdaltest.post_reason('fail') print(gdal.GetLastErrorMsg()) return 'fail' gdal.FileFromMemBuffer( '/vsimem/cartodb&POSTFIELDS=q=SELECT current_schema() LIMIT 500 OFFSET 0', """{"rows":[],"fields":{"current_schema":{"type":"string"}}}""") gdal.PushErrorHandler() ds = ogr.Open('CARTODB:foo') gdal.PopErrorHandler() if ds is not None: gdaltest.post_reason('fail') print(gdal.GetLastErrorMsg()) return 'fail' gdal.FileFromMemBuffer( '/vsimem/cartodb&POSTFIELDS=q=SELECT current_schema() LIMIT 500 OFFSET 0', """{"rows":[{"current_schema":"public"}],"fields":{"current_schema":{"type":"unknown(19)"}}}""" ) gdal.PushErrorHandler() ds = ogr.Open('CARTODB:foo') gdal.PopErrorHandler() if ds is not None: gdaltest.post_reason('fail') print(gdal.GetLastErrorMsg()) return 'fail' gdal.FileFromMemBuffer( '/vsimem/cartodb&POSTFIELDS=q=SELECT CDB_UserTables() LIMIT 500 OFFSET 0', """{"rows":[{"cdb_usertables":"table1"}],"fields":{"cdb_usertables":{"type":"string"}}}""" ) ds = ogr.Open('CARTODB:foo') if ds is None or ds.GetLayerCount() != 1: gdaltest.post_reason('fail') print(gdal.GetLastErrorMsg()) return 'fail' gdal.PushErrorHandler() lyr_defn = ds.GetLayer(0).GetLayerDefn() gdal.PopErrorHandler() if lyr_defn.GetFieldCount() != 0: gdaltest.post_reason('fail') return 'fail' # Empty layer gdal.FileFromMemBuffer( '/vsimem/cartodb&POSTFIELDS=q=SELECT * FROM "table1" LIMIT 0', """{"rows":[],"fields":{}}""") ds = ogr.Open('CARTODB:foo') lyr = ds.GetLayer(0) lyr_defn = lyr.GetLayerDefn() if lyr_defn.GetFieldCount() != 0: gdaltest.post_reason('fail') return 'fail' gdal.FileFromMemBuffer( '/vsimem/cartodb&POSTFIELDS=q=SELECT * FROM "table1" LIMIT 500 OFFSET 0', """{"rows":[{}],"fields":{}}}""") f = lyr.GetNextFeature() if f.GetFID() != 0: gdaltest.post_reason('fail') f.DumpReadable() return 'fail' # Layer without geometry or primary key gdal.FileFromMemBuffer( '/vsimem/cartodb&POSTFIELDS=q=SELECT * FROM "table1" LIMIT 0', """{"rows":[],"fields":{"strfield":{"type":"string"}, "realfield":{"type":"number"}, "boolfield":{"type":"boolean"}, "datefield":{"type":"date"}}}""" ) ds = ogr.Open('CARTODB:foo') lyr = ds.GetLayer(0) lyr_defn = lyr.GetLayerDefn() if lyr_defn.GetFieldCount() != 4: gdaltest.post_reason('fail') return 'fail' if lyr_defn.GetFieldDefn(0).GetName() != 'strfield' or \ lyr_defn.GetFieldDefn(0).GetType() != ogr.OFTString: gdaltest.post_reason('fail') return 'fail' if lyr_defn.GetFieldDefn(1).GetName() != 'realfield' or \ lyr_defn.GetFieldDefn(1).GetType() != ogr.OFTReal: gdaltest.post_reason('fail') return 'fail' if lyr_defn.GetFieldDefn(2).GetName() != 'boolfield' or \ lyr_defn.GetFieldDefn(2).GetType() != ogr.OFTInteger or \ lyr_defn.GetFieldDefn(2).GetSubType() != ogr.OFSTBoolean: gdaltest.post_reason('fail') return 'fail' if lyr_defn.GetFieldDefn(3).GetName() != 'datefield' or \ lyr_defn.GetFieldDefn(3).GetType() != ogr.OFTDateTime: gdaltest.post_reason('fail') return 'fail' gdal.FileFromMemBuffer( '/vsimem/cartodb&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': gdaltest.post_reason('fail') f.DumpReadable() return 'fail' gdal.SetConfigOption('CARTODB_API_KEY', 'foo') gdal.FileFromMemBuffer( '/vsimem/cartodb&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/cartodb&POSTFIELDS=q=SELECT CDB_UserTables() LIMIT 500 OFFSET 0&api_key=foo', """{"rows":[{"cdb_usertables":"table1"}],"fields":{"cdb_usertables":{"type":"string"}}}""" ) ds = ogr.Open('CARTODB:foo') gdal.PushErrorHandler() lyr_defn = ds.GetLayer(0).GetLayerDefn() gdal.PopErrorHandler() if lyr_defn.GetFieldCount() != 0: gdaltest.post_reason('fail') return 'fail' get_full_details_fields_url = """/vsimem/cartodb&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('CARTODB:foo') gdal.PushErrorHandler() lyr_defn = ds.GetLayer(0).GetLayerDefn() gdal.PopErrorHandler() if lyr_defn.GetFieldCount() != 0: gdaltest.post_reason('fail') return 'fail' gdal.FileFromMemBuffer( get_full_details_fields_url, """{"rows":[{"attname":"foo"}], "fields":{"attname":{"type":"string"}}}""" ) ds = ogr.Open('CARTODB:foo') lyr = ds.GetLayer(0) gdal.PushErrorHandler() lyr_defn = lyr.GetLayerDefn() gdal.PopErrorHandler() if lyr_defn.GetFieldCount() != 1: gdaltest.post_reason('fail') return 'fail' gdal.PushErrorHandler() f = lyr.GetFeature(0) gdal.PopErrorHandler() if f is not None: gdaltest.post_reason('fail') return 'fail' 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('CARTODB:foo') lyr = ds.GetLayer(0) lyr_defn = lyr.GetLayerDefn() if lyr_defn.GetFieldCount() != 5: gdaltest.post_reason('fail') return 'fail' if lyr_defn.GetFieldDefn(0).GetName() != 'strfield' or \ lyr_defn.GetFieldDefn(0).GetType() != ogr.OFTString or \ lyr_defn.GetFieldDefn(0).IsNullable() or \ lyr_defn.GetFieldDefn(0).GetDefault() != 'def_value': gdaltest.post_reason('fail') return 'fail' if lyr_defn.GetGeomFieldCount() != 1: gdaltest.post_reason('fail') return 'fail' if lyr_defn.GetGeomFieldDefn(0).GetName() != 'my_geom': gdaltest.post_reason('fail') return 'fail' if lyr_defn.GetGeomFieldDefn(0).GetType() != ogr.wkbPoint25D: gdaltest.post_reason('fail') return 'fail' if lyr_defn.GetGeomFieldDefn(0).GetSpatialRef().ExportToWkt().find( '4326') < 0: 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/cartodb&POSTFIELDS=q=SELECT COUNT(*) FROM "table1"&api_key=foo""", """{"rows":[{"foo":1}], "fields":{"foo":{"type":"number"}}}""") gdal.PushErrorHandler() fc = lyr.GetFeatureCount() gdal.PopErrorHandler() if fc != 0: gdaltest.post_reason('fail') return 'fail' gdal.FileFromMemBuffer( """/vsimem/cartodb&POSTFIELDS=q=SELECT COUNT(*) FROM "table1"&api_key=foo""", """{"rows":[{"count":9876543210}], "fields":{"count":{"type":"number"}}}""") if lyr.GetFeatureCount() != 9876543210: gdaltest.post_reason('fail') return 'fail' gdal.PushErrorHandler() extent = lyr.GetExtent() gdal.PopErrorHandler() if extent != (0, 0, 0, 0): gdaltest.post_reason('fail') return 'fail' gdal.FileFromMemBuffer( """/vsimem/cartodb&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() if extent != (0, 0, 0, 0): gdaltest.post_reason('fail') return 'fail' gdal.FileFromMemBuffer( """/vsimem/cartodb&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() if gdal.GetLastErrorMsg() == '': gdaltest.post_reason('fail') return 'fail' gdal.FileFromMemBuffer( """/vsimem/cartodb&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() if gdal.GetLastErrorMsg() == '': gdaltest.post_reason('fail') return 'fail' gdal.FileFromMemBuffer( """/vsimem/cartodb&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() if gdal.GetLastErrorMsg() == '': gdaltest.post_reason('fail') return 'fail' gdal.FileFromMemBuffer( """/vsimem/cartodb&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"}}}""") if lyr.GetExtent() != (0.0, 2.0, 1.0, 3.0): gdaltest.post_reason('fail') print(lyr.GetExtent()) return 'fail' gdal.PushErrorHandler() f = lyr.GetFeature(0) gdal.PopErrorHandler() if f is not None: gdaltest.post_reason('fail') return 'fail' gdal.FileFromMemBuffer( """/vsimem/cartodb&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() if f is not None: gdaltest.post_reason('fail') return 'fail' gdal.FileFromMemBuffer( """/vsimem/cartodb&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) if f.GetFID() != -1 or f.IsFieldSet(0): gdaltest.post_reason('fail') return 'fail' gdal.FileFromMemBuffer( """/vsimem/cartodb&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) if f.GetFID() != 0: gdaltest.post_reason('fail') return 'fail' lyr.ResetReading() gdal.PushErrorHandler() f = lyr.GetNextFeature() gdal.PopErrorHandler() if f is not None: gdaltest.post_reason('fail') return 'fail' gdal.FileFromMemBuffer( """/vsimem/cartodb&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() if f.GetFID() != 0: 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.SetConfigOption('CARTODB_PAGE_SIZE', '2') gdal.FileFromMemBuffer( """/vsimem/cartodb&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() if f.GetFID() != 0: gdaltest.post_reason('fail') return 'fail' f = lyr.GetNextFeature() if f.GetFID() != 10: gdaltest.post_reason('fail') return 'fail' gdal.FileFromMemBuffer( """/vsimem/cartodb&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() if f.GetFID() != 12: gdaltest.post_reason('fail') return 'fail' gdal.ErrorReset() f = lyr.GetNextFeature() if f is not None or gdal.GetLastErrorMsg() != '': gdaltest.post_reason('fail') return 'fail' lyr.SetAttributeFilter('strfield is NULL') gdal.PushErrorHandler() fc = lyr.GetFeatureCount() gdal.PopErrorHandler() if fc != 0: gdaltest.post_reason('fail') return 'fail' gdal.FileFromMemBuffer( """/vsimem/cartodb&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() if f is None: gdaltest.post_reason('fail') return 'fail' gdal.FileFromMemBuffer( """/vsimem/cartodb&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() if f is not None or gdal.GetLastErrorMsg() != '': gdaltest.post_reason('fail') return 'fail' gdal.FileFromMemBuffer( """/vsimem/cartodb&POSTFIELDS=q=SELECT COUNT(*) FROM "table1" WHERE (strfield is NULL)&api_key=foo""", """{"rows":[{"count":9876543210}], "fields":{"count":{"type":"number"}}}""") if lyr.GetFeatureCount() != 9876543210: gdaltest.post_reason('fail') return 'fail' lyr.SetSpatialFilterRect(-180, -90, 180, 90) gdal.PushErrorHandler() fc = lyr.GetFeatureCount() gdal.PopErrorHandler() if fc != 0: gdaltest.post_reason('fail') return 'fail' gdal.FileFromMemBuffer( """/vsimem/cartodb&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() if f is None or f.GetGeometryRef().ExportToWkt() != 'POINT (2 49)': gdaltest.post_reason('fail') return 'fail' gdal.PushErrorHandler() fc = lyr.GetFeatureCount() gdal.PopErrorHandler() if fc != 1: gdaltest.post_reason('fail') return 'fail' gdal.FileFromMemBuffer( """/vsimem/cartodb&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"}}}""") if lyr.GetFeatureCount() != 9876543210: gdaltest.post_reason('fail') return 'fail' # 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) gdal.PopErrorHandler() ds = None gdal.FileFromMemBuffer( """/vsimem/cartodb&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('CARTODB_PAGE_SIZE', None) ds = ogr.Open('CARTODB:foo', update=1) lyr = ds.CreateLayer('MY_LAYER') gdal.ErrorReset() gdal.PushErrorHandler() lyr.GetNextFeature() gdal.PopErrorHandler() if gdal.GetLastErrorMsg() == '': gdaltest.post_reason('fail') return 'fail' ds = ogr.Open('CARTODB:foo', update=1) gdal.SetConfigOption('CARTODB_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/cartodb&POSTFIELDS=q=CREATE TABLE "my_layer" ( cartodb_id SERIAL,the_geom GEOMETRY(GEOMETRY, 4326), the_geom_webmercator GEOMETRY(GEOMETRY, 3857),"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 TABLE "my_layer" ALTER COLUMN cartodb_id SET DEFAULT nextval('"my_layer_cartodb_id_seq"')&api_key=foo""", """{"rows":[], "fields":{}}""") gdal.FileFromMemBuffer( """/vsimem/cartodb&POSTFIELDS=q=SELECT cdb_cartodbfytable('my_layer')&api_key=foo""", """{"rows":[], "fields":{}}""") f = ogr.Feature(lyr.GetLayerDefn()) gdal.PushErrorHandler() ret = lyr.CreateFeature(f) gdal.PopErrorHandler() if ret == 0: gdaltest.post_reason('fail') return 'fail' f = None fld_defn = ogr.FieldDefn('INTFIELD', ogr.OFTInteger) gdal.PushErrorHandler() ret = lyr.CreateField(fld_defn) gdal.PopErrorHandler() if ret == 0: gdaltest.post_reason('fail') return 'fail' gdal.FileFromMemBuffer( """/vsimem/cartodb&POSTFIELDS=q=ALTER TABLE "my_layer" ADD COLUMN "intfield" INTEGER&api_key=foo""", """{"rows":[], "fields":{}}""") if lyr.CreateField(fld_defn) != 0: gdaltest.post_reason('fail') return 'fail' fld_defn = ogr.FieldDefn('boolfield', ogr.OFTInteger) fld_defn.SetSubType(ogr.OFSTBoolean) gdal.FileFromMemBuffer( """/vsimem/cartodb&POSTFIELDS=q=ALTER TABLE "my_layer" ADD COLUMN "boolfield" BOOLEAN&api_key=foo""", """{"rows":[], "fields":{}}""") if lyr.CreateField(fld_defn) != 0: gdaltest.post_reason('fail') return 'fail' f = ogr.Feature(lyr.GetLayerDefn()) gdal.PushErrorHandler() ret = lyr.CreateFeature(f) gdal.PopErrorHandler() if ret == 0: gdaltest.post_reason('fail') return 'fail' 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/cartodb&POSTFIELDS=q=INSERT INTO "my_layer" ("strfield", "intfield", "boolfield", "the_geom", "cartodb_id") VALUES ('foo', 1, 't', '0101000020E610000000000000000000400000000000804840', nextval('my_layer_cartodb_id_seq')) RETURNING "cartodb_id"&api_key=foo""", """{"rows":[ {"cartodb_id": 1} ], "fields":{"cartodb_id":{"type":"integer"}}}""") ret = lyr.CreateFeature(f) if ret != 0 or f.GetFID() != 1: gdaltest.post_reason('fail') return 'fail' f.SetFID(-1) gdal.PushErrorHandler() ret = lyr.SetFeature(f) gdal.PopErrorHandler() if ret == 0: gdaltest.post_reason('fail') return 'fail' f.SetFID(3) gdal.PushErrorHandler() ret = lyr.SetFeature(f) gdal.PopErrorHandler() if ret == 0: gdaltest.post_reason('fail') return 'fail' gdal.FileFromMemBuffer( """/vsimem/cartodb&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) if ret != ogr.OGRERR_NON_EXISTING_FEATURE: gdaltest.post_reason('fail') return 'fail' gdal.FileFromMemBuffer( """/vsimem/cartodb&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) if ret != 0: gdaltest.post_reason('fail') return 'fail' f = ogr.Feature(lyr.GetLayerDefn()) gdal.PushErrorHandler() ret = lyr.CreateFeature(f) gdal.PopErrorHandler() if ret == 0: gdaltest.post_reason('fail') return 'fail' gdal.FileFromMemBuffer( """/vsimem/cartodb&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) if ret != 0 or f.GetFID() != 4: gdaltest.post_reason('fail') return 'fail' gdal.PushErrorHandler() ret = lyr.DeleteFeature(0) gdal.PopErrorHandler() if ret == 0: gdaltest.post_reason('fail') return 'fail' gdal.FileFromMemBuffer( """/vsimem/cartodb&POSTFIELDS=q=DELETE FROM "my_layer" WHERE "cartodb_id" = 0&api_key=foo""", """{"total_rows": 0}""") ret = lyr.DeleteFeature(0) if ret != ogr.OGRERR_NON_EXISTING_FEATURE: gdaltest.post_reason('fail') return 'fail' gdal.FileFromMemBuffer( """/vsimem/cartodb&POSTFIELDS=q=DELETE FROM "my_layer" WHERE "cartodb_id" = 0&api_key=foo""", """{"total_rows": 1}""") ret = lyr.DeleteFeature(0) if ret != 0: gdaltest.post_reason('fail') return 'fail' gdal.SetConfigOption('CARTODB_MAX_CHUNK_SIZE', None) ds = ogr.Open('CARTODB:foo', update=1) lyr = ds.GetLayer(0) gdal.FileFromMemBuffer( """/vsimem/cartodb&POSTFIELDS=q=SELECT nextval('table1_cartodb_id_seq') 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) if ret != 0 or f.GetFID() != 11: gdaltest.post_reason('fail') return 'fail' f = ogr.Feature(lyr.GetLayerDefn()) ret = lyr.CreateFeature(f) if ret != 0 or f.GetFID() != 12: gdaltest.post_reason('fail') return 'fail' gdal.FileFromMemBuffer( """/vsimem/cartodb&POSTFIELDS=q=BEGIN;INSERT INTO "table1" ("strfield", "cartodb_id") VALUES ('foo', 11);INSERT INTO "table1" ("cartodb_id") VALUES (nextval('table1_cartodb_id_seq'));COMMIT;&api_key=foo""", """{"rows":[], "fields":{}}""") gdal.ErrorReset() ds = None if gdal.GetLastErrorMsg() != '': gdaltest.post_reason('fail') return 'fail' ds = ogr.Open('CARTODB:foo', update=1) gdal.PushErrorHandler() lyr = ds.CreateLayer('table1') gdal.PopErrorHandler() if lyr is not None: gdaltest.post_reason('fail') return 'fail' gdal.FileFromMemBuffer( """/vsimem/cartodb&POSTFIELDS=q=DROP TABLE "table1"&api_key=foo""", """{"rows":[], "fields":{}}""") lyr = ds.CreateLayer('table1', geom_type=ogr.wkbPolygon, options=['OVERWRITE=YES', 'CARTODBFY=NO']) gdal.Unlink( """/vsimem/cartodb&POSTFIELDS=q=DROP TABLE "table1"&api_key=foo""") gdal.FileFromMemBuffer( """/vsimem/cartodb&POSTFIELDS=q=CREATE TABLE "table1" ( cartodb_id SERIAL,the_geom GEOMETRY(MULTIPOLYGON, 0), the_geom_webmercator GEOMETRY(MULTIPOLYGON, 3857),PRIMARY KEY (cartodb_id) );DROP SEQUENCE IF EXISTS "table1_cartodb_id_seq" CASCADE;CREATE SEQUENCE "table1_cartodb_id_seq" START 1;ALTER TABLE "table1" ALTER COLUMN cartodb_id SET DEFAULT nextval('"table1_cartodb_id_seq"')&api_key=foo""", """{"rows":[], "fields":{}}""") f = ogr.Feature(lyr.GetLayerDefn()) f.SetGeometry(ogr.CreateGeometryFromWkt('POLYGON((0 0,0 1,1 0,0 0))')) if lyr.CreateFeature(f) != 0: gdaltest.post_reason('fail') return 'fail' gdal.FileFromMemBuffer( """/vsimem/cartodb&POSTFIELDS=q=BEGIN;INSERT INTO "table1" ("the_geom", "cartodb_id") VALUES ('0106000020E61000000100000001030000000100000004000000000000000000000000000000000000000000000000000000000000000000F03F000000000000F03F000000000000000000000000000000000000000000000000', nextval('table1_cartodb_id_seq'));COMMIT;&api_key=foo""", """{"rows":[], "fields":{}}""") gdal.ErrorReset() ds = None if gdal.GetLastErrorMsg() != '': gdaltest.post_reason('fail') return 'fail' ds = ogr.Open('CARTODB:foo', update=1) gdal.PushErrorHandler() ret = ds.DeleteLayer(0) gdal.PopErrorHandler() if ret == 0: gdaltest.post_reason('fail') return 'fail' ds = ogr.Open('CARTODB:foo', update=1) gdal.FileFromMemBuffer( """/vsimem/cartodb&POSTFIELDS=q=DROP TABLE "table1"&api_key=foo""", """{"rows":[], "fields":{}}""") gdal.ErrorReset() ds.ExecuteSQL('DELLAYER:table1') if gdal.GetLastErrorMsg() != '' or ds.GetLayerByName('table1') is not None: gdaltest.post_reason('fail') return 'fail' gdal.FileFromMemBuffer( '/vsimem/cartodb&POSTFIELDS=q=SELECT current_schema() LIMIT 500 OFFSET 0&api_key=foo', """{"rows":[{"current_schema":"my_schema"}],"fields":{"current_schema":{"type":"unknown(19)"}}}""" ) gdal.FileFromMemBuffer( '/vsimem/cartodb&POSTFIELDS=q=SELECT CDB_UserTables() LIMIT 500 OFFSET 0&api_key=foo', """{"rows":[],"fields":{"cdb_usertables":{"type":"string"}}}""") gdal.FileFromMemBuffer( """/vsimem/cartodb&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('CARTODB:foo') if ds.GetLayerByName('a_layer') is None: gdaltest.post_reason('fail') return 'fail' return 'success'
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 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, full_check=False): """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. full_check: Set to TRUe to check tile/strip leader/trailer bytes. Might be slow on remote files 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 = [] warnings = [] 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: warnings += [ 'The file is greater than 512xH or Wx512, it is recommended ' 'to include internal overviews' ] ifd_offset = int(main_band.GetMetadataItem('IFD_OFFSET', 'TIFF')) ifd_offsets = [ifd_offset] block_order_row_major = False block_leader_size_as_uint4 = False block_trailer_last_4_bytes_repeated = False mask_interleaved_with_imagery = False if ifd_offset not in (8, 16): # Check if there is GDAL hidden structural metadata f = gdal.VSIFOpenL(filename, 'rb') if not f: raise ValidateCloudOptimizedGeoTIFFException("Cannot open file") signature = struct.unpack('B' * 4, gdal.VSIFReadL(4, 1, f)) bigtiff = signature in ((0x49, 0x49, 0x2B, 0x00), (0x4D, 0x4D, 0x00, 0x2B)) if bigtiff: expected_ifd_pos = 16 else: expected_ifd_pos = 8 gdal.VSIFSeekL(f, expected_ifd_pos, 0) pattern = "GDAL_STRUCTURAL_METADATA_SIZE=%06d bytes\n" % 0 got = gdal.VSIFReadL(len(pattern), 1, f).decode('LATIN1') if len(got) == len(pattern) and got.startswith( 'GDAL_STRUCTURAL_METADATA_SIZE='): size = int(got[len('GDAL_STRUCTURAL_METADATA_SIZE='):][0:6]) extra_md = gdal.VSIFReadL(size, 1, f).decode('LATIN1') block_order_row_major = 'BLOCK_ORDER=ROW_MAJOR' in extra_md block_leader_size_as_uint4 = 'BLOCK_LEADER=SIZE_AS_UINT4' in extra_md block_trailer_last_4_bytes_repeated = 'BLOCK_TRAILER=LAST_4_BYTES_REPEATED' in extra_md mask_interleaved_with_imagery = 'MASK_INTERLEAVED_WITH_IMAGERY=YES' in extra_md if 'KNOWN_INCOMPATIBLE_EDITION=YES' in extra_md: errors += [ "KNOWN_INCOMPATIBLE_EDITION=YES is declared in the file" ] expected_ifd_pos += len(pattern) + size expected_ifd_pos += expected_ifd_pos % 2 # IFD offset starts on a 2-byte boundary gdal.VSIFCloseL(f) if expected_ifd_pos != ifd_offsets[0]: errors += [ 'The offset of the main IFD should be %d. It is %d instead' % (expected_ifd_pos, 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 def _get_block_offset(band): blockxsize, blockysize = band.GetBlockSize() for y in range(int((band.YSize + blockysize - 1) / blockysize)): for x in range(int((band.XSize + blockxsize - 1) / blockxsize)): block_offset = band.GetMetadataItem( 'BLOCK_OFFSET_%d_%d' % (x, y), 'TIFF') if block_offset: return int(block_offset) return 0 block_offset = _get_block_offset(main_band) data_offsets = [block_offset] details['data_offsets'] = {} details['data_offsets']['main'] = block_offset for i in range(ovr_count): ovr_band = ds.GetRasterBand(1).GetOverview(i) block_offset = _get_block_offset(ovr_band) data_offsets.append(block_offset) details['data_offsets']['overview_%d' % i] = block_offset if data_offsets[-1] != 0 and 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] != 0 and 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] != 0 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) ] if full_check and (block_order_row_major or block_leader_size_as_uint4 or block_trailer_last_4_bytes_repeated or mask_interleaved_with_imagery): f = gdal.VSIFOpenL(filename, 'rb') if not f: raise ValidateCloudOptimizedGeoTIFFException("Cannot open file") _full_check_band(f, 'Main resolution image', main_band, errors, block_order_row_major, block_leader_size_as_uint4, block_trailer_last_4_bytes_repeated, mask_interleaved_with_imagery) if main_band.GetMaskFlags() == gdal.GMF_PER_DATASET and \ (filename + '.msk') not in ds.GetFileList(): _full_check_band(f, 'Mask band of main resolution image', main_band.GetMaskBand(), errors, block_order_row_major, block_leader_size_as_uint4, block_trailer_last_4_bytes_repeated, False) for i in range(ovr_count): ovr_band = ds.GetRasterBand(1).GetOverview(i) _full_check_band(f, 'Overview %d' % i, ovr_band, errors, block_order_row_major, block_leader_size_as_uint4, block_trailer_last_4_bytes_repeated, mask_interleaved_with_imagery) if ovr_band.GetMaskFlags() == gdal.GMF_PER_DATASET and \ (filename + '.msk') not in ds.GetFileList(): _full_check_band(f, 'Mask band of overview %d' % i, ovr_band.GetMaskBand(), errors, block_order_row_major, block_leader_size_as_uint4, block_trailer_last_4_bytes_repeated, False) gdal.VSIFCloseL(f) return warnings, errors, details
def test_ogr_rfc41_6(): ds = ogr.GetDriverByName('memory').CreateDataSource('') sr = osr.SpatialReference() lyr = ds.CreateLayer('poly', geom_type=ogr.wkbPolygon, srs=sr) lyr.GetLayerDefn().GetGeomFieldDefn(0).SetName('geomfield') lyr.CreateField(ogr.FieldDefn('intfield', ogr.OFTInteger)) lyr.CreateField(ogr.FieldDefn('wkt', ogr.OFTString)) feat = ogr.Feature(lyr.GetLayerDefn()) feat.SetField('intfield', 1) feat.SetField('wkt', 'POINT (0 0)') feat.SetGeometryDirectly(ogr.CreateGeometryFromWkt('POLYGON EMPTY')) lyr.CreateFeature(feat) feat = ogr.Feature(lyr.GetLayerDefn()) lyr.CreateFeature(feat) feat = None # Test implicit geometry column (since poly has one single geometry column) # then explicit geometry column for sql in [ 'SELECT intfield FROM poly', 'SELECT * FROM poly', 'SELECT intfield, geomfield FROM poly', 'SELECT geomfield, intfield FROM poly' ]: sql_lyr = ds.ExecuteSQL(sql) assert sql_lyr.GetLayerDefn().GetGeomFieldDefn( 0).GetType() == ogr.wkbPolygon assert sql_lyr.GetLayerDefn().GetGeomFieldDefn( 0).GetSpatialRef() is not None feat = sql_lyr.GetNextFeature() assert feat.GetField('intfield') == 1 assert feat.GetGeomFieldRef('geomfield') is not None feat = sql_lyr.GetNextFeature() assert feat.GetGeomFieldRef('geomfield') is None feat = None ds.ReleaseResultSet(sql_lyr) # Test CAST(geometry_field AS GEOMETRY) sql_lyr = ds.ExecuteSQL( 'SELECT CAST(geomfield AS GEOMETRY) AS mygeom FROM poly WHERE CAST(geomfield AS GEOMETRY) IS NOT NULL' ) assert sql_lyr.GetLayerDefn().GetGeomFieldDefn( 0).GetType() == ogr.wkbUnknown assert sql_lyr.GetLayerDefn().GetGeomFieldDefn(0).GetSpatialRef() is None feat = sql_lyr.GetNextFeature() assert feat.GetGeomFieldRef('mygeom') is not None feat = None ds.ReleaseResultSet(sql_lyr) # Test CAST(xxx AS GEOMETRY(POLYGON)) sql_lyr = ds.ExecuteSQL( 'SELECT CAST(geomfield AS GEOMETRY(POLYGON)) AS mygeom FROM poly WHERE CAST(geomfield AS GEOMETRY(POLYGON)) IS NOT NULL' ) assert sql_lyr.GetLayerDefn().GetGeomFieldDefn( 0).GetType() == ogr.wkbPolygon assert sql_lyr.GetLayerDefn().GetGeomFieldDefn(0).GetSpatialRef() is None feat = sql_lyr.GetNextFeature() assert feat.GetGeomFieldRef('mygeom') is not None feat = None ds.ReleaseResultSet(sql_lyr) # Test CAST(xxx AS GEOMETRY(POLYGON,4326)) sql_lyr = ds.ExecuteSQL( 'SELECT CAST(geomfield AS GEOMETRY(POLYGON,4326)) AS mygeom FROM poly WHERE CAST(geomfield AS GEOMETRY(POLYGON,4326)) IS NOT NULL' ) assert sql_lyr.GetLayerDefn().GetGeomFieldDefn( 0).GetType() == ogr.wkbPolygon assert sql_lyr.GetLayerDefn().GetGeomFieldDefn( 0).GetSpatialRef().ExportToWkt().find('4326') >= 0 feat = sql_lyr.GetNextFeature() assert feat.GetGeomFieldRef('mygeom') is not None feat = None ds.ReleaseResultSet(sql_lyr) # Test CAST(a_multipolygon AS GEOMETRY(POLYGON)) sql_lyr = ds.ExecuteSQL( "SELECT CAST('MULTIPOLYGON (((0 0,0 1,1 1,1 0,0 0)))' AS GEOMETRY(POLYGON)) AS mygeom FROM poly" ) feat = sql_lyr.GetNextFeature() assert feat.GetGeomFieldRef( 'mygeom').ExportToWkt() == 'POLYGON ((0 0,0 1,1 1,1 0,0 0))' feat = None ds.ReleaseResultSet(sql_lyr) # Test CAST(a_polygon AS GEOMETRY(MULTIPOLYGON)) sql_lyr = ds.ExecuteSQL( "SELECT CAST('POLYGON ((0 0,0 1,1 1,1 0,0 0))' AS GEOMETRY(MULTIPOLYGON)) AS mygeom FROM poly" ) feat = sql_lyr.GetNextFeature() assert feat.GetGeomFieldRef( 'mygeom').ExportToWkt() == 'MULTIPOLYGON (((0 0,0 1,1 1,1 0,0 0)))' feat = None ds.ReleaseResultSet(sql_lyr) # Test CAST(a_multilinestring AS GEOMETRY(LINESTRING)) sql_lyr = ds.ExecuteSQL( "SELECT CAST('MULTILINESTRING ((0 0,0 1,1 1,1 0,0 0))' AS GEOMETRY(LINESTRING)) AS mygeom FROM poly" ) feat = sql_lyr.GetNextFeature() assert feat.GetGeomFieldRef( 'mygeom').ExportToWkt() == 'LINESTRING (0 0,0 1,1 1,1 0,0 0)' feat = None ds.ReleaseResultSet(sql_lyr) # Test CAST(a_linestring AS GEOMETRY(MULTILINESTRING)) sql_lyr = ds.ExecuteSQL( "SELECT CAST('LINESTRING (0 0,0 1,1 1,1 0,0 0)' AS GEOMETRY(MULTILINESTRING)) AS mygeom FROM poly" ) feat = sql_lyr.GetNextFeature() assert feat.GetGeomFieldRef( 'mygeom').ExportToWkt() == 'MULTILINESTRING ((0 0,0 1,1 1,1 0,0 0))' feat = None ds.ReleaseResultSet(sql_lyr) # Test expression with cast CHARACTER <--> GEOMETRY sql_lyr = ds.ExecuteSQL( 'SELECT CAST(CAST(geomfield AS CHARACTER) AS GEOMETRY) AS mygeom, intfield FROM poly' ) assert sql_lyr.GetLayerDefn().GetGeomFieldDefn( 0).GetType() == ogr.wkbUnknown assert sql_lyr.GetLayerDefn().GetGeomFieldDefn(0).GetSpatialRef() is None feat = sql_lyr.GetNextFeature() assert feat.GetField('intfield') == 1 assert feat.GetGeomFieldRef('mygeom') is not None feat = None ds.ReleaseResultSet(sql_lyr) # Test CAST(NULL AS GEOMETRY) sql_lyr = ds.ExecuteSQL('SELECT CAST(NULL AS GEOMETRY) FROM poly') assert sql_lyr.GetLayerDefn().GetGeomFieldDefn( 0).GetType() == ogr.wkbUnknown feat = sql_lyr.GetNextFeature() assert feat.GetGeomFieldRef('') is None feat = None ds.ReleaseResultSet(sql_lyr) # Test CAST(stringfield AS GEOMETRY) sql_lyr = ds.ExecuteSQL('SELECT CAST(wkt AS GEOMETRY) FROM poly') assert sql_lyr.GetLayerDefn().GetGeomFieldDefn( 0).GetType() == ogr.wkbUnknown feat = sql_lyr.GetNextFeature() assert feat.GetGeomFieldRef('wkt').ExportToWkt() == 'POINT (0 0)' feat = None ds.ReleaseResultSet(sql_lyr) # Test COUNT(geometry) sql_lyr = ds.ExecuteSQL('SELECT COUNT(geomfield) FROM poly') feat = sql_lyr.GetNextFeature() assert feat is not None assert feat.GetField(0) == 1 feat = None ds.ReleaseResultSet(sql_lyr) wrong_sql_list = [ ('SELECT DISTINCT geomfield FROM poly', 'SELECT DISTINCT on a geometry not supported'), ('SELECT COUNT(DISTINCT geomfield) FROM poly', 'SELECT COUNT DISTINCT on a geometry not supported'), ('SELECT MAX(geomfield) FROM poly', 'Use of field function MAX() on geometry field'), ('SELECT CAST(5 AS GEOMETRY) FROM poly', 'Cannot cast integer to geometry'), ('SELECT CAST(geomfield AS integer) FROM poly', 'Cannot cast geometry to integer'), ('SELECT CAST(geomfield AS GEOMETRY(2)) FROM poly', 'First argument of CAST operator should be a geometry type identifier' ), ('SELECT CAST(geomfield AS GEOMETRY(UNSUPPORTED_TYPE)) FROM poly', 'SQL Expression Parsing Error: syntax error'), ('SELECT CAST(geomfield AS GEOMETRY(UNSUPPORTED_TYPE,5)) FROM poly', 'SQL Expression Parsing Error: syntax error'), ] for (sql, error_msg) in wrong_sql_list: gdal.ErrorReset() gdal.PushErrorHandler('CPLQuietErrorHandler') sql_lyr = ds.ExecuteSQL(sql) gdal.PopErrorHandler() assert gdal.GetLastErrorMsg().find(error_msg) == 0, \ ('For %s, expected error %s, got %s' % (sql, error_msg, gdal.GetLastErrorMsg())) assert sql_lyr is None # Test invalid expressions with geometry for sql in [ "SELECT geomfield + 'a' FROM poly", "SELECT geomfield * 'a' FROM poly", "SELECT geomfield + 'a' FROM poly", "SELECT geomfield - 'a' FROM poly", "SELECT geomfield % 'a' FROM poly", "SELECT CONCAT(geomfield, 'a') FROM poly", "SELECT SUBSTR(geomfield, 0, 1) FROM poly", "SELECT * FROM poly WHERE geomfield = CAST('POINT EMPTY' AS GEOMETRY)", "SELECT * FROM poly WHERE geomfield LIKE 'a'", "SELECT * FROM poly WHERE geomfield IN( 'a' )" ]: gdal.ErrorReset() gdal.PushErrorHandler('CPLQuietErrorHandler') sql_lyr = ds.ExecuteSQL(sql) gdal.PopErrorHandler() assert gdal.GetLastErrorMsg().find( 'Cannot use geometry field in this operation') == 0 assert sql_lyr is None # Test expression with geometry in WHERE sql_lyr = ds.ExecuteSQL('SELECT * FROM poly WHERE geomfield IS NOT NULL') feat = sql_lyr.GetNextFeature() assert feat.GetField('intfield') == 1 feat = sql_lyr.GetNextFeature() assert feat is None feat = None ds.ReleaseResultSet(sql_lyr) sql_lyr = ds.ExecuteSQL('SELECT * FROM poly WHERE geomfield IS NULL') feat = sql_lyr.GetNextFeature() assert not feat.IsFieldSet(0) feat = sql_lyr.GetNextFeature() assert feat is None feat = None ds.ReleaseResultSet(sql_lyr) sql_lyr = ds.ExecuteSQL( "SELECT * FROM poly WHERE CAST(geomfield AS CHARACTER) = 'POLYGON EMPTY'" ) feat = sql_lyr.GetNextFeature() assert feat is not None feat = sql_lyr.GetNextFeature() assert feat is None feat = None ds.ReleaseResultSet(sql_lyr) sql_lyr = ds.ExecuteSQL( 'SELECT count(*) FROM poly WHERE geomfield IS NULL') feat = sql_lyr.GetNextFeature() assert feat.GetField(0) == 1 feat = None ds.ReleaseResultSet(sql_lyr) sql_lyr = ds.ExecuteSQL( 'SELECT count(*) FROM poly WHERE geomfield IS NOT NULL') feat = sql_lyr.GetNextFeature() assert feat.GetField(0) == 1 feat = None ds.ReleaseResultSet(sql_lyr) # Test spatial filter feat = lyr.GetFeature(0) feat.SetGeometryDirectly(ogr.CreateGeometryFromWkt('POINT(1 2)')) lyr.SetFeature(feat) feat = None lyr.DeleteFeature(1) sql_lyr = ds.ExecuteSQL("SELECT * FROM poly") sql_lyr.SetSpatialFilterRect(0, 0, 0, 0) feat = sql_lyr.GetNextFeature() assert feat is None feat = None sql_lyr.SetSpatialFilterRect(0, 1, 2, 1, 2) feat = sql_lyr.GetNextFeature() assert feat is not None feat = None # Test invalid spatial filter index gdal.ErrorReset() gdal.PushErrorHandler('CPLQuietErrorHandler') sql_lyr.SetSpatialFilterRect(2, 0, 0, 0, 0) gdal.PopErrorHandler() assert gdal.GetLastErrorMsg() != '' # Test invalid geometry field index gdal.ErrorReset() gdal.PushErrorHandler('CPLQuietErrorHandler') sql_lyr.GetExtent(geom_field=2) gdal.PopErrorHandler() assert gdal.GetLastErrorMsg() != '' ds.ReleaseResultSet(sql_lyr) # Test querying several geometry fields sql_lyr = ds.ExecuteSQL( 'SELECT geomfield as geom1, geomfield as geom2 FROM poly') feat = sql_lyr.GetNextFeature() assert feat is not None assert feat.GetGeomFieldRef('geom1') is not None assert feat.GetGeomFieldRef('geom2') is not None feat = None ds.ReleaseResultSet(sql_lyr) # Test querying a layer with several geometry fields lyr.CreateGeomField(ogr.GeomFieldDefn('secondarygeom', ogr.wkbPoint)) lyr.ResetReading() feat = lyr.GetNextFeature() feat.SetGeomField('secondarygeom', ogr.CreateGeometryFromWkt('POINT (10 100)')) lyr.SetFeature(feat) feat = None for sql in [ 'SELECT * FROM poly', 'SELECT geomfield, secondarygeom FROM poly', 'SELECT secondarygeom, geomfield FROM poly' ]: sql_lyr = ds.ExecuteSQL(sql) feat = sql_lyr.GetNextFeature() assert feat.GetGeomFieldRef('geomfield').ExportToWkt() == 'POINT (1 2)' assert feat.GetGeomFieldRef( 'secondarygeom').ExportToWkt() == 'POINT (10 100)' feat = None ds.ReleaseResultSet(sql_lyr) # Check that we don't get an implicit geometry field sql_lyr = ds.ExecuteSQL('SELECT intfield FROM poly') assert sql_lyr.GetLayerDefn().GetGeomFieldCount() == 0 ds.ReleaseResultSet(sql_lyr) # Check GetExtent() and SetSpatialFilter() sql_lyr = ds.ExecuteSQL('SELECT * FROM poly') assert sql_lyr.GetExtent(geom_field=0) == (1.0, 1.0, 2.0, 2.0) assert sql_lyr.GetExtent(geom_field=1) == (10.0, 10.0, 100.0, 100.0) sql_lyr.SetSpatialFilterRect(0, 0.5, 1.5, 1.5, 2.5) assert sql_lyr.GetFeatureCount() == 1 sql_lyr.SetSpatialFilterRect(0, 0, 0, 0.5, 0.5) assert sql_lyr.GetFeatureCount() == 0 sql_lyr.SetSpatialFilterRect(1, 9, 99, 11, 101) assert sql_lyr.GetFeatureCount() == 1 sql_lyr.SetSpatialFilterRect(1, 0, 0, 0.5, 0.5) assert sql_lyr.GetFeatureCount() == 0 ds.ReleaseResultSet(sql_lyr) ds = None