1

I am trying to read a specific layer from a WFS service but can't figure out how to specify which the layer to read when using OWSlib.

The code was copied from: Reading data to geopandas using WFS?

The code mentions fetching the last layer. How can I fetch the 1st or lets say 3rd layer?

import geopandas as gpd
from owslib.wfs import WebFeatureService
from requests import Request
import fiona
fiona.drvsupport.supported_drivers['WFS'] = 'r'

# URL for WFS backend
url = 'https://service.pdok.nl/kadaster/bestuurlijkegebieden/wfs/v1_0?request=GetCapabilities&service=WFS'

# Initialize
wfs = WebFeatureService(url=url)


# Fetch the last available layer (as an example) --> 'vaestoruutu:vaki2021_5km'
layer = list(wfs.contents)[-1]

# Specify the parameters for fetching the data
# Count: specificies amount of rows to return (e.g. 10000 or 100)
# startIndex: specifies at which offset to start returning rows
params = dict(service='WFS', version="1.0.0", request='GetFeature',
      typeName=layer, outputFormat='json', count=1000, startIndex=0)

# Parse the URL with parameters
wfs_request_url = Request('GET', url, params=params).prepare().url

# Read data from URL
data = gpd.read_file(wfs_request_url)

return 

I updated the WFS to the version 2.0 that you provided. Changing the the index of the layers doesn't change the layer the layer read at the end. I am trying to read 'Provinciegebied' Do you know why this might be happening?

import geopandas as gpd
from requests import Request
from owslib.wfs import WebFeatureService

# URL for WFS backend
url = 'https://service.pdok.nl/kadaster/bestuurlijkegebieden/wfs/v1_0?request=GetCapabilities&service=WFS'

# Initialize
wfs = WebFeatureService(url=url)

# Fetch the last available layer (as an example) --> 'vaestoruutu:vaki2021_5km'
layer = list(wfs.contents)[1]

# Specify the parameters for fetching the data
# Count: specificies amount of rows to return (e.g. 10000 or 100)
# startIndex: specifies at which offset to start returning rows
params = dict(service='WFS', version="2.0.0", request='GetFeature', typeName=layer, outputFormat='json', count=1000, startIndex=0)

# Parse the URL with parameters
wfs_request_url = Request('GET', url, params=params).prepare().url

# Read data from URL
data = gpd.read_file(wfs_request_url)
4
  • https://service.pdok.nl/kadaster/bestuurlijkegebieden/wfs/v1_0?request=GetCapabilities&service=WFS gives you a WFS version 2.0.0 response, but you are requesting version 1.0.0 in your params the GetCapabilities of which is: https://service.pdok.nl/kadaster/bestuurlijkegebieden/wfs/v1_0?request=GetCapabilities&service=WFS&version1.0.0 version 1.0.0 WFS doesn't support paging
    – nmtoken
    Commented Feb 21, 2023 at 15:40
  • Please see update.
    – Ben Watson
    Commented Feb 21, 2023 at 17:44
  • When I use url = 'https://service.pdok.nl/kadaster/bestuurlijkegebieden/wfs/v1_0?' wfs = WebFeatureService(url=url, version='2.0.0') I get... 0 provinciegebied.ab06abc7-1d7f-4fbe-b7aa-5563f1... ... MULTIPOLYGON (((265275.541 549247.459, 265285....
    – nmtoken
    Commented Feb 22, 2023 at 15:32
  • Thanks! I was missing version='2.0.0' from wfs = WebFeatureService(url=url).
    – Ben Watson
    Commented Feb 23, 2023 at 8:03

1 Answer 1

1

It is a pure Python problem (lists)

With your WFS backend:

# list of all layers 
print(list(wfs.contents))
['Gemeentegebied', 'Provinciegebied', 'Landgebied']
#Fetch the last available layer
list(wfs.contents)[-1]
'Landgebied'
# or Fetch the third available layer
list(wfs.contents)[2]
'Landgebied'
# Fetch the first available layer
list(wfs.contents)[0]
'Gemeentegebied'
#Fetch the second available layer
list(wfs.contents)[1]
'Provinciegebied'

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.