I know how to properly load a layer in QGIS from a WFS service, but I would like to directly save it to a .shp
file (without the preliminary loading of it into QGIS).
Is it possible to do this within QGIS or with a PyQGIS script?
A sample URL from which starting would be this one:
http://wms.pcn.minambiente.it/ogc?map=/ms_ogc/wfs/Carta_geologica.map
having these capabilities.
Answer
You can do that without PyQGIS (e.g., downloading the GML from the WFS to your disk and then converting it to Shapefile through ogr
), but since you would like to do it with PyQGIS (why not?), try this:
uri = "http://wms.pcn.minambiente.it/ogc?map=/ms_ogc/wfs/Carta_geologica.map&request=GetFeature&Version=1.1.0&Service=WFS&typename=GE.CARTAGEOLOGICA"
layer = QgsVectorLayer( uri, "my wfs layer", "WFS" )
res = QgsVectorFileWriter.writeAsVectorFormat( layer,
'/tmp/wfs_features.shp',
'System', # encoding
None, #crs
'ESRI Shapefile'
)
if res != QgsVectorFileWriter.NoError:
print 'Error number:', res
else:
print "WFS saved!"
Note that we are not loading the layer into the QGIS canvas, we are just using and intermediate layer
object that QGIS can convert to other formats.
EDIT
Since your WFS server only works with version 1.1.0 of the WFS specification, and given the issues with axis orientation for such version, you can do this in QGIS >= v2.16 to invert axis order and get your data correctly positioned:
dsu = QgsDataSourceURI()
dsu.setParam( 'url', 'http://wms.pcn.minambiente.it/ogc?map=/ms_ogc/wfs/Carta_geologica.map' )
dsu.setParam( 'version', '1.1.0' )
dsu.setParam( 'typename', 'GE.CARTAGEOLOGICA' )
dsu.setParam( 'InvertAxisOrientation', '1' )
layer = QgsVectorLayer( dsu.uri(), "my wfs layer", "WFS" )
res = QgsVectorFileWriter.writeAsVectorFormat( layer,
'/tmp/wfs_features.shp',
'System', # encoding
None, #crs
'ESRI Shapefile'
)
if res != QgsVectorFileWriter.NoError:
print 'Error number:', res
else:
print "WFS saved!"
No comments:
Post a Comment