0

I am new to PyQGIS and trying to export a WFS Feature as a Shapefile called Test.shp.

uri = r"https://www.geoportal.rlp.de/mapbender/php/wfs.php?INSPIRE=1&FEATURETYPE_ID=2939&VERSION=2.0.0"
outputPath = 'C://Users//xxx//PyQGIS//Test.shp'

lyr = QgsVectorLayer(uri, "Temp", "WFS")

res=QgsVectorFileWriter.writeAsVectorFormat(lyr, outputPath, 'utf-8', lyr.crs(), 'ESRI Shapefile')

if res != QgsVectorFileWriter.NoError:
    print ("Error number:", res, "and", QgsVectorFileWriter.WriterError())
else:
    print ("WFS saved!")

res is giving me the Error code 8 without any details.

Error number: (8, '') and 0

I read the documentation of QgsVectorFileWriter and alle the other duplicates on exchange but couldn't find any information why my code isn't working.

11
  • 1
    step 1: does it work on the command line with ogr2ogr? step 2: does the URL work in the browser? step 3: is the returned data valid for a shapefile
    – Ian Turton
    Jun 14, 2022 at 10:04
  • Thanks for the quick answer. 1) 'ogr2ogr' is not recognized as an internal or external command, operable program or batch file. Python -m pip show gdal works... 2) yes, even QgsProject.instance().addMapLayer(tempWFS) for creating a vector layer works 3) I don't know. How can i find out?
    – Mabignix
    Jun 14, 2022 at 10:41
  • 1
    The service URL is: http://map1.sgdnord.rlp.de/kartendienste_rok/mod_ogc/wfs_getmap.php?mapfile=rrop_mw_2017&
    – nmtoken
    Jun 14, 2022 at 11:07
  • 1
    You can't get all features types in one GetFeature request
    – nmtoken
    Jun 14, 2022 at 11:44
  • 1
    A GetCapabilities request/response is the designed mechanism to get the typenames from any WFS service; DescribeFeatureType gives you more information, and GetFeature fetches the data
    – nmtoken
    Jun 14, 2022 at 15:42

1 Answer 1

0

I don't know why, but I copied the "source" from the layer-properties inside QGIS and used it as my URL and it worked.

uri=r"pagingEnabled='true'preferCoordinatesForWfsT11='false'restrictToRequestBBOX='1'srsname='EPSG:25832'typename='ms:mw_161208_03_vr_landwirtschaft'url='https://www.geoportal.rlp.de/mapbender/php/wfs.phpINSPIRE=1&FEATURETYPE_ID=2935'url='https://www.geoportal.rlp.de/mapbender/php/wfs.phpINSPIRE=1&FEATURETYPE_ID=2935&VERSION=2.0.0' version='auto'"
outputPath = 'C:/Users/xxx/PyQGIS/Test.shp'

lyr = QgsVectorLayer(uri, "Temp", "WFS")

res=QgsVectorFileWriter.writeAsVectorFormat(lyr, outputPath, 'utf-8', lyr.crs(), 'ESRI Shapefile')

if res != QgsVectorFileWriter.NoError:
    print ("Error number:", res, "and", QgsVectorFileWriter.WriterError())
else:
    print ("WFS saved!")

I still get the Error number: (0, '') and 0 but maybe that's a good sign :)

EDIT:

The code doesn't work in the case it has to replace the test.shp file. So please delete the files after every try.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.