[GRASS-SVN] r52131 - grass/branches/develbranch_6/scripts/v.in.gpsbabel

svn_grass at osgeo.org svn_grass at osgeo.org
Tue Jun 19 02:48:46 PDT 2012


Author: hamish
Date: 2012-06-19 02:48:45 -0700 (Tue, 19 Jun 2012)
New Revision: 52131

Modified:
   grass/branches/develbranch_6/scripts/v.in.gpsbabel/v.in.gpsbabel
Log:
speed it up by about 60%:
  - call db.execute one time instead of once for every attribute;
  - replace temp file writes and reads with backticks;
  - fewer process calls within the loops;
  - fix a ORG -> OGR typo for routes.


Modified: grass/branches/develbranch_6/scripts/v.in.gpsbabel/v.in.gpsbabel
===================================================================
--- grass/branches/develbranch_6/scripts/v.in.gpsbabel/v.in.gpsbabel	2012-06-19 05:23:25 UTC (rev 52130)
+++ grass/branches/develbranch_6/scripts/v.in.gpsbabel/v.in.gpsbabel	2012-06-19 09:48:45 UTC (rev 52131)
@@ -382,7 +382,7 @@
     #### prepare line components
     if [ $RTE -eq 1 ] ; then
 
-	if [ $ORG_GPX -eq 1 ] ; then
+	if [ "$OGR_GPX" -eq 1 ] ; then
 	    # http://www.gdal.org/ogr/drv_gpx.html
 	    # make it really easy ; but need to reproject from WGS84/LL.
 	    #   use '-c location=', then v.proj it in, then rm -rf the temp locn?
@@ -392,57 +392,62 @@
 	    echo
 	fi
 
-
 	# this part is quite difficult using gpx... I'll explain all I've done
 	# if someone has any suggest please mail!!!!
 
 	# list of bytes where routes finish
-	cat "$TEMPFILE.gpx" | grep -n "</rte>" > "$TEMPFILE.bytes"
-	# number of routes
-	cat "$TEMPFILE.bytes" | grep -c "</rte>" > "$TEMPFILE.var"
-	ROUTE_NUMBER=0 
-	read ROUTE_NUMBER < "$TEMPFILE.var" # route number to process
+	grep -n '</rte>' "$TEMPFILE.gpx" > "$TEMPFILE.bytes"
+
+	# number of routes to process
+	ROUTE_NUMBER=`grep -c '</rte>' "$TEMPFILE.bytes"`
 	READ_BYTES=0 # offset of bytes already read
 	cp "$TEMPFILE.gpx" "$TEMPFILE.gpx2" # file to be "eaten" by head commands in while 
 	ROUTE_ID=0 # route identifier
 
 	while [ "$ROUTE_NUMBER" -gt 0 ] ; do
-	    head -n 1 "$TEMPFILE.bytes" | cut -f 1 -d ':'> "$TEMPFILE.var"
-	    END_BYTE=0
-	    read END_BYTE < "$TEMPFILE.var" # this route ends at END_BYTE in $TEMPFILE.gpx file
+	    # this route ends at END_BYTE in $TEMPFILE.gpx file
+	    END_BYTE=`head -n 1 "$TEMPFILE.bytes" | cut -f 1 -d ':'`
 
-	    TO_READ=0
-	    TO_READ="`expr $END_BYTE - $READ_BYTES`"    # bytes to read from $TEMPFILE.gpx2 file 
-	    READ_BYTES="`expr $READ_BYTES + $TO_READ`"  # update readed bytes
+	    TO_READ=`expr $END_BYTE - $READ_BYTES`    # bytes to read from $TEMPFILE.gpx2 file 
+	    READ_BYTES=`expr $READ_BYTES + $TO_READ`  # update readed bytes
 
 	    # list of points in route
-	    head -n $TO_READ "$TEMPFILE.gpx2" | grep "<rtept" > "$TEMPFILE.points" 
-	    POINTS=0 # number of points in route
-	    cat "$TEMPFILE.points" | grep -c "<rtept" > "$TEMPFILE.var"
-	    read POINTS < "$TEMPFILE.var"
+	    head -n $TO_READ "$TEMPFILE.gpx2" | grep '<rtept' > "$TEMPFILE.points" 
+
+	    # number of points in route
+	    POINTS=`grep -c '<rtept' "$TEMPFILE.points"`
+
 	    echo "L $POINTS 1" >> "$TEMPFILE.base"
+
 	    # read lat lon data
-	    cat "$TEMPFILE.points" | cut -f2 -d'<' | cut -f2,3 -d ' ' | cut -f2,4 -d '"' | tr '"' '\t' > "$TEMPFILE.latlon"
-	    cat "$TEMPFILE.latlon" | tr ',' '.' | awk '{printf(" %s %s\n", $2, $1) }' >> "$TEMPFILE.base"
+	    cut -f2 -d'<' "$TEMPFILE.points" | cut -f2,3 -d ' ' | \
+	       cut -f2,4 -d '"' | tr '"' '\t' > "$TEMPFILE.latlon"
+	    tr ',' '.' < "$TEMPFILE.latlon" | \
+	       awk '{printf(" %s %s\n", $2, $1) }' >> "$TEMPFILE.base"
+
 	    # create attribute line
-	    head -n $TO_READ "$TEMPFILE.gpx2" | grep -n "<rtept" | head -n 1 | cut -f1 -d ':' > "$TEMPFILE.var"
-	    OFFSET=0
-	    read OFFSET < "$TEMPFILE.var"
+	    OFFSET=`head -n $TO_READ "$TEMPFILE.gpx2" | grep -n '<rtept' | \
+	       head -n 1 | cut -f1 -d ':'`
+
 	    head -n $OFFSET "$TEMPFILE.gpx2" > "$TEMPFILE.rte_attr"
+
 	    # read needed attributes
 	    ROUTE_ID="`expr $ROUTE_ID + 1`"
-	    cat "$TEMPFILE.rte_attr"| grep "<number" | tail -n 1 | cut -f2 -d'<' | cut -f2 -d'>' > "$TEMPFILE.var"
-	    NUMBER=0
-	    read NUMBER < "$TEMPFILE.var" # read the route number
-	    cat "$TEMPFILE.rte_attr"| grep "<name" | tail -n 1 | cut -f2 -d'<' | cut -f2 -d'>' > "$TEMPFILE.var"
-	    R_NAME=""
-	    read R_NAME < "$TEMPFILE.var" # read the route name
 
+	    # read the route number
+	    NUMBER=`grep '<number' "$TEMPFILE.rte_attr" | tail -n 1 | cut -f2 -d'<' | \
+	      cut -f2 -d'>'`
+
+	    # read the route name
+	    R_NAME=`grep '<name' "$TEMPFILE.rte_attr" | tail -n 1 | cut -f2 -d'<' | \
+	       cut -f2 -d'>'`
+
 	    OFFSET="`expr $TO_READ - $OFFSET`"
-	    head -n $TO_READ "$TEMPFILE.gpx2" | tail -n $OFFSET | grep "<name" | head -n 1 | cut -f2 -d'<' | cut -f2 -d'>' > "$TEMPFILE.var"
-	    START_PNT=""
-	    read START_PNT < "$TEMPFILE.var" # read the name of start point
 
+	    # read the name of start point
+	    START_PNT=`head -n $TO_READ "$TEMPFILE.gpx2" | tail -n $OFFSET | grep '<name' | \
+	       head -n 1 | cut -f2 -d'<' | cut -f2 -d'>'`
+
 	    # check that numberic values don't try and pass an empty string
 ### variable names don't line up with column names ?!
 	    if [ -z "$ROUTE_ID" ] ; then
@@ -462,12 +467,8 @@
 	    tail -n $ROUTE_NUMBER "$TEMPFILE.bytes" > "$TEMPFILE.bytes2"
 	    mv "$TEMPFILE.bytes2" "$TEMPFILE.bytes"
 	    head -n $TO_READ "$TEMPFILE.gpx2" > "$TEMPFILE.points"
-	    B_GPX=0
-	    wc -c < "$TEMPFILE.gpx2" > "$TEMPFILE.var"
-	    read B_GPX < "$TEMPFILE.var"
-	    B_PNT=0
-	    wc -c < "$TEMPFILE.points" > "$TEMPFILE.var"
-	    read B_PNT < "$TEMPFILE.var"
+	    B_GPX=`wc -c < "$TEMPFILE.gpx2"`
+	    B_PNT=`wc -c < "$TEMPFILE.points"`
 	    TO_READ="`expr $B_GPX - $B_PNT`"
 	    tail -c $TO_READ "$TEMPFILE.gpx2" > "$TEMPFILE.points"
 	    mv "$TEMPFILE.points" "$TEMPFILE.gpx2"
@@ -477,62 +478,59 @@
 	ATTR_COLS='cat int, route_id int, name varchar(40), start_wpt varchar(40)'
     fi
 
-    if [ $TRK -eq 1 ] ; then
 
+    if [ $TRK -eq 1 ] ; then
 	# list of bytes where tracks finish
-	cat "$TEMPFILE.gpx" | grep -n "</trkseg>" > "$TEMPFILE.bytes"
+	grep -n '</trkseg>' "$TEMPFILE.gpx" > "$TEMPFILE.bytes"
+
 	# number of tracks
-	cat "$TEMPFILE.bytes" | grep -c "</trkseg>" > "$TEMPFILE.var"
-	TRACK_NUMBER=0 
-	read TRACK_NUMBER < "$TEMPFILE.var" # route number to process
+	TRACK_NUMBER=`grep -c '</trkseg>' "$TEMPFILE.bytes"`
+
 	READ_BYTES=0 # offset of bytes already read
+
 	cp "$TEMPFILE.gpx" "$TEMPFILE.gpx2" # file to be "eaten" by head commands in while 
+
 	TRACK_ID=0 # track identifier
+
 	while [ "$TRACK_NUMBER" -gt 0 ] ; do
-	    head -n 1 "$TEMPFILE.bytes" | cut -f 1 -d ':'> "$TEMPFILE.var"
-	    END_BYTE=0
-	    read END_BYTE < "$TEMPFILE.var" # this route ends at END_BYTE in $TEMPFILE.gpx file
-	    TO_READ=0
+	    # this track ends at END_BYTE in the $TEMPFILE.gpx file
+	    END_BYTE=`head -n 1 "$TEMPFILE.bytes" | cut -f1 -d':'`
 
+	    TO_READ=0
 	    TO_READ="`expr $END_BYTE - $READ_BYTES`"    # bytes to read from $TEMPFILE.gpx2 file 
 	    READ_BYTES="`expr $READ_BYTES + $TO_READ`"  # update readed bytes
 
 	    # list of points in route
-	    head -n $TO_READ "$TEMPFILE.gpx2" | grep "<trkpt" > "$TEMPFILE.points" 
-	    POINTS=0 # number of points in track 
-	    cat "$TEMPFILE.points" | grep -c "<trkpt" > "$TEMPFILE.var"
-	    read POINTS < "$TEMPFILE.var"
+	    head -n $TO_READ "$TEMPFILE.gpx2" | grep '<trkpt' > "$TEMPFILE.points" 
+
+	    # number of points in track
+	    POINTS=`grep -c '<trkpt' "$TEMPFILE.points"`
+
 	    echo "L $POINTS 1" >> "$TEMPFILE.base"
+
 	    # read lat lon data
-	    cat "$TEMPFILE.points" | cut -f2 -d'<' | cut -f2,3 -d ' ' | cut -f2,4 -d '"' | tr '"' '\t' > "$TEMPFILE.latlon"
-	    cat "$TEMPFILE.latlon" | tr ',' '.' | awk '{printf(" %s %s\n", $2, $1) }' >> "$TEMPFILE.base"
+	    cut -f2 -d'<' "$TEMPFILE.points" | cut -f2,3 -d ' ' | \
+	      cut -f2,4 -d '"' | tr '"' '\t' > "$TEMPFILE.latlon"
+	    tr ',' '.' < "$TEMPFILE.latlon" | \
+	      awk '{printf(" %s %s\n", $2, $1) }' >> "$TEMPFILE.base"
+
 	    # create attribute line
-	    head -n $TO_READ "$TEMPFILE.gpx2" | grep -n "<trk" | head -n 1 | cut -f1 -d ':' > "$TEMPFILE.var"
-	    OFFSET=0
-	    read OFFSET < "$TEMPFILE.var"
-	    S_LAT=""
-	    head -n 1 "$TEMPFILE.latlon" | cut -f1 > "$TEMPFILE.var"
-	    read S_LAT < "$TEMPFILE.var"
-	    S_LON=""
-	    head -n 1 "$TEMPFILE.latlon" | cut -f2 > "$TEMPFILE.var"
-	    read S_LON < "$TEMPFILE.var"
-	    E_LAT=""
-	    tail -n 1 "$TEMPFILE.latlon" | cut -f1 > "$TEMPFILE.var"
-	    read E_LAT < "$TEMPFILE.var"
-	    E_LON=""
-	    tail -n 1 "$TEMPFILE.latlon" | cut -f2 > "$TEMPFILE.var"
-	    read E_LON < "$TEMPFILE.var"
+	    OFFSET=`head -n $TO_READ "$TEMPFILE.gpx2" | grep -n '<trk' | \
+	       head -n 1 | cut -f1 -d ':'`
 
+	    S_LAT=`head -n 1 "$TEMPFILE.latlon" | cut -f1`
+	    S_LON=`head -n 1 "$TEMPFILE.latlon" | cut -f2`
+	    E_LAT=`tail -n 1 "$TEMPFILE.latlon" | cut -f1`
+	    E_LON=`tail -n 1 "$TEMPFILE.latlon" | cut -f2`
+
 	    OFFSET="`expr $TO_READ - $OFFSET`"
 
-	    head -n $TO_READ "$TEMPFILE.gpx2" | tail -n $OFFSET | grep "<time" > "$TEMPFILE.time"
-	    S_TIME=""
-	    head -n 1 "$TEMPFILE.time" | cut -f 2 -d'>' | cut -f1 -d'<' > "$TEMPFILE.var"
-	    read S_TIME < "$TEMPFILE.var"
-	    E_TIME=""
-	    tail -n 1 "$TEMPFILE.time" | cut -f 2 -d'>' | cut -f1 -d'<' > "$TEMPFILE.var"
-	    read E_TIME < "$TEMPFILE.var"
+	    head -n $TO_READ "$TEMPFILE.gpx2" | tail -n $OFFSET | \
+	       grep '<time' > "$TEMPFILE.time"
 
+	    S_TIME=`head -n 1 "$TEMPFILE.time" | cut -f 2 -d'>' | cut -f1 -d'<'`
+	    E_TIME=`tail -n 1 "$TEMPFILE.time" | cut -f 2 -d'>' | cut -f1 -d'<'`
+
 	    TRACK_ID="`expr $TRACK_ID + 1`"
 
 	    # check that numberic values don't try and pass an empty string
@@ -557,7 +555,8 @@
 		g.message -w "Track $TRACK_NUMBER:  ending lon was empty. Bug?"
 	    fi
 
-	    echo "$TRACK_ID|$S_TIME|$S_LAT|$S_LON|$E_TIME|$E_LAT|$E_LON" >> "$TEMPFILE.track_atts"
+	    echo "$TRACK_ID|$S_TIME|$S_LAT|$S_LON|$E_TIME|$E_LAT|$E_LON" \
+	       >> "$TEMPFILE.track_atts"
 
 	    TRACK_NUMBER="`expr $TRACK_NUMBER - 1`"
 
@@ -565,15 +564,9 @@
 	    tail -n $TRACK_NUMBER "$TEMPFILE.bytes" > "$TEMPFILE.bytes2"
 	    mv "$TEMPFILE.bytes2" "$TEMPFILE.bytes"
 	    head -n $TO_READ "$TEMPFILE.gpx2" > "$TEMPFILE.points"
-	    B_GPX=0
-	    wc -c < "$TEMPFILE.gpx2" > "$TEMPFILE.var"
-	    read B_GPX < "$TEMPFILE.var"
-	    B_PNT=0
-	    wc -c < "$TEMPFILE.points" > "$TEMPFILE.var"
-	    read B_PNT < "$TEMPFILE.var"
-
+	    B_GPX=`wc -c < "$TEMPFILE.gpx2"`
+	    B_PNT=`wc -c < "$TEMPFILE.points"`
 	    TO_READ="`expr $B_GPX - $B_PNT`"
-	    
 	    tail -c $TO_READ "$TEMPFILE.gpx2" > "$TEMPFILE.points"
 	    mv "$TEMPFILE.points" "$TEMPFILE.gpx2"
 	done
@@ -678,19 +671,21 @@
 	if [ $RTE -eq 1 ] ; then
 	    # =>  echo "INSERT INTO $NAME VALUES (2, 1, 'AB', 'B')" | db.execute
 	    cat "$ATTR_FILE" | \
-	      awk -F'|' '{
-		printf("echo \"INSERT INTO $NAME VALUES (%d, %d, ^%s^, ^%s^)\" | db.execute\n", $1, $2, $3, $4) }' \
-		    | tr '^' \' > "$ATTR_FILE"_exe
+	      awk -F'|' -v DBNAME="$NAME" '{
+		printf("INSERT INTO %s VALUES (%d, %d, ^%s^, ^%s^);\n",
+			DBNAME, $1, $2, $3, $4) }' \
+		    | tr '^' \' > "$ATTR_FILE".sql
 	fi
 	if [ $TRK -eq 1 ] ; then
 	    # =>  echo "INSERT INTO $NAME VALUES ()" | db.execute
 	    cat "$ATTR_FILE" | \
-	      awk -F'|' '{
-		printf("echo \"INSERT INTO $NAME VALUES (%d, ^%s^, %s, %s, ^%s^, %s, %s)\" | db.execute\n", $1, $2, $3, $4, $5, $6, $7) }' \
-		    | tr '^' \' > "$ATTR_FILE"_exe
+	      awk -F'|' -v DBNAME="$NAME" '{
+		printf("INSERT INTO %s VALUES (%d, ^%s^, %s, %s, ^%s^, %s, %s);\n",
+			DBNAME, $1, $2, $3, $4, $5, $6, $7) }' \
+		    | tr '^' \' > "$ATTR_FILE".sql
 	fi
 
-	. "$ATTR_FILE"_exe
+	db.execute input="$ATTR_FILE".sql
 ### </hack>
 
 	g.message "Connecting attribute table to vector file..."



More information about the grass-commit mailing list