v.in.mapgen.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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, import
  30. #%End
  31. #%flag
  32. #% key: f
  33. #% description: Input map is in Matlab format
  34. #%end
  35. #%flag
  36. #% key: z
  37. #% description: Create a 3D vector points map from 3 column Matlab data
  38. #%end
  39. #%option
  40. #% key: input
  41. #% type: string
  42. #% gisprompt: old_file,file,input
  43. #% description: Name of input file in Mapgen/Matlab format
  44. #% required : yes
  45. #%end
  46. #%option
  47. #% key: output
  48. #% type: string
  49. #% gisprompt: new,vector,vector
  50. #% description: Name for output vector map (omit for display to stdout)
  51. #% required : no
  52. #%end
  53. import sys
  54. import os
  55. import atexit
  56. import string
  57. import time
  58. import shutil
  59. import grass
  60. def cleanup():
  61. grass.try_remove(tmp)
  62. grass.try_remove(tmp + '.dig')
  63. def main():
  64. global tmp
  65. infile = options['input']
  66. output = options['output']
  67. matlab = flags['f']
  68. threeD = flags['z']
  69. prog = 'v.in.mapgen'
  70. opts = ""
  71. if not os.path.isfile(infile):
  72. grass.fatal("Input file <%s> not found." % infile)
  73. if output:
  74. name = output
  75. else:
  76. name = ''
  77. if threeD:
  78. matlab = True
  79. if threeD:
  80. do3D = 'z'
  81. else:
  82. do3D = ''
  83. tmp = grass.tempfile()
  84. #### create ascii vector file
  85. inf = file(infile)
  86. outf = file(tmp, 'w')
  87. if matlab:
  88. ## HB: OLD v.in.mapgen.sh Matlab import command follows.
  89. ## I have no idea what it's all about, so "new" matlab format will be
  90. ## a series of x y with "nan nan" breaking lines. (as NOAA provides)
  91. ## Old command:
  92. # tac $infile | $AWK 'BEGIN { FS="," ; R=0 }
  93. # $1~/\d*/ { printf("L %d\n", R) }
  94. # $1~/ .*/ { printf(" %lf %lf\n", $2, $1) ; ++R }
  95. # $1~/END/ { }' | tac > "$TMP"
  96. ## matlab format.
  97. points = []
  98. for line in inf:
  99. f = line.split()
  100. if f[0].lower() == 'nan':
  101. if points != []:
  102. outf.write("L %d\n" % len(points))
  103. for point in points:
  104. outf.write(" %.8f %.8f %.8f\n" % tuple(map(float,point)))
  105. points = []
  106. else:
  107. if len(f) == 2:
  108. f.append('0')
  109. points.append(f)
  110. if points != []:
  111. outf.write("L %d\n" % len(points))
  112. for point in points:
  113. outf.write(" %.8f %.8f %.8f\n" % tuple(map(float,point)))
  114. else:
  115. ## mapgen format.
  116. points = []
  117. for line in inf:
  118. if line[0] == '#':
  119. if points != []:
  120. outf.write("L %d\n" % len(points))
  121. for point in points:
  122. outf.write(" %.8f %.8f\n" % tuple(map(float,point)))
  123. points = []
  124. else:
  125. points.append(line.rstrip('\r\n').split('\t'))
  126. if points != []:
  127. outf.write("L %d\n" % len(points))
  128. for point in points:
  129. outf.write(" %.8f %.8f\n" % tuple(map(float,point)))
  130. outf.close()
  131. inf.close()
  132. #### create digit header
  133. digfile = tmp + '.dig'
  134. outf = file(digfile, 'w')
  135. t = string.Template(\
  136. """ORGANIZATION: GRASSroots organization
  137. DIGIT DATE: $date
  138. DIGIT NAME: $prog
  139. MAP NAME: $name
  140. MAP DATE: $year
  141. MAP SCALE: 1
  142. OTHER INFO: Imported by $user@$host
  143. ZONE: 0
  144. MAP THRESH: 0
  145. VERTI:
  146. """)
  147. date = time.strftime("%m/%d/%y")
  148. year = time.strftime("%Y")
  149. user = os.getenv('USERNAME') or os.getenv('LOGNAME')
  150. host = os.getenv('COMPUTERNAME') or os.uname()[1]
  151. s = t.substitute(prog = prog, name = name, date = date, year = year,
  152. user = user, host = host)
  153. outf.write(s)
  154. #### process points list to ascii vector file (merge in vertices)
  155. inf = file(tmp)
  156. shutil.copyfileobj(inf, outf)
  157. inf.close()
  158. outf.close()
  159. if not name:
  160. #### if no name for vector file given, cat to stdout
  161. grass.message("Output to stdout")
  162. inf = file(digfile)
  163. shutil.copyfileobj(inf, sys.stdout)
  164. inf.close()
  165. else:
  166. #### import to binary vector file
  167. grass.message("Importing with v.in.ascii ...")
  168. if grass.run_command('v.in.ascii', flags = do3D, input = digfile,
  169. output = name, format = 'standard') == 0:
  170. grass.message('"%s" successfully created' % name)
  171. else:
  172. grass.fatal('An error occured on creating "%s", please check' % name)
  173. if __name__ == "__main__":
  174. options, flags = grass.parser()
  175. atexit.register(cleanup)
  176. main()