v.to.lines.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. #% keyword: vector
  21. #% keyword: geometry
  22. #% keyword: area
  23. #% keyword: line
  24. #% keyword: 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.script.utils import decode
  40. from grass.exceptions import CalledModuleError
  41. import os
  42. def main():
  43. # Get the options
  44. input = options["input"]
  45. input_name = input.split('@')[0]
  46. output = options["output"]
  47. method = options["method"]
  48. min_cat = None
  49. max_cat = None
  50. point = None
  51. overwrite = grass.overwrite()
  52. quiet = True
  53. if grass.verbosity() > 2:
  54. quiet = False
  55. in_info = grass.vector_info(input)
  56. # check for wild mixture of vector types
  57. if in_info['points'] > 0 and in_info['boundaries'] > 0:
  58. grass.fatal(_("The input vector map contains both polygons and points,"
  59. " cannot handle mixed types"))
  60. pid = os.getpid()
  61. # process points via triangulation, then exit
  62. if in_info['points'] > 0:
  63. point = True
  64. layer = 1 # hardcoded for now
  65. out_temp = '{inp}_point_tmp_{pid}'.format(inp=input_name, pid=pid)
  66. if method == 'delaunay':
  67. grass.message(_("Processing point data (%d points found)...") % in_info['points'])
  68. grass.run_command('v.delaunay', input=input, layer=layer,
  69. output=out_temp, quiet=quiet)
  70. grass.run_command('v.db.addtable', map=out_temp, quiet=True)
  71. input = out_temp
  72. in_info = grass.vector_info(input)
  73. # process areas
  74. if in_info['areas'] == 0 and in_info['boundaries'] == 0:
  75. grass.fatal(_("The input vector map does not contain polygons"))
  76. out_type = '{inp}_type_{pid}'.format(inp=input_name, pid=pid)
  77. input_tmp = '{inp}_tmp_{pid}'.format(inp=input_name, pid=pid)
  78. remove_names = "%s,%s" % (out_type, input_tmp)
  79. grass.message(_("Processing area data (%d areas found)...") % in_info['areas'])
  80. try:
  81. grass.run_command('v.category', layer="2", type='boundary',
  82. option='add', input=input, out=input_tmp,
  83. quiet=quiet)
  84. except CalledModuleError:
  85. grass.run_command('g.remove', flags='f', type='vector',
  86. name=input_tmp, quiet=quiet)
  87. grass.fatal(_("Error creating layer 2"))
  88. try:
  89. grass.run_command('v.db.addtable', map=input_tmp, layer="2",
  90. columns="left integer,right integer",
  91. quiet=quiet)
  92. except CalledModuleError:
  93. grass.run_command('g.remove', flags='f', type='vector',
  94. name=input_tmp, quiet=quiet)
  95. grass.fatal(_("Error creating new table for layer 2"))
  96. try:
  97. grass.run_command('v.to.db', map=input_tmp, option="sides",
  98. columns="left,right", layer="2", quiet=quiet)
  99. except CalledModuleError:
  100. grass.run_command('g.remove', flags='f', type='vector',
  101. name=input_tmp, quiet=quiet)
  102. grass.fatal(_("Error populating new table for layer 2"))
  103. try:
  104. grass.run_command('v.type', input=input_tmp, output=out_type,
  105. from_type='boundary', to_type='line',
  106. quiet=quiet, layer="2")
  107. except CalledModuleError:
  108. grass.run_command('g.remove', flags='f', type='vector',
  109. name=remove_names, quiet=quiet)
  110. grass.fatal(_("Error converting polygon to line"))
  111. report = grass.read_command('v.category', flags='g', input=out_type,
  112. option='report', quiet=quiet)
  113. report = decode(report).split('\n')
  114. for r in report:
  115. if r.find('centroid') != -1:
  116. min_cat = report[0].split()[-2]
  117. max_cat = report[0].split()[-1]
  118. break
  119. if min_cat and max_cat:
  120. try:
  121. grass.run_command('v.edit', map=out_type, tool='delete',
  122. type='centroid', layer=2, quiet=quiet,
  123. cats='{mi}-{ma}'.format(mi=min_cat, ma=max_cat))
  124. except CalledModuleError:
  125. grass.run_command('g.remove', flags='f', type='vector',
  126. name=remove_names, quiet=quiet)
  127. grass.fatal(_("Error removing centroids"))
  128. try:
  129. try:
  130. # TODO: fix magic numbers for layer here and there
  131. grass.run_command('v.db.droptable', map=out_type, layer=1,
  132. flags='f', quiet=True)
  133. except CalledModuleError:
  134. grass.run_command('g.remove', flags='f', type='vector',
  135. name=remove_names, quiet=quiet)
  136. grass.fatal(_("Error removing table from layer 1"))
  137. # TODO: when this except is happaning, it seems that never, so it seems wrong
  138. except:
  139. grass.warning(_("No table for layer %d" % 1))
  140. try:
  141. grass.run_command('v.category', input=out_type, option='transfer',
  142. output=output, layer="2,1", quiet=quiet,
  143. overwrite=overwrite)
  144. except CalledModuleError:
  145. grass.run_command('g.remove', flags='f', type='vector',
  146. name=remove_names, quiet=quiet)
  147. grass.fatal(_("Error adding categories"))
  148. grass.run_command('g.remove', flags='f', type='vector',
  149. name=remove_names, quiet=quiet)
  150. if point:
  151. grass.run_command('g.remove', flags='f', type='vector',
  152. name=out_temp, quiet=quiet)
  153. if __name__ == "__main__":
  154. options, flags = grass.parser()
  155. main()