v.in.gps.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. #!/usr/bin/env python
  2. #
  3. ############################################################################
  4. #
  5. # MODULE: v.in.gps
  6. #
  7. # PURPOSE: Import GPS data from a GPS receiver or file into a GRASS
  8. # vector map using gpsbabel
  9. #
  10. # COPYRIGHT: (c) 2011 Hamish Bowman, and 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. # AUTHOR: Hamish Bowman, Dunedin, New Zealand
  16. # Python version based on v.out.gps.py by Glynn Clements
  17. # Work-alike of the v.in.gpsbabel shell script from GRASS 6
  18. #
  19. #############################################################################
  20. #
  21. # REQUIREMENTS:
  22. # - GPSBabel from http://gpsbabel.sourceforge.net
  23. # - cs2cs from PROJ.4 (for m.proj) http://proj.osgeo.org
  24. #
  25. # - report supported GPSBabel formats:
  26. # gpsbabel -^2 | tr '\t' ';' | sort -t';' -k3
  27. #
  28. #############################################################################
  29. #
  30. # How to do it
  31. # http://www.gdal.org/ogr/drv_gpx.html
  32. # gpsbabel [options] -i INTYPE -f INFILE -o OUTTYPE -F OUTFILE
  33. #
  34. #############################################################################
  35. #%Module
  36. #% description: Import waypoints, routes, and tracks from a GPS receiver or GPS download file into a vector map.
  37. #% keywords: vector
  38. #% keywords: import
  39. #% keywords: GPS
  40. #%End
  41. #%flag
  42. #% key: w
  43. #% description: Import as waypoints
  44. #%end
  45. #%flag
  46. #% key: r
  47. #% description: Import as routes
  48. #%end
  49. #%flag
  50. #% key: t
  51. #% description: Import as tracks
  52. #%end
  53. #%flag
  54. #% key: p
  55. #% description: Force vertices of track or route data as points
  56. #%end
  57. #%flag
  58. #% key: k
  59. #% description: Do not attempt projection transform from WGS84
  60. #%end
  61. #%option G_OPT_F_INPUT
  62. #% description: Device or file used to import data
  63. #%end
  64. #%option G_OPT_V_OUTPUT
  65. #%end
  66. #%option
  67. #% key: format
  68. #% type: string
  69. #% description: GPSBabel supported output format
  70. #% answer: gpx
  71. #%end
  72. #%option
  73. #% key: proj
  74. #% type: string
  75. #% description: Projection of input data (PROJ.4 style), if not set Lat/Lon WGS84 is assumed
  76. #% required: no
  77. #%end
  78. import sys
  79. import os
  80. import atexit
  81. import string
  82. import re
  83. import grass.script as grass
  84. #.... todo ....
  85. def main():
  86. format = options['format']
  87. input = options['input']
  88. output = options['output']
  89. proj_terms = options['proj']
  90. wpt = flags['w']
  91. rte = flags['r']
  92. trk = flags['t']
  93. points_mode = flags['p']
  94. no_reproj = flags['k']
  95. nflags = len(filter(None, [wpt, rte, trk]))
  96. if nflags > 1:
  97. grass.fatal(_("One feature at a time please."))
  98. if nflags < 1:
  99. grass.fatal(_("No features requested for import."))
  100. #### check for gpsbabel
  101. ### FIXME: may need --help or similar?
  102. if not grass.find_program("gpsbabel"):
  103. grass.fatal(_("The gpsbabel program was not found, please install it first.\n") +
  104. "http://gpsbabel.sourceforge.net")
  105. #### check for cs2cs
  106. if not grass.find_program("cs2cs"):
  107. grass.fatal(_("The cs2cs program was not found, please install it first.\n") +
  108. "http://proj.osgeo.org")
  109. #todo
  110. # # check if we will overwrite data
  111. # if grass.findfile(output) and not grass.overwrite():
  112. # grass.fatal(_("Output file already exists."))
  113. #### set temporary files
  114. tmp = grass.tempfile()
  115. # import as GPX using v.in.ogr
  116. # if trk:
  117. # linetype = "FORCE_GPX_TRACK=YES"
  118. # elif rte:
  119. # linetype = "FORCE_GPX_TRACK=YES"
  120. # else:
  121. # linetype = None
  122. if format == 'gpx':
  123. # short circuit, we have what we came for.
  124. #todo
  125. # grass.try_remove(output)
  126. # os.rename(tmp_gpx, output)
  127. grass.verbose("Fast exit.")
  128. sys.exit()
  129. # run gpsbabel
  130. if wpt:
  131. gtype = '-w'
  132. elif trk:
  133. gtype = '-t'
  134. elif rte:
  135. gtype = '-r'
  136. else:
  137. gtype = ''
  138. grass.verbose("Running GPSBabel ...")
  139. ret = grass.call(['gpsbabel',
  140. gtype,
  141. '-i', format,
  142. '-f', output,
  143. '-o', 'gpx',
  144. '-F', tmp + '.gpx')
  145. if ret != 0:
  146. grass.fatal(_("Error running GPSBabel"))
  147. grass.verbose("Importing data ...")
  148. tmp_gpx = tmp + ".gpx"
  149. ret = grass.run_command('v.in.ogr', dsn = tmp_gpx, output = output,
  150. type = type, format = 'GPX', lco = linetype,
  151. dsco = "GPX_USE_EXTENSIONS=YES", quiet = True)
  152. if ret != 0:
  153. grass.fatal(_("Error importing data"))
  154. #### set up projection info
  155. # TODO: check if we are already in ll/WGS84. If so skip m.proj step.
  156. # TODO: multi layer will probably fail badly due to sed 's/^ 1 /'
  157. # output as old GRASS 4 vector ascii and fight with dig_ascii/?
  158. # Change to s/^ \([0-9] .*\) /# \1/' ??? mmph.
  159. #todo (taken from Glynn's v.out.gps)
  160. # reproject to lat/lon WGS84
  161. # grass.verbose("Reprojecting data ...")
  162. #
  163. # re1 = re.compile(r'^\([PLBCFKA]\)')
  164. # re2 = re.compile(r'^ 1 ')
  165. #
  166. # re3 = re.compile(r'\t\([-\.0-9]*\) .*')
  167. # re4 = re.compile(r'^\([-\.0-9]\)')
  168. # re5 = re.compile(r'^#')
  169. #
  170. # tmp_proj = tmp + ".proj"
  171. # tf = open(tmp_proj, 'w')
  172. # p1 = grass.pipe_command('v.out.ascii', input = inmap, format = 'standard')
  173. # p2 = grass.feed_command('m.proj', input = '-', flags = 'od', quiet = True, stdout = tf)
  174. # tf.close()
  175. #
  176. # lineno = 0
  177. # for line in p1.stdout:
  178. # lineno += 1
  179. # if lineno < 11:
  180. # continue
  181. # line = re1.sub(r'#\1', line)
  182. # line = re2.sub(r'# 1 ', line)
  183. # p2.stdin.write(line)
  184. #
  185. # p2.stdin.close()
  186. # p1.wait()
  187. # p2.wait()
  188. #
  189. # if p1.returncode != 0 or p2.returncode != 0:
  190. # grass.fatal(_("Error reprojecting data"))
  191. #
  192. # tmp_vogb = "tmp_vogb_epsg4326_%d" % os.getpid()
  193. # p3 = grass.feed_command('v.in.ascii', out = tmp_vogb, format = 'standard', flags = 'n', quiet = True)
  194. # tf = open(tmp_proj, 'r')
  195. #
  196. # for line in tf:
  197. # line = re3.sub(r' \1', line)
  198. # line = re4.sub(r' \1', line)
  199. # line = re5.sub('', line)
  200. # p3.stdin.write(line)
  201. #
  202. # p3.stdin.close()
  203. # tf.close()
  204. # p3.wait()
  205. #
  206. # if p3.returncode != 0:
  207. # grass.fatal(_("Error reprojecting data"))
  208. grass.verbose("Done.")
  209. if __name__ == "__main__":
  210. options, flags = grass.parser()
  211. atexit.register(cleanup)
  212. main()