[GRASS-SVN] r48154 - grass/trunk/scripts/v.in.gps

svn_grass at osgeo.org svn_grass at osgeo.org
Mon Sep 5 18:31:19 EDT 2011


Author: hamish
Date: 2011-09-05 15:31:19 -0700 (Mon, 05 Sep 2011)
New Revision: 48154

Added:
   grass/trunk/scripts/v.in.gps/v.in.gps.html
   grass/trunk/scripts/v.in.gps/v.in.gps.py
Removed:
   grass/trunk/scripts/v.in.gps/v.in.gpsbabel.html
Log:
initial python framework (non-functional - still needs its guts implemented)

Copied: grass/trunk/scripts/v.in.gps/v.in.gps.html (from rev 48153, grass/trunk/scripts/v.in.gps/v.in.gpsbabel.html)
===================================================================
--- grass/trunk/scripts/v.in.gps/v.in.gps.html	                        (rev 0)
+++ grass/trunk/scripts/v.in.gps/v.in.gps.html	2011-09-05 22:31:19 UTC (rev 48154)
@@ -0,0 +1,101 @@
+<h2>DESCRIPTION</h2>
+
+<em>v.in.gps</em> allows the user to import waypoint, route, and track
+data from a locally connected GPS receiver or a text file containing 
+GPS data of many common formats. Translation is done via the 
+<em><a href="http://www.gpsbabel.org">GPSBabel</a></em> program. 
+
+<p>
+This software is not intended as a primary means of navigation.
+
+<h2>NOTES</h2>
+
+<em>v.in.gps</em> automatically reprojects data using the 
+projection settings of the current location.
+
+The default input data projection is lat/lon WGS84. If your GPS outputs data
+using another projection or map datum, you may include the 
+<em><a href="http://trac.osgeo.org/proj/">PROJ.4</a></em>
+parameters defining your projection in the <b>proj</b> option and 
+<em>v.in.gps</em> will reproject your data accordingly.
+Great care must be taken to get these parameters correct!
+
+The automatic transform may be skipped by using the <b>-k</b> flag in which 
+case the data will be imported unprojected, as it appears in the <b>input</b>.
+
+<p>
+Route and Track data may be uploaded as a series of points by using the <b>-p</b> 
+flag, otherwise they will be imported as lines. You can run <em>v.in.gps</em>
+multiple times and merge the line and point vectors with the <em>v.patch</em>
+command if you want, but take care when merging dissimilar attribute tables.
+
+<h2>EXAMPLES</h2>
+
+<h3>GPS device connected via USB adapter</h3>
+
+Import waypoints, tracks, routes from /dev/ttyUSB0 and save
+to a GRASS vector map:
+<div class="code"><pre>
+v.in.gps -w input=/dev/ttyUSB0 format=garmin output=waypoints
+v.in.gps -t input=/dev/ttyUSB0 format=garmin output=tracks
+v.in.gps -r input=/dev/ttyUSB0 format=garmin output=routes
+</pre></div>
+
+<h3>GPS device connected via serial adapter</h3>
+
+Import waypoint data from a Garmin GPS connected at /dev/ttyS0 and save
+to a GRASS vector map named <i>waypoints</i>:
+<div class="code"><pre>
+v.in.gps -w input=/dev/ttyS0 format=garmin output=waypoints
+</pre></div>
+
+<h3>Import track data from a GPX</h3>
+
+Import track data from a GPX text file and save to a GRASS vector map 
+named <i>tracks</i>.
+<!-- (currently buggy) The gpxlogger program distributed with 
+<a href="http://gpsd.berlios.de/">GPSd</a> >2.30 is a good program for 
+creating these logs. -->
+<div class="code"><pre>
+v.in.gps -t input=gpslog.gpx format=gpx output=tracks
+</pre></div>
+
+<h3>Import route data from GPS connected at /dev/gps</h3>
+
+Import route data as a series of points from a Garmin GPS connected at 
+/dev/gps and save to a GRASS vector map named <i>routePoints</i>:
+<div class="code"><pre>
+v.in.gps -r -p file=/dev/gps format=garmin output=routePoints
+</pre></div>
+
+
+<h2>SEE ALSO</h2>
+<em>
+<a href="db.execute.html">db.execute</a>,
+<a href="v.in.ascii.html">v.in.ascii</a>,
+<a href="v.in.garmin.html">v.in.garmin</a>,
+<a href="v.db.connect.html">v.db.connect</a>,
+<a href="v.patch.html">v.patch</a>
+</em>
+
+<a href="http://www.gpsbabel.org">gpsbabel</a> from gpsbabel.org<br>
+cs2cs from <A HREF="http://trac.osgeo.org/proj/">PROJ.4</a>
+
+<h2>AUTHORS</h2>
+Claudio Porta and Lucio Davide Spano, students of Computer Science at
+University of Pisa (Italy).<br>
+Commission from Faunalia Pontedera (PI)<br><br>
+Based on <em>v.in.garmin</em> for GRASS 6.0 by Hamish Bowman<br>
+and <em>v.in.garmin.sh</em> for GRASS 5 by Andreas Lange
+<br>
+
+<!--
+When we tested the script with a Garmin GPS we noticed that importing waypoints 
+the <i>time</i> field is not correctly set. The data reported is 
+a default system time, while with a gpx text file this thing did not 
+happen. We believe that is a <i>gpsbabel</i> trouble in translating 
+from <i>garmin</i> to <i>xcsv</i> ... 
+-->
+
+<p>
+<i>Last changed: $Date$</i>

Added: grass/trunk/scripts/v.in.gps/v.in.gps.py
===================================================================
--- grass/trunk/scripts/v.in.gps/v.in.gps.py	                        (rev 0)
+++ grass/trunk/scripts/v.in.gps/v.in.gps.py	2011-09-05 22:31:19 UTC (rev 48154)
@@ -0,0 +1,242 @@
+#!/usr/bin/env python
+#
+############################################################################
+#
+# MODULE:	v.in.gps
+#
+# PURPOSE:	Import GPS data from a GPS receiver or file into a GRASS
+#	       vector map using gpsbabel
+#
+# COPYRIGHT:	(c) 2011 Hamish Bowman, and the GRASS Development Team
+#		This program is free software under the GNU General Public
+#		License (>=v2). Read the file COPYING that comes with GRASS
+#		for details.
+#
+# AUTHOR:	Hamish Bowman, Dunedin, New Zealand
+#		Python version based on v.out.gps.py by Glynn Clements
+#		Work-alike of the v.in.gpsbabel shell script from GRASS 6
+#
+#############################################################################
+#
+# REQUIREMENTS:
+#      -  GPSBabel from 	http://gpsbabel.sourceforge.net
+#      -  cs2cs from PROJ.4 (for m.proj)	http://proj.osgeo.org
+#
+#      - report supported GPSBabel formats:
+#	 gpsbabel -^2 | tr '\t' ';' | sort -t';' -k3
+#
+#############################################################################
+#
+# How to do it
+#   http://www.gdal.org/ogr/drv_gpx.html
+#   gpsbabel [options] -i INTYPE -f INFILE -o OUTTYPE -F OUTFILE
+#
+#############################################################################
+
+#%Module
+#%  description: Import waypoints, routes, and tracks from a GPS receiver or GPS download file into a vector map.
+#%  keywords: vector
+#%  keywords: import
+#%  keywords: GPS
+#%End
+#%flag
+#%  key: w
+#%  description: Import as waypoints
+#%end
+#%flag
+#%  key: r
+#%  description: Import as routes
+#%end
+#%flag
+#%  key: t
+#%  description: Import as tracks
+#%end
+#%flag
+#%  key: p
+#%  description: Force vertices of track or route data as points
+#%end
+#%flag
+#%  key: k
+#%  description: Do not attempt projection transform from WGS84
+#%end
+#%option G_OPT_F_INPUT
+#% description: Device or file used to import data
+#%end
+#%option G_OPT_V_OUTPUT
+#%end
+#%option
+#% key: format
+#% type: string
+#% description: GPSBabel supported output format
+#% answer: gpx
+#%end
+#%option
+#% key: proj
+#% type: string
+#% description: Projection of input data (PROJ.4 style), if not set Lat/Lon WGS84 is assumed
+#% required: no
+#%end
+
+
+
+import sys
+import os
+import atexit
+import string
+import re
+
+import grass.script as grass
+
+#.... todo ....
+
+def main():
+    format = options['format']
+    input = options['input']
+    output = options['output']
+    proj_terms = options['proj']
+    wpt = flags['w']
+    rte = flags['r']
+    trk = flags['t']
+    points_mode = flags['p']
+    no_reproj = flags['k']
+
+    nflags = len(filter(None, [wpt, rte, trk]))
+    if nflags > 1:
+	grass.fatal(_("One feature at a time please."))
+    if nflags < 1:
+	grass.fatal(_("No features requested for import."))
+
+
+    #### check for gpsbabel
+    ### FIXME: may need --help or similar?
+    if not grass.find_program("gpsbabel"):
+	grass.fatal(_("The gpsbabel program was not found, please install it first.\n") +
+		    "http://gpsbabel.sourceforge.net")
+
+    #### check for cs2cs
+    if not grass.find_program("cs2cs"):
+	grass.fatal(_("The cs2cs program was not found, please install it first.\n") +
+		    "http://proj.osgeo.org")
+
+#todo
+#    # check if we will overwrite data
+#    if grass.findfile(output) and not grass.overwrite():
+#	grass.fatal(_("Output file already exists."))
+
+    #### set temporary files
+    tmp = grass.tempfile()
+
+    # import as GPX using v.in.ogr
+#     if trk:
+# 	linetype = "FORCE_GPX_TRACK=YES"
+#     elif rte:
+# 	linetype = "FORCE_GPX_TRACK=YES"
+#     else:
+# 	linetype = None
+
+    if format == 'gpx':
+	# short circuit, we have what we came for.
+#todo
+#	grass.try_remove(output)
+#	os.rename(tmp_gpx, output)
+	grass.verbose("Fast exit.")
+	sys.exit()
+
+    # run gpsbabel
+    if wpt:
+	gtype = '-w'
+    elif trk:
+	gtype = '-t'
+    elif rte:
+	gtype = '-r'
+    else:
+	gtype = ''
+
+    grass.verbose("Running GPSBabel ...")
+
+    ret = grass.call(['gpsbabel',
+		      gtype,
+		      '-i', format,
+		      '-f', output,
+		      '-o', 'gpx',
+		      '-F', tmp + '.gpx')
+
+    if ret != 0:
+	grass.fatal(_("Error running GPSBabel"))
+
+
+    grass.verbose("Importing data ...")
+
+    tmp_gpx = tmp + ".gpx"
+    ret = grass.run_command('v.in.ogr', dsn = tmp_gpx, output = output,
+			    type = type, format = 'GPX', lco = linetype,
+			    dsco = "GPX_USE_EXTENSIONS=YES", quiet = True)
+    if ret != 0:
+	grass.fatal(_("Error importing data"))
+
+
+    #### set up projection info
+    # TODO: check if we are already in ll/WGS84.  If so skip m.proj step.
+
+    # TODO: multi layer will probably fail badly due to sed 's/^ 1   /'
+    #   output as old GRASS 4 vector ascii and fight with dig_ascii/?
+    #   Change to s/^ \([0-9]   .*\)    /# \1/' ??? mmph.
+
+#todo (taken from Glynn's v.out.gps)
+    # reproject to lat/lon WGS84
+#     grass.verbose("Reprojecting data ...")
+# 
+#     re1 = re.compile(r'^\([PLBCFKA]\)')
+#     re2 = re.compile(r'^ 1     ')
+# 
+#     re3 = re.compile(r'\t\([-\.0-9]*\) .*')
+#     re4 = re.compile(r'^\([-\.0-9]\)')
+#     re5 = re.compile(r'^#')
+# 
+#     tmp_proj = tmp + ".proj"
+#     tf = open(tmp_proj, 'w')
+#     p1 = grass.pipe_command('v.out.ascii', input = inmap, format = 'standard')
+#     p2 = grass.feed_command('m.proj', input = '-', flags = 'od', quiet = True, stdout = tf)
+#     tf.close()
+# 
+#     lineno = 0
+#     for line in p1.stdout:
+# 	lineno += 1
+# 	if lineno < 11:
+# 	    continue
+# 	line = re1.sub(r'#\1', line)
+# 	line = re2.sub(r'# 1  ', line)
+# 	p2.stdin.write(line)
+# 
+#     p2.stdin.close()
+#     p1.wait()
+#     p2.wait()
+# 
+#     if p1.returncode != 0 or p2.returncode != 0:
+# 	grass.fatal(_("Error reprojecting data"))
+# 
+#     tmp_vogb = "tmp_vogb_epsg4326_%d" % os.getpid()
+#     p3 = grass.feed_command('v.in.ascii', out = tmp_vogb, format = 'standard', flags = 'n', quiet = True)
+#     tf = open(tmp_proj, 'r')
+# 
+#     for line in tf:
+# 	line = re3.sub(r' \1', line)
+# 	line = re4.sub(r' \1', line)
+# 	line = re5.sub('', line)
+# 	p3.stdin.write(line)
+# 
+#     p3.stdin.close()
+#     tf.close()
+#     p3.wait()
+# 
+#     if p3.returncode != 0:
+# 	grass.fatal(_("Error reprojecting data"))
+
+
+
+    grass.verbose("Done.")
+
+if __name__ == "__main__":
+    options, flags = grass.parser()
+    atexit.register(cleanup)
+    main()


Property changes on: grass/trunk/scripts/v.in.gps/v.in.gps.py
___________________________________________________________________
Added: svn:executable
   + *
Added: svn:mime-type
   + text/x-python
Added: svn:eol-style
   + native

Deleted: grass/trunk/scripts/v.in.gps/v.in.gpsbabel.html
===================================================================
--- grass/trunk/scripts/v.in.gps/v.in.gpsbabel.html	2011-09-05 22:09:25 UTC (rev 48153)
+++ grass/trunk/scripts/v.in.gps/v.in.gpsbabel.html	2011-09-05 22:31:19 UTC (rev 48154)
@@ -1,101 +0,0 @@
-<h2>DESCRIPTION</h2>
-
-<em>v.in.gpsbabel</em> allows the user to import waypoint, route, and track
-data from a locally connected GPS receiver or a text file containing 
-GPS data of many common formats. Translation is done via the 
-<em><a href="http://www.gpsbabel.org">GPSBabel</a></em> program. 
-
-<p>
-This software is not intended as a primary means of navigation.
-
-<h2>NOTES</h2>
-
-<em>v.in.gpsbabel</em> automatically reprojects data using the 
-projection settings of the current location.
-
-The default input data projection is lat/lon WGS84. If your GPS outputs data
-using another projection or map datum, you may include the 
-<em><a href="http://trac.osgeo.org/proj/">PROJ.4</a></em>
-parameters defining your projection in the <b>proj</b> option and 
-<em>v.in.gpsbabel</em> will reproject your data accordingly.
-Great care must be taken to get these parameters correct!
-
-The automatic transform may be skipped by using the <b>-k</b> flag in which 
-case the data will be imported unprojected, as it appears in the <b>input</b>.
-
-<p>
-Route and Track data may be uploaded as a series of points by using the <b>-p</b> 
-flag, otherwise they will be imported as lines. You can run <em>v.in.gpsbabel</em>
-multiple times and merge the line and point vectors with the <em>v.patch</em>
-command if you want, but take care when merging dissimilar attribute tables.
-
-<h2>EXAMPLES</h2>
-
-<h3>GPS device connected via USB adapter</h3>
-
-Import waypoints, tracks, routes from /dev/ttyUSB0 and save
-to a GRASS vector map:
-<div class="code"><pre>
-v.in.gpsbabel -w input=/dev/ttyUSB0 format=garmin output=waypoints
-v.in.gpsbabel -t input=/dev/ttyUSB0 format=garmin output=tracks
-v.in.gpsbabel -r input=/dev/ttyUSB0 format=garmin output=routes
-</pre></div>
-
-<h3>GPS device connected via serial adapter</h3>
-
-Import waypoint data from a Garmin GPS connected at /dev/ttyS0 and save
-to a GRASS vector map named <i>waypoints</i>:
-<div class="code"><pre>
-v.in.gpsbabel -w input=/dev/ttyS0 format=garmin output=waypoints
-</pre></div>
-
-<h3>Import track data from a GPX</h3>
-
-Import track data from a GPX text file and save to a GRASS vector map 
-named <i>tracks</i>.
-<!-- (currently buggy) The gpxlogger program distributed with 
-<a href="http://gpsd.berlios.de/">GPSd</a> >2.30 is a good program for 
-creating these logs. -->
-<div class="code"><pre>
-v.in.gpsbabel -t input=gpslog.gpx format=gpx output=tracks
-</pre></div>
-
-<h3>Import route data from GPS connected at /dev/gps</h3>
-
-Import route data as a series of points from a Garmin GPS connected at 
-/dev/gps and save to a GRASS vector map named <i>routePoints</i>:
-<div class="code"><pre>
-v.in.gpsbabel -r -p file=/dev/gps format=garmin output=routePoints
-</pre></div>
-
-
-<h2>SEE ALSO</h2>
-<em>
-<a href="db.execute.html">db.execute</a>,
-<a href="v.in.ascii.html">v.in.ascii</a>,
-<a href="v.in.garmin.html">v.in.garmin</a>,
-<a href="v.db.connect.html">v.db.connect</a>,
-<a href="v.patch.html">v.patch</a>
-</em>
-
-<a href="http://www.gpsbabel.org">gpsbabel</a> from gpsbabel.org<br>
-cs2cs from <A HREF="http://trac.osgeo.org/proj/">PROJ.4</a>
-
-<h2>AUTHORS</h2>
-Claudio Porta and Lucio Davide Spano, students of Computer Science at
-University of Pisa (Italy).<br>
-Commission from Faunalia Pontedera (PI)<br><br>
-Based on <em>v.in.garmin</em> for GRASS 6.0 by Hamish Bowman<br>
-and <em>v.in.garmin.sh</em> for GRASS 5 by Andreas Lange
-<br>
-
-<!--
-When we tested the script with a Garmin GPS we noticed that importing waypoints 
-the <i>time</i> field is not correctly set. The data reported is 
-a default system time, while with a gpx text file this thing did not 
-happen. We believe that is a <i>gpsbabel</i> trouble in translating 
-from <i>garmin</i> to <i>xcsv</i> ... 
--->
-
-<p>
-<i>Last changed: $Date$</i>



More information about the grass-commit mailing list