I'm new to GIS. Trying to use Finnish population data on 1km tiles available here: http://www.stat.fi/org/avoindata/paikkatietoaineistot_en.html

The WFS service is here: http://geo.stat.fi/geoserver/vaestoruutu/wfs

I'm using owslib to access the service:

from owslib.wfs import WebFeatureService
url = 'http://geo.stat.fi/geoserver/tilastointialueet/wfs'
wfs20 = WebFeatureService(url=url, version='2.0.0')
response = wfs20.getfeature(typename='tilastointialueet:hila1km')

First of all this response is huge, but I don't know how to ask for a smaller response. I don't understand how to get this response to a geopandas dataframe. I've tried:

out = open('data.gml', 'wb')
out.write(bytes(response.read(), 'UTF-8'))
import geopandas
tiles = geopandas.read_file('data.gml')

but get the error CPLE_OpenFailedError: b'Unable to open EPSG support file gcs.csv. Try setting the GDAL_DATA environment variable to point to the directory containing EPSG csv files.' I think that the correct EPSG should be 3067, but I don't know how to specify this when reading the file.

How to get this data to a geopandas dataframe?

  • 2
    The data contains the coordinate system code but problem is as the error message tries to tell that geopandas is not properly configured and it does not know where it could find necessary support files. Set environment for GDAL_DATA. – user30184 Oct 20 '18 at 7:01

UPDATE: Geopandas 0.4.0, allows to read data from URL directly. Thanks @SamTux for pointing it out!

OWSLib is good for reading the details and capabilities of specific WFS. For reading the data, I would use request module to first parse the URL with parameters, and then you can read the data directly from that URL using gpd.read_file().

Read WFS capabilities and metadata:

from owslib.wfs import WebFeatureService

# URL for WFS backend
url = "http://geo.stat.fi/geoserver/vaestoruutu/wfs"

# See details about this particular WFS
# -------------------------------------

# Initialize
wfs = WebFeatureService(url=url)

# Service provider 

# Get WFS version

# Available methods
print([operation.name for operation in wfs.operations])

# Available data layers

# Print all metadata of all layers
for layer, meta in wfs.items():

Read the data into GeoDataFrame:

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)

# Get data from WFS
# -----------------

# Fetch the last available layer (as an example) --> 'vaestoruutu:vaki2017_5km'
layer = list(wfs.contents)[-1]

# Specify the parameters for fetching the data
params = dict(service='WFS', version="1.0.0", request='GetFeature',
      typeName=layer, outputFormat='json')

# Parse the URL with parameters
q = Request('GET', url, params=params).prepare().url

# Read data from URL
data = gpd.read_file(q)
  • In the version 0.4 .0 supports direct [reading from the GeoJSON Web link][1] : import geopandas as gpd url = "geo.stat.fi/geoserver/vaestoruutu/…" [1]: geopandas.org/io.html – SamTux Nov 15 '18 at 16:33
  • Cool! Thanks for pointing out. Will update the answer accordingly. – htenkanen Nov 16 '18 at 18:07
  • seems like there is a limitation on the number of rows, how can we avoid it ? – Basile Jun 12 '20 at 13:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.