v.to.lines.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. ############################################################################
  4. #
  5. # MODULE: v.to.lines (former v.polytoline)
  6. # AUTHOR(S): Luca Delucchi
  7. # point support added by Markus Neteler
  8. #
  9. # PURPOSE: Converts polygons and points to lines
  10. # COPYRIGHT: (C) 2013-2014 by the GRASS Development Team
  11. #
  12. # This program is free software under the GNU General Public
  13. # License (version 2). Read the file COPYING that comes with GRASS
  14. # for details.
  15. # TODO
  16. # support centroids (treat as points)?
  17. #############################################################################
  18. #%module
  19. #% description: Converts vector polygons or points to lines.
  20. #% keywords: vector
  21. #% keywords: geometry
  22. #% keywords: area
  23. #% keywords: line
  24. #% keywords: point
  25. #%end
  26. #%option G_OPT_V_INPUT
  27. #%end
  28. #%option G_OPT_V_OUTPUT
  29. #%end
  30. #%option
  31. #% key: method
  32. #% type: string
  33. #% description: Method used for point interpolation
  34. #% options: delaunay
  35. #% answer: delaunay
  36. #% guisection: Area
  37. #%end
  38. import grass.script as grass
  39. from grass.exceptions import CalledModuleError
  40. import os
  41. def main():
  42. # Get the options
  43. input = options["input"]
  44. output = options["output"]
  45. method = options["method"]
  46. min_cat = None
  47. max_cat = None
  48. point = None
  49. overwrite = grass.overwrite()
  50. quiet = True
  51. if grass.verbosity() > 2:
  52. quiet = False
  53. in_info = grass.vector_info(input)
  54. # check for wild mixture of vector types
  55. if in_info['points'] > 0 and in_info['boundaries'] > 0:
  56. grass.fatal(_("The input vector map contains both polygons and points,"
  57. " cannot handle mixed types"))
  58. pid = os.getpid()
  59. # process points via triangulation, then exit
  60. if in_info['points'] > 0:
  61. point = True
  62. layer = 1 # hardcoded for now
  63. out_temp = '{inp}_point_tmp_{pid}'.format(inp=input, pid=pid)
  64. if method == 'delaunay':
  65. grass.message(_("Processing point data (%d points found)...") % in_info['points'])
  66. grass.run_command('v.delaunay', input=input, layer=layer,
  67. output=out_temp, quiet=quiet)
  68. grass.run_command('v.db.addtable', map=out_temp, quiet=True)
  69. input = out_temp
  70. in_info = grass.vector_info(input)
  71. # process areas
  72. if in_info['areas'] == 0 and in_info['boundaries'] == 0:
  73. grass.fatal(_("The input vector map does not contain polygons"))
  74. out_type = '{inp}_type_{pid}'.format(inp=input, pid=pid)
  75. input_tmp = '{inp}_tmp_{pid}'.format(inp=input, pid=pid)
  76. remove_names = "%s,%s" % (out_type, input_tmp)
  77. grass.message(_("Processing area data (%d areas found)...") % in_info['areas'])
  78. try:
  79. grass.run_command('v.category', layer="2", type='boundary',
  80. option='add', input=input, out=input_tmp,
  81. quiet=quiet)
  82. except CalledModuleError:
  83. grass.run_command('g.remove', type='vect', name=input_tmp, quiet=quiet,
  84. flags='f')
  85. grass.fatal(_("Error creating layer 2"))
  86. try:
  87. grass.run_command('v.db.addtable', map=input_tmp, layer="2",
  88. columns="left integer,right integer",
  89. quiet=quiet)
  90. except CalledModuleError:
  91. grass.run_command('g.remove', type='vect', name=input_tmp, quiet=quiet,
  92. flags='f')
  93. grass.fatal(_("Error creating new table for layer 2"))
  94. try:
  95. grass.run_command('v.to.db', map=input_tmp, option="sides",
  96. columns="left,right", layer="2", quiet=quiet)
  97. except CalledModuleError:
  98. grass.run_command('g.remove', type='vect', name=input_tmp, quiet=quiet,
  99. flags='f')
  100. grass.fatal(_("Error populating new table for layer 2"))
  101. try:
  102. grass.run_command('v.type', input=input_tmp, output=out_type,
  103. from_type='boundary', to_type='line',
  104. quiet=quiet, layer="2")
  105. except CalledModuleError:
  106. grass.run_command('g.remove', type='vect', name=remove_names,
  107. quiet=quiet, flags='f')
  108. grass.fatal(_("Error converting polygon to line"))
  109. report = grass.read_command('v.category', flags='g', input=out_type,
  110. option='report', quiet=quiet).split('\n')
  111. for r in report:
  112. if r.find('centroid') != -1:
  113. min_cat = report[0].split()[-2]
  114. max_cat = report[0].split()[-1]
  115. break
  116. if min_cat and max_cat:
  117. try:
  118. grass.run_command('v.edit', map=out_type, tool='delete',
  119. type='centroid', layer=2, quiet=quiet,
  120. cats='{mi}-{ma}'.format(mi=min_cat, ma=max_cat))
  121. except CalledModuleError:
  122. grass.run_command('g.remove', type='vect', name=remove_names,
  123. quiet=quiet, flags='f')
  124. grass.fatal(_("Error removing centroids"))
  125. try:
  126. try:
  127. # TODO: fix magic numbers for layer here and there
  128. grass.run_command('v.db.droptable', map=out_type, layer=1,
  129. flags='f', quiet=True)
  130. except CalledModuleError:
  131. grass.run_command('g.remove', type='vect', name=remove_names,
  132. quiet=quiet, flags='f')
  133. grass.fatal(_("Error removing table from layer 1"))
  134. # TODO: when this except is happaning, it seems that never, so it seems wrong
  135. except:
  136. grass.warning(_("No table for layer %d" % 1))
  137. try:
  138. grass.run_command('v.category', input=out_type, option='transfer',
  139. output=output, layer="2,1", quiet=quiet,
  140. overwrite=overwrite)
  141. except CalledModuleError:
  142. grass.run_command('g.remove', type='vect', name=remove_names,
  143. quiet=quiet, flags='f')
  144. grass.fatal(_("Error adding categories"))
  145. grass.run_command('g.remove', type='vect', name=remove_names, quiet=quiet,
  146. flags='f')
  147. if point:
  148. grass.run_command('g.remove', type='vect', name=out_temp, quiet=quiet,
  149. flags='f')
  150. if __name__ == "__main__":
  151. options, flags = grass.parser()
  152. main()