v.to.lines.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. import os
  37. import sys
  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. if 0 != grass.run_command('v.category', layer="2", type='boundary',
  76. option='add', input=input, out=input_tmp,
  77. quiet=quiet):
  78. grass.run_command('g.remove', flags='f', type='vect',
  79. pattern=input_tmp, quiet=quiet)
  80. grass.fatal(_("Error creating layer 2"))
  81. if 0 != grass.run_command('v.db.addtable', map=input_tmp, layer="2",
  82. columns="left integer,right integer",
  83. quiet=quiet):
  84. grass.run_command('g.remove', flags='f', type='vect',
  85. pattern=input_tmp, quiet=quiet)
  86. grass.fatal(_("Error creating new table for layer 2"))
  87. if 0 != grass.run_command('v.to.db', map=input_tmp, option="sides",
  88. columns="left,right", layer="2", quiet=quiet):
  89. grass.run_command('g.remove', flags='f', type='vect',
  90. pattern=input_tmp, quiet=quiet)
  91. grass.fatal(_("Error populating new table for layer 2"))
  92. if 0 != grass.run_command('v.type', input=input_tmp, output=out_type,
  93. from_type='boundary', to_type='line',
  94. quiet=quiet, layer="2"):
  95. grass.run_command('g.remove', flags='f', type='vect',
  96. pattern=remove_names, quiet=quiet)
  97. grass.fatal(_("Error converting polygon to line"))
  98. report = grass.read_command('v.category', flags='g', input=out_type,
  99. option='report', quiet=quiet).split('\n')
  100. for r in report:
  101. if r.find('centroid') != -1:
  102. min_cat = report[0].split()[-2]
  103. max_cat = report[0].split()[-1]
  104. break
  105. if min_cat and max_cat:
  106. if 0 != grass.run_command('v.edit', map=out_type, tool='delete',
  107. type='centroid', layer=2, quiet=quiet,
  108. cats='{mi}-{ma}'.format(mi=min_cat, ma=max_cat)):
  109. grass.run_command('g.remove', flags='f', type='vect',
  110. pattern=remove_names, quiet=quiet)
  111. grass.fatal(_("Error removing centroids"))
  112. try:
  113. if 0 != grass.run_command('v.db.droptable', map=out_type, layer=1,
  114. flags='f', quiet=True):
  115. grass.run_command('g.remove', flags='f', type='vect',
  116. pattern=remove_names, quiet=quiet)
  117. grass.fatal(_("Error removing table from layer 1"))
  118. except:
  119. grass.warning(_("No table for layer %d" % 1))
  120. if 0 != grass.run_command('v.category', input=out_type, option='transfer',
  121. output=output, layer="2,1", quiet=quiet,
  122. overwrite=overwrite):
  123. grass.run_command('g.remove', flags='f', type='vect',
  124. pattern=remove_names, quiet=quiet)
  125. grass.fatal(_("Error adding categories"))
  126. grass.run_command('g.remove', flags='f', type='vect',
  127. pattern=remove_names, quiet=quiet)
  128. if point:
  129. grass.run_command('g.remove', flags='f', type='vect',
  130. pattern=out_temp, quiet=quiet)
  131. if __name__ == "__main__":
  132. options, flags = grass.parser()
  133. main()