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)
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 pagingurl = '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....