v.in.gps.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. from grass.exceptions import CalledModuleError
  85. #.... todo ....
  86. def main():
  87. format = options['format']
  88. input = options['input']
  89. output = options['output']
  90. proj_terms = options['proj']
  91. wpt = flags['w']
  92. rte = flags['r']
  93. trk = flags['t']
  94. points_mode = flags['p']
  95. no_reproj = flags['k']
  96. nflags = len(filter(None, [wpt, rte, trk]))
  97. if nflags > 1:
  98. grass.fatal(_("One feature at a time please."))
  99. if nflags < 1:
  100. grass.fatal(_("No features requested for import."))
  101. #### check for gpsbabel
  102. ### FIXME: may need --help or similar?
  103. if not grass.find_program("gpsbabel"):
  104. grass.fatal(_("The gpsbabel program was not found, please install it first.\n") +
  105. "http://gpsbabel.sourceforge.net")
  106. #### check for cs2cs
  107. if not grass.find_program("cs2cs"):
  108. grass.fatal(_("The cs2cs program was not found, please install it first.\n") +
  109. "http://proj.osgeo.org")
  110. #todo
  111. # # check if we will overwrite data
  112. # if grass.findfile(output) and not grass.overwrite():
  113. # grass.fatal(_("Output file already exists."))
  114. #### set temporary files
  115. tmp = grass.tempfile()
  116. # import as GPX using v.in.ogr
  117. # if trk:
  118. # linetype = "FORCE_GPX_TRACK=YES"
  119. # elif rte:
  120. # linetype = "FORCE_GPX_TRACK=YES"
  121. # else:
  122. # linetype = None
  123. if format == 'gpx':
  124. # short circuit, we have what we came for.
  125. #todo
  126. # grass.try_remove(output)
  127. # os.rename(tmp_gpx, output)
  128. grass.verbose("Fast exit.")
  129. sys.exit()
  130. # run gpsbabel
  131. if wpt:
  132. gtype = '-w'
  133. elif trk:
  134. gtype = '-t'
  135. elif rte:
  136. gtype = '-r'
  137. else:
  138. gtype = ''
  139. grass.verbose("Running GPSBabel ...")
  140. ret = grass.call(['gpsbabel',
  141. gtype,
  142. '-i', format,
  143. '-f', output,
  144. '-o', 'gpx',
  145. '-F', tmp + '.gpx')
  146. if ret != 0:
  147. grass.fatal(_("Error running GPSBabel"))
  148. grass.verbose("Importing data ...")
  149. tmp_gpx = tmp + ".gpx"
  150. try:
  151. grass.run_command('v.in.ogr', dsn=tmp_gpx, output=output,
  152. type=type, format='GPX', lco=linetype,
  153. dsco="GPX_USE_EXTENSIONS=YES", quiet=True)
  154. except CalledModuleError:
  155. grass.fatal(_("Error importing data"))
  156. #### set up projection info
  157. # TODO: check if we are already in ll/WGS84. If so skip m.proj step.
  158. # TODO: multi layer will probably fail badly due to sed 's/^ 1 /'
  159. # output as old GRASS 4 vector ascii and fight with dig_ascii/?
  160. # Change to s/^ \([0-9] .*\) /# \1/' ??? mmph.
  161. #todo (taken from Glynn's v.out.gps)
  162. # reproject to lat/lon WGS84
  163. # grass.verbose("Reprojecting data ...")
  164. #
  165. # re1 = re.compile(r'^\([PLBCFKA]\)')
  166. # re2 = re.compile(r'^ 1 ')
  167. #
  168. # re3 = re.compile(r'\t\([-\.0-9]*\) .*')
  169. # re4 = re.compile(r'^\([-\.0-9]\)')
  170. # re5 = re.compile(r'^#')
  171. #
  172. # tmp_proj = tmp + ".proj"
  173. # tf = open(tmp_proj, 'w')
  174. # p1 = grass.pipe_command('v.out.ascii', input = inmap, format = 'standard')
  175. # p2 = grass.feed_command('m.proj', input = '-', flags = 'od', quiet = True, stdout = tf)
  176. # tf.close()
  177. #
  178. # lineno = 0
  179. # for line in p1.stdout:
  180. # lineno += 1
  181. # if lineno < 11:
  182. # continue
  183. # line = re1.sub(r'#\1', line)
  184. # line = re2.sub(r'# 1 ', line)
  185. # p2.stdin.write(line)
  186. #
  187. # p2.stdin.close()
  188. # p1.wait()
  189. # p2.wait()
  190. #
  191. # if p1.returncode != 0 or p2.returncode != 0:
  192. # grass.fatal(_("Error reprojecting data"))
  193. #
  194. # tmp_vogb = "tmp_vogb_epsg4326_%d" % os.getpid()
  195. # p3 = grass.feed_command('v.in.ascii', out = tmp_vogb, format = 'standard', flags = 'n', quiet = True)
  196. # tf = open(tmp_proj, 'r')
  197. #
  198. # for line in tf:
  199. # line = re3.sub(r' \1', line)
  200. # line = re4.sub(r' \1', line)
  201. # line = re5.sub('', line)
  202. # p3.stdin.write(line)
  203. #
  204. # p3.stdin.close()
  205. # tf.close()
  206. # p3.wait()
  207. #
  208. # if p3.returncode != 0:
  209. # grass.fatal(_("Error reprojecting data"))
  210. grass.verbose("Done.")
  211. if __name__ == "__main__":
  212. options, flags = grass.parser()
  213. atexit.register(cleanup)
  214. main()