v.to.lines.py 6.4 KB

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