def ogr_join_23(): ds = ogr.GetDriverByName('Memory').CreateDataSource('') lyr = ds.CreateLayer('first') ogrtest.quick_create_layer_def(lyr, [['f']]) ogrtest.quick_create_feature(lyr, [None], None) ogrtest.quick_create_feature(lyr, ['key1'], None) lyr = ds.CreateLayer('second') ogrtest.quick_create_layer_def(lyr, [['f']]) ogrtest.quick_create_feature(lyr, ['key1'], None) ogrtest.quick_create_feature(lyr, [None], None) sql_lyr = ds.ExecuteSQL("SELECT * FROM first JOIN second ON first.f = second.f") feat = sql_lyr.GetNextFeature() if feat.IsFieldSetAndNotNull('second.f'): gdaltest.post_reason('fail') feat.DumpReadable() return 'fail' feat = sql_lyr.GetNextFeature() if feat['f'] != 'key1' or feat['second.f'] != 'key1': gdaltest.post_reason('fail') feat.DumpReadable() return 'fail' ds.ReleaseResultSet(sql_lyr) ds = None return 'success'
def ogr_index_2(): drv = ogr.GetDriverByName('ESRI Shapefile') gdaltest.s_ds = drv.CreateDataSource('join_t.dbf') gdaltest.s_lyr = gdaltest.s_ds.CreateLayer('join_t', geom_type=ogr.wkbNone) ogrtest.quick_create_layer_def(gdaltest.s_lyr, [('SKEY', ogr.OFTInteger), ('VALUE', ogr.OFTString, 16)]) for i in range(20): ogrtest.quick_create_feature(gdaltest.s_lyr, [i, 'Value ' + str(i)], None) if gdaltest.s_lyr.GetFeatureCount() != 20: gdaltest.post_reason('FeatureCount wrong') return 'fail' gdaltest.s_ds.Release() gdaltest.s_lyr = None gdaltest.s_ds = None gdaltest.s_ds = ogr.OpenShared('join_t.dbf', update=1) gdaltest.s_lyr = gdaltest.s_ds.GetLayerByName('join_t') return 'success'
def ogr_join_22(): ds = ogr.GetDriverByName('Memory').CreateDataSource('') lyr = ds.CreateLayer('first') ogrtest.quick_create_layer_def(lyr, [['id.1'], ['id2']]) ogrtest.quick_create_feature(lyr, [ 'key1', 'key2' ], None) lyr = ds.CreateLayer('second') ogrtest.quick_create_layer_def(lyr, [['id.1'], ['id2'], ['val']]) ogrtest.quick_create_feature(lyr, [ 'key1', 'keyX', '1' ], None) ogrtest.quick_create_feature(lyr, [ 'key1', 'key2', '2' ], None) ogrtest.quick_create_feature(lyr, [ 'key1', 'keyY', '3' ], None) sql_lyr = ds.ExecuteSQL("SELECT val FROM first JOIN second ON first.\"id.1\" = second.\"id.1\" AND first.id2 = second.id2") feat = sql_lyr.GetNextFeature() val = feat.GetFieldAsString(0) ds.ReleaseResultSet(sql_lyr) ds = None if val != '2': gdaltest.post_reason('fail') print(val) return 'fail' return 'success'
def ogr_mysql_20(): if gdaltest.mysql_ds is None: return 'skip' layer = gdaltest.mysql_ds.CreateLayer('select') ogrtest.quick_create_layer_def( layer, [ ('desc', ogr.OFTString) , ('select', ogr.OFTString) ] ) dst_feat = ogr.Feature( feature_def = layer.GetLayerDefn() ) dst_feat.SetField( 'desc', 'desc' ) dst_feat.SetField( 'select', 'select' ) # We are obliged to create a fake geometry dst_feat.SetGeometryDirectly( ogr.CreateGeometryFromWkt('POINT(0 1)') ) layer.CreateFeature( dst_feat ) dst_feat.Destroy() layer = gdaltest.mysql_ds.GetLayerByName('select') layer.ResetReading() feat = layer.GetNextFeature() if feat.desc == 'desc' and feat.select == 'select': return 'success' else: return 'fail'
def ogr_mysql_22(): # On Travis fails with # ERROR 1: MySQL error message:Field 'SHAPE' doesn't have a default value Description: INSERT INTO `tablewithoutspatialindex` (`name`) VALUES ('name') # FIXME if gdaltest.skip_on_travis(): return 'skip' if gdaltest.mysql_ds is None: return 'skip' layer = gdaltest.mysql_ds.CreateLayer('tablewithoutspatialindex', geom_type = ogr.wkbPoint, options = [ 'SPATIAL_INDEX=NO', 'ENGINE=MyISAM' ] ) ogrtest.quick_create_layer_def( layer, [ ('name', ogr.OFTString) ] ) dst_feat = ogr.Feature( feature_def = layer.GetLayerDefn() ) dst_feat.SetField( 'name', 'name' ) layer.CreateFeature( dst_feat ) dst_feat.Destroy() layer.ResetReading() feat = layer.GetNextFeature() if feat is None: return 'fail' return 'success'
def ogr_join_22(): ds = ogr.GetDriverByName("Memory").CreateDataSource("") lyr = ds.CreateLayer("first") ogrtest.quick_create_layer_def(lyr, [["id.1"], ["id2"]]) ogrtest.quick_create_feature(lyr, ["key1", "key2"], None) lyr = ds.CreateLayer("second") ogrtest.quick_create_layer_def(lyr, [["id.1"], ["id2"], ["val"]]) ogrtest.quick_create_feature(lyr, ["key1", "keyX", "1"], None) ogrtest.quick_create_feature(lyr, ["key1", "key2", "2"], None) ogrtest.quick_create_feature(lyr, ["key1", "keyY", "3"], None) sql_lyr = ds.ExecuteSQL( 'SELECT val FROM first JOIN second ON first."id.1" = second."id.1" AND first.id2 = second.id2' ) feat = sql_lyr.GetNextFeature() val = feat.GetFieldAsString(0) ds.ReleaseResultSet(sql_lyr) ds = None if val != "2": gdaltest.post_reason("fail") print(val) return "fail" return "success"
def test_ogr_pgdump_1(): try: os.remove('tmp/tpoly.sql') except OSError: pass ds = ogr.GetDriverByName('PGDump').CreateDataSource('tmp/tpoly.sql') ###################################################### # Create Layer lyr = ds.CreateLayer('tpoly', options=['DIM=3']) ###################################################### # Setup Schema ogrtest.quick_create_layer_def(lyr, [('AREA', ogr.OFTReal), ('EAS_ID', ogr.OFTInteger), ('PRFEDEA', ogr.OFTString), ('SHORTNAME', ogr.OFTString, 8)]) ###################################################### # Copy in poly.shp dst_feat = ogr.Feature(feature_def=lyr.GetLayerDefn()) shp_ds = ogr.Open('data/poly.shp') shp_lyr = shp_ds.GetLayer(0) feat = shp_lyr.GetNextFeature() gdaltest.poly_feat = [] while feat is not None: gdaltest.poly_feat.append(feat) dst_feat.SetFrom(feat) lyr.CreateFeature(dst_feat) feat = shp_lyr.GetNextFeature() dst_feat.Destroy() ds.Destroy() f = open('tmp/tpoly.sql') sql = f.read() f.close() assert (not (sql.find("""DROP TABLE IF EXISTS "public"."tpoly" CASCADE;""") == -1 or \ sql.find("""DELETE FROM geometry_columns WHERE f_table_name = 'tpoly' AND f_table_schema = 'public';""") == -1 or \ sql.find("""BEGIN;""") == -1 or \ sql.find("""CREATE TABLE "public"."tpoly" ( "ogc_fid" SERIAL, CONSTRAINT "tpoly_pk" PRIMARY KEY ("ogc_fid") );""") == -1 or \ sql.find("""SELECT AddGeometryColumn('public','tpoly','wkb_geometry',-1,'GEOMETRY',3);""") == -1 or \ sql.find("""CREATE INDEX "tpoly_wkb_geometry_geom_idx" ON "public"."tpoly" USING GIST ("wkb_geometry");""") == -1 or \ sql.find("""ALTER TABLE "public"."tpoly" ADD COLUMN "area" FLOAT8;""") == -1 or \ sql.find("""ALTER TABLE "public"."tpoly" ADD COLUMN "eas_id" INTEGER;""") == -1 or \ sql.find("""ALTER TABLE "public"."tpoly" ADD COLUMN "prfedea" VARCHAR;""") == -1 or \ sql.find("""ALTER TABLE "public"."tpoly" ADD COLUMN "shortname" VARCHAR(8);""") == -1 or \ sql.find("""INSERT INTO "public"."tpoly" ("wkb_geometry" , "area", "eas_id", "prfedea") VALUES ('01030000800100000005000000000000C01A481D4100000080072D5241000000000000000000000060AA461D4100000080FF2C524100000000000000000000006060461D41000000400C2D52410000000000000000000000A0DF471D4100000000142D52410000000000000000000000C01A481D4100000080072D52410000000000000000', 5268.813, 170, '35043413');""") == -1 or \ sql.find("""COMMIT;""") == -1))
def ogr_oci_2(): if gdaltest.oci_ds is None: return 'skip' gdal.PushErrorHandler( 'CPLQuietErrorHandler' ) gdaltest.oci_ds.ExecuteSQL( 'DELLAYER:tpoly' ) gdal.PopErrorHandler() ###################################################### # Create Oracle Layer gdaltest.oci_lyr = gdaltest.oci_ds.CreateLayer( 'tpoly', options = ['DIM=3'] ) ###################################################### # Setup Schema ogrtest.quick_create_layer_def( gdaltest.oci_lyr, [ ('AREA', ogr.OFTReal), ('EAS_ID', ogr.OFTInteger), ('PRFEDEA', ogr.OFTString) ] ) ###################################################### # Copy in poly.shp dst_feat = ogr.Feature( feature_def = gdaltest.oci_lyr.GetLayerDefn() ) shp_ds = ogr.Open( 'data/poly.shp' ) gdaltest.shp_ds = shp_ds shp_lyr = shp_ds.GetLayer(0) feat = shp_lyr.GetNextFeature() gdaltest.poly_feat = [] while feat is not None: gdaltest.poly_feat.append( feat ) dst_feat.SetFrom( feat ) gdaltest.oci_lyr.CreateFeature( dst_feat ) feat = shp_lyr.GetNextFeature() dst_feat.Destroy() # Test updating non-existing feature shp_lyr.ResetReading() feat = shp_lyr.GetNextFeature() feat.SetFID(-10) if gdaltest.oci_lyr.SetFeature( feat ) != ogr.OGRERR_NON_EXISTING_FEATURE: gdaltest.post_reason( 'Expected failure of SetFeature().' ) return 'fail' # Test deleting non-existing feature if gdaltest.oci_lyr.DeleteFeature( -10 ) != ogr.OGRERR_NON_EXISTING_FEATURE: gdaltest.post_reason( 'Expected failure of DeleteFeature().' ) return 'fail' return 'success'
def test_ogr_gmt_5(): ####################################################### # Create gmtory Layer gmt_drv = ogr.GetDriverByName('GMT') gdaltest.gmt_ds = gmt_drv.CreateDataSource('tmp/mpoly.gmt') gdaltest.gmt_lyr = gdaltest.gmt_ds.CreateLayer('mpoly') ####################################################### # Setup Schema ogrtest.quick_create_layer_def(gdaltest.gmt_lyr, [('ID', ogr.OFTInteger)]) ####################################################### # Write a first multipolygon dst_feat = ogr.Feature(feature_def=gdaltest.gmt_lyr.GetLayerDefn()) dst_feat.SetGeometryDirectly( ogr.CreateGeometryFromWkt('MULTIPOLYGON(((0 0,0 10,10 10,0 10,0 0),(3 3,4 4, 3 4,3 3)),((12 0,14 0,12 3,12 0)))')) dst_feat.SetField('ID', 15) gdal.SetConfigOption('GMT_USE_TAB', 'TRUE') # Ticket #6453 gdaltest.gmt_lyr.CreateFeature(dst_feat) gdal.SetConfigOption('GMT_USE_TAB', None) dst_feat = ogr.Feature(feature_def=gdaltest.gmt_lyr.GetLayerDefn()) dst_feat.SetGeometryDirectly( ogr.CreateGeometryFromWkt('MULTIPOLYGON(((30 20,40 20,30 30,30 20)))')) dst_feat.SetField('ID', 16) gdaltest.gmt_lyr.CreateFeature(dst_feat) gdaltest.gmt_lyr = None gdaltest.gmt_ds = None # Reopen. ds = ogr.Open('tmp/mpoly.gmt') lyr = ds.GetLayer(0) assert lyr.GetLayerDefn().GetGeomType() == ogr.wkbMultiPolygon, \ 'did not get expected multipolygon type.' feat = lyr.GetNextFeature() assert not ogrtest.check_feature_geometry(feat, 'MULTIPOLYGON(((0 0,0 10,10 10,0 10,0 0),(3 3,4 4, 3 4,3 3)),((12 0,14 0,12 3,12 0)))') assert feat.GetField('ID') == 15, 'got wrong id, first feature' feat = lyr.GetNextFeature() assert not ogrtest.check_feature_geometry(feat, 'MULTIPOLYGON(((30 20,40 20,30 30,30 20)))') assert feat.GetField('ID') == 16, 'got wrong ID, second feature' feat = lyr.GetNextFeature() assert feat is None, 'did not get null feature when expected.'
def ogr_mysql_2(): if gdaltest.mysql_ds is None: return "skip" shp_ds = ogr.Open("data/poly.shp") gdaltest.shp_ds = shp_ds shp_lyr = shp_ds.GetLayer(0) ###################################################### # Create Layer gdaltest.mysql_lyr = gdaltest.mysql_ds.CreateLayer("tpoly", srs=shp_lyr.GetSpatialRef(), options=["ENGINE=MyISAM"]) ###################################################### # Setup Schema ogrtest.quick_create_layer_def( gdaltest.mysql_lyr, [ ("AREA", ogr.OFTReal), ("EAS_ID", ogr.OFTInteger), ("PRFEDEA", ogr.OFTString), ("SHORTNAME", ogr.OFTString, 8), ("INT64", ogr.OFTInteger64), ], ) ###################################################### # Copy in poly.shp dst_feat = ogr.Feature(feature_def=gdaltest.mysql_lyr.GetLayerDefn()) feat = shp_lyr.GetNextFeature() gdaltest.poly_feat = [] while feat is not None: gdaltest.poly_feat.append(feat) dst_feat.SetFrom(feat) dst_feat.SetField("INT64", 1234567890123) gdaltest.mysql_lyr.CreateFeature(dst_feat) feat = shp_lyr.GetNextFeature() dst_feat.Destroy() if gdaltest.mysql_lyr.GetFeatureCount() != shp_lyr.GetFeatureCount(): gdaltest.post_reason("not matching feature count") return "failure" if not gdaltest.mysql_lyr.GetSpatialRef().IsSame(shp_lyr.GetSpatialRef()): gdaltest.post_reason("not matching spatial ref") return "failure" return "success"
def rasterize_4(): # Setup working spatial reference sr_wkt = 'LOCAL_CS["arbitrary"]' sr = osr.SpatialReference( sr_wkt ) # Create a memory raster to rasterize into. target_ds = gdal.GetDriverByName('MEM').Create( '', 100, 100, 3, gdal.GDT_Byte ) target_ds.SetGeoTransform( (1000,1,0,1100,0,-1) ) target_ds.SetProjection( sr_wkt ) # Create a memory layer to rasterize from. rast_ogr_ds = ogr.GetDriverByName('Memory').CreateDataSource( 'wrk' ) rast_mem_lyr = rast_ogr_ds.CreateLayer( 'poly', srs=sr ) # Setup Schema ogrtest.quick_create_layer_def( rast_mem_lyr, [ ('CELSIUS', ogr.OFTReal) ] ) # Add polygons and linestrings and a field named CELSIUS. wkt_geom = ['POLYGON((1020 1030 40,1020 1045 30,1050 1045 20,1050 1030 35,1020 1030 40))', 'POLYGON((1010 1046 85,1015 1055 35,1055 1060 26,1054 1048 35,1010 1046 85))', 'POLYGON((1020 1076 190,1025 1085 35,1065 1090 26,1064 1078 35,1020 1076 190),(1023 1079 5,1061 1081 35,1062 1087 26,1028 1082 35,1023 1079 85))', 'LINESTRING(1005 1000 10, 1100 1050 120)', 'LINESTRING(1000 1000 150, 1095 1050 -5, 1080 1080 200)'] celsius_field_values = [50,255,60,100,180] i = 0 for g in wkt_geom: feat = ogr.Feature( rast_mem_lyr.GetLayerDefn() ) feat.SetGeometryDirectly( ogr.Geometry(wkt = g) ) feat.SetField( 'CELSIUS', celsius_field_values[i] ) rast_mem_lyr.CreateFeature( feat ) i = i + 1 # Run the algorithm. err = gdal.RasterizeLayer( target_ds, [1,2,3], rast_mem_lyr, options = ["ATTRIBUTE=CELSIUS"] ) if err != 0: print(err) gdaltest.post_reason( 'got non-zero result code from RasterizeLayer' ) return 'fail' # Check results. expected = 16265 checksum = target_ds.GetRasterBand(2).Checksum() if checksum != expected: print(checksum) gdaltest.post_reason( 'Did not get expected image checksum' ) gdal.GetDriverByName('GTiff').CreateCopy('tmp/rasterize_4.tif',target_ds) return 'fail' return 'success'
def ogr_sqlite_14(): if gdaltest.sl_ds is None: return 'skip' gdaltest.sl_lyr = gdaltest.sl_ds.CreateLayer( 'testtypes' ) ogrtest.quick_create_layer_def( gdaltest.sl_lyr, [ ('INTEGER', ogr.OFTInteger), ('FLOAT', ogr.OFTReal), ('STRING', ogr.OFTString), ('BLOB', ogr.OFTBinary), ('BLOB2', ogr.OFTBinary) ] ) dst_feat = ogr.Feature( feature_def = gdaltest.sl_lyr.GetLayerDefn() ) dst_feat.SetField('INTEGER', 1) dst_feat.SetField('FLOAT', 1.2) dst_feat.SetField('STRING', 'myString\'a') gdaltest.sl_lyr.CreateFeature( dst_feat ) dst_feat.Destroy() # Set the BLOB attribute via SQL UPDATE instructions as there's no binding # for OGR_F_SetFieldBinary gdaltest.sl_ds.ExecuteSQL("UPDATE testtypes SET BLOB = x'0001FF' WHERE OGC_FID = 1") ###################################################### # Reopen DB gdaltest.sl_ds.Destroy() gdaltest.sl_ds = ogr.Open( 'tmp/sqlite_test.db', update = 1 ) gdaltest.sl_lyr = gdaltest.sl_ds.GetLayerByName('testtypes') # Duplicate the first record dst_feat = ogr.Feature( feature_def = gdaltest.sl_lyr.GetLayerDefn() ) feat_read = gdaltest.sl_lyr.GetNextFeature() dst_feat.SetFrom(feat_read) gdaltest.sl_lyr.CreateFeature( dst_feat ) dst_feat.Destroy() # Check the 2 records gdaltest.sl_lyr.ResetReading() for i in range(2): feat_read = gdaltest.sl_lyr.GetNextFeature() if feat_read.GetField('INTEGER') != 1 or \ feat_read.GetField('FLOAT') != 1.2 or \ feat_read.GetField('STRING') != 'myString\'a' or \ feat_read.GetFieldAsString('BLOB') != '0001FF': return 'fail' gdaltest.sl_lyr.ResetReading() return 'success'
def ogr_mssqlspatial_2(): if gdaltest.mssqlspatial_ds is None: return 'skip' shp_ds = ogr.Open('data/poly.shp') gdaltest.shp_ds = shp_ds shp_lyr = shp_ds.GetLayer(0) ###################################################### # Create Layer gdaltest.mssqlspatial_lyr = gdaltest.mssqlspatial_ds.CreateLayer( 'tpoly', srs=shp_lyr.GetSpatialRef()) ###################################################### # Setup Schema ogrtest.quick_create_layer_def(gdaltest.mssqlspatial_lyr, [('AREA', ogr.OFTReal), ('EAS_ID', ogr.OFTInteger), ('PRFEDEA', ogr.OFTString), ('SHORTNAME', ogr.OFTString, 8), ('INT64', ogr.OFTInteger64)]) ###################################################### # Copy in poly.shp dst_feat = ogr.Feature( feature_def=gdaltest.mssqlspatial_lyr.GetLayerDefn()) feat = shp_lyr.GetNextFeature() gdaltest.poly_feat = [] while feat is not None: gdaltest.poly_feat.append(feat) dst_feat.SetFrom(feat) dst_feat.SetField('INT64', 1234567890123) gdaltest.mssqlspatial_lyr.CreateFeature(dst_feat) feat = shp_lyr.GetNextFeature() dst_feat = None if gdaltest.mssqlspatial_lyr.GetFeatureCount() != shp_lyr.GetFeatureCount(): gdaltest.post_reason('not matching feature count') return 'fail' if not gdaltest.mssqlspatial_lyr.GetSpatialRef().IsSame(shp_lyr.GetSpatialRef()): gdaltest.post_reason('not matching spatial ref') return 'fail' return 'success'
def ogr_mitab_2(): if gdaltest.mapinfo_ds is None: return 'skip' # This should convert to MapInfo datum name 'New_Zealand_GD49' WEIRD_SRS = 'PROJCS["NZGD49 / UTM zone 59S",GEOGCS["NZGD49",DATUM["NZGD49",SPHEROID["International 1924",6378388,297,AUTHORITY["EPSG","7022"]],TOWGS84[59.47,-5.04,187.44,0.47,-0.1,1.024,-4.5993],AUTHORITY["EPSG","6272"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4272"]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",171],PARAMETER["scale_factor",0.9996],PARAMETER["false_easting",500000],PARAMETER["false_northing",10000000],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AXIS["Easting",EAST],AXIS["Northing",NORTH],AUTHORITY["EPSG","27259"]]' gdaltest.mapinfo_srs = osr.SpatialReference() gdaltest.mapinfo_srs.ImportFromWkt(WEIRD_SRS) ####################################################### # Create memory Layer gdaltest.mapinfo_lyr = gdaltest.mapinfo_ds.CreateLayer('tpoly', gdaltest.mapinfo_srs) ####################################################### # Setup Schema ogrtest.quick_create_layer_def( gdaltest.mapinfo_lyr, [ ('AREA', ogr.OFTReal), ('EAS_ID', ogr.OFTInteger), ('PRFEDEA', ogr.OFTString) ] ) ####################################################### # Copy in poly.shp dst_feat = ogr.Feature( feature_def = gdaltest.mapinfo_lyr.GetLayerDefn() ) shp_ds = ogr.Open( 'data/poly.shp' ) gdaltest.shp_ds = shp_ds shp_lyr = shp_ds.GetLayer(0) feat = shp_lyr.GetNextFeature() gdaltest.poly_feat = [] while feat is not None: gdaltest.poly_feat.append( feat ) dst_feat.SetFrom( feat ) gdaltest.mapinfo_lyr.CreateFeature( dst_feat ) feat = shp_lyr.GetNextFeature() dst_feat.Destroy() ####################################################### # Close file. gdaltest.mapinfo_ds.Destroy() gdaltest.mapinfo_ds = None return 'success'
def test_ogr_mssqlspatial_2(): if gdaltest.mssqlspatial_ds is None: pytest.skip() shp_ds = ogr.Open('data/poly.shp') gdaltest.shp_ds = shp_ds shp_lyr = shp_ds.GetLayer(0) ###################################################### # Create Layer gdaltest.mssqlspatial_lyr = gdaltest.mssqlspatial_ds.CreateLayer( 'tpoly', srs=shp_lyr.GetSpatialRef()) ###################################################### # Setup Schema ogrtest.quick_create_layer_def(gdaltest.mssqlspatial_lyr, [('AREA', ogr.OFTReal), ('EAS_ID', ogr.OFTInteger), ('PRFEDEA', ogr.OFTString), ('SHORTNAME', ogr.OFTString, 8), ('INT64', ogr.OFTInteger64)]) ###################################################### # Copy in poly.shp dst_feat = ogr.Feature( feature_def=gdaltest.mssqlspatial_lyr.GetLayerDefn()) feat = shp_lyr.GetNextFeature() gdaltest.poly_feat = [] while feat is not None: gdaltest.poly_feat.append(feat) dst_feat.SetFrom(feat) dst_feat.SetField('INT64', 1234567890123) gdaltest.mssqlspatial_lyr.CreateFeature(dst_feat) feat = shp_lyr.GetNextFeature() dst_feat = None assert gdaltest.mssqlspatial_lyr.GetFeatureCount() == shp_lyr.GetFeatureCount(), \ 'not matching feature count' got_srs = gdaltest.mssqlspatial_lyr.GetSpatialRef() expected_srs = shp_lyr.GetSpatialRef() assert got_srs.GetAuthorityCode(None) == expected_srs.GetAuthorityCode(None), \ 'not matching spatial ref'
def ogr_mysql_2(): if gdaltest.mysql_ds is None: return 'skip' shp_ds = ogr.Open( 'data/poly.shp' ) gdaltest.shp_ds = shp_ds shp_lyr = shp_ds.GetLayer(0) ###################################################### # Create Layer gdaltest.mysql_lyr = gdaltest.mysql_ds.CreateLayer( 'tpoly', srs = shp_lyr.GetSpatialRef(), options = [ 'DIM=3', 'ENGINE=MyISAM' ] ) ###################################################### # Setup Schema ogrtest.quick_create_layer_def( gdaltest.mysql_lyr, [ ('AREA', ogr.OFTReal), ('EAS_ID', ogr.OFTInteger), ('PRFEDEA', ogr.OFTString), ('SHORTNAME', ogr.OFTString, 8) ] ) ###################################################### # Copy in poly.shp dst_feat = ogr.Feature( feature_def = gdaltest.mysql_lyr.GetLayerDefn() ) feat = shp_lyr.GetNextFeature() gdaltest.poly_feat = [] while feat is not None: gdaltest.poly_feat.append( feat ) dst_feat.SetFrom( feat ) gdaltest.mysql_lyr.CreateFeature( dst_feat ) feat = shp_lyr.GetNextFeature() dst_feat.Destroy() if gdaltest.mysql_lyr.GetFeatureCount() != shp_lyr.GetFeatureCount(): gdaltest.post_reason( 'not matching feature count' ) return 'failure' if not gdaltest.mysql_lyr.GetSpatialRef().IsSame(shp_lyr.GetSpatialRef()): gdaltest.post_reason( 'not matching spatial ref' ) return 'failure' return 'success'
def test_ogr_mysql_2(): if gdaltest.mysql_ds is None: pytest.skip() shp_ds = ogr.Open('data/poly.shp') gdaltest.shp_ds = shp_ds shp_lyr = shp_ds.GetLayer(0) ###################################################### # Create Layer gdaltest.mysql_lyr = gdaltest.mysql_ds.CreateLayer('tpoly', srs=shp_lyr.GetSpatialRef(), options=['ENGINE=MyISAM']) ###################################################### # Setup Schema ogrtest.quick_create_layer_def(gdaltest.mysql_lyr, [('AREA', ogr.OFTReal), ('EAS_ID', ogr.OFTInteger), ('PRFEDEA', ogr.OFTString), ('SHORTNAME', ogr.OFTString, 8), ('INT64', ogr.OFTInteger64)]) ###################################################### # Copy in poly.shp dst_feat = ogr.Feature(feature_def=gdaltest.mysql_lyr.GetLayerDefn()) feat = shp_lyr.GetNextFeature() gdaltest.poly_feat = [] while feat is not None: gdaltest.poly_feat.append(feat) dst_feat.SetFrom(feat) dst_feat.SetField('INT64', 1234567890123) gdaltest.mysql_lyr.CreateFeature(dst_feat) feat = shp_lyr.GetNextFeature() dst_feat.Destroy() assert gdaltest.mysql_lyr.GetFeatureCount() == shp_lyr.GetFeatureCount(), \ 'not matching feature count' assert gdaltest.mysql_lyr.GetSpatialRef().IsSame(shp_lyr.GetSpatialRef()), \ 'not matching spatial ref'
def ogr_mitab_12(): if gdaltest.mapinfo_drv is None: return 'skip' ds = gdaltest.mapinfo_drv.CreateDataSource( 'tmp', options = ['FORMAT=MIF'] ) lyr = ds.CreateLayer( 'testlyrdef' ) defn = lyr.GetLayerDefn() if defn is None: return 'fail' ogrtest.quick_create_layer_def( lyr, [ ('AREA', ogr.OFTReal) ] ) ds.Destroy() return 'success'
def ogr_mitab_8(): if gdaltest.mapinfo_ds is None: return 'skip' ####################################################### # Create memory Layer gdaltest.mapinfo_lyr = gdaltest.mapinfo_ds.CreateLayer( 'tpoly' ) ####################################################### # Setup Schema ogrtest.quick_create_layer_def( gdaltest.mapinfo_lyr, [ ('AREA', ogr.OFTReal), ('EAS_ID', ogr.OFTInteger), ('PRFEDEA', ogr.OFTString) ] ) ####################################################### # Copy in poly.shp dst_feat = ogr.Feature( feature_def = gdaltest.mapinfo_lyr.GetLayerDefn() ) shp_ds = ogr.Open( 'data/poly.shp' ) gdaltest.shp_ds = shp_ds shp_lyr = shp_ds.GetLayer(0) feat = shp_lyr.GetNextFeature() gdaltest.poly_feat = [] while feat is not None: gdaltest.poly_feat.append( feat ) dst_feat.SetFrom( feat ) gdaltest.mapinfo_lyr.CreateFeature( dst_feat ) feat = shp_lyr.GetNextFeature() dst_feat.Destroy() ####################################################### # Close file. gdaltest.mapinfo_ds.Destroy() gdaltest.mapinfo_ds = None return 'success'
def ogr_mem_2(): if gdaltest.mem_ds is None: return 'skip' if gdaltest.mem_ds.TestCapability(ogr.ODsCCreateLayer) == 0: gdaltest.post_reason('ODsCCreateLayer TestCapability failed.') return 'fail' ####################################################### # Create memory Layer gdaltest.mem_lyr = gdaltest.mem_ds.CreateLayer('tpoly') ####################################################### # Setup Schema ogrtest.quick_create_layer_def(gdaltest.mem_lyr, [('AREA', ogr.OFTReal), ('EAS_ID', ogr.OFTInteger), ('PRFEDEA', ogr.OFTString), ('WHEN', ogr.OFTDateTime)]) ####################################################### # Copy in poly.shp dst_feat = ogr.Feature(feature_def=gdaltest.mem_lyr.GetLayerDefn()) shp_ds = ogr.Open('data/poly.shp') gdaltest.shp_ds = shp_ds shp_lyr = shp_ds.GetLayer(0) feat = shp_lyr.GetNextFeature() gdaltest.poly_feat = [] while feat is not None: gdaltest.poly_feat.append(feat) dst_feat.SetFrom(feat) ret = gdaltest.mem_lyr.CreateFeature(dst_feat) if ret != 0: gdaltest.post_reason('CreateFeature() failed.') return 'fail' feat = shp_lyr.GetNextFeature() return 'success'
def ogr_mem_2(): if gdaltest.mem_ds is None: return "skip" if gdaltest.mem_ds.TestCapability(ogr.ODsCCreateLayer) == 0: gdaltest.post_reason("ODsCCreateLayer TestCapability failed.") return "fail" ####################################################### # Create memory Layer gdaltest.mem_lyr = gdaltest.mem_ds.CreateLayer("tpoly") ####################################################### # Setup Schema ogrtest.quick_create_layer_def( gdaltest.mem_lyr, [("AREA", ogr.OFTReal), ("EAS_ID", ogr.OFTInteger), ("PRFEDEA", ogr.OFTString), ("WHEN", ogr.OFTDateTime)], ) ####################################################### # Copy in poly.shp dst_feat = ogr.Feature(feature_def=gdaltest.mem_lyr.GetLayerDefn()) shp_ds = ogr.Open("data/poly.shp") gdaltest.shp_ds = shp_ds shp_lyr = shp_ds.GetLayer(0) feat = shp_lyr.GetNextFeature() gdaltest.poly_feat = [] while feat is not None: gdaltest.poly_feat.append(feat) dst_feat.SetFrom(feat) ret = gdaltest.mem_lyr.CreateFeature(dst_feat) if ret != 0: gdaltest.post_reason("CreateFeature() failed.") return "fail" feat = shp_lyr.GetNextFeature() return "success"
def test_ogr_mysql_22(): if gdaltest.mysql_ds is None: pytest.skip() layer = gdaltest.mysql_ds.CreateLayer('tablewithoutspatialindex', geom_type=ogr.wkbPoint, options=['SPATIAL_INDEX=NO', 'ENGINE=MyISAM']) ogrtest.quick_create_layer_def(layer, [('name', ogr.OFTString)]) dst_feat = ogr.Feature(feature_def=layer.GetLayerDefn()) dst_feat.SetField('name', 'name') layer.CreateFeature(dst_feat) dst_feat.Destroy() layer.ResetReading() feat = layer.GetNextFeature() assert feat is not None
def ogr_sde_8(): "Test spatial references" if gdaltest.sde_dr is None: return 'skip' base = 'SDE:%s,%s,%s,%s,%s' % (sde_server, sde_port, sde_db, sde_user, sde_password) shp_ds = ogr.Open( 'data/poly.shp' ) gdaltest.shp_ds = shp_ds shp_lyr = shp_ds.GetLayer(0) ref = osr.SpatialReference() ref.ImportFromWkt('LOCAL_CS["IMAGE"]') ds = ogr.Open(base, update=1) lyr = ds.CreateLayer( 'SDE.TPOLY' ,geom_type=ogr.wkbPolygon, srs=ref,options = [ 'OVERWRITE=YES' ] ) ref.ImportFromEPSG(4326) lyr = ds.CreateLayer( 'SDE.TPOLY' ,geom_type=ogr.wkbPolygon, srs=ref,options = [ 'OVERWRITE=YES' ] ) ogrtest.quick_create_layer_def( lyr, [ ('AREA', ogr.OFTReal), ('EAS_ID', ogr.OFTInteger), ('PRFEDEA', ogr.OFTString), ('WHEN', ogr.OFTDateTime) ] ) ####################################################### # Copy in poly.shp dst_feat = ogr.Feature( feature_def = lyr.GetLayerDefn() ) feat = shp_lyr.GetNextFeature() gdaltest.poly_feat = [] while feat is not None: gdaltest.poly_feat.append( feat ) dst_feat.SetFrom( feat ) lyr.CreateFeature( dst_feat ) feat = shp_lyr.GetNextFeature() dst_feat.Destroy() return 'success'
def ogr_oci_2(): if gdaltest.oci_ds is None: return 'skip' gdal.PushErrorHandler( 'CPLQuietErrorHandler' ) gdaltest.oci_ds.ExecuteSQL( 'DELLAYER:tpoly' ) gdal.PopErrorHandler() ###################################################### # Create Oracle Layer gdaltest.oci_lyr = gdaltest.oci_ds.CreateLayer( 'tpoly' ) ###################################################### # Setup Schema ogrtest.quick_create_layer_def( gdaltest.oci_lyr, [ ('AREA', ogr.OFTReal), ('EAS_ID', ogr.OFTInteger), ('PRFEDEA', ogr.OFTString) ] ) ###################################################### # Copy in poly.shp dst_feat = ogr.Feature( feature_def = gdaltest.oci_lyr.GetLayerDefn() ) shp_ds = ogr.Open( 'data/poly.shp' ) gdaltest.shp_ds = shp_ds shp_lyr = shp_ds.GetLayer(0) feat = shp_lyr.GetNextFeature() gdaltest.poly_feat = [] while feat is not None: gdaltest.poly_feat.append( feat ) dst_feat.SetFrom( feat ) gdaltest.oci_lyr.CreateFeature( dst_feat ) feat = shp_lyr.GetNextFeature() dst_feat.Destroy() return 'success'
def ogr_ingres_2(): if gdaltest.ingres_ds is None: return 'skip' ####################################################### # Create Layer gdaltest.ingres_lyr = gdaltest.ingres_ds.CreateLayer( \ 'tpoly', geom_type=ogr.wkbPolygon, options = [ 'OVERWRITE=YES' ] ) ####################################################### # Setup Schema ogrtest.quick_create_layer_def( gdaltest.ingres_lyr, [ ('AREA', ogr.OFTReal), ('EAS_ID', ogr.OFTInteger), ('PRFEDEA', ogr.OFTString) ] ) ####################################################### # Copy in poly.shp dst_feat = ogr.Feature( feature_def = gdaltest.ingres_lyr.GetLayerDefn() ) shp_ds = ogr.Open( 'data/poly.shp' ) gdaltest.shp_ds = shp_ds shp_lyr = shp_ds.GetLayer(0) feat = shp_lyr.GetNextFeature() gdaltest.poly_feat = [] while feat is not None: gdaltest.poly_feat.append( feat ) dst_feat.SetFrom( feat ) gdaltest.ingres_lyr.CreateFeature( dst_feat ) feat = shp_lyr.GetNextFeature() dst_feat.Destroy() return 'success'
def test_ogr_mysql_21(): if gdaltest.mysql_ds is None: pytest.skip() layer = gdaltest.mysql_ds.CreateLayer('tablewithspatialindex', geom_type=ogr.wkbPoint, options=['ENGINE=MyISAM']) ogrtest.quick_create_layer_def(layer, [('name', ogr.OFTString)]) dst_feat = ogr.Feature(feature_def=layer.GetLayerDefn()) dst_feat.SetField('name', 'name') # The insertion MUST fail gdal.PushErrorHandler('CPLQuietErrorHandler') layer.CreateFeature(dst_feat) gdal.PopErrorHandler() dst_feat.Destroy() layer.ResetReading() feat = layer.GetNextFeature() assert feat is None
def ogr_index_1(): from osgeo import gdal gdal.PushErrorHandler('CPLQuietErrorHandler') try: ogr.GetDriverByName('MapInfo File').DeleteDataSource('index_p.mif') except AttributeError: pass try: ogr.GetDriverByName('ESRI Shapefile').DeleteDataSource('join_t.dbf') except AttributeError: pass gdal.PopErrorHandler() drv = ogr.GetDriverByName('MapInfo File') gdaltest.p_ds = drv.CreateDataSource('index_p.mif') gdaltest.p_lyr = gdaltest.p_ds.CreateLayer('index_p') ogrtest.quick_create_layer_def(gdaltest.p_lyr, [('PKEY', ogr.OFTInteger)]) ogrtest.quick_create_feature(gdaltest.p_lyr, [5], None) ogrtest.quick_create_feature(gdaltest.p_lyr, [10], None) ogrtest.quick_create_feature(gdaltest.p_lyr, [9], None) ogrtest.quick_create_feature(gdaltest.p_lyr, [4], None) ogrtest.quick_create_feature(gdaltest.p_lyr, [3], None) ogrtest.quick_create_feature(gdaltest.p_lyr, [1], None) # It turns out mapinfo format doesn't allow GetFeatureCount() calls while # writing ... it just blows an assert! # if gdaltest.p_lyr.GetFeatureCount() != 7: # gdaltest.post_reason( 'FeatureCount wrong' ) # return 'fail' # Close and reopen, since it seems the .mif driver does not allow reading # from a newly created (updatable) file. gdaltest.p_ds = None gdaltest.p_ds = ogr.OpenShared('index_p.mif', update=0) gdaltest.p_lyr = gdaltest.p_ds.GetLayerByName('index_p') return 'success'
def ogr_mysql_22(): if gdaltest.mysql_ds is None: return 'skip' layer = gdaltest.mysql_ds.CreateLayer('tablewithoutspatialindex', geom_type = ogr.wkbPoint, options = [ 'SPATIAL_INDEX=NO' ] ) ogrtest.quick_create_layer_def( layer, [ ('name', ogr.OFTString) ] ) dst_feat = ogr.Feature( feature_def = layer.GetLayerDefn() ) dst_feat.SetField( 'name', 'name' ) layer.CreateFeature( dst_feat ) dst_feat.Destroy() layer.ResetReading() feat = layer.GetNextFeature() if feat is None: return 'fail' return 'success'
def test_ogr_join_15(): ds = ogr.GetDriverByName('CSV').CreateDataSource('/vsimem/ogr_join_14') lyr = ds.CreateLayer('first') ogrtest.quick_create_layer_def(lyr, [['id']]) ogrtest.quick_create_feature(lyr, ['key'], None) lyr = ds.CreateLayer('second') ogrtest.quick_create_layer_def(lyr, [['col1_2'], ['id'], ['col3_2']]) ogrtest.quick_create_feature(lyr, ['a2', 'key', 'c2'], None) lyr = ds.CreateLayer('third') ogrtest.quick_create_layer_def(lyr, [['col1_3'], ['id'], ['col3_3']]) ogrtest.quick_create_feature(lyr, ['a3', 'key', 'c3'], None) sql_lyr = ds.ExecuteSQL("SELECT concat(col3_2, ''), col3_2 FROM first JOIN second ON first.id = second.id JOIN third ON first.id = third.id") feat = sql_lyr.GetNextFeature() val1 = feat.GetFieldAsString(0) val2 = feat.GetFieldAsString(1) ds.ReleaseResultSet(sql_lyr) ds = None gdal.Unlink('/vsimem/ogr_join_14/first.csv') gdal.Unlink('/vsimem/ogr_join_14/second.csv') gdal.Unlink('/vsimem/ogr_join_14/third.csv') gdal.Unlink('/vsimem/ogr_join_14') assert val1 == 'c2' assert val2 == 'c2'
def ogr_gmt_2(): ####################################################### # Create gmtory Layer gdaltest.gmt_lyr = gdaltest.gmt_ds.CreateLayer( 'tpoly' ) ####################################################### # Setup Schema ogrtest.quick_create_layer_def( gdaltest.gmt_lyr, [ ('AREA', ogr.OFTReal), ('EAS_ID', ogr.OFTInteger), ('PRFEDEA', ogr.OFTString) ] ) ####################################################### # Copy in poly.shp dst_feat = ogr.Feature( feature_def = gdaltest.gmt_lyr.GetLayerDefn() ) shp_ds = ogr.Open( 'data/poly.shp' ) gdaltest.shp_ds = shp_ds shp_lyr = shp_ds.GetLayer(0) feat = shp_lyr.GetNextFeature() gdaltest.poly_feat = [] while feat is not None: gdaltest.poly_feat.append( feat ) dst_feat.SetFrom( feat ) gdaltest.gmt_lyr.CreateFeature( dst_feat ) feat = shp_lyr.GetNextFeature() gdaltest.gmt_lyr = None gdaltest.gmt_ds = None return 'success'
def test_ogr_oci_17(): if gdaltest.oci_ds is None: pytest.skip() gdaltest.oci_ds.ExecuteSQL('DELLAYER:xpoly') ###################################################### # Create Oracle Layer gdaltest.oci_lyr = gdaltest.oci_ds.CreateLayer('xpoly') ###################################################### # Setup Schema ogrtest.quick_create_layer_def(gdaltest.oci_lyr, [('AREA', ogr.OFTReal), ('EAS_ID', ogr.OFTInteger), ('PRFEDEA', ogr.OFTString)]) ###################################################### # Copy in poly.shp dst_feat = ogr.Feature(feature_def=gdaltest.oci_lyr.GetLayerDefn()) shp_ds = ogr.Open('data/poly.shp') gdaltest.shp_ds = shp_ds shp_lyr = shp_ds.GetLayer(0) feat = shp_lyr.GetNextFeature() gdaltest.poly_feat = [] while feat is not None: gdaltest.poly_feat.append(feat) dst_feat.SetFrom(feat) gdaltest.oci_lyr.CreateFeature(dst_feat) feat = shp_lyr.GetNextFeature() dst_feat.Destroy() ###################################################### # Create a distinct connection to the same database to monitor the # metadata table. oci_ds2 = ogr.Open(os.environ['OCI_DSNAME']) sql_lyr = oci_ds2.ExecuteSQL( "select column_name from user_sdo_geom_metadata where table_name = 'XPOLY'" ) assert sql_lyr.GetFeatureCount( ) <= 0, 'user_sdo_geom_metadata already populated!' oci_ds2.ReleaseResultSet(sql_lyr) result = gdaltest.oci_ds.SyncToDisk() assert result == 0, 'SyncToDisk() failed.' sql_lyr = oci_ds2.ExecuteSQL( "select column_name from user_sdo_geom_metadata where table_name = 'XPOLY'" ) assert sql_lyr.GetFeatureCount( ) != 0, 'user_sdo_geom_metadata still not populated!' oci_ds2.ReleaseResultSet(sql_lyr) oci_ds2 = None
def ogr_gmt_5(): ####################################################### # Create gmtory Layer gmt_drv = ogr.GetDriverByName('GMT') gdaltest.gmt_ds = gmt_drv.CreateDataSource('tmp/mpoly.gmt') gdaltest.gmt_lyr = gdaltest.gmt_ds.CreateLayer('mpoly') ####################################################### # Setup Schema ogrtest.quick_create_layer_def(gdaltest.gmt_lyr, [('ID', ogr.OFTInteger)]) ####################################################### # Write a first multipolygon dst_feat = ogr.Feature(feature_def=gdaltest.gmt_lyr.GetLayerDefn()) dst_feat.SetGeometryDirectly( ogr.CreateGeometryFromWkt( 'MULTIPOLYGON(((0 0,0 10,10 10,0 10,0 0),(3 3,4 4, 3 4,3 3)),((12 0,14 0,12 3,12 0)))' )) dst_feat.SetField('ID', 15) gdal.SetConfigOption('GMT_USE_TAB', 'TRUE') # Ticket #6453 gdaltest.gmt_lyr.CreateFeature(dst_feat) gdal.SetConfigOption('GMT_USE_TAB', None) dst_feat = ogr.Feature(feature_def=gdaltest.gmt_lyr.GetLayerDefn()) dst_feat.SetGeometryDirectly( ogr.CreateGeometryFromWkt('MULTIPOLYGON(((30 20,40 20,30 30,30 20)))')) dst_feat.SetField('ID', 16) gdaltest.gmt_lyr.CreateFeature(dst_feat) gdaltest.gmt_lyr = None gdaltest.gmt_ds = None # Reopen. ds = ogr.Open('tmp/mpoly.gmt') lyr = ds.GetLayer(0) if lyr.GetLayerDefn().GetGeomType() != ogr.wkbMultiPolygon: gdaltest.post_reason('did not get expected multipolygon type.') return 'fail' feat = lyr.GetNextFeature() if ogrtest.check_feature_geometry( feat, 'MULTIPOLYGON(((0 0,0 10,10 10,0 10,0 0),(3 3,4 4, 3 4,3 3)),((12 0,14 0,12 3,12 0)))' ): return 'fail' if feat.GetField('ID') != 15: gdaltest.post_reason('got wrong id, first feature') return 'fail' feat = lyr.GetNextFeature() if ogrtest.check_feature_geometry( feat, 'MULTIPOLYGON(((30 20,40 20,30 30,30 20)))'): return 'fail' if feat.GetField('ID') != 16: gdaltest.post_reason('got wrong ID, second feature') return 'fail' feat = lyr.GetNextFeature() if feat is not None: gdaltest.post_reason('did not get null feature when expected.') return 'fail' return 'success'
def rasterize_4(): try: x = gdal.RasterizeLayer gdaltest.have_ng = 1 except: gdaltest.have_ng = 0 return 'skip' # Setup working spatial reference sr_wkt = 'LOCAL_CS["arbitrary"]' sr = osr.SpatialReference(sr_wkt) # Create a memory raster to rasterize into. target_ds = gdal.GetDriverByName('MEM').Create('', 100, 100, 3, gdal.GDT_Byte) target_ds.SetGeoTransform((1000, 1, 0, 1100, 0, -1)) target_ds.SetProjection(sr_wkt) # Create a memory layer to rasterize from. rast_ogr_ds = ogr.GetDriverByName('Memory').CreateDataSource('wrk') rast_mem_lyr = rast_ogr_ds.CreateLayer('poly', srs=sr) # Setup Schema ogrtest.quick_create_layer_def(rast_mem_lyr, [('CELSIUS', ogr.OFTReal)]) # Add polygons and linestrings and a field named CELSIUS. wkt_geom = [ 'POLYGON((1020 1030 40,1020 1045 30,1050 1045 20,1050 1030 35,1020 1030 40))', 'POLYGON((1010 1046 85,1015 1055 35,1055 1060 26,1054 1048 35,1010 1046 85))', 'POLYGON((1020 1076 190,1025 1085 35,1065 1090 26,1064 1078 35,1020 1076 190),(1023 1079 5,1061 1081 35,1062 1087 26,1028 1082 35,1023 1079 85))', 'LINESTRING(1005 1000 10, 1100 1050 120)', 'LINESTRING(1000 1000 150, 1095 1050 -5, 1080 1080 200)' ] celsius_field_values = [50, 255, 60, 100, 180] i = 0 for g in wkt_geom: feat = ogr.Feature(rast_mem_lyr.GetLayerDefn()) feat.SetGeometryDirectly(ogr.Geometry(wkt=g)) feat.SetField('CELSIUS', celsius_field_values[i]) rast_mem_lyr.CreateFeature(feat) i = i + 1 # Run the algorithm. err = gdal.RasterizeLayer(target_ds, [1, 2, 3], rast_mem_lyr, options=["ATTRIBUTE=CELSIUS"]) if err != 0: print(err) gdaltest.post_reason('got non-zero result code from RasterizeLayer') return 'fail' # Check results. expected = 16265 checksum = target_ds.GetRasterBand(2).Checksum() if checksum != expected: print(checksum) gdaltest.post_reason('Did not get expected image checksum') gdal.GetDriverByName('GTiff').CreateCopy('tmp/rasterize_4.tif', target_ds) return 'fail' return 'success'
def ogr_pgdump_3(): try: os.remove('tmp/tpoly.sql') except: pass gdal.SetConfigOption('PG_USE_COPY', 'YES') ds = ogr.GetDriverByName('PGDump').CreateDataSource( 'tmp/tpoly.sql', options=['LINEFORMAT=LF']) ###################################################### # Create Layer lyr = ds.CreateLayer('tpoly', geom_type=ogr.wkbNone, options=['SCHEMA=another_schema']) ###################################################### # Setup Schema ogrtest.quick_create_layer_def(lyr, [('EMPTYCHAR', ogr.OFTString), ('AREA', ogr.OFTReal), ('EAS_ID', ogr.OFTInteger), ('PRFEDEA', ogr.OFTString), ('SHORTNAME', ogr.OFTString, 8)]) ###################################################### # Copy in poly.shp dst_feat = ogr.Feature(feature_def=lyr.GetLayerDefn()) shp_ds = ogr.Open('data/poly.shp') shp_lyr = shp_ds.GetLayer(0) feat = shp_lyr.GetNextFeature() gdaltest.poly_feat = [] i = 0 while feat is not None: gdaltest.poly_feat.append(feat) dst_feat.SetFrom(feat) if i == 0: # Be perverse and test the case where a feature has a geometry # even if it's a wkbNone layer ! (#4040) dst_feat.SetGeometry(ogr.CreateGeometryFromWkt('POINT(0 1)')) elif i == 1: # Field with 0 character (not empty!) (#4040) dst_feat.SetField(0, '') i = i + 1 lyr.CreateFeature(dst_feat) feat = shp_lyr.GetNextFeature() dst_feat.Destroy() ds.Destroy() gdal.SetConfigOption('PG_USE_COPY', 'NO') f = open('tmp/tpoly.sql') sql = f.read() f.close() if sql.find("""DROP TABLE IF EXISTS "another_schema"."tpoly" CASCADE;""") == -1 or \ sql.find("""DELETE FROM geometry_columns""") != -1 or \ sql.find("""BEGIN;""") == -1 or \ sql.find("""CREATE TABLE "another_schema"."tpoly" ( "ogc_fid" SERIAL, CONSTRAINT "tpoly_pk" PRIMARY KEY ("ogc_fid") );""") == -1 or \ sql.find("""SELECT AddGeometryColumn""") != -1 or \ sql.find("""CREATE INDEX "tpoly_wkb_geometry_geom_idx""") != -1 or \ sql.find("""ALTER TABLE "another_schema"."tpoly" ADD COLUMN "area" FLOAT8;""") == -1 or \ sql.find("""ALTER TABLE "another_schema"."tpoly" ADD COLUMN "eas_id" INTEGER;""") == -1 or \ sql.find("""ALTER TABLE "another_schema"."tpoly" ADD COLUMN "prfedea" VARCHAR;""") == -1 or \ sql.find("""ALTER TABLE "another_schema"."tpoly" ADD COLUMN "shortname" VARCHAR(8);""") == -1 or \ sql.find("""COPY "another_schema"."tpoly" ("emptychar", "area", "eas_id", "prfedea", "shortname") FROM STDIN;""") == -1 or \ sql.find("""\\N 215229.266 168 35043411 \\N""") == -1 or \ sql.find(""" 5268.813 170 35043413 \\N""") == -1 or \ sql.find("""\\.""") == -1 or \ sql.find("""COMMIT;""") == -1 : print(sql) return 'fail' return 'success'
def test_ogr_pgdump_2(): try: os.remove('tmp/tpoly.sql') except OSError: pass gdal.SetConfigOption('PG_USE_COPY', 'YES') ds = ogr.GetDriverByName('PGDump').CreateDataSource( 'tmp/tpoly.sql', options=['LINEFORMAT=CRLF']) ###################################################### # Create Layer lyr = ds.CreateLayer('tpoly', geom_type=ogr.wkbPolygon, options=[ 'SCHEMA=another_schema', 'SRID=4326', 'GEOMETRY_NAME=the_geom' ]) ###################################################### # Setup Schema ogrtest.quick_create_layer_def(lyr, [('AREA', ogr.OFTReal), ('EAS_ID', ogr.OFTInteger), ('PRFEDEA', ogr.OFTString), ('SHORTNAME', ogr.OFTString, 8)]) ###################################################### # Copy in poly.shp dst_feat = ogr.Feature(feature_def=lyr.GetLayerDefn()) shp_ds = ogr.Open('data/poly.shp') shp_lyr = shp_ds.GetLayer(0) feat = shp_lyr.GetNextFeature() gdaltest.poly_feat = [] while feat is not None: gdaltest.poly_feat.append(feat) dst_feat.SetFrom(feat) lyr.CreateFeature(dst_feat) feat = shp_lyr.GetNextFeature() dst_feat.Destroy() ds.Destroy() gdal.SetConfigOption('PG_USE_COPY', 'NO') f = open('tmp/tpoly.sql') sql = f.read() f.close() assert (not (sql.find("""DROP TABLE IF EXISTS "another_schema"."tpoly" CASCADE;""") == -1 or \ sql.find("""BEGIN;""") == -1 or \ sql.find("""CREATE TABLE "another_schema"."tpoly" ( "ogc_fid" SERIAL, CONSTRAINT "tpoly_pk" PRIMARY KEY ("ogc_fid") );""") == -1 or \ sql.find("""SELECT AddGeometryColumn('another_schema','tpoly','the_geom',4326,'POLYGON',2);""") == -1 or \ sql.find("""CREATE INDEX "tpoly_the_geom_geom_idx" ON "another_schema"."tpoly" USING GIST ("the_geom");""") == -1 or \ sql.find("""ALTER TABLE "another_schema"."tpoly" ADD COLUMN "area" FLOAT8;""") == -1 or \ sql.find("""ALTER TABLE "another_schema"."tpoly" ADD COLUMN "eas_id" INTEGER;""") == -1 or \ sql.find("""ALTER TABLE "another_schema"."tpoly" ADD COLUMN "prfedea" VARCHAR;""") == -1 or \ sql.find("""ALTER TABLE "another_schema"."tpoly" ADD COLUMN "shortname" VARCHAR(8);""") == -1 or \ sql.find("""COPY "another_schema"."tpoly" ("the_geom", "area", "eas_id", "prfedea", "shortname") FROM STDIN;""") == -1 or \ sql.find("0103000020E61000000100000005000000000000C01A481D4100000080072D524100000060AA461D4100000080FF2C52410000006060461D41000000400C2D5241000000A0DF471D4100000000142D5241000000C01A481D4100000080072D5241 5268.813 170 35043413 \\N") == -1 or \ sql.find("""\.""") == -1 or \ sql.find("""COMMIT;""") == -1))
def copy_shape_to_geojson(gjname, compress=None): if gdaltest.geojson_drv is None: return False if compress is not None: if compress[0:5] == '/vsig': dst_name = os.path.join('/vsigzip/', 'tmp', gjname + '.geojson' + '.gz') elif compress[0:4] == '/vsiz': dst_name = os.path.join('/vsizip/', 'tmp', gjname + '.geojson' + '.zip') elif compress == '/vsistdout/': dst_name = compress else: return False else: dst_name = os.path.join('tmp', gjname + '.geojson') ds = gdaltest.geojson_drv.CreateDataSource(dst_name) if ds is None: return False ###################################################### # Create layer lyr = ds.CreateLayer(gjname) if lyr is None: return False ###################################################### # Setup schema (all test shapefiles use common schmea) ogrtest.quick_create_layer_def(lyr, [('FID', ogr.OFTReal), ('NAME', ogr.OFTString)]) ###################################################### # Copy in gjpoint.shp dst_feat = ogr.Feature(feature_def=lyr.GetLayerDefn()) src_name = os.path.join('data', gjname + '.shp') shp_ds = ogr.Open(src_name) shp_lyr = shp_ds.GetLayer(0) feat = shp_lyr.GetNextFeature() gdaltest.gjpoint_feat = [] while feat is not None: gdaltest.gjpoint_feat.append(feat) dst_feat.SetFrom(feat) lyr.CreateFeature(dst_feat) feat = shp_lyr.GetNextFeature() dst_feat.Destroy() shp_lyr = None lyr = None # Required by old-gen python bindings shp_ds.Destroy() ds.Destroy() return True
def ogr_pgdump_1(): try: os.remove('tmp/tpoly.sql') except: pass ds = ogr.GetDriverByName('PGDump').CreateDataSource('tmp/tpoly.sql') ###################################################### # Create Layer lyr = ds.CreateLayer('tpoly', options=['DIM=3']) ###################################################### # Setup Schema ogrtest.quick_create_layer_def(lyr, [('AREA', ogr.OFTReal), ('EAS_ID', ogr.OFTInteger), ('PRFEDEA', ogr.OFTString), ('SHORTNAME', ogr.OFTString, 8)]) ###################################################### # Copy in poly.shp dst_feat = ogr.Feature(feature_def=lyr.GetLayerDefn()) shp_ds = ogr.Open('data/poly.shp') shp_lyr = shp_ds.GetLayer(0) feat = shp_lyr.GetNextFeature() gdaltest.poly_feat = [] while feat is not None: gdaltest.poly_feat.append(feat) dst_feat.SetFrom(feat) lyr.CreateFeature(dst_feat) feat = shp_lyr.GetNextFeature() dst_feat.Destroy() ds.Destroy() f = open('tmp/tpoly.sql') sql = f.read() f.close() if sql.find("""DROP TABLE IF EXISTS "public"."tpoly" CASCADE;""") == -1 or \ sql.find("""DELETE FROM geometry_columns WHERE f_table_name = 'tpoly' AND f_table_schema = 'public';""") == -1 or \ sql.find("""BEGIN;""") == -1 or \ sql.find("""CREATE TABLE "public"."tpoly" ( "ogc_fid" SERIAL, CONSTRAINT "tpoly_pk" PRIMARY KEY ("ogc_fid") );""") == -1 or \ sql.find("""SELECT AddGeometryColumn('public','tpoly','wkb_geometry',-1,'GEOMETRY',3);""") == -1 or \ sql.find("""CREATE INDEX "tpoly_wkb_geometry_geom_idx" ON "public"."tpoly" USING GIST ("wkb_geometry");""") == -1 or \ sql.find("""ALTER TABLE "public"."tpoly" ADD COLUMN "area" FLOAT8;""") == -1 or \ sql.find("""ALTER TABLE "public"."tpoly" ADD COLUMN "eas_id" INTEGER;""") == -1 or \ sql.find("""ALTER TABLE "public"."tpoly" ADD COLUMN "prfedea" VARCHAR;""") == -1 or \ sql.find("""ALTER TABLE "public"."tpoly" ADD COLUMN "shortname" VARCHAR(8);""") == -1 or \ sql.find("""INSERT INTO "public"."tpoly" ("wkb_geometry" , "area", "eas_id", "prfedea") VALUES ('01030000800100000005000000000000C01A481D4100000080072D5241000000000000000000000060AA461D4100000080FF2C524100000000000000000000006060461D41000000400C2D52410000000000000000000000A0DF471D4100000000142D52410000000000000000000000C01A481D4100000080072D52410000000000000000', 5268.813, 170, '35043413');""") == -1 or \ sql.find("""COMMIT;""") == -1 : print(sql) return 'fail' return 'success'
def test_ogr_gmt_2(): gmt_drv = ogr.GetDriverByName('GMT') gmt_ds = gmt_drv.CreateDataSource('tmp/tpoly.gmt') ####################################################### # Create gmtory Layer gmt_lyr = gmt_ds.CreateLayer('tpoly') ####################################################### # Setup Schema ogrtest.quick_create_layer_def(gmt_lyr, [('AREA', ogr.OFTReal), ('EAS_ID', ogr.OFTInteger), ('PRFEDEA', ogr.OFTString)]) ####################################################### # Copy in poly.shp dst_feat = ogr.Feature(feature_def=gmt_lyr.GetLayerDefn()) shp_ds = ogr.Open('data/poly.shp') shp_lyr = shp_ds.GetLayer(0) feat = shp_lyr.GetNextFeature() poly_feat = [] while feat is not None: poly_feat.append(feat) dst_feat.SetFrom(feat) gmt_lyr.CreateFeature(dst_feat) feat = shp_lyr.GetNextFeature() gmt_lyr = None gmt_ds = None # Verify that stuff we just wrote is still OK. gmt_ds = ogr.Open('tmp/tpoly.gmt') gmt_lyr = gmt_ds.GetLayer(0) expect = [168, 169, 166, 158, 165] gmt_lyr.SetAttributeFilter('eas_id < 170') tr = ogrtest.check_features_against_list(gmt_lyr, 'eas_id', expect) gmt_lyr.SetAttributeFilter(None) for i in range(len(poly_feat)): orig_feat = poly_feat[i] read_feat = gmt_lyr.GetNextFeature() assert (ogrtest.check_feature_geometry(read_feat, orig_feat.GetGeometryRef(), max_error=0.000000001) == 0) for fld in range(3): assert orig_feat.GetField(fld) == read_feat.GetField(fld), \ ('Attribute %d does not match' % fld) assert tr