Retrieving data from Web Feature Service (WFS)#
Contents:
Introduce OGC WFS
Use OWSLib to get capabilities of WFS API
Retrieve data to geopandas
import geopandas as gpd
import requests
import geojson
from pyproj import CRS
from owslib.wfs import WebFeatureService
# Specify the url for the backend.
# Here we are using data from Statistics Finland: https://www.stat.fi/org/avoindata/paikkatietoaineistot_en.html. (CC BY 4.0)
url = "http://geo.stat.fi/geoserver/tilastointialueet/wfs"
# Specify parameters (read data in json format).
params = dict(
service="WFS",
version="2.0.0",
request="GetFeature",
typeName="tilastointialueet:kunta4500k",
outputFormat="json",
)
# Fetch data from WFS using requests
r = requests.get(url, params=params)
# Create GeoDataFrame from geojson and set coordinate reference system
data = gpd.GeoDataFrame.from_features(geojson.loads(r.content), crs="EPSG:3067")
data.head()
geometry | kunta | vuosi | nimi | namn | name | bbox | |
---|---|---|---|---|---|---|---|
0 | POLYGON ((366787.924 7001300.583, 362458.797 6... | 005 | 2022 | Alajärvi | Alajärvi | Alajärvi | [321987.07200161, 6959704.55099558, 366787.924... |
1 | POLYGON ((382543.364 7120022.976, 372645.944 7... | 009 | 2022 | Alavieska | Alavieska | Alavieska | [360962.99200022, 7104339.03799839, 382543.364... |
2 | POLYGON ((343298.204 6961570.195, 345569.224 6... | 010 | 2022 | Alavus | Alavo | Alavus | [303353.32000378, 6922242.40698068, 345569.224... |
3 | POLYGON ((436139.680 6798279.085, 435912.756 6... | 016 | 2022 | Asikkala | Asikkala | Asikkala | [403543.81899999, 6774122.31100019, 442401.762... |
4 | POLYGON ((426631.036 6720528.076, 432565.266 6... | 018 | 2022 | Askola | Askola | Askola | [413073.96299999, 6704555.87800016, 435459.201... |
# Prepare data for writing to various file formats
data = data.drop(columns=["bbox"])
# Check crs
data.crs
<Projected CRS: EPSG:3067>
Name: ETRS89 / TM35FIN(E,N)
Axis Info [cartesian]:
- E[east]: Easting (metre)
- N[north]: Northing (metre)
Area of Use:
- name: Finland - onshore and offshore.
- bounds: (19.08, 58.84, 31.59, 70.09)
Coordinate Operation:
- name: TM35FIN
- method: Transverse Mercator
Datum: European Terrestrial Reference System 1989 ensemble
- Ellipsoid: GRS 1980
- Prime Meridian: Greenwich
# filename
layer_name = "finland_municipalities.gpkg"
# Write data to disk
# data.to_file(file_name, driver="GPKG")