Skip to content

Instantly share code, notes, and snippets.

@maxcollombin
Created August 21, 2023 08:08
Show Gist options
  • Save maxcollombin/94e31099407a55dab46dd084f78a99ed to your computer and use it in GitHub Desktop.
Save maxcollombin/94e31099407a55dab46dd084f78a99ed to your computer and use it in GitHub Desktop.
Script to read data from an OGC WFS service to GeoPandas
# -*- coding: utf-8 -*-
"""
wfs-geopandas.py
Script to read data from an OGC WFS service to GeoPandas
Author: [Maxime Collombin]
Reference: https://gis.stackexchange.com/questions/299567/reading-data-to-geopandas-using-wfs
Date: [14/08/2023]
"""
import geopandas as gpd
from requests import Request
from owslib.wfs import WebFeatureService
# URL for WFS backend
url = "http://geo.stat.fi/geoserver/vaestoruutu/wfs"
# Initialize
wfs = WebFeatureService(url=url)
# Fetch the last available layer (as an example) --> 'vaestoruutu:vaki2021_5km'
layer_name = 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_name, outputFormat='json', count=100, 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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment