v.centroids.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #!/usr/bin/env python
  2. ############################################################################
  3. #
  4. # MODULE: v.centroids
  5. # AUTHOR: Hamish Bowman
  6. # PURPOSE: Add missing centroids (frontend to v.category opt=add)
  7. # COPYRIGHT: (c) 2006 Hamish Bowman, and the GRASS Development Team
  8. # This program is free software under the GNU General Public
  9. # License (>=v2). Read the file COPYING that comes with GRASS
  10. # for details.
  11. #
  12. #############################################################################
  13. #%Module
  14. #% description: Adds missing centroids to closed boundaries.
  15. #% keywords: vector, centroid, area
  16. #%End
  17. #%option
  18. #% key: input
  19. #% type: string
  20. #% gisprompt: old,vector,vector
  21. #% key_desc: name
  22. #% description: Name of input vector map
  23. #%required: yes
  24. #%end
  25. #%option
  26. #% key: output
  27. #% type: string
  28. #% gisprompt: new,vector,vector
  29. #% key_desc: name
  30. #% description: Name for output vector map
  31. #% required: yes
  32. #%end
  33. #%option
  34. #% key: option
  35. #% type: string
  36. #% description: Action to be taken
  37. #% options: add
  38. #% answer: add
  39. #% required: no
  40. #%end
  41. #%option
  42. #% key: layer
  43. #% type: integer
  44. #% description: Layer number
  45. #% gisprompt: new_layer,layer,layer
  46. #% answer: 1
  47. #% required: no
  48. #%end
  49. #%option
  50. #% key: cat
  51. #% type: integer
  52. #% description: Category number starting value
  53. #% answer: 1
  54. #% required: no
  55. #%end
  56. #%option
  57. #% key: step
  58. #% type: integer
  59. #% description: Category increment
  60. #% answer: 1
  61. #% required: no
  62. #%end
  63. import sys
  64. import os
  65. import re
  66. import grass
  67. def main():
  68. if options['option'] == 'add':
  69. num_bound = 0
  70. tenv = os.environ.copy()
  71. tenv['LC_ALL'] = 'C'
  72. lines = grass.read_command("v.info", map = options['input'], env = tenv).splitlines()
  73. e = re.compile("Number of boundaries: +([0-9]+) +")
  74. for line in lines:
  75. mo = e.search(line)
  76. if mo:
  77. num_bound = int(mo.group(1))
  78. break
  79. if num_bound == 0:
  80. grass.fatal("Input vector map contains no boundaries.")
  81. grass.exec_command("v.category", type = 'area', **options)
  82. sys.exit(0)
  83. if __name__ == "__main__":
  84. options, flags = grass.parser()
  85. main()