i.band.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #!/usr/bin/env python3
  2. ############################################################################
  3. #
  4. # MODULE: i.band
  5. # AUTHOR(S): Martin Landa <landa.martin gmail com>
  6. #
  7. # PURPOSE: Manages band reference information assigned to a single
  8. # raster map or to a list of raster maps.
  9. #
  10. # COPYRIGHT: (C) 2019 by mundialis GmbH & Co.KG, and the GRASS Development Team
  11. #
  12. # This program is free software under the GNU General
  13. # Public License (>=v2). Read the file COPYING that
  14. # comes with GRASS for details.
  15. #
  16. #############################################################################
  17. #%module
  18. #% description: Manages band reference information assigned to a single raster map or to a list of raster maps.
  19. #% keyword: general
  20. #% keyword: imagery
  21. #% keyword: band reference
  22. #% keyword: image collections
  23. #%end
  24. #%option G_OPT_R_MAPS
  25. #%end
  26. #%option
  27. #% key: band
  28. #% type: string
  29. #% key_desc: name
  30. #% description: Name of band reference identifier (example: S2_1)
  31. #% required: no
  32. #% multiple: yes
  33. #%end
  34. #%option
  35. #% key: operation
  36. #% type: string
  37. #% required: yes
  38. #% multiple: no
  39. #% options: add,remove,print
  40. #% description: Operation to be performed
  41. #% answer: add
  42. import sys
  43. import grass.script as gs
  44. from grass.exceptions import GrassError, OpenError
  45. def print_map_band_reference(name, band_reader):
  46. """Print band reference information assigned to a single raster map
  47. :param str name: raster map name
  48. """
  49. from grass.pygrass.raster import RasterRow
  50. try:
  51. with RasterRow(name) as rast:
  52. band_ref = rast.info.band_reference
  53. if band_ref:
  54. shortcut, band = band_ref.split('_')
  55. band_reader.print_info(shortcut, band)
  56. else:
  57. gs.info(_("No band reference assigned to <{}>").format(name))
  58. except OpenError as e:
  59. gs.error(_("Map <{}> not found").format(name))
  60. def manage_map_band_reference(name, band_ref):
  61. """Manage band reference assigned to a single raster map
  62. :param str name: raster map name
  63. :param str band_ref: band reference (None for dissociating band reference)
  64. :return int: return code
  65. """
  66. from grass.pygrass.raster import RasterRow
  67. try:
  68. with RasterRow(name) as rast:
  69. if band_ref:
  70. gs.debug(_("Band reference <{}> assigned to raster map <{}>").format(
  71. band_ref, name), 1)
  72. else:
  73. gs.debug(_("Band reference dissociated from raster map <{}>").format(
  74. name), 1)
  75. try:
  76. rast.info.band_reference = band_ref
  77. except GrassError as e:
  78. gs.error(_("Unable to assign/dissociate band reference. {}").format(e))
  79. return 1
  80. except OpenError as e:
  81. gs.error(_("Map <{}> not found in current mapset").format(name))
  82. return 1
  83. return 0
  84. def main():
  85. maps = options['map'].split(',')
  86. if options['operation'] == 'add':
  87. if not options['band']:
  88. gs.fatal(_("Operation {}: required parameter <{}> not set").format(
  89. options['operation'], 'band')
  90. )
  91. bands = options['band'].split(',')
  92. if len(bands) > 1 and len(bands) != len(maps):
  93. gs.fatal(_("Number of maps differs from number of bands"))
  94. else:
  95. bands = [None]
  96. if options['operation'] == 'print':
  97. from grass.bandref import BandReferenceReader
  98. band_reader = BandReferenceReader()
  99. else:
  100. band_reader = None
  101. multi_bands = len(bands) > 1
  102. ret = 0
  103. for i in range(len(maps)):
  104. band_ref = bands[i] if multi_bands else bands[0]
  105. if options['operation'] == 'print':
  106. print_map_band_reference(maps[i], band_reader)
  107. else:
  108. if manage_map_band_reference(maps[i], band_ref) != 0:
  109. ret = 1
  110. return ret
  111. if __name__ == "__main__":
  112. options, flags = gs.parser()
  113. sys.exit(
  114. main()
  115. )