v.in.mapgen.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #!/usr/bin/env python
  2. #
  3. ############################################################################
  4. #
  5. # MODULE: v.in.mapgen
  6. #
  7. # AUTHOR(S): Andreas Lange, andreas.lange@rhein-main.de
  8. # Updated for GRASS 6 by Hamish Bowman
  9. # Converted to Python by Glynn Clements
  10. #
  11. # PURPOSE: Import data from Mapgen or Matlab into a GRASS vector map
  12. #
  13. # COPYRIGHT: Original version (c) Andreas Lange
  14. # Updates by Hamish Bowman
  15. # (C) 2008 the GRASS Development Team
  16. #
  17. # This program is free software under the GNU General Public
  18. # License (>=v2). Read the file COPYING that comes with GRASS
  19. # for details.
  20. #############################################################################
  21. #
  22. # REQUIREMENTS: awk
  23. #
  24. # DATA AVAILABILITY: e.g., NOAA's Online Coastline Extractor
  25. # http://www.ngdc.noaa.gov/mgg/shorelines/shorelines.html
  26. #
  27. #%Module
  28. #% description: Import Mapgen or Matlab vector maps into GRASS.
  29. #% keywords: vector
  30. #% keywords: import
  31. #%End
  32. #%flag
  33. #% key: f
  34. #% description: Input map is in Matlab format
  35. #%end
  36. #%flag
  37. #% key: z
  38. #% description: Create a 3D vector points map from 3 column Matlab data
  39. #%end
  40. #%option
  41. #% key: input
  42. #% type: string
  43. #% gisprompt: old_file,file,input
  44. #% description: Name of input file in Mapgen/Matlab format
  45. #% required : yes
  46. #%end
  47. #%option
  48. #% key: output
  49. #% type: string
  50. #% gisprompt: new,vector,vector
  51. #% description: Name for output vector map (omit for display to stdout)
  52. #% required : no
  53. #%end
  54. import sys
  55. import os
  56. import atexit
  57. import string
  58. import time
  59. import shutil
  60. from grass.script import core as grass
  61. def cleanup():
  62. grass.try_remove(tmp)
  63. grass.try_remove(tmp + '.dig')
  64. def main():
  65. global tmp
  66. infile = options['input']
  67. output = options['output']
  68. matlab = flags['f']
  69. threeD = flags['z']
  70. prog = 'v.in.mapgen'
  71. opts = ""
  72. if not os.path.isfile(infile):
  73. grass.fatal(_("Input file <%s> not found.") % infile)
  74. if output:
  75. name = output
  76. else:
  77. name = ''
  78. if threeD:
  79. matlab = True
  80. if threeD:
  81. do3D = 'z'
  82. else:
  83. do3D = ''
  84. tmp = grass.tempfile()
  85. #### create ascii vector file
  86. inf = file(infile)
  87. outf = file(tmp, 'w')
  88. if matlab:
  89. ## HB: OLD v.in.mapgen.sh Matlab import command follows.
  90. ## I have no idea what it's all about, so "new" matlab format will be
  91. ## a series of x y with "nan nan" breaking lines. (as NOAA provides)
  92. ## Old command:
  93. # tac $infile | $AWK 'BEGIN { FS="," ; R=0 }
  94. # $1~/\d*/ { printf("L %d\n", R) }
  95. # $1~/ .*/ { printf(" %lf %lf\n", $2, $1) ; ++R }
  96. # $1~/END/ { }' | tac > "$TMP"
  97. ## matlab format.
  98. points = []
  99. for line in inf:
  100. f = line.split()
  101. if f[0].lower() == 'nan':
  102. if points != []:
  103. outf.write("L %d\n" % len(points))
  104. for point in points:
  105. outf.write(" %.8f %.8f %.8f\n" % tuple(map(float,point)))
  106. points = []
  107. else:
  108. if len(f) == 2:
  109. f.append('0')
  110. points.append(f)
  111. if points != []:
  112. outf.write("L %d\n" % len(points))
  113. for point in points:
  114. outf.write(" %.8f %.8f %.8f\n" % tuple(map(float,point)))
  115. else:
  116. ## mapgen format.
  117. points = []
  118. for line in inf:
  119. if line[0] == '#':
  120. if points != []:
  121. outf.write("L %d\n" % len(points))
  122. for point in points:
  123. outf.write(" %.8f %.8f\n" % tuple(map(float,point)))
  124. points = []
  125. else:
  126. points.append(line.rstrip('\r\n').split('\t'))
  127. if points != []:
  128. outf.write("L %d\n" % len(points))
  129. for point in points:
  130. outf.write(" %.8f %.8f\n" % tuple(map(float,point)))
  131. outf.close()
  132. inf.close()
  133. #### create digit header
  134. digfile = tmp + '.dig'
  135. outf = file(digfile, 'w')
  136. t = string.Template(\
  137. """ORGANIZATION: GRASSroots organization
  138. DIGIT DATE: $date
  139. DIGIT NAME: $user@$host
  140. MAP NAME: $name
  141. MAP DATE: $year
  142. MAP SCALE: 1
  143. OTHER INFO: Imported with $proj
  144. ZONE: 0
  145. MAP THRESH: 0
  146. VERTI:
  147. """)
  148. date = time.strftime("%m/%d/%y")
  149. year = time.strftime("%Y")
  150. user = os.getenv('USERNAME') or os.getenv('LOGNAME')
  151. host = os.getenv('COMPUTERNAME') or os.uname()[1]
  152. s = t.substitute(prog = prog, name = name, date = date, year = year,
  153. user = user, host = host)
  154. outf.write(s)
  155. #### process points list to ascii vector file (merge in vertices)
  156. inf = file(tmp)
  157. shutil.copyfileobj(inf, outf)
  158. inf.close()
  159. outf.close()
  160. if not name:
  161. #### if no name for vector file given, cat to stdout
  162. grass.message(_("Output to stdout"))
  163. inf = file(digfile)
  164. shutil.copyfileobj(inf, sys.stdout)
  165. inf.close()
  166. else:
  167. #### import to binary vector file
  168. grass.message(_("Importing with v.in.ascii ..."))
  169. if grass.run_command('v.in.ascii', flags = do3D, input = digfile,
  170. output = name, format = 'standard') == 0:
  171. grass.message(_('"%s" successfully created') % name)
  172. else:
  173. grass.fatal(_('An error occured on creating "%s", please check') % name)
  174. if __name__ == "__main__":
  175. options, flags = grass.parser()
  176. atexit.register(cleanup)
  177. main()