1

Purpose: to create and add a vector layer instance to the project with Python

This is what is written in https://docs.qgis.org/3.22/en/docs/pyqgis_developer_cookbook/loadlayer.html#id1 to connect with a geoservice by python.

From the workbook

WFS connection: the connection is defined with a URI and using the WFS provider:

uri = "https://demo.mapserver.org/cgi-bin/wfs?service=WFS&version=2.0.0&request=GetFeature&typename=ms:cities"
vlayer = QgsVectorLayer(uri, "my wfs layer", "WFS")

My attempt

I tried to do with a my connection WFS URL: https://geodienste.ch/db/av/deu with the layer "ms:DPRSF".

uri = "https://geodienste.ch/db/av/deu"
vlayer = QgsVectorLayer(uri, "ms:DPRSF", "WFS")

Question

I know that the problem of my attempt is the URI. I don't know how/where to extrapolate the URI from a URL of WFS connection.

How do I get a modified attempt script that works fine?

Answer of Ian Turton with the URI https://wfs.geodienste.ch/av_0/deu?SERVICE=WFS&REQUEST=GetFeature&VERSION=2.0.0&TYPENAMES=ms:DPRSF&SRSNAME=urn:ogc:def:crs:EPSG::2056 does not work.

Besides, the problem is that I am able to build the URI only for this part even if I access to the captabilities: https://wfs.geodienste.ch/av_0/deu?SERVICE=WFS&REQUEST=GetFeature&VERSION=2.0.0

for the last part (&TYPENAMES=ms:DPRSF&SRSNAME=urn:ogc:def:crs:EPSG::2056) I am not able to build alone! There is no link that can be easily copied and pasted.

Why did you write "TYPENAMES", "SRSNAME". Here Access WFS with authentication from PyQGIS is written 'srsName', 'typename'

Furthermore the order https://wfs.geodienste.ch/av_0/deu?SERVICE=WFS&REQUEST=GetFeature&VERSION=2.0.0&TYPENAMES=ms:DPRSF&SRSNAME=urn:ogc:def:crs:EPSG::2056

and what happen if I wrote https://wfs.geodienste.ch/av_0/deu?SERVICE=WFS&REQUEST=GetFeature&VERSION=2.0.0&SRSNAME=urn:ogc:def:crs:EPSG::2056&TYPENAMES=ms:DPRSF?

3
  • Using the example given, and following on from Ian's answer, I assume that what you need is uri = "https://wfs.geodienste.ch/av_0/deu?service=WFS&version=2.0.2&request=GetFeature&typename=ms:DPRSF" Ian uses typenames because the WFS 2.0.0 specification says that is the correct parameter name to identify the feature type in a GetFeature request, earlier versions used typename. Most 2.0.0 services support both spellings, so you should be OK if pyqgis only works with the wrong spelling.
    – nmtoken
    Jul 1, 2022 at 15:40
  • Actually looking at the documentation again I think you should be fine using typenames like uri = "https://wfs.geodienste.ch/av_0/deu?service=WFS&version=2.0.2&request=GetFeature&typenames=ms:DPRSF" PyQGIS will append any additional request parameters to that string
    – nmtoken
    Jul 1, 2022 at 15:44
  • uri = "wfs.geodienste.ch/av_0/…" is the right answer. With typenames does not work. However, the URI is written in the geoservice layer under "Source". You have only to copy the link of the source. Thank you @mmtoken
    – simone100
    Jul 7, 2022 at 14:23

1 Answer 1

2

The normal approach is to look at the GetCapabilities response of the service. All OGC services provide this as a way of accessing the data.

Ideally adding ?request=GetCapabilities&service=WFS to the end of your URL should give you an XML document that tells you (or your client) everything they need to know to access the data.

https://geodienste.ch/db/av/deu?SERVICE=WFS&REQUEST=GetCapabilities

Which amongst other things says that you can make GetFeature requests to https://wfs.geodienste.ch/av_0/deu?

<ows:Operation name="GetFeature">
  <ows:DCP>
    <ows:HTTP>
      <ows:Get xlink:type="simple" xlink:href="https://wfs.geodienste.ch/av_0/deu?"/>
      <ows:Post xlink:type="simple" xlink:href="https://wfs.geodienste.ch/av_0/deu?"/>
    </ows:HTTP>
  </ows:DCP>
  <ows:Parameter name="outputFormat">
    <ows:AllowedValues>
      <ows:Value>application/gml+xml; version=3.2</ows:Value>
      <ows:Value>text/xml; subtype=gml/3.2.1</ows:Value>
      <ows:Value>text/xml; subtype=gml/3.1.1</ows:Value>
      <ows:Value>text/xml; subtype=gml/2.1.2</ows:Value>
      <ows:Value>application/json; subtype=geojson</ows:Value>
    </ows:AllowedValues>
  </ows:Parameter>
</ows:Operation>

and that the FeatureType you want is:

<FeatureType>
  <Name>ms:DPRSF</Name>
  <Title>SDRechte</Title>
  <Abstract>SDRechte</Abstract>
  <DefaultCRS>urn:ogc:def:crs:EPSG::2056</DefaultCRS>
  <OtherCRS>urn:ogc:def:crs:EPSG::4326</OtherCRS>
  <OtherCRS>urn:ogc:def:crs:EPSG::3857</OtherCRS>
  <OutputFormats>
    <Format>application/gml+xml; version=3.2</Format>
    <Format>text/xml; subtype=gml/3.2.1</Format>
    <Format>text/xml; subtype=gml/3.1.1</Format>
    <Format>text/xml; subtype=gml/2.1.2</Format>
    <Format>application/json; subtype=geojson</Format>
  </OutputFormats>
  <ows:WGS84BoundingBox dimensions="2">
    <ows:LowerCorner>5.70210665339108 45.7392294088812</ows:LowerCorner>
    <ows:UpperCorner>10.6432129882749 47.8504920823022</ows:UpperCorner>
  </ows:WGS84BoundingBox>
  <MetadataURL xlink:href="https://wfs.geodienste.ch/av_0/deu?request=GetMetadata&amp;layer=DPRSF"/>
</FeatureType>

Thus your URL should be something like https://wfs.geodienste.ch/av_0/deu?SERVICE=WFS&REQUEST=GetFeature&VERSION=2.0.0&TYPENAMES=ms:DPRSF&SRSNAME=urn:ogc:def:crs:EPSG::2056

0

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.