[GRASS-SVN] r53180 - grass/trunk/scripts/v.in.wfs
svn_grass at osgeo.org
svn_grass at osgeo.org
Sun Sep 16 00:49:16 PDT 2012
Author: hamish
Date: 2012-09-16 00:49:16 -0700 (Sun, 16 Sep 2012)
New Revision: 53180
Modified:
grass/trunk/scripts/v.in.wfs/v.in.wfs.html
grass/trunk/scripts/v.in.wfs/v.in.wfs.py
Log:
featurify
Modified: grass/trunk/scripts/v.in.wfs/v.in.wfs.html
===================================================================
--- grass/trunk/scripts/v.in.wfs/v.in.wfs.html 2012-09-14 08:00:39 UTC (rev 53179)
+++ grass/trunk/scripts/v.in.wfs/v.in.wfs.html 2012-09-16 07:49:16 UTC (rev 53180)
@@ -9,13 +9,34 @@
Import of GRASS user map:
<p><div class="code"><pre>
# run in LatLong location:
-v.in.wfs \
- url="http://mapserver.gdf-hannover.de/cgi-bin/grassuserwfs?REQUEST=GetFeature&SERVICE=WFS&VERSION=1.0.0" out=grass_users
+v.in.wfs url="http://mapserver.gdf-hannover.de/cgi-bin/grassuserwfs?" output=grass_users
v.db.select grass_users where="name ~ 'Markus'"
</pre></div>
+<p>
+Download 25 ship wrecks from LINZ data service:
+<br>
+(first create yourself a free API key at
+ <a href="http://data.linz.govt.nz/p/web-services/">http://data.linz.govt.nz/p/web-services/</a>)
+<p>
+<div class="code"><pre>
+# run in LatLong location:
+URL='http://wfs.data.linz.govt.nz/<PUT YOUR API KEY HERE>/wfs?'
+
+# download available layers to wms_capabilities.xml
+v.in.wfs -l url="$URL"
+</pre></div>
+
+From that file we learn that the shipwreck layer is called "<tt>v:x633</tt>"
+and that EPSG code 4326 (LatLong WGS84) is a supported SRS for this data layer.
+
+<div class="code"><pre>
+v.in.wfs url="$URL" output=linz_hydro_25_wrecks name="v:x633" srs="EPSG:4326" max=25
+</pre></div>
+
+
<h2>REQUIREMENTS</h2>
The OGR library on the system needs to be compiled with Xerces C++ XML
@@ -25,6 +46,7 @@
<h2>SEE ALSO</h2>
<em>
+<a href="g.region.html">g.region</a>,
<a href="r.in.wms.html">r.in.wms</a>,
<a href="v.in.ogr.html">v.in.ogr</a>
</em>
Modified: grass/trunk/scripts/v.in.wfs/v.in.wfs.py
===================================================================
--- grass/trunk/scripts/v.in.wfs/v.in.wfs.py 2012-09-14 08:00:39 UTC (rev 53179)
+++ grass/trunk/scripts/v.in.wfs/v.in.wfs.py 2012-09-16 07:49:16 UTC (rev 53180)
@@ -4,10 +4,10 @@
#
# MODULE: v.in.wfs
# AUTHOR(S): Markus Neteler. neteler itc it
-# Hamish Bowman (fixes)
+# Hamish Bowman
# Converted to Python by Glynn Clements
# PURPOSE: WFS support
-# COPYRIGHT: (C) 2006, 2007, 2008, 2010 Markus Neteler and GRASS Development Team
+# COPYRIGHT: (C) 2006-2012 Markus Neteler and the GRASS Development Team
#
# This program is free software under the GNU General
# Public License (>=v2). Read the file COPYING that
@@ -17,8 +17,13 @@
# http://mapserver.gdf-hannover.de/cgi-bin/grassuserwfs?REQUEST=GetFeature&SERVICE=WFS&VERSION=1.0.0
#############################################################################
+#
+# TODO: suggest to depend on the OWSLib for OGC web service needs
+# http://pypi.python.org/pypi/OWSLib
+#
+
#%Module
-#% description: Imports GetFeature from WFS.
+#% description: Imports GetFeature from a WFS server.
#% keywords: vector
#% keywords: import
#% keywords: wfs
@@ -26,13 +31,51 @@
#%option
#% key: url
#% type: string
-#% description: GetFeature URL starting with 'http'
+#% description: Base URL starting with 'http' and ending in '?'
#% required: yes
#%end
#%option G_OPT_V_OUTPUT
#%end
+#%option
+#% key: name
+#% type: string
+#% description: Comma separated names of data layers to download
+#% multiple: yes
+#% required: no
+#%end
+#%option
+#% key: srs
+#% type: string
+#% label: Specify alternate spatial reference system (example: EPSG:4326)
+#% description: The given code must be supported by the server, consult the capabilities file
+#% required: no
+#%end
+#%option
+#% key: maximum_features
+#% type: integer
+#% label: Maximum number of features to download
+#% description: (default: unlimited)
+#%end
+#%option
+#% key: start_index
+#% type: integer
+#% label: Skip earlier feature IDs and start downloading at this one
+#% description: (default: start with the first feature)
+#%end
+#%flag
+#% key: l
+#todo #% description: List available layers and exit
+#% description: Download server capabilities to 'wms_capabilities.xml' in the current directory and exit
+#% suppress_required: yes
+#%end
+#%flag
+#% key: r
+#% description: Restrict fetch to features which touch the current region
+#%end
+
import os
+import sys
from grass.script import core as grass
import urllib
@@ -40,9 +83,37 @@
out = options['output']
wfs_url = options['url']
+ request_base = 'REQUEST=GetFeature&SERVICE=WFS&VERSION=1.0.0'
+ wfs_url += request_base
+
+ if options['name']:
+ wfs_url += '&TYPENAME=' + options['name']
+
+ if options['srs']:
+ wfs_url += '&SRS=' + options['srs']
+
+ if options['maximum_features']:
+ wfs_url += '&MAXFEATURES=' + options['maximum_features']
+ if int(options['maximum_features']) < 1:
+ grass.fatal('Invalid maximum number of features')
+
+ if options['start_index']:
+ wfs_url += '&STARTINDEX=' + options['start_index']
+ if int(options['start_index']) < 1:
+ grass.fatal('Features begin with index "1"')
+
+ if flags['r']:
+ bbox = grass.read_command("g.region", flags = 'w').split('=')[1]
+ wfs_url += '&BBOX=' + bbox
+
+ if flags['l']:
+ wfs_url = options['url'] + 'REQUEST=GetCapabilities&SERVICE=WFS&VERSION=1.0.0'
+
tmp = grass.tempfile()
tmpxml = tmp + '.xml'
+ grass.debug(wfs_url)
+
grass.message(_("Retrieving data..."))
inf = urllib.urlopen(wfs_url)
outf = file(tmpxml, 'wb')
@@ -54,6 +125,16 @@
inf.close()
outf.close()
+ if flags['l']:
+ import shutil
+ if os.path.exists('wms_capabilities.xml'):
+ grass.fatal('A file called "wms_capabilities.xml" already exists here')
+ # os.move() might fail if the temp file is on another volume, so we copy instead
+ shutil.copy(tmpxml, 'wms_capabilities.xml')
+ grass.try_remove(tmpxml)
+ sys.exit(0)
+
+
grass.message(_("Importing data..."))
ret = grass.run_command('v.in.ogr', flags = 'o', dsn = tmpxml, out = out)
grass.try_remove(tmpxml)
More information about the grass-commit
mailing list