def test_ogr_feature_nullable_validate(): # No fields feat_def = ogr.FeatureDefn('test') f = ogr.Feature(feat_def) assert f.Validate() == 1 # Field with default parameter and empty feature feat_def = ogr.FeatureDefn('test') field_def = ogr.FieldDefn('field_string', ogr.OFTString) assert field_def.IsNullable() == 1 assert feat_def.GetGeomFieldDefn(0).IsNullable() == 1 feat_def.AddFieldDefn(field_def) f = ogr.Feature(feat_def) assert f.Validate() == 1 # Field with not NULL constraint and empty feature feat_def = ogr.FeatureDefn('test') field_def = ogr.FieldDefn('field_string', ogr.OFTString) field_def.SetNullable(0) assert field_def.IsNullable() == 0 feat_def.AddFieldDefn(field_def) f = ogr.Feature(feat_def) gdal.PushErrorHandler() ret = f.Validate() gdal.PopErrorHandler() assert ret == 0 # Field with not NULL constraint and non-empty feature feat_def = ogr.FeatureDefn('test') field_def = ogr.FieldDefn('field_string', ogr.OFTString) field_def.SetNullable(0) feat_def.AddFieldDefn(field_def) f = ogr.Feature(feat_def) f.SetField(0, 'foo') assert f.Validate() == 1 # Field with width constraint that is met feat_def = ogr.FeatureDefn('test') field_def = ogr.FieldDefn('field_string', ogr.OFTString) field_def.SetWidth(3) feat_def.AddFieldDefn(field_def) f = ogr.Feature(feat_def) f.SetField(0, 'foo') assert f.Validate() == 1 # Field with width constraint that is not met feat_def = ogr.FeatureDefn('test') field_def = ogr.FieldDefn('field_string', ogr.OFTString) field_def.SetWidth(2) feat_def.AddFieldDefn(field_def) f = ogr.Feature(feat_def) f.SetField(0, 'foo') gdal.PushErrorHandler() ret = f.Validate() gdal.PopErrorHandler() assert ret == 0 # Geometry field with not-null geometry constraint that is met feat_def = ogr.FeatureDefn('test') feat_def.SetGeomType(ogr.wkbNone) gfield_def = ogr.GeomFieldDefn('test', ogr.wkbUnknown) gfield_def.SetNullable(0) assert gfield_def.IsNullable() == 0 feat_def.AddGeomFieldDefn(gfield_def) f = ogr.Feature(feat_def) f.SetGeometry(ogr.CreateGeometryFromWkt('POINT(0 1)')) assert f.Validate() == 1 # Geometry field with not-null geometry constraint that is not met feat_def = ogr.FeatureDefn('test') feat_def.SetGeomType(ogr.wkbNone) gfield_def = ogr.GeomFieldDefn('test', ogr.wkbPoint) gfield_def.SetNullable(0) feat_def.AddGeomFieldDefn(gfield_def) f = ogr.Feature(feat_def) gdal.PushErrorHandler() ret = f.Validate() gdal.PopErrorHandler() assert ret == 0 # Geometry field with Point geometry type that is met feat_def = ogr.FeatureDefn('test') feat_def.SetGeomType(ogr.wkbPoint) f = ogr.Feature(feat_def) f.SetGeometry(ogr.CreateGeometryFromWkt('POINT(0 1)')) assert f.Validate() == 1 # Geometry field with LineString geometry type that is not met feat_def = ogr.FeatureDefn('test') feat_def.SetGeomType(ogr.wkbLineString) f = ogr.Feature(feat_def) f.SetGeometry(ogr.CreateGeometryFromWkt('POINT(0 1)')) gdal.PushErrorHandler() ret = f.Validate() gdal.PopErrorHandler() assert ret == 0
def ogr_feature_nullable_validate(): # No fields feat_def = ogr.FeatureDefn('test') f = ogr.Feature(feat_def) if f.Validate() != 1: gdaltest.post_reason('fail') return 'fail' # Field with default parameter and empty feature feat_def = ogr.FeatureDefn('test') field_def = ogr.FieldDefn('field_string', ogr.OFTString) if field_def.IsNullable() != 1: gdaltest.post_reason('fail') return 'fail' if feat_def.GetGeomFieldDefn(0).IsNullable() != 1: gdaltest.post_reason('fail') return 'fail' feat_def.AddFieldDefn(field_def) f = ogr.Feature(feat_def) if f.Validate() != 1: gdaltest.post_reason('fail') return 'fail' # Field with not NULL constraint and empty feature feat_def = ogr.FeatureDefn('test') field_def = ogr.FieldDefn('field_string', ogr.OFTString) field_def.SetNullable(0) if field_def.IsNullable() != 0: gdaltest.post_reason('fail') return 'fail' feat_def.AddFieldDefn(field_def) f = ogr.Feature(feat_def) gdal.PushErrorHandler() ret = f.Validate() gdal.PopErrorHandler() if ret != 0: gdaltest.post_reason('fail') return 'fail' # Field with not NULL constraint and non-empty feature feat_def = ogr.FeatureDefn('test') field_def = ogr.FieldDefn('field_string', ogr.OFTString) field_def.SetNullable(0) feat_def.AddFieldDefn(field_def) f = ogr.Feature(feat_def) f.SetField(0, 'foo') if f.Validate() != 1: gdaltest.post_reason('fail') return 'fail' # Field with width constraint that is met feat_def = ogr.FeatureDefn('test') field_def = ogr.FieldDefn('field_string', ogr.OFTString) field_def.SetWidth(3) feat_def.AddFieldDefn(field_def) f = ogr.Feature(feat_def) f.SetField(0, 'foo') if f.Validate() != 1: gdaltest.post_reason('fail') return 'fail' # Field with width constraint that is not met feat_def = ogr.FeatureDefn('test') field_def = ogr.FieldDefn('field_string', ogr.OFTString) field_def.SetWidth(2) feat_def.AddFieldDefn(field_def) f = ogr.Feature(feat_def) f.SetField(0, 'foo') gdal.PushErrorHandler() ret = f.Validate() gdal.PopErrorHandler() if ret != 0: gdaltest.post_reason('fail') return 'fail' # Geometry field with not-null geometry constraint that is met feat_def = ogr.FeatureDefn('test') feat_def.SetGeomType(ogr.wkbNone) gfield_def = ogr.GeomFieldDefn('test', ogr.wkbUnknown) gfield_def.SetNullable(0) if gfield_def.IsNullable() != 0: gdaltest.post_reason('fail') return 'fail' feat_def.AddGeomFieldDefn(gfield_def) f = ogr.Feature(feat_def) f.SetGeometry(ogr.CreateGeometryFromWkt('POINT(0 1)')) if f.Validate() != 1: gdaltest.post_reason('fail') return 'fail' # Geometry field with not-null geometry constraint that is not met feat_def = ogr.FeatureDefn('test') feat_def.SetGeomType(ogr.wkbNone) gfield_def = ogr.GeomFieldDefn('test', ogr.wkbPoint) gfield_def.SetNullable(0) feat_def.AddGeomFieldDefn(gfield_def) f = ogr.Feature(feat_def) gdal.PushErrorHandler() ret = f.Validate() gdal.PopErrorHandler() if ret != 0: gdaltest.post_reason('fail') return 'fail' # Geometry field with Point geometry type that is met feat_def = ogr.FeatureDefn('test') feat_def.SetGeomType(ogr.wkbPoint) f = ogr.Feature(feat_def) f.SetGeometry(ogr.CreateGeometryFromWkt('POINT(0 1)')) if f.Validate() != 1: gdaltest.post_reason('fail') return 'fail' # Geometry field with LineString geometry type that is not met feat_def = ogr.FeatureDefn('test') feat_def.SetGeomType(ogr.wkbLineString) f = ogr.Feature(feat_def) f.SetGeometry(ogr.CreateGeometryFromWkt('POINT(0 1)')) gdal.PushErrorHandler() ret = f.Validate() gdal.PopErrorHandler() if ret != 0: gdaltest.post_reason('fail') return 'fail' return 'success'
def ogr_rfc41_1(): gfld_defn = ogr.GeomFieldDefn() # Check default values if gfld_defn.GetName() != '': gdaltest.post_reason('fail') return 'fail' if gfld_defn.GetType() != ogr.wkbUnknown: gdaltest.post_reason('fail') return 'fail' if gfld_defn.GetSpatialRef() is not None: gdaltest.post_reason('fail') return 'fail' if gfld_defn.IsIgnored() != 0: gdaltest.post_reason('fail') return 'fail' # Test SetName() / GetName() gfld_defn.SetName('foo') if gfld_defn.GetName() != 'foo': gdaltest.post_reason('fail') return 'fail' # Test SetType() / GetType() gfld_defn.SetType(ogr.wkbPoint) if gfld_defn.GetType() != ogr.wkbPoint: gdaltest.post_reason('fail') return 'fail' # Test SetSpatialRef() / GetSpatialRef() sr = osr.SpatialReference() gfld_defn.SetSpatialRef(sr) got_sr = gfld_defn.GetSpatialRef() if got_sr.IsSame(sr) == 0: gdaltest.post_reason('fail') return 'fail' gfld_defn.SetSpatialRef(None) if gfld_defn.GetSpatialRef() is not None: gdaltest.post_reason('fail') return 'fail' gfld_defn.SetSpatialRef(sr) # Test SetIgnored() / IsIgnored() gfld_defn.SetIgnored(1) if gfld_defn.IsIgnored() != 1: gdaltest.post_reason('fail') return 'fail' # Test setting invalid value old_val = gfld_defn.GetType() gdal.PushErrorHandler('CPLQuietErrorHandler') gfld_defn.SetType(-3) gdal.PopErrorHandler() if gfld_defn.GetType() != old_val: gdaltest.post_reason('fail') return 'fail' gfld_defn = None return 'success'
def 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) if sql_lyr.GetLayerDefn().GetGeomFieldDefn( 0).GetType() != ogr.wkbPolygon: gdaltest.post_reason('fail') return 'fail' if sql_lyr.GetLayerDefn().GetGeomFieldDefn(0).GetSpatialRef() is None: gdaltest.post_reason('fail') return 'fail' feat = sql_lyr.GetNextFeature() if feat.GetField('intfield') != 1: gdaltest.post_reason('fail') return 'fail' if feat.GetGeomFieldRef('geomfield') is None: gdaltest.post_reason('fail') return 'fail' feat = sql_lyr.GetNextFeature() if feat.GetGeomFieldRef('geomfield') is not None: gdaltest.post_reason('fail') return 'fail' 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' ) if sql_lyr.GetLayerDefn().GetGeomFieldDefn(0).GetType() != ogr.wkbUnknown: gdaltest.post_reason('fail') return 'fail' if sql_lyr.GetLayerDefn().GetGeomFieldDefn(0).GetSpatialRef() is not None: gdaltest.post_reason('fail') return 'fail' feat = sql_lyr.GetNextFeature() if feat.GetGeomFieldRef('mygeom') is None: gdaltest.post_reason('fail') return 'fail' 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' ) if sql_lyr.GetLayerDefn().GetGeomFieldDefn(0).GetType() != ogr.wkbPolygon: gdaltest.post_reason('fail') return 'fail' if sql_lyr.GetLayerDefn().GetGeomFieldDefn(0).GetSpatialRef() is not None: gdaltest.post_reason('fail') return 'fail' feat = sql_lyr.GetNextFeature() if feat.GetGeomFieldRef('mygeom') is None: gdaltest.post_reason('fail') return 'fail' 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' ) if sql_lyr.GetLayerDefn().GetGeomFieldDefn(0).GetType() != ogr.wkbPolygon: gdaltest.post_reason('fail') return 'fail' if sql_lyr.GetLayerDefn().GetGeomFieldDefn( 0).GetSpatialRef().ExportToWkt().find('4326') < 0: gdaltest.post_reason('fail') return 'fail' feat = sql_lyr.GetNextFeature() if feat.GetGeomFieldRef('mygeom') is None: gdaltest.post_reason('fail') return 'fail' 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() if feat.GetGeomFieldRef( 'mygeom').ExportToWkt() != 'POLYGON ((0 0,0 1,1 1,1 0,0 0))': gdaltest.post_reason('fail') return 'fail' 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() if feat.GetGeomFieldRef('mygeom').ExportToWkt( ) != 'MULTIPOLYGON (((0 0,0 1,1 1,1 0,0 0)))': gdaltest.post_reason('fail') return 'fail' 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() if feat.GetGeomFieldRef( 'mygeom').ExportToWkt() != 'LINESTRING (0 0,0 1,1 1,1 0,0 0)': gdaltest.post_reason('fail') return 'fail' 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() if feat.GetGeomFieldRef('mygeom').ExportToWkt( ) != 'MULTILINESTRING ((0 0,0 1,1 1,1 0,0 0))': gdaltest.post_reason('fail') return 'fail' 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' ) if sql_lyr.GetLayerDefn().GetGeomFieldDefn(0).GetType() != ogr.wkbUnknown: gdaltest.post_reason('fail') return 'fail' if sql_lyr.GetLayerDefn().GetGeomFieldDefn(0).GetSpatialRef() is not None: gdaltest.post_reason('fail') return 'fail' feat = sql_lyr.GetNextFeature() if feat.GetField('intfield') != 1: gdaltest.post_reason('fail') return 'fail' if feat.GetGeomFieldRef('mygeom') is None: gdaltest.post_reason('fail') return 'fail' feat = None ds.ReleaseResultSet(sql_lyr) # Test CAST(NULL AS GEOMETRY) sql_lyr = ds.ExecuteSQL('SELECT CAST(NULL AS GEOMETRY) FROM poly') if sql_lyr.GetLayerDefn().GetGeomFieldDefn(0).GetType() != ogr.wkbUnknown: gdaltest.post_reason('fail') return 'fail' feat = sql_lyr.GetNextFeature() if feat.GetGeomFieldRef('') is not None: gdaltest.post_reason('fail') return 'fail' feat = None ds.ReleaseResultSet(sql_lyr) # Test CAST(stringfield AS GEOMETRY) sql_lyr = ds.ExecuteSQL('SELECT CAST(wkt AS GEOMETRY) FROM poly') if sql_lyr.GetLayerDefn().GetGeomFieldDefn(0).GetType() != ogr.wkbUnknown: gdaltest.post_reason('fail') return 'fail' feat = sql_lyr.GetNextFeature() if feat.GetGeomFieldRef('wkt').ExportToWkt() != 'POINT (0 0)': gdaltest.post_reason('fail') return 'fail' feat = None ds.ReleaseResultSet(sql_lyr) # Test COUNT(geometry) sql_lyr = ds.ExecuteSQL('SELECT COUNT(geomfield) FROM poly') feat = sql_lyr.GetNextFeature() if feat is None: gdaltest.post_reason('fail') return 'fail' if feat.GetField(0) != 1: gdaltest.post_reason('fail') return 'fail' 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() if gdal.GetLastErrorMsg().find(error_msg) != 0: gdaltest.post_reason('fail') print('For %s, expected error %s, got %s' % (sql, error_msg, gdal.GetLastErrorMsg())) return 'fail' if sql_lyr is not None: gdaltest.post_reason('fail') return 'fail' # 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() if gdal.GetLastErrorMsg().find( 'Cannot use geometry field in this operation') != 0: gdaltest.post_reason('fail') print(gdal.GetLastErrorMsg()) return 'fail' if sql_lyr is not None: gdaltest.post_reason('fail') return 'fail' # Test expression with geometry in WHERE sql_lyr = ds.ExecuteSQL('SELECT * FROM poly WHERE geomfield IS NOT NULL') feat = sql_lyr.GetNextFeature() if feat.GetField('intfield') != 1: gdaltest.post_reason('fail') return 'fail' feat = sql_lyr.GetNextFeature() if feat is not None: gdaltest.post_reason('fail') return 'fail' feat = None ds.ReleaseResultSet(sql_lyr) sql_lyr = ds.ExecuteSQL('SELECT * FROM poly WHERE geomfield IS NULL') feat = sql_lyr.GetNextFeature() if feat.IsFieldSet(0): gdaltest.post_reason('fail') return 'fail' feat = sql_lyr.GetNextFeature() if feat is not None: gdaltest.post_reason('fail') return 'fail' feat = None ds.ReleaseResultSet(sql_lyr) sql_lyr = ds.ExecuteSQL( "SELECT * FROM poly WHERE CAST(geomfield AS CHARACTER) = 'POLYGON EMPTY'" ) feat = sql_lyr.GetNextFeature() if feat is None: gdaltest.post_reason('fail') return 'fail' feat = sql_lyr.GetNextFeature() if feat is not None: gdaltest.post_reason('fail') return 'fail' feat = None ds.ReleaseResultSet(sql_lyr) sql_lyr = ds.ExecuteSQL( 'SELECT count(*) FROM poly WHERE geomfield IS NULL') feat = sql_lyr.GetNextFeature() if feat.GetField(0) != 1: gdaltest.post_reason('fail') return 'fail' feat = None ds.ReleaseResultSet(sql_lyr) sql_lyr = ds.ExecuteSQL( 'SELECT count(*) FROM poly WHERE geomfield IS NOT NULL') feat = sql_lyr.GetNextFeature() if feat.GetField(0) != 1: gdaltest.post_reason('fail') return 'fail' 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() if feat is not None: gdaltest.post_reason('fail') return 'fail' feat = None sql_lyr.SetSpatialFilterRect(0, 1, 2, 1, 2) feat = sql_lyr.GetNextFeature() if feat is None: gdaltest.post_reason('fail') return 'fail' feat = None # Test invalid spatial filter index gdal.ErrorReset() gdal.PushErrorHandler('CPLQuietErrorHandler') sql_lyr.SetSpatialFilterRect(2, 0, 0, 0, 0) gdal.PopErrorHandler() if gdal.GetLastErrorMsg() == '': gdaltest.post_reason('fail') return 'fail' # Test invalid geometry field index gdal.ErrorReset() gdal.PushErrorHandler('CPLQuietErrorHandler') sql_lyr.GetExtent(geom_field=2) gdal.PopErrorHandler() if gdal.GetLastErrorMsg() == '': gdaltest.post_reason('fail') return 'fail' 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() if feat is None: gdaltest.post_reason('fail') return 'fail' if feat.GetGeomFieldRef('geom1') is None: gdaltest.post_reason('fail') return 'fail' if feat.GetGeomFieldRef('geom2') is None: gdaltest.post_reason('fail') return 'fail' 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() if feat.GetGeomFieldRef('geomfield').ExportToWkt() != 'POINT (1 2)': gdaltest.post_reason('fail') return 'fail' if feat.GetGeomFieldRef( 'secondarygeom').ExportToWkt() != 'POINT (10 100)': gdaltest.post_reason('fail') return 'fail' feat = None ds.ReleaseResultSet(sql_lyr) # Check that we don't get an implicit geometry field sql_lyr = ds.ExecuteSQL('SELECT intfield FROM poly') if sql_lyr.GetLayerDefn().GetGeomFieldCount() != 0: gdaltest.post_reason('fail') return 'fail' ds.ReleaseResultSet(sql_lyr) # Check GetExtent() and SetSpatialFilter() sql_lyr = ds.ExecuteSQL('SELECT * FROM poly') if sql_lyr.GetExtent(geom_field=0) != (1.0, 1.0, 2.0, 2.0): gdaltest.post_reason('fail') return 'fail' if sql_lyr.GetExtent(geom_field=1) != (10.0, 10.0, 100.0, 100.0): gdaltest.post_reason('fail') return 'fail' sql_lyr.SetSpatialFilterRect(0, 0.5, 1.5, 1.5, 2.5) if sql_lyr.GetFeatureCount() != 1: gdaltest.post_reason('fail') return 'fail' sql_lyr.SetSpatialFilterRect(0, 0, 0, 0.5, 0.5) if sql_lyr.GetFeatureCount() != 0: gdaltest.post_reason('fail') return 'fail' sql_lyr.SetSpatialFilterRect(1, 9, 99, 11, 101) if sql_lyr.GetFeatureCount() != 1: gdaltest.post_reason('fail') return 'fail' sql_lyr.SetSpatialFilterRect(1, 0, 0, 0.5, 0.5) if sql_lyr.GetFeatureCount() != 0: gdaltest.post_reason('fail') return 'fail' ds.ReleaseResultSet(sql_lyr) ds = None return 'success'
def ogr_rfc41_3(): # Test with just one geometry field feature_defn = ogr.FeatureDefn() feature = ogr.Feature(feature_defn) if feature.GetGeomFieldCount() != 1: gdaltest.post_reason('fail') return 'fail' if feature.GetGeomFieldDefnRef(0).GetName() != '': gdaltest.post_reason('fail') return 'fail' if feature.GetGeomFieldDefnRef(0).GetType() != ogr.wkbUnknown: gdaltest.post_reason('fail') return 'fail' if feature.GetGeomFieldIndex('') != 0: gdaltest.post_reason('fail') return 'fail' if feature.GetGeomFieldIndex('non_existing') != -1: gdaltest.post_reason('fail') return 'fail' if feature.GetGeomFieldRef(-1) is not None: gdaltest.post_reason('fail') return 'fail' if feature.GetGeomFieldRef(0) is not None: gdaltest.post_reason('fail') return 'fail' if feature.GetGeomFieldRef(1) is not None: gdaltest.post_reason('fail') return 'fail' feature_clone_without_geom = feature.Clone() if not feature.Equal(feature_clone_without_geom): gdaltest.post_reason('fail') return 'fail' if feature.SetGeomField(0, ogr.Geometry(ogr.wkbPoint)) != 0: gdaltest.post_reason('fail') return 'fail' if feature.GetGeomFieldRef(0).ExportToWkt() != 'POINT EMPTY': gdaltest.post_reason('fail') return 'fail' if not feature.Equal(feature.Clone()): gdaltest.post_reason('fail') return 'fail' if feature.Equal(feature_clone_without_geom): gdaltest.post_reason('fail') return 'fail' feature_clone_with_other_geom = feature.Clone() feature_clone_with_other_geom.SetGeometry(ogr.Geometry(ogr.wkbLineString)) if feature.Equal(feature_clone_with_other_geom): gdaltest.post_reason('fail') return 'fail' if feature.SetGeomFieldDirectly(-1, None) == 0: gdaltest.post_reason('fail') return 'fail' if feature.SetGeomFieldDirectly(0, ogr.Geometry(ogr.wkbLineString)) != 0: gdaltest.post_reason('fail') return 'fail' if feature.GetGeomFieldRef(0).ExportToWkt() != 'LINESTRING EMPTY': gdaltest.post_reason('fail') return 'fail' feature_clone_with_geom = feature.Clone() if feature.SetGeomFieldDirectly(0, None) != 0: gdaltest.post_reason('fail') return 'fail' if feature.GetGeomFieldRef(0) is not None: gdaltest.post_reason('fail') return 'fail' if feature.Equal(feature_clone_with_geom): gdaltest.post_reason('fail') return 'fail' feature = None # Test one a feature with 0 geometry field feature_defn = ogr.FeatureDefn() feature_defn.SetGeomType(ogr.wkbNone) feature = ogr.Feature(feature_defn) if not feature.Equal(feature.Clone()): gdaltest.post_reason('fail') return 'fail' if feature.GetGeomFieldCount() != 0: gdaltest.post_reason('fail') return 'fail' # This used to work before RFC 41, but it no longer will if feature.SetGeometry(ogr.Geometry(ogr.wkbPoint)) == 0: gdaltest.post_reason('fail') return 'fail' if feature.SetGeomField(0, ogr.Geometry(ogr.wkbPoint)) == 0: gdaltest.post_reason('fail') return 'fail' if feature.GetGeometryRef() is not None: gdaltest.post_reason('fail') return 'fail' if feature.GetGeomFieldRef(0) is not None: gdaltest.post_reason('fail') return 'fail' if feature.SetGeometryDirectly(ogr.Geometry(ogr.wkbPoint)) == 0: gdaltest.post_reason('fail') return 'fail' if feature.SetGeomFieldDirectly(0, ogr.Geometry(ogr.wkbPoint)) == 0: gdaltest.post_reason('fail') return 'fail' feature = None # Test one a feature with several geometry fields feature_defn = ogr.FeatureDefn() feature_defn.SetGeomType(ogr.wkbNone) gfld_defn = ogr.GeomFieldDefn('polygon_field', ogr.wkbPolygon) feature_defn.AddGeomFieldDefn(gfld_defn) gfld_defn = ogr.GeomFieldDefn('point_field', ogr.wkbPoint) feature_defn.AddGeomFieldDefn(gfld_defn) feature = ogr.Feature(feature_defn) feature.SetGeomField(0, ogr.Geometry(ogr.wkbPolygon)) feature.SetGeomField(1, ogr.Geometry(ogr.wkbPoint)) if feature.GetGeomFieldRef(0).ExportToWkt() != 'POLYGON EMPTY': gdaltest.post_reason('fail') return 'fail' if feature.GetGeomFieldRef(1).ExportToWkt() != 'POINT EMPTY': gdaltest.post_reason('fail') return 'fail' if not feature.Equal(feature.Clone()): gdaltest.post_reason('fail') return 'fail' other_feature = ogr.Feature(feature_defn) if feature.Equal(other_feature): gdaltest.post_reason('fail') return 'fail' other_feature.SetFrom(feature) if not feature.Equal(other_feature): gdaltest.post_reason('fail') return 'fail' # Test that in SetFrom() where target has a single geometry field, # we get the first geometry of the source even if we cannot find a # source geometry field with the right name. feature_defn_default = ogr.FeatureDefn() feature_default = ogr.Feature(feature_defn_default) feature_default.SetFrom(feature) if feature_default.GetGeomFieldRef(0).ExportToWkt() != 'POLYGON EMPTY': gdaltest.post_reason('fail') return 'fail' return 'success'
def ogr_rfc41_4(): ds = ogr.GetDriverByName('memory').CreateDataSource('') if ds.TestCapability(ogr.ODsCCreateGeomFieldAfterCreateLayer) == 0: gdaltest.post_reason('fail') return 'fail' sr = osr.SpatialReference() lyr = ds.CreateLayer('test', geom_type=ogr.wkbPoint, srs=sr) if lyr.TestCapability(ogr.OLCCreateGeomField) == 0: gdaltest.post_reason('fail') return 'fail' if lyr.GetSpatialRef().IsSame(sr) == 0: gdaltest.post_reason('fail') return 'fail' if lyr.GetLayerDefn().GetGeomFieldDefn(0).GetSpatialRef().IsSame(sr) == 0: gdaltest.post_reason('fail') return 'fail' lyr.GetLayerDefn().GetGeomFieldDefn(0).SetName('a_name') feat = ogr.Feature(lyr.GetLayerDefn()) feat.SetGeometry(ogr.CreateGeometryFromWkt('POINT (1 2)')) lyr.CreateFeature(feat) lyr.ResetReading() feat = lyr.GetNextFeature() geom = feat.GetGeometryRef() if geom.GetSpatialReference().IsSame(sr) == 0: gdaltest.post_reason('fail') return 'fail' feat = None lyr.CreateGeomField(ogr.GeomFieldDefn('another_geom_field', ogr.wkbPolygon)) lyr.ResetReading() feat = lyr.GetNextFeature() feat.SetGeomField( 1, ogr.CreateGeometryFromWkt('POLYGON ((10 10,10 11,11 11,11 10,10 10))')) lyr.SetFeature(feat) lyr.ResetReading() feat = lyr.GetNextFeature() geom = feat.GetGeomFieldRef(0) if geom.ExportToWkt() != 'POINT (1 2)': gdaltest.post_reason('fail') return 'fail' geom = feat.GetGeomFieldRef('another_geom_field') if geom.ExportToWkt() != 'POLYGON ((10 10,10 11,11 11,11 10,10 10))': gdaltest.post_reason('fail') return 'fail' # Test GetExtent() got_extent = lyr.GetExtent(geom_field=1) if got_extent != (10.0, 11.0, 10.0, 11.0): gdaltest.post_reason('fail') return 'fail' # Test invalid geometry field index gdal.ErrorReset() gdal.PushErrorHandler('CPLQuietErrorHandler') got_extent = lyr.GetExtent(geom_field=2) gdal.PopErrorHandler() if gdal.GetLastErrorMsg() == '': gdaltest.post_reason('fail') return 'fail' # Test SetSpatialFilter() lyr.SetSpatialFilter( 1, ogr.CreateGeometryFromWkt( 'POLYGON ((-10 10,-10 11,-11 11,-11 10,-10 10))')) lyr.ResetReading() feat = lyr.GetNextFeature() if feat is not None: gdaltest.post_reason('fail') return 'fail' lyr.SetSpatialFilter( 1, ogr.CreateGeometryFromWkt('POLYGON ((10 10,10 11,11 11,11 10,10 10))')) lyr.ResetReading() feat = lyr.GetNextFeature() if feat is None: gdaltest.post_reason('fail') return 'fail' lyr.SetSpatialFilterRect(1, 10, 10, 11, 11) lyr.ResetReading() feat = lyr.GetNextFeature() if feat is None: gdaltest.post_reason('fail') return 'fail' # Test invalid spatial filter index gdal.ErrorReset() gdal.PushErrorHandler('CPLQuietErrorHandler') lyr.SetSpatialFilterRect(2, 0, 0, 0, 0) gdal.PopErrorHandler() if gdal.GetLastErrorMsg() == '': gdaltest.post_reason('fail') return 'fail' lyr.SetSpatialFilter(None) another_lyr = ds.CopyLayer(lyr, 'dup_test') dup_feat = another_lyr.GetNextFeature() geom = dup_feat.GetGeomFieldRef('a_name') if geom.ExportToWkt() != 'POINT (1 2)': gdaltest.post_reason('fail') return 'fail' geom = dup_feat.GetGeomFieldRef('another_geom_field') if geom.ExportToWkt() != 'POLYGON ((10 10,10 11,11 11,11 10,10 10))': gdaltest.post_reason('fail') return 'fail' return 'success'
def ogr_rfc41_8(): import ogr_sql_sqlite if not ogr_sql_sqlite.ogr_sql_sqlite_available(): return 'skip' ds = ogr.GetDriverByName('memory').CreateDataSource('') lyr = ds.CreateLayer('mytable', geom_type=ogr.wkbPolygon) lyr.GetLayerDefn().GetGeomFieldDefn(0).SetName('geomfield') gfld_defn = ogr.GeomFieldDefn('geomfield2', ogr.wkbPoint25D) sr = osr.SpatialReference() sr.ImportFromEPSG(4326) gfld_defn.SetSpatialRef(sr) lyr.CreateGeomField(gfld_defn) # Check that we get the geometry columns, even with no features sql_lyr = ds.ExecuteSQL('SELECT * FROM mytable', dialect='SQLite') if sql_lyr.GetLayerDefn().GetGeomFieldCount() != 2: print(sql_lyr.GetLayerDefn().GetGeomFieldCount()) gdaltest.post_reason('fail') return 'fail' if sql_lyr.GetLayerDefn().GetGeomFieldDefn(0).GetType() != ogr.wkbPolygon: gdaltest.post_reason('fail') return 'fail' if sql_lyr.GetLayerDefn().GetGeomFieldDefn(0).GetSpatialRef() is not None: gdaltest.post_reason('fail') return 'fail' if sql_lyr.GetLayerDefn().GetGeomFieldDefn(1).GetType() != ogr.wkbPoint25D: gdaltest.post_reason('fail') return 'fail' srs = sql_lyr.GetLayerDefn().GetGeomFieldDefn(1).GetSpatialRef() if srs.GetAuthorityCode(None) != '4326': gdaltest.post_reason('fail') return 'fail' ds.ReleaseResultSet(sql_lyr) # Test INSERT INTO request ds.ExecuteSQL("INSERT INTO mytable (geomfield, geomfield2) VALUES (" + "GeomFromText('POLYGON ((0 0,0 1,1 1,1 0,0 0))'), " + "GeomFromText('POINT Z(0 1 2)') )", dialect='SQLite') # Check output sql_lyr = ds.ExecuteSQL('SELECT geomfield2, geomfield FROM mytable', dialect='SQLite') feat = sql_lyr.GetNextFeature() geom = feat.GetGeomFieldRef('geomfield') if geom.ExportToWkt() != 'POLYGON ((0 0,0 1,1 1,1 0,0 0))': feat.DumpReadable() gdaltest.post_reason('fail') return 'fail' geom = feat.GetGeomFieldRef('geomfield2') if geom.ExportToWkt() != 'POINT (0 1 2)': feat.DumpReadable() gdaltest.post_reason('fail') return 'fail' feat = None ds.ReleaseResultSet(sql_lyr) # Test UPDATE ds.ExecuteSQL("UPDATE mytable SET geomfield2 = " + "GeomFromText('POINT Z(3 4 5)')", dialect='SQLite') # Check output sql_lyr = ds.ExecuteSQL('SELECT geomfield2, geomfield FROM mytable', dialect='SQLite') feat = sql_lyr.GetNextFeature() geom = feat.GetGeomFieldRef('geomfield') if geom.ExportToWkt() != 'POLYGON ((0 0,0 1,1 1,1 0,0 0))': feat.DumpReadable() gdaltest.post_reason('fail') return 'fail' geom = feat.GetGeomFieldRef('geomfield2') if geom.ExportToWkt() != 'POINT (3 4 5)': feat.DumpReadable() gdaltest.post_reason('fail') return 'fail' feat = None ds.ReleaseResultSet(sql_lyr) return 'success'
def ogr_rfc41_2(): # Check implicit geometry field creation feature_defn = ogr.FeatureDefn() if feature_defn.GetGeomFieldCount() != 1: gdaltest.post_reason('fail') return 'fail' if feature_defn.GetGeomType() != ogr.wkbUnknown: gdaltest.post_reason('fail') return 'fail' # Test IsSame() if feature_defn.IsSame(feature_defn) != 1: gdaltest.post_reason('fail') return 'fail' other_feature_defn = ogr.FeatureDefn() if feature_defn.IsSame(other_feature_defn) != 1: gdaltest.post_reason('fail') return 'fail' other_feature_defn.GetGeomFieldDefn(0).SetSpatialRef( osr.SpatialReference()) if feature_defn.IsSame(other_feature_defn) != 0: gdaltest.post_reason('fail') return 'fail' feature_defn.GetGeomFieldDefn(0).SetSpatialRef(osr.SpatialReference()) if feature_defn.IsSame(other_feature_defn) != 1: gdaltest.post_reason('fail') return 'fail' other_feature_defn.GetGeomFieldDefn(0).SetSpatialRef(None) if feature_defn.IsSame(other_feature_defn) != 0: gdaltest.post_reason('fail') return 'fail' feature_defn = None feature_defn = ogr.FeatureDefn() # Check changing geometry type feature_defn.SetGeomType(ogr.wkbPoint) if feature_defn.GetGeomType() != ogr.wkbPoint: gdaltest.post_reason('fail') return 'fail' if feature_defn.GetGeomFieldDefn(0).GetType() != ogr.wkbPoint: gdaltest.post_reason('fail') return 'fail' # Check setting to wkbNone and implicitly destroying the field. for i in range(2): feature_defn.SetGeomType(ogr.wkbNone) if feature_defn.GetGeomFieldCount() != 0: gdaltest.post_reason('fail') return 'fail' if feature_defn.GetGeomType() != ogr.wkbNone: gdaltest.post_reason('fail') return 'fail' # Recreate the field for t in [ogr.wkbPoint, ogr.wkbLineString]: feature_defn.SetGeomType(t) if feature_defn.GetGeomFieldCount() != 1: gdaltest.post_reason('fail') return 'fail' if feature_defn.GetGeomType() != t: gdaltest.post_reason('fail') return 'fail' if feature_defn.GetGeomFieldDefn(0).GetType() != t: gdaltest.post_reason('fail') return 'fail' # Test setting invalid value old_val = feature_defn.GetGeomType() gdal.PushErrorHandler('CPLQuietErrorHandler') feature_defn.SetGeomType(-3) gdal.PopErrorHandler() if feature_defn.GetGeomType() != old_val: gdaltest.post_reason('fail') return 'fail' # Test SetIgnored() / IsIgnored() if feature_defn.IsGeometryIgnored() != 0: gdaltest.post_reason('fail') return 'fail' if feature_defn.GetGeomFieldDefn(0).IsIgnored() != 0: gdaltest.post_reason('fail') return 'fail' feature_defn.SetGeometryIgnored(1) if feature_defn.IsGeometryIgnored() != 1: gdaltest.post_reason('fail') return 'fail' if feature_defn.GetGeomFieldDefn(0).IsIgnored() != 1: gdaltest.post_reason('fail') return 'fail' # Test wrong index values for GetGeomFieldDefn() for idx in [-1, 1]: gdal.PushErrorHandler('CPLQuietErrorHandler') ret = feature_defn.GetGeomFieldDefn(idx) gdal.PopErrorHandler() if ret is not None: gdaltest.post_reason('fail') return 'fail' # Test GetGeomFieldIndex() if feature_defn.GetGeomFieldIndex("") != 0: gdaltest.post_reason('fail') return 'fail' if feature_defn.GetGeomFieldIndex("invalid") != -1: gdaltest.post_reason('fail') return 'fail' # Test AddGeomFieldDefn() gfld_defn = ogr.GeomFieldDefn('polygon_field', ogr.wkbPolygon) feature_defn.AddGeomFieldDefn(gfld_defn) if feature_defn.GetGeomFieldCount() != 2: gdaltest.post_reason('fail') return 'fail' if feature_defn.GetGeomFieldIndex("polygon_field") != 1: gdaltest.post_reason('fail') return 'fail' if feature_defn.GetGeomFieldDefn(1).GetName() != 'polygon_field': gdaltest.post_reason('fail') return 'fail' # Test DeleteGeomFieldDefn() : error cases if feature_defn.DeleteGeomFieldDefn(-1) == 0: gdaltest.post_reason('fail') return 'fail' if feature_defn.DeleteGeomFieldDefn(2) == 0: gdaltest.post_reason('fail') return 'fail' if feature_defn.GetGeomFieldCount() != 2: gdaltest.post_reason('fail') return 'fail' # Test DeleteGeomFieldDefn() : valid cases if feature_defn.DeleteGeomFieldDefn(0) != 0: gdaltest.post_reason('fail') return 'fail' if feature_defn.GetGeomFieldCount() != 1: gdaltest.post_reason('fail') return 'fail' if feature_defn.GetGeomFieldIndex("polygon_field") != 0: gdaltest.post_reason('fail') return 'fail' if feature_defn.DeleteGeomFieldDefn(0) != 0: gdaltest.post_reason('fail') return 'fail' if feature_defn.GetGeomFieldCount() != 0: gdaltest.post_reason('fail') return 'fail' if feature_defn.IsSame(feature_defn) != 1: gdaltest.post_reason('fail') return 'fail' if feature_defn.IsSame(ogr.FeatureDefn()) != 0: gdaltest.post_reason('fail') return 'fail' feature_defn = None return 'success'
def test_ogr_mongodb_2(): if ogrtest.mongodb_drv is None: pytest.skip() ogrtest.mongodb_ds = ogr.Open(ogrtest.mongodb_test_uri, update=1) assert ogrtest.mongodb_ds.GetLayerByName('not_existing') is None assert ogrtest.mongodb_ds.TestCapability(ogr.ODsCCreateLayer) == 1 assert ogrtest.mongodb_ds.TestCapability(ogr.ODsCDeleteLayer) == 1 assert ogrtest.mongodb_ds.TestCapability( ogr.ODsCCreateGeomFieldAfterCreateLayer) == 1 # Create layer a_uuid = str(uuid.uuid1()).replace('-', '_') ogrtest.mongodb_layer_name = 'test_' + a_uuid srs = osr.SpatialReference() srs.ImportFromEPSG(4258) # ETRS 89 will reproject identically to EPSG:4326 lyr = ogrtest.mongodb_ds.CreateLayer( ogrtest.mongodb_layer_name, geom_type=ogr.wkbPolygon, srs=srs, options=['GEOMETRY_NAME=location.mygeom', 'FID=']) gdal.PushErrorHandler() ret = lyr.CreateGeomField( ogr.GeomFieldDefn('location.mygeom', ogr.wkbPoint)) gdal.PopErrorHandler() assert ret != 0 ret = lyr.CreateField(ogr.FieldDefn('str', ogr.OFTString)) assert ret == 0 gdal.PushErrorHandler() ret = lyr.CreateField(ogr.FieldDefn('str', ogr.OFTString)) gdal.PopErrorHandler() assert ret != 0 lyr.CreateField(ogr.FieldDefn('location.name', ogr.OFTString)) bool_field = ogr.FieldDefn('bool', ogr.OFTInteger) bool_field.SetSubType(ogr.OFSTBoolean) lyr.CreateField(bool_field) lyr.CreateField(ogr.FieldDefn('int', ogr.OFTInteger)) lyr.CreateField(ogr.FieldDefn('int64', ogr.OFTInteger64)) lyr.CreateField(ogr.FieldDefn('real', ogr.OFTReal)) lyr.CreateField(ogr.FieldDefn('dt', ogr.OFTDateTime)) lyr.CreateField(ogr.FieldDefn('embed.str', ogr.OFTString)) lyr.CreateField(ogr.FieldDefn('binary', ogr.OFTBinary)) lyr.CreateField(ogr.FieldDefn('strlist', ogr.OFTStringList)) lyr.CreateField(ogr.FieldDefn('intlist', ogr.OFTIntegerList)) lyr.CreateField(ogr.FieldDefn('int64list', ogr.OFTInteger64List)) lyr.CreateField(ogr.FieldDefn('realist', ogr.OFTRealList)) lyr.CreateField(ogr.FieldDefn('embed.embed2.int', ogr.OFTInteger)) lyr.CreateField(ogr.FieldDefn('embed.embed2.real', ogr.OFTReal)) lyr.CreateField(ogr.FieldDefn('str_is_null', ogr.OFTString)) lyr.CreateField(ogr.FieldDefn('str_is_unset', ogr.OFTString)) # Test CreateFeature() f = ogr.Feature(lyr.GetLayerDefn()) f['str'] = 'str' f['location.name'] = 'Paris' f['bool'] = 1 f['int'] = 1 f['int64'] = 1234567890123456 # put a number larger than 1 << 40 so that fromjson() doesn't pick double f['real'] = 1.23 f['dt'] = '1234/12/31 23:59:59.123+00' f.SetFieldBinaryFromHexString('binary', '00FF') f['strlist'] = ['a', 'b'] f['intlist'] = [1, 2] f['int64list'] = [1234567890123456, 1234567890123456] f['realist'] = [1.23, 4.56] f['embed.str'] = 'foo' f['embed.embed2.int'] = 3 f['embed.embed2.real'] = 3.45 f.SetFieldNull('str_is_null') f.SetGeometryDirectly( ogr.CreateGeometryFromWkt('POLYGON((2 49,2 50,3 50,3 49,2 49))')) assert lyr.CreateFeature(f) == 0 assert f['_id'] is not None f_ref = f.Clone() # Test GetFeatureCount() assert lyr.GetFeatureCount() == 1 # Test GetNextFeature() lyr.ResetReading() f = lyr.GetNextFeature() if not f.Equal(f_ref): f.DumpReadable() pytest.fail() f = lyr.GetNextFeature() if f is not None: f.DumpReadable() pytest.fail() # Test GetFeature() f = lyr.GetFeature(1) if not f.Equal(f_ref): f.DumpReadable() pytest.fail() # Test SetFeature() f['bool'] = 0 assert lyr.SetFeature(f) == 0 f_ref = f.Clone() f = lyr.GetFeature(1) if f['bool'] != 0: f.DumpReadable() pytest.fail() # Test (not working) DeleteFeature() gdal.PushErrorHandler() ret = lyr.DeleteFeature(1) gdal.PopErrorHandler() assert ret != 0 # Test Mongo filter lyr.SetAttributeFilter('{ "int": 1 }') lyr.ResetReading() f = lyr.GetNextFeature() if not f.Equal(f_ref): f.DumpReadable() pytest.fail() lyr.SetAttributeFilter('{ "int": 2 }') lyr.ResetReading() f = lyr.GetNextFeature() if f is not None: f.DumpReadable() pytest.fail() # Test OGR filter lyr.SetAttributeFilter('int = 1') lyr.ResetReading() f = lyr.GetNextFeature() if not f.Equal(f_ref): f.DumpReadable() pytest.fail() lyr.SetAttributeFilter('int = 2') lyr.ResetReading() f = lyr.GetNextFeature() if f is not None: f.DumpReadable() pytest.fail() # Test geometry filter lyr.SetAttributeFilter(None) lyr.SetSpatialFilterRect(2.1, 49.1, 2.9, 49.9) lyr.ResetReading() f = lyr.GetNextFeature() if not f.Equal(f_ref): f.DumpReadable() pytest.fail() lyr.SetSpatialFilterRect(1.1, 49.1, 1.9, 49.9) lyr.ResetReading() f = lyr.GetNextFeature() if f is not None: f.DumpReadable() pytest.fail() f = f_ref.Clone() f.SetFID(-1) f.SetGeometryDirectly(None) assert lyr.CreateFeature(f) == 0 # Duplicate key gdal.PushErrorHandler() ret = lyr.SyncToDisk() gdal.PopErrorHandler() assert ret != 0 f['_id'] = None lyr.CreateFeature(f) ret = lyr.SyncToDisk() assert ret == 0 # Missing _id f.UnsetField('_id') gdal.PushErrorHandler() ret = lyr.SetFeature(f) gdal.PopErrorHandler() assert ret != 0 # MongoDB dialect of ExecuteSQL() with invalid JSON gdal.PushErrorHandler() sql_lyr = ogrtest.mongodb_ds.ExecuteSQL('{', dialect='MongoDB') gdal.PopErrorHandler() # MongoDB dialect of ExecuteSQL() with nonexistent command. sql_lyr = ogrtest.mongodb_ds.ExecuteSQL('{ "foo": 1 }', dialect='MongoDB') assert sql_lyr is not None ogrtest.mongodb_ds.ReleaseResultSet(sql_lyr) # MongoDB dialect of ExecuteSQL() with existing commnand sql_lyr = ogrtest.mongodb_ds.ExecuteSQL('{ "listCommands" : 1 }', dialect='MongoDB') assert sql_lyr is not None f = sql_lyr.GetNextFeature() assert f is not None f = sql_lyr.GetNextFeature() assert f is None sql_lyr.GetLayerDefn() sql_lyr.ResetReading() sql_lyr.TestCapability('') ogrtest.mongodb_ds.ReleaseResultSet(sql_lyr) # Regular ExecuteSQL() sql_lyr = ogrtest.mongodb_ds.ExecuteSQL('SELECT * FROM ' + ogrtest.mongodb_layer_name) assert sql_lyr is not None ogrtest.mongodb_ds.ReleaseResultSet(sql_lyr) # Test CreateLayer again with same name gdal.PushErrorHandler() lyr = ogrtest.mongodb_ds.CreateLayer(ogrtest.mongodb_layer_name) gdal.PopErrorHandler() assert lyr is None ogrtest.mongodb_ds = gdal.OpenEx( ogrtest.mongodb_test_uri, gdal.OF_UPDATE, open_options=[ 'FEATURE_COUNT_TO_ESTABLISH_FEATURE_DEFN=-1', 'BULK_INSERT=NO', 'JSON_FIELD=TRUE' ]) # Check after reopening lyr = ogrtest.mongodb_ds.GetLayerByName(ogrtest.mongodb_layer_name) assert lyr.TestCapability(ogr.OLCFastSpatialFilter) != 0 f = lyr.GetNextFeature() json_field = f['_json'] # We cannot use feature.Equal() has the C++ layer defn has changed for i in range(f_ref.GetDefnRef().GetFieldCount()): if f.GetField(i) != f_ref.GetField(i) or \ f.GetFieldDefnRef(i).GetType() != f_ref.GetFieldDefnRef(i).GetType() or \ f.GetFieldDefnRef(i).GetSubType() != f_ref.GetFieldDefnRef(i).GetSubType(): f.DumpReadable() f_ref.DumpReadable() pytest.fail() for i in range(f_ref.GetDefnRef().GetGeomFieldCount()): if not f.GetGeomFieldRef(i).Equals(f_ref.GetGeomFieldRef(i)) or \ f.GetGeomFieldDefnRef(i).GetName() != f_ref.GetGeomFieldDefnRef(i).GetName() or \ f.GetGeomFieldDefnRef(i).GetType() != f_ref.GetGeomFieldDefnRef(i).GetType(): f.DumpReadable() f_ref.DumpReadable() pytest.fail() lyr.SetSpatialFilterRect(2.1, 49.1, 2.9, 49.9) lyr.ResetReading() if f is None: f.DumpReadable() pytest.fail() # Create a feature only from its _json content and do not store any ogr metadata related to the layer ogrtest.mongodb_layer_name_no_ogr_metadata = ogrtest.mongodb_layer_name + "_no_ogr_metadata" lyr = ogrtest.mongodb_ds.CreateLayer( ogrtest.mongodb_layer_name_no_ogr_metadata, options=[ 'GEOMETRY_NAME=location.mygeom', 'FID=', 'WRITE_OGR_METADATA=NO' ]) f = ogr.Feature(lyr.GetLayerDefn()) f['_json'] = json_field assert lyr.CreateFeature(f) == 0 ogrtest.mongodb_layer_name_guess_types = ogrtest.mongodb_layer_name + "_guess_types" lyr = ogrtest.mongodb_ds.CreateLayer( ogrtest.mongodb_layer_name_guess_types, geom_type=ogr.wkbNone, options=['FID=', 'WRITE_OGR_METADATA=NO']) f = ogr.Feature(lyr.GetLayerDefn()) f['_json'] = '{' f['_json'] += '"int": 2, ' f['_json'] += '"int64": { "$numberLong" : "1234567890123456" }, ' f['_json'] += '"real": 2.34, ' f['_json'] += '"intlist" : [2], ' f['_json'] += '"reallist" : [2.34], ' f['_json'] += '"int64list" : [{ "$numberLong" : "1234567890123456" }], ' f['_json'] += '"int_str" : 2, ' f['_json'] += '"str_int" : "2", ' f['_json'] += '"int64_str" : { "$numberLong" : "1234567890123456" }, ' f['_json'] += '"str_int64" : "2", ' f['_json'] += '"int_int64": 2, ' f['_json'] += '"int64_int": { "$numberLong" : "1234567890123456" }, ' f['_json'] += '"int_real": 2, ' f['_json'] += '"real_int": 3.45, ' f['_json'] += '"int64_real": { "$numberLong" : "1234567890123456" }, ' f['_json'] += '"real_int64": 3.45, ' f['_json'] += '"real_str": 3.45, ' f['_json'] += '"str_real": "3.45", ' f['_json'] += '"int_bool" : 2, ' f['_json'] += '"bool_int" : true, ' f['_json'] += '"intlist_strlist" : [2], ' f['_json'] += '"strlist_intlist" : ["2"], ' f['_json'] += '"intlist_int64list": [2], ' f['_json'] += '"int64list_intlist": [{ "$numberLong" : "1234567890123456" }], ' f['_json'] += '"intlist_reallist": [2], ' f['_json'] += '"reallist_intlist": [3.45], ' f['_json'] += '"int64list_reallist": [{ "$numberLong" : "1234567890123456" }], ' f['_json'] += '"reallist_int64list": [3.45], ' f['_json'] += '"intlist_boollist" : [2], ' f['_json'] += '"boollist_intlist" : [true], ' f['_json'] += '"mixedlist": [true,1,{ "$numberLong" : "1234567890123456" },3.45],' f['_json'] += '"mixedlist2": [true,1,{ "$numberLong" : "1234567890123456" },3.45,"str"]' f['_json'] += '}' assert lyr.CreateFeature(f) == 0 f = ogr.Feature(lyr.GetLayerDefn()) f['_json'] = '{' f['_json'] += '"int_str" : "3", ' f['_json'] += '"str_int" : 3, ' f['_json'] += '"int64_str" : "2", ' f['_json'] += '"str_int64" : { "$numberLong" : "1234567890123456" }, ' f['_json'] += '"int_int64": { "$numberLong" : "1234567890123456" }, ' f['_json'] += '"int64_int": 2, ' f['_json'] += '"int_real" : 3.45, ' f['_json'] += '"real_int": 2, ' f['_json'] += '"int64_real": 3.45, ' f['_json'] += '"real_int64": { "$numberLong" : "1234567890123456" }, ' f['_json'] += '"real_str": "3.45", ' f['_json'] += '"str_real": 3.45, ' f['_json'] += '"int_bool" : true, ' f['_json'] += '"bool_int" : 2, ' f['_json'] += '"intlist_strlist" : ["3"], ' f['_json'] += '"strlist_intlist" : [3], ' f['_json'] += '"intlist_int64list": [{ "$numberLong" : "1234567890123456" }], ' f['_json'] += '"int64list_intlist": [2], ' f['_json'] += '"intlist_reallist": [3.45], ' f['_json'] += '"reallist_intlist": [2], ' f['_json'] += '"int64list_reallist": [3.45], ' f['_json'] += '"reallist_int64list": [{ "$numberLong" : "1234567890123456" }], ' f['_json'] += '"intlist_boollist" : [true], ' f['_json'] += '"boollist_intlist" : [2]' f['_json'] += '}' assert lyr.CreateFeature(f) == 0 # This new features will not be taken into account by below the FEATURE_COUNT_TO_ESTABLISH_FEATURE_DEFN=2 f = ogr.Feature(lyr.GetLayerDefn()) f['_json'] = '{' f['_json'] += '"int": { "$minKey": 1 }, ' f['_json'] += '"int64": { "$minKey": 1 }, ' f['_json'] += '"real": { "$minKey": 1 }, ' f['_json'] += '"intlist" : [1, "1", { "$minKey": 1 },{ "$maxKey": 1 },{ "$numberLong" : "-1234567890123456" }, { "$numberLong" : "1234567890123456" }, -1234567890123456.1, 1234567890123456.1, { "$numberLong" : "1" }, 1.23 ], ' f['_json'] += '"int64list" : [1, { "$numberLong" : "1234567890123456" }, "1", { "$minKey": 1 },{ "$maxKey": 1 }, -1e300, 1e300, 1.23 ], ' f['_json'] += '"reallist" : [1, { "$numberLong" : "1234567890123456" }, 1.0, "1", { "$minKey": 1 },{ "$maxKey": 1 }, { "$numberLong" : "1234567890123456" } ] ' f['_json'] += '}' assert lyr.CreateFeature(f) == 0 f = ogr.Feature(lyr.GetLayerDefn()) f['_json'] = '{' f['_json'] += '"int": { "$maxKey": 1 }, ' f['_json'] += '"int64": { "$maxKey": 1 }, ' f['_json'] += '"real": { "$maxKey": 1 } ' f['_json'] += '}' assert lyr.CreateFeature(f) == 0 ogrtest.mongodb_layer_name_with_2d_index = ogrtest.mongodb_layer_name + "_with_2d_index" gdal.SetConfigOption('OGR_MONGODB_SPAT_INDEX_TYPE', '2d') lyr = ogrtest.mongodb_ds.CreateLayer( ogrtest.mongodb_layer_name_with_2d_index, geom_type=ogr.wkbPoint, options=['FID=', 'WRITE_OGR_METADATA=NO']) gdal.SetConfigOption('OGR_MONGODB_SPAT_INDEX_TYPE', None) f = ogr.Feature(lyr.GetLayerDefn()) f.SetGeometryDirectly(ogr.CreateGeometryFromWkt('POINT(2 49)')) assert lyr.CreateFeature(f) == 0 ogrtest.mongodb_layer_name_no_spatial_index = ogrtest.mongodb_layer_name + "_no_spatial_index" for i in range(2): lyr = ogrtest.mongodb_ds.CreateLayer( ogrtest.mongodb_layer_name_no_spatial_index, options=['SPATIAL_INDEX=NO', 'OVERWRITE=YES']) f = ogr.Feature(lyr.GetLayerDefn()) f.SetGeometryDirectly(ogr.CreateGeometryFromWkt('POINT(2 49)')) assert lyr.CreateFeature(f) == 0 ogrtest.mongodb_ds.ExecuteSQL( 'WRITE_OGR_METADATA ' + ogrtest.mongodb_layer_name_no_spatial_index) # Open "ghost" layer lyr = ogrtest.mongodb_ds.GetLayerByName('_ogr_metadata') assert lyr is not None lyr.SetAttributeFilter("layer LIKE '%s%%'" % ogrtest.mongodb_layer_name) assert lyr.GetFeatureCount() == 2 assert ogrtest.mongodb_ds.DeleteLayer(-1) != 0 lyr = ogrtest.mongodb_ds.GetLayerByName(ogrtest.mongodb_test_dbname + '.' + '_ogr_metadata') assert lyr is not None ogrtest.mongodb_ds = None # Reopen in read-only ogrtest.mongodb_ds = gdal.OpenEx( ogrtest.mongodb_test_uri, 0, open_options=[ 'FEATURE_COUNT_TO_ESTABLISH_FEATURE_DEFN=2', 'JSON_FIELD=TRUE' ]) lyr = ogrtest.mongodb_ds.GetLayerByName( ogrtest.mongodb_layer_name_no_ogr_metadata) assert lyr.TestCapability(ogr.OLCFastSpatialFilter) == 0 f = lyr.GetNextFeature() for i in range(f_ref.GetDefnRef().GetFieldCount()): if f_ref.GetFieldDefnRef(i).GetNameRef() == "str_is_null": continue if f_ref.GetFieldDefnRef(i).GetNameRef() == "str_is_unset": continue # Order might be a bit different... j = f.GetDefnRef().GetFieldIndex(f_ref.GetFieldDefnRef(i).GetNameRef()) if f.GetField(j) != f_ref.GetField(i) or \ f.GetFieldDefnRef(j).GetType() != f_ref.GetFieldDefnRef(i).GetType() or \ f.GetFieldDefnRef(j).GetSubType() != f_ref.GetFieldDefnRef(i).GetSubType(): f.DumpReadable() f_ref.DumpReadable() pytest.fail() for i in range(f_ref.GetDefnRef().GetGeomFieldCount()): # Order might be a bit different... j = f.GetDefnRef().GetGeomFieldIndex( f_ref.GetGeomFieldDefnRef(i).GetNameRef()) if not f.GetGeomFieldRef(j).Equals(f_ref.GetGeomFieldRef(i)) or \ f.GetGeomFieldDefnRef(j).GetName() != f_ref.GetGeomFieldDefnRef(i).GetName() or \ f.GetGeomFieldDefnRef(j).GetType() != f_ref.GetGeomFieldDefnRef(i).GetType(): f.DumpReadable() f_ref.DumpReadable() print(f_ref.GetGeomFieldDefnRef(i).GetType()) pytest.fail(f.GetGeomFieldDefnRef(j).GetType()) lyr.SetSpatialFilterRect(2.1, 49.1, 2.9, 49.9) lyr.ResetReading() if f is None: f.DumpReadable() pytest.fail() lyr = ogrtest.mongodb_ds.GetLayerByName( ogrtest.mongodb_layer_name_guess_types) expected_fields = [ ("int", ogr.OFTInteger), ("int64", ogr.OFTInteger64), ("real", ogr.OFTReal), ("intlist", ogr.OFTIntegerList), ("reallist", ogr.OFTRealList), ("int64list", ogr.OFTInteger64List), ("int_str", ogr.OFTString), ("str_int", ogr.OFTString), ("int64_str", ogr.OFTString), ("str_int64", ogr.OFTString), ("int_int64", ogr.OFTInteger64), ("int64_int", ogr.OFTInteger64), ("int_real", ogr.OFTReal), ("real_int", ogr.OFTReal), ("int64_real", ogr.OFTReal), ("real_int64", ogr.OFTReal), ("real_str", ogr.OFTString), ("str_real", ogr.OFTString), ("int_bool", ogr.OFTInteger), ("bool_int", ogr.OFTInteger), ("intlist_strlist", ogr.OFTStringList), ("strlist_intlist", ogr.OFTStringList), ("intlist_int64list", ogr.OFTInteger64List), ("int64list_intlist", ogr.OFTInteger64List), ("intlist_reallist", ogr.OFTRealList), ("reallist_intlist", ogr.OFTRealList), ("int64list_reallist", ogr.OFTRealList), ("reallist_int64list", ogr.OFTRealList), ("intlist_boollist", ogr.OFTIntegerList), ("boollist_intlist", ogr.OFTIntegerList), ("mixedlist", ogr.OFTRealList), ("mixedlist2", ogr.OFTStringList) ] for (fieldname, fieldtype) in expected_fields: fld_defn = lyr.GetLayerDefn().GetFieldDefn( lyr.GetLayerDefn().GetFieldIndex(fieldname)) assert fld_defn.GetType() == fieldtype, fieldname assert fld_defn.GetSubType() == ogr.OFSTNone f = lyr.GetNextFeature() f = lyr.GetNextFeature() f = lyr.GetNextFeature() if f['intlist'] != [1, 1, -2147483648, 2147483647, -2147483648, 2147483647, -2147483648, 2147483647, 1, 1] or \ f['int64list'] != [1, 1234567890123456, 1, -9223372036854775808, 9223372036854775807, -9223372036854775808, 9223372036854775807, 1] or \ f['int'] != -2147483648 or f['int64'] != -9223372036854775808 or f['real'] - 1 != f['real']: f.DumpReadable() pytest.fail() f = lyr.GetNextFeature() if f['int'] != 2147483647 or f[ 'int64'] != 9223372036854775807 or f['real'] + 1 != f['real']: f.DumpReadable() pytest.fail() lyr = ogrtest.mongodb_ds.GetLayerByName( ogrtest.mongodb_layer_name_with_2d_index) assert lyr.TestCapability(ogr.OLCFastSpatialFilter) != 0 lyr.SetSpatialFilterRect(1.9, 48.9, 2.1, 49.1) lyr.ResetReading() f = lyr.GetNextFeature() assert f is not None lyr.SetSpatialFilterRect(1.9, 48.9, 1.95, 48.95) lyr.ResetReading() f = lyr.GetNextFeature() assert f is None lyr = ogrtest.mongodb_ds.GetLayerByName( ogrtest.mongodb_layer_name_no_spatial_index) assert lyr.TestCapability(ogr.OLCFastSpatialFilter) == 0 lyr.SetSpatialFilterRect(1.9, 48.9, 2.1, 49.1) lyr.ResetReading() f = lyr.GetNextFeature() assert f is not None gdal.PushErrorHandler() lyr = ogrtest.mongodb_ds.CreateLayer('foo') gdal.PopErrorHandler() assert lyr is None gdal.ErrorReset() gdal.PushErrorHandler() ogrtest.mongodb_ds.ExecuteSQL('WRITE_OGR_METADATA ' + ogrtest.mongodb_layer_name) gdal.PopErrorHandler() assert gdal.GetLastErrorMsg() != '' lyr_count_before = ogrtest.mongodb_ds.GetLayerCount() gdal.PushErrorHandler() ogrtest.mongodb_ds.ExecuteSQL('DELLAYER:' + ogrtest.mongodb_layer_name) gdal.PopErrorHandler() assert ogrtest.mongodb_ds.GetLayerCount() == lyr_count_before lyr = ogrtest.mongodb_ds.GetLayerByName(ogrtest.mongodb_layer_name) gdal.PushErrorHandler() ret = lyr.CreateField(ogr.FieldDefn('foo', ogr.OFTString)) gdal.PopErrorHandler() assert ret != 0 gdal.PushErrorHandler() ret = lyr.CreateGeomField(ogr.GeomFieldDefn('foo', ogr.wkbPoint)) gdal.PopErrorHandler() assert ret != 0 f = ogr.Feature(lyr.GetLayerDefn()) gdal.PushErrorHandler() ret = lyr.CreateFeature(f) gdal.PopErrorHandler() assert ret != 0 gdal.PushErrorHandler() ret = lyr.SetFeature(f) gdal.PopErrorHandler() assert ret != 0 gdal.PushErrorHandler() ret = lyr.DeleteFeature(1) gdal.PopErrorHandler() assert ret != 0
def ogr_pgdump_13(): tests = [ [ ogr.wkbUnknown, [], 'POINT ZM (1 2 3 4)', ["'GEOMETRY',2)", "0101000000000000000000F03F0000000000000040"] ], [ ogr.wkbUnknown, ['GEOM_TYPE=geography'], 'POINT ZM (1 2 3 4)', [ "geography(GEOMETRY", "0101000000000000000000F03F0000000000000040" ] ], [ ogr.wkbUnknown, ['DIM=XYZ'], 'POINT ZM (1 2 3 4)', [ "'GEOMETRY',3)", "0101000080000000000000F03F00000000000000400000000000000840" ] ], [ ogr.wkbUnknown, ['DIM=XYZ', 'GEOM_TYPE=geography'], 'POINT ZM (1 2 3 4)', [ "geography(GEOMETRYZ,", "0101000080000000000000F03F00000000000000400000000000000840" ] ], [ ogr.wkbPoint, ['DIM=XYZ'], 'POINT ZM (1 2 3 4)', [ "'POINT',3)", "0101000080000000000000F03F00000000000000400000000000000840" ] ], [ ogr.wkbPoint25D, [], 'POINT ZM (1 2 3 4)', [ "'POINT',3)", "0101000080000000000000F03F00000000000000400000000000000840" ] ], [ ogr.wkbPoint, ['DIM=XYZ', 'GEOM_TYPE=geography'], 'POINT ZM (1 2 3 4)', [ "geography(POINTZ,", "0101000080000000000000F03F00000000000000400000000000000840" ] ], [ ogr.wkbUnknown, ['DIM=XYM'], 'POINT ZM (1 2 3 4)', [ "'GEOMETRY',3)", "0101000040000000000000F03F00000000000000400000000000001040" ] ], [ ogr.wkbUnknown, ['DIM=XYM', 'GEOM_TYPE=geography'], 'POINT ZM (1 2 3 4)', [ "geography(GEOMETRYM,", "0101000040000000000000F03F00000000000000400000000000001040" ] ], [ ogr.wkbPoint, ['DIM=XYM'], 'POINT ZM (1 2 3 4)', [ "'POINTM',3)", "0101000040000000000000F03F00000000000000400000000000001040" ] ], [ ogr.wkbPointM, [], 'POINT ZM (1 2 3 4)', [ "'POINTM',3)", "0101000040000000000000F03F00000000000000400000000000001040" ] ], [ ogr.wkbPoint, ['DIM=XYM', 'GEOM_TYPE=geography'], 'POINT ZM (1 2 3 4)', [ "geography(POINTM,", "0101000040000000000000F03F00000000000000400000000000001040" ] ], [ ogr.wkbUnknown, ['DIM=XYZM'], 'POINT ZM (1 2 3 4)', [ "'GEOMETRY',4)", "01010000C0000000000000F03F000000000000004000000000000008400000000000001040" ] ], [ ogr.wkbUnknown, ['DIM=XYZM', 'GEOM_TYPE=geography'], 'POINT ZM (1 2 3 4)', [ "geography(GEOMETRYZM,", "01010000C0000000000000F03F000000000000004000000000000008400000000000001040" ] ], [ ogr.wkbPoint, ['DIM=XYZM'], 'POINT ZM (1 2 3 4)', [ "'POINT',4)", "01010000C0000000000000F03F000000000000004000000000000008400000000000001040" ] ], [ ogr.wkbPointZM, [], 'POINT ZM (1 2 3 4)', [ "'POINT',4)", "01010000C0000000000000F03F000000000000004000000000000008400000000000001040" ] ], [ ogr.wkbPoint, ['DIM=XYZM', 'GEOM_TYPE=geography'], 'POINT ZM (1 2 3 4)', [ "geography(POINTZM,", "01010000C0000000000000F03F000000000000004000000000000008400000000000001040" ] ], ] for (geom_type, options, wkt, expected_strings) in tests: ds = ogr.GetDriverByName('PGDump').CreateDataSource( '/vsimem/ogr_pgdump_13.sql', options=['LINEFORMAT=LF']) lyr = ds.CreateLayer('test', geom_type=geom_type, options=options) f = ogr.Feature(lyr.GetLayerDefn()) f.SetGeometryDirectly(ogr.CreateGeometryFromWkt(wkt)) lyr.CreateFeature(f) f = None ds = None f = gdal.VSIFOpenL('/vsimem/ogr_pgdump_13.sql', 'rb') sql = gdal.VSIFReadL(1, 10000, f).decode('utf8') gdal.VSIFCloseL(f) gdal.Unlink('/vsimem/ogr_pgdump_13.sql') for expected_string in expected_strings: if sql.find(expected_string) < 0: gdaltest.post_reason('fail') print(geom_type, options, wkt, expected_string) print(sql) return 'fail' if 'GEOM_TYPE=geography' in options: continue ds = ogr.GetDriverByName('PGDump').CreateDataSource( '/vsimem/ogr_pgdump_13.sql', options=['LINEFORMAT=LF']) lyr = ds.CreateLayer('test', geom_type=ogr.wkbNone, options=options) lyr.CreateGeomField(ogr.GeomFieldDefn("my_geom", geom_type)) f = ogr.Feature(lyr.GetLayerDefn()) f.SetGeometryDirectly(ogr.CreateGeometryFromWkt(wkt)) lyr.CreateFeature(f) f = None ds = None f = gdal.VSIFOpenL('/vsimem/ogr_pgdump_13.sql', 'rb') sql = gdal.VSIFReadL(1, 10000, f).decode('utf8') gdal.VSIFCloseL(f) gdal.Unlink('/vsimem/ogr_pgdump_13.sql') for expected_string in expected_strings: if sql.find(expected_string) < 0: gdaltest.post_reason('fail') print(geom_type, options, wkt, expected_string) print(sql) return 'fail' return 'success'
# This file is generated by the OGR_API_SPY mechanism. from osgeo import gdal from osgeo import ogr from osgeo import osr import os import shutil os.access shutil.copy gdal.SetConfigOption('FOO', 'BAR') ogr.Open('non_existing', update=0) ogr.Open('non_existing', update=1) ds1 = ogr.GetDriverByName('CSV').CreateDataSource('/vsimem/test.csv', options=['GEOMETRY=AS_WKT']) ds1_lyr1 = ds1.CreateLayer('test', srs=None, geom_type=ogr.wkbUnknown, options=[]) geom_fd = ogr.GeomFieldDefn('geomfield', ogr.wkbPolygon) geom_fd.SetSpatialRef(osr.SpatialReference("""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"]]""")) geom_fd.SetNullable(0) ds1_lyr1.CreateGeomField(geom_fd, approx_ok=1) ds1 = None ds1 = ogr.Open('/vsimem/test.csv', update=1) ds1 = None ogr.GetDriverByName('CSV').DeleteDataSource('/vsimem/test.csv') ds1 = ogr.GetDriverByName('ESRI Shapefile').CreateDataSource('/vsimem/test', options=[]) ds1_lyr1 = ds1.CreateLayer('test', srs=osr.SpatialReference("""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"]]"""), geom_type=ogr.wkbPoint, options=['ENCODING=UTF-8', 'ENCODING=UTF-8']) fdefn1 = ds1_lyr1.GetLayerDefn() f = ogr.Feature(fdefn1) ds1_lyr1.CreateFeature(f) f = None fd = ogr.FieldDefn('intfield', ogr.OFTInteger) ds1_lyr1.CreateField(fd, approx_ok=1) fd = ogr.FieldDefn('realfield', ogr.OFTReal)
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 test_ogr_rfc41_8(require_ogr_sql_sqlite): # noqa ds = ogr.GetDriverByName('memory').CreateDataSource('') lyr = ds.CreateLayer('mytable', geom_type=ogr.wkbPolygon) lyr.GetLayerDefn().GetGeomFieldDefn(0).SetName('geomfield') gfld_defn = ogr.GeomFieldDefn('geomfield2', ogr.wkbPoint25D) sr = osr.SpatialReference() sr.ImportFromEPSG(4326) gfld_defn.SetSpatialRef(sr) lyr.CreateGeomField(gfld_defn) # Check that we get the geometry columns, even with no features sql_lyr = ds.ExecuteSQL('SELECT * FROM mytable', dialect='SQLite') assert sql_lyr.GetLayerDefn().GetGeomFieldCount() == 2 assert sql_lyr.GetLayerDefn().GetGeomFieldDefn( 0).GetType() == ogr.wkbPolygon assert sql_lyr.GetLayerDefn().GetGeomFieldDefn(0).GetSpatialRef() is None assert sql_lyr.GetLayerDefn().GetGeomFieldDefn( 1).GetType() == ogr.wkbPoint25D srs = sql_lyr.GetLayerDefn().GetGeomFieldDefn(1).GetSpatialRef() assert srs.GetAuthorityCode(None) == '4326' ds.ReleaseResultSet(sql_lyr) # Test INSERT INTO request ds.ExecuteSQL("INSERT INTO mytable (geomfield, geomfield2) VALUES (" + "GeomFromText('POLYGON ((0 0,0 1,1 1,1 0,0 0))'), " + "GeomFromText('POINT Z(0 1 2)') )", dialect='SQLite') # Check output sql_lyr = ds.ExecuteSQL('SELECT geomfield2, geomfield FROM mytable', dialect='SQLite') feat = sql_lyr.GetNextFeature() geom = feat.GetGeomFieldRef('geomfield') if geom.ExportToWkt() != 'POLYGON ((0 0,0 1,1 1,1 0,0 0))': feat.DumpReadable() pytest.fail() geom = feat.GetGeomFieldRef('geomfield2') if geom.ExportToWkt() != 'POINT (0 1 2)': feat.DumpReadable() pytest.fail() feat = None ds.ReleaseResultSet(sql_lyr) # Test UPDATE ds.ExecuteSQL("UPDATE mytable SET geomfield2 = " + "GeomFromText('POINT Z(3 4 5)')", dialect='SQLite') # Check output sql_lyr = ds.ExecuteSQL('SELECT geomfield2, geomfield FROM mytable', dialect='SQLite') feat = sql_lyr.GetNextFeature() geom = feat.GetGeomFieldRef('geomfield') if geom.ExportToWkt() != 'POLYGON ((0 0,0 1,1 1,1 0,0 0))': feat.DumpReadable() pytest.fail() geom = feat.GetGeomFieldRef('geomfield2') if geom.ExportToWkt() != 'POINT (3 4 5)': feat.DumpReadable() pytest.fail() feat = None ds.ReleaseResultSet(sql_lyr)