v.to.lines.py 6.2 KB

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