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)