Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v.in.wfs: add different WFS version support, import only specified layers and define area of interest #3311

Merged
merged 7 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions scripts/v.in.wfs/v.in.wfs.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ <h3>WFS import without credentials</h3>
# run in Latitude-Longitude location (EPGS code 4326):
# download "sentinel:mgrs" layer:
v.in.wfs url="https://geoserver.mundialis.de/geoserver/sentinel/wfs?" name="sentinel:mgrs" output=sentinel2_mgrs
# download NRW ALKIS "ave:Flurstueck" attribute:
# set the AOI beforehand with g.region and limit import to current region with -r flag
v.in.wfs url="https://www.wfs.nrw.de/geobasis/wfs_nw_alkis_vereinfacht?" -r output=wfs_alkis_vereinfacht srs=25832
name="ave:Flurstueck" version="2.0.0" layer="Flurstueck"
</pre></div>

<h3>WFS import with API key</h3>
Expand Down
48 changes: 39 additions & 9 deletions scripts/v.in.wfs/v.in.wfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
# AUTHOR(S): Markus Neteler. neteler itc it
# Hamish Bowman
# Converted to Python by Glynn Clements
# German ALKIS support added by Veronica Köß
# PURPOSE: WFS support
# COPYRIGHT: (C) 2006-2012 Markus Neteler and the GRASS Development Team
# COPYRIGHT: (C) 2006-2024 Markus Neteler and the GRASS Development Team
#
# This program is free software under the GNU General
# Public License (>=v2). Read the file COPYING that
Expand Down Expand Up @@ -45,6 +46,13 @@
# % required: no
# %end
# %option
# % key: layer
# % type: string
# % description: Name of data layers to import
# % multiple: yes
# % required: no
# %end
# %option
# % key: srs
# % type: string
# % label: Specify alternate spatial reference system (example: EPSG:4326)
Expand All @@ -64,6 +72,14 @@
# % description: (default: start with the first feature)
# %end
# %option
# % key: version
# % type: string
# % required: no
# % multiple: no
# % description: version of WFS, e.g.:1.0.0 or 2.0.0
# % answer: 1.0.0
# %end
# %option
# % key: username
# % type: string
# % required: no
Expand Down Expand Up @@ -96,19 +112,24 @@

from urllib.request import urlopen
from urllib.request import build_opener, install_opener
from urllib.request import HTTPPasswordMgrWithDefaultRealm, HTTPBasicAuthHandler
from urllib.request import HTTPPasswordMgrWithDefaultRealm
from urllib.request import HTTPBasicAuthHandler
from urllib.error import URLError, HTTPError


def main():
out = options["output"]
wfs_url = options["url"]
version_num = options["version"]

request_base = "REQUEST=GetFeature&SERVICE=WFS&VERSION=1.0.0"
request_base = "REQUEST=GetFeature&SERVICE=WFS&VERSION=" + version_num
wfs_url += request_base

if options["name"]:
wfs_url += "&TYPENAME=" + options["name"]
if tuple([int(x) for x in version_num.split(".")]) >= (2, 0, 0):
wfs_url += "&TYPENAMES=" + options["name"]
else:
wfs_url += "&TYPENAME=" + options["name"]

if options["srs"]:
wfs_url += "&SRS=" + options["srs"]
Expand All @@ -130,7 +151,9 @@ def main():
wfs_url += "&BBOX=" + bbox

if flags["l"]:
wfs_url = options["url"] + "REQUEST=GetCapabilities&SERVICE=WFS&VERSION=1.0.0"
wfs_url = options["url"] + "REQUEST=GetCapabilities&SERVICE=WFS"

print(wfs_url)

tmp = grass.tempfile()
tmpxml = tmp + ".xml"
Expand Down Expand Up @@ -169,9 +192,7 @@ def main():
inf = urlopen(wfs_url)
except HTTPError as e:
# GTC WFS request HTTP failure
grass.fatal(
_("The server couldn't fulfill the request.\nError code: %s") % e.code
)
grass.fatal(_("Server couldn't fulfill the request.\nError code: %s") % e.code)
except URLError as e:
# GTC WFS request network failure
grass.fatal(_("Failed to reach the server.\nReason: %s") % e.reason)
Expand All @@ -197,7 +218,16 @@ def main():

grass.message(_("Importing data..."))
try:
grass.run_command("v.in.ogr", flags="o", input=tmpxml, output=out)
if options["layer"]:
grass.run_command(
"v.in.ogr",
flags="o",
input=tmpxml,
output=out,
layer=options["layer"],
)
else:
grass.run_command("v.in.ogr", flags="o", input=tmpxml, output=out)
grass.message(_("Vector map <%s> imported from WFS.") % out)
except Exception:
grass.message(_("WFS import failed"))
Expand Down
Loading