Tuesday 19 July 2016

Reading data to geopandas using WFS?


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'))
out.close()
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?



Answer



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
print(wfs.identification.title)

# Get WFS version

print(wfs.version)

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

# Available data layers
print(list(wfs.contents))

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

print(meta.__dict__)

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)

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...