v.to.lines.py 6.3 KB

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