def test_ogr2ogr_lib_8(): srcDS = gdal.OpenEx('../ogr/data/poly.shp') ds = gdal.VectorTranslate('', srcDS, format='Memory', layers=['poly']) if ds is None or ds.GetLayer(0).GetFeatureCount() != 10: return 'fail' return 'success'
def test_ogr2ogr_lib_11(): srcDS = gdal.OpenEx('../ogr/data/poly.shp') ds = gdal.VectorTranslate('', srcDS, format='Memory', spatFilter=[479609, 4764629, 479764, 4764817]) if ogrtest.have_geos(): assert ds is not None and ds.GetLayer(0).GetFeatureCount() == 4 else: assert ds is not None and ds.GetLayer(0).GetFeatureCount() == 5
def test_ogr2ogr_lib_7(): srcDS = gdal.OpenEx('../ogr/data/poly.shp') ds = gdal.VectorTranslate('/vsimem/poly.shp', srcDS, layerCreationOptions=['SHPT=POLYGONZ']) assert ds.GetLayer(0).GetLayerDefn().GetGeomType() == ogr.wkbPolygon25D ds = None ogr.GetDriverByName('ESRI Shapefile').DeleteDataSource('/vsimem/poly.shp')
def test_ogr2ogr_lib_5(): srcDS = gdal.OpenEx('../ogr/data/poly.shp') ds = gdal.VectorTranslate('', srcDS, format='Memory', dstSRS='EPSG:4326') if str(ds.GetLayer(0).GetSpatialRef()).find('1984') == -1: return 'fail' return 'success'
def test_ogr_gmt_write_stdout(): gdal.VectorTranslate("/vsistdout_redirect//vsimem/test.gmt", "data/poly.shp", format="GMT") ds = ogr.Open("/vsimem/test.gmt") lyr = ds.GetLayer(0) assert lyr.GetGeomType() == ogr.wkbPolygon assert lyr.GetFeatureCount() == 10 ds = None gdal.Unlink("/vsimem/test.gmt")
def test_ogr_s57_write(): gdal.Unlink('tmp/ogr_s57_9.000') gdal.SetConfigOption( 'OGR_S57_OPTIONS', 'RETURN_PRIMITIVES=ON,RETURN_LINKAGES=ON,LNAM_REFS=ON') ds = ogr.GetDriverByName('S57').CreateDataSource('tmp/ogr_s57_9.000') src_ds = ogr.Open('data/s57/1B5X02NE.000') gdal.SetConfigOption('OGR_S57_OPTIONS', None) for src_lyr in src_ds: if src_lyr.GetName() == 'DSID': continue lyr = ds.GetLayerByName(src_lyr.GetName()) for src_feat in src_lyr: feat = ogr.Feature(lyr.GetLayerDefn()) feat.SetFrom(src_feat) lyr.CreateFeature(feat) src_ds = None ds = None ds = ogr.Open('tmp/ogr_s57_9.000') assert ds is not None gdaltest.s57_ds = ds test_ogr_s57_check_layers() test_ogr_s57_COALNE() test_ogr_s57_M_QUAL() test_ogr_s57_SOUNDG() gdaltest.s57_ds = None gdal.Unlink('tmp/ogr_s57_9.000') gdal.SetConfigOption( 'OGR_S57_OPTIONS', 'RETURN_PRIMITIVES=ON,RETURN_LINKAGES=ON,LNAM_REFS=ON') gdal.VectorTranslate( 'tmp/ogr_s57_9.000', 'data/s57/1B5X02NE.000', options="-f S57 IsolatedNode ConnectedNode Edge Face M_QUAL SOUNDG") gdal.SetConfigOption('OGR_S57_OPTIONS', None) ds = gdal.OpenEx('tmp/ogr_s57_9.000', open_options=['RETURN_PRIMITIVES=ON']) assert ds is not None assert ds.GetLayerByName('IsolatedNode') is not None gdaltest.s57_ds = ds test_ogr_s57_M_QUAL() test_ogr_s57_SOUNDG() gdaltest.s57_ds = None gdal.Unlink('tmp/ogr_s57_9.000')
def convert_and_import(xml_file): QgsProject.instance().clear() with tempfile.NamedTemporaryFile(delete=True) as f: out_f = f.name config_file = os.path.join(os.path.dirname(__file__), "gmlasconf.xml") gdal.SetConfigOption("OGR_SQLITE_SYNCHRONOUS", "OFF") ds = gdal.OpenEx( "GMLAS:{}".format(xml_file), open_options=[ "EXPOSE_METADATA_LAYERS=YES", "CONFIG_FILE={}".format(config_file), ], ) srs = osr.SpatialReference() qgs_srs = QgsCoordinateReferenceSystem("EPSG:4326") srs.ImportFromWkt(qgs_srs.toWkt()) params = { "destNameOrDestDS": out_f, "srcDS": ds, "format": "SQLite", "accessMode": "overwrite", "datasetCreationOptions": ["SPATIALITE=YES"], "options": ["-forceNullable", "-skipfailures"] # , 'srcSRS': srs # , 'dstSRS': srs , "geometryType": "CONVERT_TO_LINEAR", "reproject": False, } # call gdal to convert gdal.VectorTranslate(**params) # fix geometry types ds = None # populate the qgis project import_in_qgis(out_f, "SQLite") layers = [] for lid in sorted(QgsProject.instance().mapLayers().keys()): vl = QgsProject.instance().mapLayer(lid) layers.append((vl.name(), vl.wkbType())) rels = [] relations = QgsProject.instance().relationManager().relations() for relid in sorted(relations.keys()): rel = relations[relid] p = rel.fieldPairs() rels.append( ( rel.id()[0:3], rel.referencingLayer().name(), list(p.keys())[0], rel.referencedLayer().name(), list(p.values())[0], ) ) return sorted(layers), sorted(rels)
def test_ogr2ogr_lib_15(): srcDS = gdal.OpenEx('../ogr/data/poly.shp') with gdaltest.error_handler(): ds = gdal.VectorTranslate('', srcDS, format='Memory', zField='foo') lyr = ds.GetLayer(0) if lyr.GetGeomType() != ogr.wkbPolygon: return 'fail' return 'success'
def test_ogr2ogr_lib_ct_no_srs(): ds = gdal.VectorTranslate('', '../ogr/data/poly.shp', format='Memory', coordinateOperation="+proj=affine +s11=-1") lyr = ds.GetLayer(0) assert lyr.GetSpatialRef().GetAuthorityCode(None) == '27700' f = lyr.GetNextFeature() #f.DumpReadable() assert ogrtest.check_feature_geometry(f, "POLYGON ((-479819.84375 4765180.5,-479690.1875 4765259.5,-479647.0 4765369.5,-479730.375 4765400.5,-480039.03125 4765539.5,-480035.34375 4765558.5,-480159.78125 4765610.5,-480202.28125 4765482.0,-480365.0 4765015.5,-480389.6875 4764950.0,-480133.96875 4764856.5,-480080.28125 4764979.5,-480082.96875 4765049.5,-480088.8125 4765139.5,-480059.90625 4765239.5,-480019.71875 4765319.5,-479980.21875 4765409.5,-479909.875 4765370.0,-479859.875 4765270.0,-479819.84375 4765180.5))") == 0
def test_ogr2ogr_lib_17(): ds = gdal.GetDriverByName('Memory').Create('', 0, 0, 0) gdal.VectorTranslate(ds, gdal.OpenEx('../ogr/data/poly.shp')) lyr = ds.GetLayer(0) if lyr.GetFeatureCount() != 10: return 'fail' ds = None return 'success'
def test_ogr2ogr_lib_2(): srcDS = gdal.OpenEx('../ogr/data/poly.shp') ds = gdal.VectorTranslate('', srcDS, format='Memory', SQLStatement='select * from poly', SQLDialect='OGRSQL') if ds is None or ds.GetLayer(0).GetFeatureCount() != 10: gdaltest.post_reason('fail') return 'fail' # Test @filename syntax gdal.FileFromMemBuffer( '/vsimem/sql.txt', '-- initial comment\nselect * from poly\n-- trailing comment') ds = gdal.VectorTranslate('', srcDS, format='Memory', SQLStatement='@/vsimem/sql.txt') if ds is None or ds.GetLayer(0).GetFeatureCount() != 10: gdaltest.post_reason('fail') return 'fail' gdal.Unlink('/vsimem/sql.txt') # Test @filename syntax with a UTF-8 BOM if sys.version_info >= (3, 0, 0): gdal.FileFromMemBuffer( '/vsimem/sql.txt', '\xEF\xBB\xBFselect * from poly'.encode('LATIN1')) else: gdal.FileFromMemBuffer('/vsimem/sql.txt', '\xEF\xBB\xBFselect * from poly') ds = gdal.VectorTranslate('', srcDS, format='Memory', SQLStatement='@/vsimem/sql.txt') if ds is None or ds.GetLayer(0).GetFeatureCount() != 10: gdaltest.post_reason('fail') return 'fail' gdal.Unlink('/vsimem/sql.txt') return 'success'
def test_ogr2ogr_lib_9(): srcDS = gdal.OpenEx('../ogr/data/poly.shp') ds = gdal.VectorTranslate('', srcDS, format='Memory', segmentizeMaxDist=100) assert ds is not None and ds.GetLayer(0).GetFeatureCount() == 10 feat = ds.GetLayer(0).GetNextFeature() assert feat.GetGeometryRef().GetGeometryRef(0).GetPointCount() == 36
def test_ogr2ogr_lib_4(): srcDS = gdal.OpenEx('../ogr/data/poly.shp') ds = gdal.VectorTranslate('/vsimem/poly.shp', srcDS) if ds.GetLayer(0).GetFeatureCount() != 10: gdaltest.post_reason('wrong feature count') print(ds.GetLayer(0).GetFeatureCount()) return 'fail' ds= None ds = gdal.VectorTranslate('/vsimem/poly.shp', srcDS, accessMode='append') if ds is None: gdaltest.post_reason('ds is None') return 'fail' if ds.GetLayer(0).GetFeatureCount() != 20: gdaltest.post_reason('wrong feature count') print(ds.GetLayer(0).GetFeatureCount()) return 'fail' ret = gdal.VectorTranslate(ds, srcDS, accessMode='append') if ret != 1: gdaltest.post_reason('ds is None') return 'fail' if ds.GetLayer(0).GetFeatureCount() != 30: gdaltest.post_reason('wrong feature count') print(ds.GetLayer(0).GetFeatureCount()) return 'fail' feat10 = ds.GetLayer(0).GetFeature(10) if feat10.GetFieldAsDouble('AREA') != 215229.266: print(feat10.GetFieldAsDouble('AREA')) gdaltest.post_reason('Did not get expected value for field AREA') return 'fail' if feat10.GetFieldAsString('PRFEDEA') != '35043411': print(feat10.GetFieldAsString('PRFEDEA')) gdaltest.post_reason('Did not get expected value for field PRFEDEA') return 'fail' ds = None ogr.GetDriverByName('ESRI Shapefile').DeleteDataSource('/vsimem/poly.shp') return 'success'
def test_ogr2ogr_lib_13(): with gdaltest.error_handler(): ds = gdal.VectorTranslate('', '../ogr/data/poly.shp', format='Memory', callback=mycallback_with_failure) if ds is not None: return 'fail' return 'success'
def test_ogr_xlsx_15(): drv = ogr.GetDriverByName('XLSX') if drv is None: pytest.skip() out_filename = '/vsimem/ogr_xlsx_15.xlsx' gdal.VectorTranslate(out_filename, 'data/poly.shp', options='-f XLSX -nln first') gdal.VectorTranslate(out_filename, 'data/poly.shp', options='-update -nln second') ds = ogr.Open(out_filename) assert ds.GetLayerByName('first').GetFeatureCount() != 0 assert ds.GetLayerByName('second').GetFeatureCount() != 0 ds = None gdal.Unlink(out_filename)
def test_ogr2ogr_assign_coord_epoch(): src_ds = gdal.GetDriverByName('Memory').Create('', 0, 0, 0, gdal.GDT_Unknown) src_ds.CreateLayer('layer') ds = gdal.VectorTranslate( '', src_ds, options='-f Memory -a_srs EPSG:7665 -a_coord_epoch 2021.3') lyr = ds.GetLayer(0) srs = lyr.GetSpatialRef() assert srs.GetCoordinateEpoch() == 2021.3
def test_ogr2ogr_lib_3(): srcDS = gdal.OpenEx('../ogr/data/poly.shp') ds = gdal.VectorTranslate('', srcDS, format='Memory', where='EAS_ID=171') if ds is None or ds.GetLayer(0).GetFeatureCount() != 1: gdaltest.post_reason('fail') return 'fail' # Test @filename syntax gdal.FileFromMemBuffer('/vsimem/filter.txt', 'EAS_ID=171') ds = gdal.VectorTranslate('', srcDS, format='Memory', where='@/vsimem/filter.txt') if ds is None or ds.GetLayer(0).GetFeatureCount() != 1: gdaltest.post_reason('fail') return 'fail' gdal.Unlink('/vsimem/filter.txt') return 'success'
def test_ogr2ogr_lib_1(): srcDS = gdal.OpenEx('../ogr/data/poly.shp') ds = gdal.VectorTranslate('', srcDS, format='Memory') assert ds is not None and ds.GetLayer(0).GetFeatureCount() == 10 feat0 = ds.GetLayer(0).GetFeature(0) assert feat0.GetFieldAsDouble('AREA') == 215229.266, \ 'Did not get expected value for field AREA' assert feat0.GetFieldAsString('PRFEDEA') == '35043411', \ 'Did not get expected value for field PRFEDEA'
def test_ogr2ogr_lib_9(): srcDS = gdal.OpenEx('../ogr/data/poly.shp') ds = gdal.VectorTranslate('', srcDS, format = 'Memory', segmentizeMaxDist=100) if ds is None or ds.GetLayer(0).GetFeatureCount() != 10: return 'fail' feat = ds.GetLayer(0).GetNextFeature() if feat.GetGeometryRef().GetGeometryRef(0).GetPointCount() != 36: return 'fail' return 'success'
def test_ogr2ogr_lib_12(): tab = [0] ds = gdal.VectorTranslate('', '../ogr/data/poly.shp', format='Memory', callback=mycallback, callback_data=tab) assert ds is not None and ds.GetLayer(0).GetFeatureCount() == 10 assert tab[0] == 1.0, 'Bad percentage'
def test_ogr2ogr_lib_12(): tab = [ 0 ] ds = gdal.VectorTranslate('', '../ogr/data/poly.shp', format = 'Memory', callback = mycallback, callback_data = tab) if ds is None or ds.GetLayer(0).GetFeatureCount() != 10: return 'fail' if tab[0] != 1.0: gdaltest.post_reason('Bad percentage') return 'fail' return 'success'
def test_ogr2ogr_lib_11(): srcDS = gdal.OpenEx('../ogr/data/poly.shp') ds = gdal.VectorTranslate('',srcDS, format = 'Memory',spatFilter = [479609,4764629,479764,4764817]) if ogrtest.have_geos(): if ds is None or ds.GetLayer(0).GetFeatureCount() != 4: return 'fail' else: if ds is None or ds.GetLayer(0).GetFeatureCount() != 5: return 'fail' return 'success'
def ogr_xlsx_15(): drv = ogr.GetDriverByName('XLSX') if drv is None: return 'skip' out_filename = '/vsimem/ogr_xlsx_15.xlsx' gdal.VectorTranslate(out_filename, 'data/poly.shp', options='-f XLSX -nln first') gdal.VectorTranslate(out_filename, 'data/poly.shp', options='-update -nln second') ds = ogr.Open(out_filename) if ds.GetLayerByName('first').GetFeatureCount() == 0: gdaltest.post_reason('fail') return 'fail' if ds.GetLayerByName('second').GetFeatureCount() == 0: gdaltest.post_reason('fail') return 'fail' ds = None gdal.Unlink(out_filename) return 'success'
def test_ogr2ogr_lib_sql_filename(): with gdaltest.tempfile( '/vsimem/my.sql', """-- initial comment\nselect\n'--''--' as literalfield,* from --comment\npoly\n-- trailing comment""" ): ds = gdal.VectorTranslate('', '../ogr/data/poly.shp', options='-f Memory -sql @/vsimem/my.sql') lyr = ds.GetLayer(0) assert lyr.GetFeatureCount() == 10 assert lyr.GetLayerDefn().GetFieldIndex('literalfield') == 0
def _coordinate_system_conversion_vector(self, src_dataset, dst_dataset, src_sr, dst_sr): ''' 矢量数据的坐标转换 :param src_dataset:输入数据 :param dst_dataset:输出数据 :param src_sr:格式为"EPSG:4212" :param dst_sr:格式为"EPSG:4326" :return: ''' gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES") gdal.SetConfigOption("SHAPE_ENCODING", "GBK") gdal.VectorTranslate(src_dataset, dst_dataset, srcSRS=src_sr, dstSRS=dst_sr)
def put_object(db_table_name, srcSRS, dstSRS, filename, engine): dstDS = gdal.OpenEx(engine, gdal.OF_VECTOR) srcDS = gdal.OpenEx(filename, open_options=['AUTODETECT_TYPE=NO', 'EMPTY_STRING_AS_NULL=YES', 'GEOM_POSSIBLE_NAMES=the_geom']) gdal.VectorTranslate( dstDS, srcDS, format='PostgreSQL', dstSRS=dstSRS, srcSRS=srcSRS, layerName=db_table_name, accessMode='overwrite')
def test_ogr_dgnv8_4(): if gdaltest.dgnv8_drv is None: pytest.skip() tmp_dgn = 'tmp/ogr_dgnv8_4.dgn' gdal.VectorTranslate(tmp_dgn, 'data/test_dgnv8.dgn', format='DGNv8') tmp_csv = '/vsimem/ogr_dgnv8_4.csv' gdal.VectorTranslate(tmp_csv, tmp_dgn, options='-f CSV -dsco geometry=as_wkt -sql "select *, ogr_style from my_model"') gdal.Unlink(tmp_dgn) ds_ref = ogr.Open(tmp_csv) lyr_ref = ds_ref.GetLayer(0) ds = ogr.Open('data/test_dgnv8_write_ref.csv') lyr = ds.GetLayer(0) ret = ogrtest.compare_layers(lyr, lyr_ref, excluded_fields=['WKT']) gdal.Unlink(tmp_csv) return ret
def test_ogr_flatgeobuf_mixed(): srcDS = gdal.OpenEx('data/testfgb/testmixed.geojson') destDS = gdal.VectorTranslate('/vsimem/test.fgb', srcDS=srcDS, format = 'FlatGeobuf', layerCreationOptions = ['SPATIAL_INDEX=NO']) srcDS = None destDS = None srcDS = ogr.Open('data/testfgb/testmixed.geojson') destDS = ogr.Open('/vsimem/test.fgb') srcLyr = srcDS.GetLayer(0) destLyr = destDS.GetLayer(0) ogrtest.compare_layers(srcLyr, destLyr) ogr.GetDriverByName('FlatGeobuf').DeleteDataSource('/vsimem/test.fgb') assert not gdal.VSIStatL('/vsimem/test.fgb')
def shp_to_csv(table_name): sourcePath = Path(__file__).parent shapefiles = [ filepath for filepath in Path(sourcePath / 'tmp').glob('**/*') if filepath.name == 'nycCOLPall_2018.shp' ] srcDS = gdal.OpenEx(str(shapefiles[0])) gdal.VectorTranslate(str(sourcePath.joinpath(f'tmp/{table_name}.csv')), srcDS, format='CSV', dstSRS='EPSG:4326', options=['-progress'], layerCreationOptions=['GEOMETRY=AS_WKT'])
def test_ogr2ogr_lib_21(): src_ds = gdal.GetDriverByName('Memory').Create('', 0, 0, 0) lyr = src_ds.CreateLayer('layer') lyr.CreateField(ogr.FieldDefn('foo')) lyr.CreateField(ogr.FieldDefn('bar')) f = ogr.Feature(lyr.GetLayerDefn()) f['foo'] = 'bar' f['bar'] = 'foo' lyr.CreateFeature(f) ds = gdal.VectorTranslate('', src_ds, format='Memory') with gdaltest.error_handler(): gdal.VectorTranslate(ds, src_ds, accessMode='append', selectFields=['foo']) ds = None f.Destroy() src_ds = None assert gdal.GetLastErrorNo() == gdalconst.CPLE_IllegalArg, \ 'expected use of -select and -append together to be invalid'