{ "cells": [ { "cell_type": "markdown", "id": "54f244d8", "metadata": {}, "source": [ "# Retrieving data from Web Feature Service (WFS)\n", "\n", "Contents:\n", "- Introduce OGC WFS\n", "- Use OWSLib to get capabilities of WFS API\n", "- Retrieve data to geopandas" ] }, { "cell_type": "code", "execution_count": 1, "id": "721a81ff", "metadata": {}, "outputs": [], "source": [ "import geopandas as gpd\n", "import requests\n", "import geojson\n", "from pyproj import CRS\n", "from owslib.wfs import WebFeatureService\n", "\n", "# Specify the url for the backend.\n", "# Here we are using data from Statistics Finland: https://www.stat.fi/org/avoindata/paikkatietoaineistot_en.html. (CC BY 4.0)\n", "url = \"http://geo.stat.fi/geoserver/tilastointialueet/wfs\"\n", "\n", "# Specify parameters (read data in json format).\n", "params = dict(\n", " service=\"WFS\",\n", " version=\"2.0.0\",\n", " request=\"GetFeature\",\n", " typeName=\"tilastointialueet:kunta4500k\",\n", " outputFormat=\"json\",\n", ")\n", "\n", "# Fetch data from WFS using requests\n", "r = requests.get(url, params=params)\n", "\n", "# Create GeoDataFrame from geojson and set coordinate reference system\n", "data = gpd.GeoDataFrame.from_features(geojson.loads(r.content), crs=\"EPSG:3067\")" ] }, { "cell_type": "code", "execution_count": 2, "id": "295d482e", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
geometrykuntavuosiniminamnnamebbox
0POLYGON ((366787.924 7001300.583, 362458.797 6...0052022AlajärviAlajärviAlajärvi[321987.07200161, 6959704.55099558, 366787.924...
1POLYGON ((382543.364 7120022.976, 372645.944 7...0092022AlavieskaAlavieskaAlavieska[360962.99200022, 7104339.03799839, 382543.364...
2POLYGON ((343298.204 6961570.195, 345569.224 6...0102022AlavusAlavoAlavus[303353.32000378, 6922242.40698068, 345569.224...
3POLYGON ((436139.680 6798279.085, 435912.756 6...0162022AsikkalaAsikkalaAsikkala[403543.81899999, 6774122.31100019, 442401.762...
4POLYGON ((426631.036 6720528.076, 432565.266 6...0182022AskolaAskolaAskola[413073.96299999, 6704555.87800016, 435459.201...
\n", "
" ], "text/plain": [ " geometry kunta vuosi nimi \\\n", "0 POLYGON ((366787.924 7001300.583, 362458.797 6... 005 2022 Alajärvi \n", "1 POLYGON ((382543.364 7120022.976, 372645.944 7... 009 2022 Alavieska \n", "2 POLYGON ((343298.204 6961570.195, 345569.224 6... 010 2022 Alavus \n", "3 POLYGON ((436139.680 6798279.085, 435912.756 6... 016 2022 Asikkala \n", "4 POLYGON ((426631.036 6720528.076, 432565.266 6... 018 2022 Askola \n", "\n", " namn name bbox \n", "0 Alajärvi Alajärvi [321987.07200161, 6959704.55099558, 366787.924... \n", "1 Alavieska Alavieska [360962.99200022, 7104339.03799839, 382543.364... \n", "2 Alavo Alavus [303353.32000378, 6922242.40698068, 345569.224... \n", "3 Asikkala Asikkala [403543.81899999, 6774122.31100019, 442401.762... \n", "4 Askola Askola [413073.96299999, 6704555.87800016, 435459.201... " ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.head()" ] }, { "cell_type": "code", "execution_count": 3, "id": "f2336f9b", "metadata": {}, "outputs": [], "source": [ "# Prepare data for writing to various file formats\n", "data = data.drop(columns=[\"bbox\"])" ] }, { "cell_type": "code", "execution_count": 4, "id": "9a8d503d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\n", "Name: ETRS89 / TM35FIN(E,N)\n", "Axis Info [cartesian]:\n", "- E[east]: Easting (metre)\n", "- N[north]: Northing (metre)\n", "Area of Use:\n", "- name: Finland - onshore and offshore.\n", "- bounds: (19.08, 58.84, 31.59, 70.09)\n", "Coordinate Operation:\n", "- name: TM35FIN\n", "- method: Transverse Mercator\n", "Datum: European Terrestrial Reference System 1989 ensemble\n", "- Ellipsoid: GRS 1980\n", "- Prime Meridian: Greenwich" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Check crs\n", "data.crs" ] }, { "cell_type": "code", "execution_count": 7, "id": "e75296b5", "metadata": {}, "outputs": [], "source": [ "# filename\n", "layer_name = \"finland_municipalities.gpkg\"\n", "\n", "# Write data to disk\n", "# data.to_file(file_name, driver=\"GPKG\")" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.7" } }, "nbformat": 4, "nbformat_minor": 5 }