[GRASS-SVN] r57798 - grass/branches/releasebranch_6_4/scripts/v.in.gpsbabel
svn_grass at osgeo.org
svn_grass at osgeo.org
Sun Sep 22 01:44:56 PDT 2013
Author: hamish
Date: 2013-09-22 01:44:55 -0700 (Sun, 22 Sep 2013)
New Revision: 57798
Modified:
grass/branches/releasebranch_6_4/scripts/v.in.gpsbabel/v.in.gpsbabel
Log:
sync with devbr6 (big speedup, many improvements)
Modified: grass/branches/releasebranch_6_4/scripts/v.in.gpsbabel/v.in.gpsbabel
===================================================================
--- grass/branches/releasebranch_6_4/scripts/v.in.gpsbabel/v.in.gpsbabel 2013-09-22 08:26:32 UTC (rev 57797)
+++ grass/branches/releasebranch_6_4/scripts/v.in.gpsbabel/v.in.gpsbabel 2013-09-22 08:44:55 UTC (rev 57798)
@@ -26,7 +26,7 @@
# REQUIREMENTS:
# - gpsbabel from:
# http://gpsbabel.sourceforge.net
-# - unix tools: grep, cat, tac, cut, paste, awk/nawk/gawk, sed
+# - unix tools: grep, cat, cut, paste, awk/nawk/gawk, sed
# - cs2cs from PROJ.4 http://proj.maptools.org/
#
# - report supported formats:
@@ -109,10 +109,11 @@
LC_NUMERIC=C
export LC_NUMERIC
-eval `g.gisenv`
-: ${GISBASE?} ${GISDBASE?} ${LOCATION_NAME?} ${MAPSET?}
-LOCATION="$GISDBASE"/"$LOCATION_NAME"/"$MAPSET"
-
+### setup enviro vars ###
+MAPSET=`g.gisenv get=MAPSET`
+LOCATION_NAME=`g.gisenv get=LOCATION_NAME`
+GISDBASE=`g.gisenv get=GISDBASE`
+LOCATION="$GISDBASE/$LOCATION_NAME/$MAPSET"
PROG=`basename "$0"`
#### check for gpsbabel
@@ -135,6 +136,13 @@
exit 1
fi
+if [ `ogr2ogr --formats | grep -cw GPX` -eq 1 ] ; then
+ g.message -v "OGR is GPX capable."
+ OGR_GPX=1
+else
+ OGR_GPX=0
+fi
+
#### set temporary files
TEMPFILE="`g.tempfile pid=$$`"
if [ $? -ne 0 ] || [ -z "$TEMPFILE" ] ; then
@@ -216,7 +224,15 @@
else
IN_PROJ="+proj=longlat +datum=WGS84"
fi
-OUT_PROJ="`g.proj -jf`"
+
+OUT_PROJ=`g.proj -j | (
+ OUT_PROJ=
+ while read line ; do
+ OUT_PROJ="$OUT_PROJ '$line'"
+ done
+ echo "$OUT_PROJ"
+)`
+
PROJ_TYPE=`g.region -p | grep 'projection' | cut -f2 -d" "`
if [ $PROJ_TYPE -eq 0 ] && [ $KEEP_WGS84 -ne 1 ] ; then
@@ -241,7 +257,6 @@
if [ $WPT -eq 1 ] ; then
g.message "Loading Waypoints from <$GIS_OPT_INPUT>..."
TYPE="-w"
- EXITCODE=$?
elif [ $RTE -eq 1 ] ; then
g.message "Loading Routes as points from <$GIS_OPT_INPUT>..."
TYPE="-r"
@@ -264,11 +279,9 @@
if [ $RTE -eq 1 ] ; then
g.message "Loading Routes from <$GIS_OPT_INPUT>..."
TYPE="-r"
- EXITCODE=$?
elif [ $TRK -eq 1 ] ; then
g.message "Loading Tracks from <$GIS_OPT_INPUT>..."
TYPE="-t"
- EXITCODE=$?
fi
gpsbabel $TYPE -i $GIS_OPT_FORMAT -f "$GPSdevice" $PROGVERBOSE \
-o gpx -F "$TEMPFILE".gpx
@@ -310,8 +323,12 @@
cp "${TEMPFILE}.base" "${TEMPFILE}.P_base"
else
g.message "Attempting waypoint projection transform with cs2cs"
- cs2cs -f %.7f $IN_PROJ +to $OUT_PROJ < "${TEMPFILE}.base" > "${TEMPFILE}.P_base"
+
+ eval cs2cs -f %.9f `echo $IN_PROJ` +to `echo $OUT_PROJ` \
+ < "${TEMPFILE}.base" > "${TEMPFILE}.P_base"
+
EXITCODE=$?
+
# check if transform REALLY worked (e.g. when the grid file is not found)
BAD_PTS="`grep -c "^\*" "${TEMPFILE}.P_base"`"
if [ "$BAD_PTS" -gt 0 ] ; then
@@ -365,57 +382,73 @@
#### prepare line components
if [ $RTE -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?
+ #? GPX_USE_EXTENSIONS=YES
+ #? export GPX_USE_EXTENSIONS
+ #v.in.ogr dsn="$TEMPFILE.gpx" out=$map_name
+ 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
@@ -435,12 +468,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"
@@ -450,62 +479,59 @@
ATTR_COLS='cat int, route_id int, name varchar(40), start_wpt varchar(40)'
fi
+
if [ $TRK -eq 1 ] ; then
+ # list of bytes where tracks finish
+ grep -n '</trkseg>' "$TEMPFILE.gpx" > "$TEMPFILE.bytes"
- # list of bytes where tracks finish
- cat "$TEMPFILE.gpx" | grep -n "</trkseg>" > "$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
+ # 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
@@ -530,7 +556,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`"
@@ -538,15 +565,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
@@ -566,10 +587,14 @@
$1!="L" { printf(" %s %s\n", $1, $2) } END {;}' > "${TEMPFILE}.P_base"
else
g.message "Attempting waypoint projection transform with cs2cs"
- cs2cs -tL -f %.7f $IN_PROJ +to $OUT_PROJ < "${TEMPFILE}.base" > "${TEMPFILE}.P_mid"
+
+ eval cs2cs -tL -f %.9f `echo $IN_PROJ` +to `echo $OUT_PROJ` \
+ < "${TEMPFILE}.base" > "${TEMPFILE}.P_mid"
+
EXITCODE=$?
- # check if transform REALLY worked (e.g. grid file not found)
- BAD_PTS="`grep -c "^\*" "${TEMPFILE}.P_mid"`"
+
+ # check if transform REALLY worked (e.g. grid file not found)
+ BAD_PTS="`grep -c "^\*" "${TEMPFILE}.P_mid"`"
if [ "$BAD_PTS" -gt 0 ] ; then
g.message message=""
g.message -w "$BAD_PTS vertices failed reprojection."
@@ -647,19 +672,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