v.in.gpsbabel 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  1. #!/bin/sh
  2. #
  3. ############################################################################
  4. #
  5. # MODULE: v.in.gpsbabel
  6. #
  7. # PURPOSE: Import GPS data from a GPS receiver or file into a GRASS
  8. # vector map using gpsbabel
  9. #
  10. # COPYRIGHT: (c) 2000-2007 the GRASS Development Team
  11. # This program is free software under the GNU General Public
  12. # License (>=v2). Read the file COPYING that comes with GRASS
  13. # for details.
  14. #
  15. # AUTHORS: Claudio Porta & Lucio Davide Spano, students of Computer
  16. # Science, University of Pisa (Italy).
  17. # Commission from Faunalia Pontedera (PI)
  18. #
  19. # With updates by Markus Neteler and Hamish Bowman
  20. #
  21. # Based on v.in.garmin for GRASS 6 by Hamish Bowman
  22. # and v.in.garmin.sh for GRASS 5 by Andreas Lange
  23. #
  24. #############################################################################
  25. #
  26. # REQUIREMENTS:
  27. # - gpsbabel from:
  28. # http://gpsbabel.sourceforge.net
  29. # - unix tools: grep, cat, cut, paste, awk/nawk/gawk, sed
  30. # - cs2cs from PROJ.4 http://proj.maptools.org/
  31. #
  32. # - report supported formats:
  33. # gpsbabel -^2 | tr '\t' ';' | sort -t';' -k3
  34. #
  35. #############################################################################
  36. #%Module
  37. #% description: Import waypoints, routes, and tracks from a GPS receiver or GPS download file into a vector map.
  38. #% keywords: vector, import, GPS
  39. #%End
  40. #%flag
  41. #% key: v
  42. #% description: Verbose mode
  43. #%end
  44. #%flag
  45. #% key: w
  46. #% description: Import waypoints
  47. #%end
  48. #%flag
  49. #% key: r
  50. #% description: Import routes
  51. #%end
  52. #%flag
  53. #% key: t
  54. #% description: Import track
  55. #%end
  56. #%flag
  57. #% key: p
  58. #% description: Force vertices of track or route data as points
  59. #%end
  60. #%flag
  61. #% key: k
  62. #% description: Do not attempt projection transform from WGS84
  63. #%end
  64. #%option
  65. #% key: input
  66. #% type: string
  67. #% description: Device or file used to import data
  68. #% gisprompt: old_file,file,file
  69. #% answer: /dev/gps
  70. #%end
  71. #%option
  72. #% key: output
  73. #% type: string
  74. #% gisprompt: new,vector,vector
  75. #% description: Name for output vector map (omit for display to stdout)
  76. #% required : no
  77. #%end
  78. #%option
  79. #% key: format
  80. #% type: string
  81. #% description: Format of GPS input data (use gpsbabel supported formats)
  82. #% answer: garmin
  83. #%end
  84. #%option
  85. #% key: proj
  86. #% type: string
  87. #% description: Projection of input data (PROJ.4 style), if not set Lat/Lon WGS84 is assumed
  88. #% required: no
  89. #%end
  90. if [ -z "$GISBASE" ] ; then
  91. echo "You must be in GRASS GIS to run this program." 1>&2
  92. exit 1
  93. fi
  94. if [ "$1" != "@ARGS_PARSED@" ] ; then
  95. # save command line
  96. CMDLINE=`basename "$0"`
  97. for arg in "$@" ; do
  98. CMDLINE="$CMDLINE \"$arg\""
  99. done
  100. export CMDLINE
  101. exec g.parser "$0" "$@"
  102. fi
  103. # set environment so that awk works properly in all languages
  104. unset LC_ALL
  105. LC_NUMERIC=C
  106. export LC_NUMERIC
  107. eval `g.gisenv`
  108. : ${GISBASE?} ${GISDBASE?} ${LOCATION_NAME?} ${MAPSET?}
  109. LOCATION="$GISDBASE"/"$LOCATION_NAME"/"$MAPSET"
  110. PROG=`basename "$0"`
  111. #### check for gpsbabel
  112. if [ ! -x "`which gpsbabel`" ] ; then
  113. g.message -e "The gpsbabel program was not found, please install it first.
  114. http://gpsbabel.sourceforge.net"
  115. exit 1
  116. fi
  117. #### check for cs2cs
  118. if [ ! -x "`which cs2cs`" ] ; then
  119. g.message -e "The cs2cs program was not found, please install it first.
  120. http://proj.maptools.org"
  121. exit 1
  122. fi
  123. #### check if we have awk
  124. if [ ! -x "`which awk`" ] ; then
  125. g.message -e "awk is required, please install awk or gawk first."
  126. exit 1
  127. fi
  128. if [ `ogr2ogr --formats | grep -cw GPX` -eq 1 ] ; then
  129. g.message -v "OGR is GPX capable."
  130. OGR_GPX=1
  131. else
  132. OGR_GPX=0
  133. fi
  134. #### set temporary files
  135. TEMPFILE="`g.tempfile pid=$$`"
  136. if [ $? -ne 0 ] || [ -z "$TEMPFILE" ] ; then
  137. g.message -e "Unable to create temporary files"
  138. exit 1
  139. fi
  140. #### trap ctrl-c so that we can clean up tmp
  141. trap 'rm -f "${TEMPFILE}."*' 2 3 15
  142. #### process command line arguments
  143. WPT=1 ; RTE=0 ; TRK=0 ; KEEP_WGS84=0; VERBOSE=0
  144. if [ $GIS_FLAG_V -eq 1 ] ; then
  145. VERBOSE=1
  146. PROGVERBOSE="-D 1"
  147. fi
  148. if [ -n "$GIS_OPT_OUTPUT" ] ; then
  149. NAME="$GIS_OPT_OUTPUT"
  150. if [ $VERBOSE -eq 1 ] ; then
  151. g.message message="output=$NAME"
  152. fi
  153. fi
  154. if [ -n "$GIS_OPT_INPUT" ] ; then
  155. GPSdevice="$GIS_OPT_INPUT"
  156. if [ $VERBOSE -eq 1 ] ; then
  157. g.message message="file=$GIS_OPT_INPUT"
  158. fi
  159. fi
  160. if [ $GIS_FLAG_W -eq 1 ] && ([ $GIS_FLAG_R -eq 1 ] || [ $GIS_FLAG_T -eq 1 ]) ; then
  161. g.message -e "One feature at a time please. Use v.patch if you need to combine them."
  162. rm -f "${TEMPFILE}"
  163. exit 1
  164. fi
  165. # logic eludes me at pressent.. [combine with above]
  166. if [ $GIS_FLAG_R -eq 1 ] && [ $GIS_FLAG_T -eq 1 ] ; then
  167. g.message -e "One feature at a time please. Use v.patch if you need to combine them."
  168. rm -f "${TEMPFILE}"
  169. exit 1
  170. fi
  171. CREATE_POINTS=0
  172. if [ $GIS_FLAG_W -eq 1 ] ; then
  173. WPT=1
  174. CREATE_POINTS=1
  175. elif [ $GIS_FLAG_R -eq 1 ] ; then
  176. WPT=0
  177. RTE=1
  178. CREATE_POINTS=0
  179. elif [ $GIS_FLAG_T -eq 1 ] ; then
  180. WPT=0
  181. TRK=1
  182. CREATE_POINTS=0
  183. fi
  184. if [ $GIS_FLAG_P -eq 1 ] ; then
  185. CREATE_POINTS=1
  186. fi
  187. if [ $GIS_FLAG_K -eq 1 ] ; then
  188. KEEP_WGS84=1
  189. fi
  190. if [ $GIS_FLAG_W -eq 0 ] && [ $GIS_FLAG_R -eq 0 ] && [ $GIS_FLAG_T -eq 0 ] ; then
  191. g.message -w "No features requested for download. Assuming waypoints."
  192. WPT=1
  193. CREATE_POINTS=1
  194. fi
  195. #### set up projection info
  196. if [ -n "$GIS_OPT_PROJ" ] ; then
  197. IN_PROJ="$GIS_OPT_PROJ"
  198. else
  199. IN_PROJ="+proj=longlat +datum=WGS84"
  200. fi
  201. OUT_PROJ=`g.proj -j | (
  202. OUT_PROJ=
  203. while read line ; do
  204. OUT_PROJ="$OUT_PROJ '$line'"
  205. done
  206. echo "$OUT_PROJ"
  207. )`
  208. PROJ_TYPE=`g.region -p | grep 'projection' | cut -f2 -d" "`
  209. if [ $PROJ_TYPE -eq 0 ] && [ $KEEP_WGS84 -ne 1 ] ; then
  210. g.message -e "Cannot project to a XY location."
  211. rm -f "${TEMPFILE}"
  212. exit 1
  213. fi
  214. STYLE="$GISBASE/etc/grass_write_ascii.style"
  215. # How to do it
  216. #
  217. # gpsbabel [options] -i INTYPE -f INFILE -o OUTTYPE -F OUTFILE
  218. # gpsbabel [options] -i INTYPE -o OUTTYPE INFILE [OUTFILE]
  219. #
  220. #(GPX file example):
  221. # gpsbabel -w -i gpx -o xcsv,style=grass_write_ascii.style outfile.csv
  222. #### receive data...
  223. if [ $CREATE_POINTS -eq 1 ] ; then
  224. if [ $WPT -eq 1 ] ; then
  225. g.message "Loading Waypoints from <$GIS_OPT_INPUT>..."
  226. TYPE="-w"
  227. elif [ $RTE -eq 1 ] ; then
  228. g.message "Loading Routes as points from <$GIS_OPT_INPUT>..."
  229. TYPE="-r"
  230. elif [ $TRK -eq 1 ] ; then
  231. g.message "Loading Tracks as points from <$GIS_OPT_INPUT>..."
  232. TYPE="-t"
  233. fi
  234. gpsbabel $TYPE -i $GIS_OPT_FORMAT -f "$GPSdevice" $PROGVERBOSE \
  235. -o xcsv,style="$STYLE" -F "$TEMPFILE".xcsv
  236. EXITCODE=$?
  237. if [ "`wc -l < "${TEMPFILE}.xcsv"`" -eq 0 ] ; then
  238. g.message -w 'No data! Exiting.'
  239. rm -f "${TEMPFILE}."*
  240. exit 0
  241. fi
  242. else
  243. if [ $RTE -eq 1 ] ; then
  244. g.message "Loading Routes from <$GIS_OPT_INPUT>..."
  245. TYPE="-r"
  246. elif [ $TRK -eq 1 ] ; then
  247. g.message "Loading Tracks from <$GIS_OPT_INPUT>..."
  248. TYPE="-t"
  249. fi
  250. gpsbabel $TYPE -i $GIS_OPT_FORMAT -f "$GPSdevice" $PROGVERBOSE \
  251. -o gpx -F "$TEMPFILE".gpx
  252. EXITCODE=$?
  253. if [ $EXITCODE -eq 0 ] && \
  254. [ "`grep -c '<rte>' "${TEMPFILE}.gpx"`" -eq 0 ] && \
  255. [ "`grep -c '<trk>' "${TEMPFILE}.gpx"`" -eq 0 ] ; then
  256. g.message -e 'No data! Exiting.'
  257. rm -f "${TEMPFILE}."*
  258. exit 0
  259. fi
  260. fi
  261. #### check success/failure
  262. if [ $EXITCODE -ne 0 ] ; then
  263. g.message -e "Error loading data from gpsbabel"
  264. rm -f "${TEMPFILE}."*
  265. exit 1
  266. fi
  267. ###################################
  268. if [ $CREATE_POINTS -eq 1 ] ; then
  269. ###################################
  270. cat "${TEMPFILE}.xcsv" | cut -f 1,2 -d '|'| tr '|' ' ' > "$TEMPFILE".base
  271. # FIXME: if last field (comments) is empty it causes a not-enough fields error in v.in.ascii
  272. # FIXME: if altitude column is empty (1st attr) v.in.ascii complains as the column type is defined as 'double'
  273. cat "${TEMPFILE}.xcsv" | tr '+' '|' | cut -f3,4,5,6 -d '|' | \
  274. sed -e 's/-99999999.000000//' -e 's/|$/|_/' \
  275. -e 's/01\/01\/1970|00:00:00 AM/|/' \
  276. -e 's/^|/-9999|/'> "$TEMPFILE".attributes
  277. #### reproject if needed
  278. if [ "$IN_PROJ" = "$OUT_PROJ" ] || [ $KEEP_WGS84 -eq 1 ] ; then
  279. g.message "No projection transformation performed"
  280. cp "${TEMPFILE}.base" "${TEMPFILE}.P_base"
  281. else
  282. g.message "Attempting waypoint projection transform with cs2cs"
  283. eval cs2cs -f %.9f `echo $IN_PROJ` +to `echo $OUT_PROJ` \
  284. < "${TEMPFILE}.base" > "${TEMPFILE}.P_base"
  285. EXITCODE=$?
  286. # check if transform REALLY worked (e.g. when the grid file is not found)
  287. BAD_PTS="`grep -c "^\*" "${TEMPFILE}.P_base"`"
  288. if [ "$BAD_PTS" -gt 0 ] ; then
  289. g.message message=""
  290. g.message -w "$BAD_PTS point(s) failed reprojection."
  291. EXITCODE=1
  292. fi
  293. if [ $EXITCODE -ne 0 ] ; then
  294. g.message -w "Projection transform failed, retaining WGS84"
  295. g.message message=""
  296. cp -f "${TEMPFILE}.base" "${TEMPFILE}.P_base"
  297. fi
  298. fi
  299. cat "${TEMPFILE}.P_base" | awk '{print $1 "|" $2}' > "${TEMPFILE}.vertices"
  300. #### and put back together
  301. # wpt list: x|y|<attr1>|...|<attr n>
  302. paste -d"|" "$TEMPFILE".vertices "$TEMPFILE".attributes > "$TEMPFILE".asc
  303. #### output or import
  304. if [ -z "$NAME" ] ; then
  305. g.message "ASCII file redirected to stdout"
  306. cat "${TEMPFILE}.asc" 2> /dev/null
  307. else
  308. #### import into new points file
  309. if [ $WPT -eq 1 ] ; then
  310. g.message "Importing Waypoints..."
  311. elif [ $RTE -eq 1 ] ; then
  312. g.message "Importing Routes as points..."
  313. elif [ $TRK -eq 1 ] ; then
  314. g.message "Importing Tracks as points..."
  315. fi
  316. v.in.ascii in="${TEMPFILE}.asc" output="$NAME" cat=0 \
  317. columns='x double precision, y double precision, altitude double precision, gmt_date varchar(10), gmt_time varchar(11), comments varchar(40)'
  318. EXITCODE=$?
  319. # EXITCODE also used at pgm termination below!
  320. if [ $EXITCODE -ne 0 ] ; then
  321. g.message -e "While Importing data with v.in.ascii"
  322. rm -f "${TEMPFILE}"*
  323. exit 1
  324. fi
  325. fi
  326. #####################
  327. else # CREATE_LINES
  328. #####################
  329. #### prepare line components
  330. if [ $RTE -eq 1 ] ; then
  331. if [ $ORG_GPX -eq 1 ] ; then
  332. # http://www.gdal.org/ogr/drv_gpx.html
  333. # make it really easy ; but need to reproject from WGS84/LL.
  334. # use '-c location=', then v.proj it in, then rm -rf the temp locn?
  335. #? GPX_USE_EXTENSIONS=YES
  336. #? export GPX_USE_EXTENSIONS
  337. #v.in.ogr dsn="$TEMPFILE.gpx" out=$map_name
  338. echo
  339. fi
  340. # this part is quite difficult using gpx... I'll explain all I've done
  341. # if someone has any suggest please mail!!!!
  342. # list of bytes where routes finish
  343. cat "$TEMPFILE.gpx" | grep -n "</rte>" > "$TEMPFILE.bytes"
  344. # number of routes
  345. cat "$TEMPFILE.bytes" | grep -c "</rte>" > "$TEMPFILE.var"
  346. ROUTE_NUMBER=0
  347. read ROUTE_NUMBER < "$TEMPFILE.var" # route number to process
  348. READ_BYTES=0 # offset of bytes already read
  349. cp "$TEMPFILE.gpx" "$TEMPFILE.gpx2" # file to be "eaten" by head commands in while
  350. ROUTE_ID=0 # route identifier
  351. while [ "$ROUTE_NUMBER" -gt 0 ] ; do
  352. head -n 1 "$TEMPFILE.bytes" | cut -f 1 -d ':'> "$TEMPFILE.var"
  353. END_BYTE=0
  354. read END_BYTE < "$TEMPFILE.var" # this route ends at END_BYTE in $TEMPFILE.gpx file
  355. TO_READ=0
  356. TO_READ="`expr $END_BYTE - $READ_BYTES`" # bytes to read from $TEMPFILE.gpx2 file
  357. READ_BYTES="`expr $READ_BYTES + $TO_READ`" # update readed bytes
  358. # list of points in route
  359. head -n $TO_READ "$TEMPFILE.gpx2" | grep "<rtept" > "$TEMPFILE.points"
  360. POINTS=0 # number of points in route
  361. cat "$TEMPFILE.points" | grep -c "<rtept" > "$TEMPFILE.var"
  362. read POINTS < "$TEMPFILE.var"
  363. echo "L $POINTS 1" >> "$TEMPFILE.base"
  364. # read lat lon data
  365. cat "$TEMPFILE.points" | cut -f2 -d'<' | cut -f2,3 -d ' ' | cut -f2,4 -d '"' | tr '"' '\t' > "$TEMPFILE.latlon"
  366. cat "$TEMPFILE.latlon" | tr ',' '.' | awk '{printf(" %s %s\n", $2, $1) }' >> "$TEMPFILE.base"
  367. # create attribute line
  368. head -n $TO_READ "$TEMPFILE.gpx2" | grep -n "<rtept" | head -n 1 | cut -f1 -d ':' > "$TEMPFILE.var"
  369. OFFSET=0
  370. read OFFSET < "$TEMPFILE.var"
  371. head -n $OFFSET "$TEMPFILE.gpx2" > "$TEMPFILE.rte_attr"
  372. # read needed attributes
  373. ROUTE_ID="`expr $ROUTE_ID + 1`"
  374. cat "$TEMPFILE.rte_attr"| grep "<number" | tail -n 1 | cut -f2 -d'<' | cut -f2 -d'>' > "$TEMPFILE.var"
  375. NUMBER=0
  376. read NUMBER < "$TEMPFILE.var" # read the route number
  377. cat "$TEMPFILE.rte_attr"| grep "<name" | tail -n 1 | cut -f2 -d'<' | cut -f2 -d'>' > "$TEMPFILE.var"
  378. R_NAME=""
  379. read R_NAME < "$TEMPFILE.var" # read the route name
  380. OFFSET="`expr $TO_READ - $OFFSET`"
  381. head -n $TO_READ "$TEMPFILE.gpx2" | tail -n $OFFSET | grep "<name" | head -n 1 | cut -f2 -d'<' | cut -f2 -d'>' > "$TEMPFILE.var"
  382. START_PNT=""
  383. read START_PNT < "$TEMPFILE.var" # read the name of start point
  384. # check that numberic values don't try and pass an empty string
  385. ### variable names don't line up with column names ?!
  386. if [ -z "$ROUTE_ID" ] ; then
  387. ROUTE_ID="NULL"
  388. g.message -w "Route $ROUTE_NUMBER: category number was empty. Bug?"
  389. fi
  390. if [ -z "$NUMBER" ] ; then
  391. NUMBER="NULL"
  392. g.message -w "Route $ROUTE_NUMBER: route ID was empty. Bug?"
  393. fi
  394. echo "$ROUTE_ID|$NUMBER|$R_NAME|$START_PNT" >> "$TEMPFILE.route_atts"
  395. ROUTE_NUMBER="`expr $ROUTE_NUMBER - 1`"
  396. # eat files
  397. tail -n $ROUTE_NUMBER "$TEMPFILE.bytes" > "$TEMPFILE.bytes2"
  398. mv "$TEMPFILE.bytes2" "$TEMPFILE.bytes"
  399. head -n $TO_READ "$TEMPFILE.gpx2" > "$TEMPFILE.points"
  400. B_GPX=0
  401. wc -c < "$TEMPFILE.gpx2" > "$TEMPFILE.var"
  402. read B_GPX < "$TEMPFILE.var"
  403. B_PNT=0
  404. wc -c < "$TEMPFILE.points" > "$TEMPFILE.var"
  405. read B_PNT < "$TEMPFILE.var"
  406. TO_READ="`expr $B_GPX - $B_PNT`"
  407. tail -c $TO_READ "$TEMPFILE.gpx2" > "$TEMPFILE.points"
  408. mv "$TEMPFILE.points" "$TEMPFILE.gpx2"
  409. done
  410. # create attr table: cat(int), id number(int 0-19), name varchar(16+), starting_wpt(varchar 10)
  411. ATTR_FILE="${TEMPFILE}.route_atts"
  412. ATTR_COLS='cat int, route_id int, name varchar(40), start_wpt varchar(40)'
  413. fi
  414. if [ $TRK -eq 1 ] ; then
  415. # list of bytes where tracks finish
  416. cat "$TEMPFILE.gpx" | grep -n "</trkseg>" > "$TEMPFILE.bytes"
  417. # number of tracks
  418. cat "$TEMPFILE.bytes" | grep -c "</trkseg>" > "$TEMPFILE.var"
  419. TRACK_NUMBER=0
  420. read TRACK_NUMBER < "$TEMPFILE.var" # route number to process
  421. READ_BYTES=0 # offset of bytes already read
  422. cp "$TEMPFILE.gpx" "$TEMPFILE.gpx2" # file to be "eaten" by head commands in while
  423. TRACK_ID=0 # track identifier
  424. while [ "$TRACK_NUMBER" -gt 0 ] ; do
  425. head -n 1 "$TEMPFILE.bytes" | cut -f 1 -d ':'> "$TEMPFILE.var"
  426. END_BYTE=0
  427. read END_BYTE < "$TEMPFILE.var" # this route ends at END_BYTE in $TEMPFILE.gpx file
  428. TO_READ=0
  429. TO_READ="`expr $END_BYTE - $READ_BYTES`" # bytes to read from $TEMPFILE.gpx2 file
  430. READ_BYTES="`expr $READ_BYTES + $TO_READ`" # update readed bytes
  431. # list of points in route
  432. head -n $TO_READ "$TEMPFILE.gpx2" | grep "<trkpt" > "$TEMPFILE.points"
  433. POINTS=0 # number of points in track
  434. cat "$TEMPFILE.points" | grep -c "<trkpt" > "$TEMPFILE.var"
  435. read POINTS < "$TEMPFILE.var"
  436. echo "L $POINTS 1" >> "$TEMPFILE.base"
  437. # read lat lon data
  438. cat "$TEMPFILE.points" | cut -f2 -d'<' | cut -f2,3 -d ' ' | cut -f2,4 -d '"' | tr '"' '\t' > "$TEMPFILE.latlon"
  439. cat "$TEMPFILE.latlon" | tr ',' '.' | awk '{printf(" %s %s\n", $2, $1) }' >> "$TEMPFILE.base"
  440. # create attribute line
  441. head -n $TO_READ "$TEMPFILE.gpx2" | grep -n "<trk" | head -n 1 | cut -f1 -d ':' > "$TEMPFILE.var"
  442. OFFSET=0
  443. read OFFSET < "$TEMPFILE.var"
  444. S_LAT=""
  445. head -n 1 "$TEMPFILE.latlon" | cut -f1 > "$TEMPFILE.var"
  446. read S_LAT < "$TEMPFILE.var"
  447. S_LON=""
  448. head -n 1 "$TEMPFILE.latlon" | cut -f2 > "$TEMPFILE.var"
  449. read S_LON < "$TEMPFILE.var"
  450. E_LAT=""
  451. tail -n 1 "$TEMPFILE.latlon" | cut -f1 > "$TEMPFILE.var"
  452. read E_LAT < "$TEMPFILE.var"
  453. E_LON=""
  454. tail -n 1 "$TEMPFILE.latlon" | cut -f2 > "$TEMPFILE.var"
  455. read E_LON < "$TEMPFILE.var"
  456. OFFSET="`expr $TO_READ - $OFFSET`"
  457. head -n $TO_READ "$TEMPFILE.gpx2" | tail -n $OFFSET | grep "<time" > "$TEMPFILE.time"
  458. S_TIME=""
  459. head -n 1 "$TEMPFILE.time" | cut -f 2 -d'>' | cut -f1 -d'<' > "$TEMPFILE.var"
  460. read S_TIME < "$TEMPFILE.var"
  461. E_TIME=""
  462. tail -n 1 "$TEMPFILE.time" | cut -f 2 -d'>' | cut -f1 -d'<' > "$TEMPFILE.var"
  463. read E_TIME < "$TEMPFILE.var"
  464. TRACK_ID="`expr $TRACK_ID + 1`"
  465. # check that numberic values don't try and pass an empty string
  466. if [ -z "$TRACK_ID" ] ; then
  467. TRACK_ID="NULL"
  468. g.message -w "Track $TRACK_NUMBER: category number was empty. Bug?"
  469. fi
  470. if [ -z "$S_LAT" ] ; then
  471. S_LAT="NULL"
  472. g.message -w "Track $TRACK_NUMBER: starting lat was empty. Bug?"
  473. fi
  474. if [ -z "$S_LON" ] ; then
  475. S_LON="NULL"
  476. g.message -w "Track $TRACK_NUMBER: starting lon was empty. Bug?"
  477. fi
  478. if [ -z "$E_LAT" ] ; then
  479. E_LAT="NULL"
  480. g.message -w "Track $TRACK_NUMBER: ending lat was empty. Bug?"
  481. fi
  482. if [ -z "$E_LON" ] ; then
  483. E_LON="NULL"
  484. g.message -w "Track $TRACK_NUMBER: ending lon was empty. Bug?"
  485. fi
  486. echo "$TRACK_ID|$S_TIME|$S_LAT|$S_LON|$E_TIME|$E_LAT|$E_LON" >> "$TEMPFILE.track_atts"
  487. TRACK_NUMBER="`expr $TRACK_NUMBER - 1`"
  488. # eat files
  489. tail -n $TRACK_NUMBER "$TEMPFILE.bytes" > "$TEMPFILE.bytes2"
  490. mv "$TEMPFILE.bytes2" "$TEMPFILE.bytes"
  491. head -n $TO_READ "$TEMPFILE.gpx2" > "$TEMPFILE.points"
  492. B_GPX=0
  493. wc -c < "$TEMPFILE.gpx2" > "$TEMPFILE.var"
  494. read B_GPX < "$TEMPFILE.var"
  495. B_PNT=0
  496. wc -c < "$TEMPFILE.points" > "$TEMPFILE.var"
  497. read B_PNT < "$TEMPFILE.var"
  498. TO_READ="`expr $B_GPX - $B_PNT`"
  499. tail -c $TO_READ "$TEMPFILE.gpx2" > "$TEMPFILE.points"
  500. mv "$TEMPFILE.points" "$TEMPFILE.gpx2"
  501. done
  502. # create attr table: cat(int), start_time varchar(40), start_lat(double precision), start_lon(double precision), \
  503. # end_time varchar(40), end_lat(double precision), end_lon(double precision)
  504. ATTR_FILE="${TEMPFILE}.track_atts"
  505. ATTR_COLS='cat int, start_time varchar(40), start_lat double precision, start_lon double precision, end_time varchar(40), end_lat double precision, end_lon double precision'
  506. fi
  507. #### reproject if needed
  508. if [ "$IN_PROJ" = "$OUT_PROJ" ] || [ $KEEP_WGS84 -eq 1 ] ; then
  509. g.message "No projection transformation performed"
  510. cat "${TEMPFILE}.base" | awk 'BEGIN {;} $1=="L" { print } ; \
  511. $1!="L" { printf(" %s %s\n", $1, $2) } END {;}' > "${TEMPFILE}.P_base"
  512. else
  513. g.message "Attempting waypoint projection transform with cs2cs"
  514. eval cs2cs -tL -f %.9f `echo $IN_PROJ` +to `echo $OUT_PROJ` \
  515. < "${TEMPFILE}.base" > "${TEMPFILE}.P_mid"
  516. EXITCODE=$?
  517. # check if transform REALLY worked (e.g. grid file not found)
  518. BAD_PTS="`grep -c "^\*" "${TEMPFILE}.P_mid"`"
  519. if [ "$BAD_PTS" -gt 0 ] ; then
  520. g.message message=""
  521. g.message -w "$BAD_PTS vertices failed reprojection."
  522. EXITCODE=1
  523. fi
  524. if [ $EXITCODE -ne 0 ] ; then
  525. g.message "Projection transform failed, retaining WGS84"
  526. g.message message=""
  527. cat "${TEMPFILE}.base" | awk 'BEGIN {;} $1=="L" { print } ; \
  528. $1!="L" { printf(" %s %s\n", $1, $2) } END {;}' > "${TEMPFILE}.P_base"
  529. else
  530. cat "${TEMPFILE}.P_mid" | awk 'BEGIN {;} $1=="L" { print } ; \
  531. $1!="L" { printf(" %s %s\n", $1, $2) } END {;}' > "${TEMPFILE}.P_base"
  532. fi
  533. fi
  534. # add category numbers
  535. cat "${TEMPFILE}.P_base" | awk 'BEGIN { FS=" " ; R=0 } \
  536. $1=="L" { printf(" 1 %d\nL %d 1\n", R, $2) ; ++R } ; \
  537. $1!="L" { print } END { printf(" 1 %d\n", R) }' | sed -e '1d' > "${TEMPFILE}.P_baseC"
  538. #### create digit header
  539. cat << EOF > "${TEMPFILE}.dig"
  540. ORGANIZATION: GRASSroots organization
  541. DIGIT DATE: `date "+%Y/%m/%d"`
  542. DIGIT NAME: $PROG
  543. MAP NAME: $NAME
  544. MAP DATE: `date +%Y`
  545. MAP SCALE: 1
  546. OTHER INFO: Imported by `echo $USER@$HOSTNAME`
  547. ZONE: 0
  548. MAP THRESH: 0
  549. VERTI:
  550. EOF
  551. #### merge in vertices
  552. #HB#?? locale test fixed this
  553. cat "${TEMPFILE}.P_baseC" | tr ',' '.' >> "${TEMPFILE}.dig"
  554. #### if no name for vector file given, cat to stdout
  555. if [ -z "$NAME" ] ; then
  556. g.message "ASCII file redirected to stdout"
  557. cat "${TEMPFILE}.dig" 2> /dev/null
  558. else
  559. #### import to binary vector file
  560. g.message "Importing with v.in.ascii"
  561. v.in.ascii format=standard output="$NAME" input="${TEMPFILE}.dig"
  562. #### check success/failure
  563. if [ $? -eq 0 ] ; then
  564. g.message message="Line vector <$NAME> successfully created."
  565. else
  566. g.message -e message='An error occured on creating <$NAME>, please check!'
  567. exit 1
  568. fi
  569. ## create db links if they don't exist
  570. db.connect -c
  571. DRIVER="`db.connect -p | grep '^driver:' | cut -f2 -d:`"
  572. if [ "$DRIVER" = "dbf" ] && [ ! -d "$LOCATION/dbf/" ] ; then
  573. mkdir "$LOCATION/dbf/"
  574. fi
  575. g.message "Creating new table.."
  576. echo "CREATE TABLE $NAME ($ATTR_COLS)" | db.execute
  577. g.message "Populating table with attributes.."
  578. # echo "COPY $NAME FROM '$ATTR_FILE'" | db.execute
  579. # "COPY table FROM 'filename.txt'" does not work for DBF and there is no
  580. # db.in.ascii yet, so we use a hack...
  581. ### <hack>
  582. if [ $RTE -eq 1 ] ; then
  583. # => echo "INSERT INTO $NAME VALUES (2, 1, 'AB', 'B')" | db.execute
  584. cat "$ATTR_FILE" | \
  585. awk -F'|' '{
  586. printf("echo \"INSERT INTO $NAME VALUES (%d, %d, ^%s^, ^%s^)\" | db.execute\n", $1, $2, $3, $4) }' \
  587. | tr '^' \' > "$ATTR_FILE"_exe
  588. fi
  589. if [ $TRK -eq 1 ] ; then
  590. # => echo "INSERT INTO $NAME VALUES ()" | db.execute
  591. cat "$ATTR_FILE" | \
  592. awk -F'|' '{
  593. printf("echo \"INSERT INTO $NAME VALUES (%d, ^%s^, %s, %s, ^%s^, %s, %s)\" | db.execute\n", $1, $2, $3, $4, $5, $6, $7) }' \
  594. | tr '^' \' > "$ATTR_FILE"_exe
  595. fi
  596. . "$ATTR_FILE"_exe
  597. ### </hack>
  598. g.message "Connecting attribute table to vector file..."
  599. v.db.connect map="$NAME" table="$NAME"
  600. EXITCODE=$?
  601. fi
  602. fi # if CREATE_POINTS or CREATE_LINES
  603. # list column names
  604. if [ $VERBOSE -eq 1 ] && [ -n "$NAME" ] ; then
  605. g.message message="<$NAME> columns:"
  606. db.columns "$NAME"
  607. fi
  608. # write cmd history:
  609. v.support map="$NAME" cmdhist="${CMDLINE}"
  610. #### clean up the mess
  611. rm -f "${TEMPFILE}."* "${TEMPFILE}"
  612. #### end
  613. g.message "Done."
  614. exit $EXITCODE