i.band.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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.pygrass.raster import RasterRow
  45. from grass.exceptions import GrassError, OpenError
  46. def print_map_band_reference(name, band_reader):
  47. """Print band reference information assigned to a single raster map
  48. :param str name: raster map name
  49. """
  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. try:
  67. with RasterRow(name) as rast:
  68. if band_ref:
  69. gs.debug(_("Band reference <{}> assigned to raster map <{}>").format(
  70. band_ref, name), 1)
  71. else:
  72. gs.debug(_("Band reference dissociated from raster map <{}>").format(
  73. name), 1)
  74. try:
  75. rast.info.band_reference = band_ref
  76. except GrassError as e:
  77. gs.error(_("Unable to assign/dissociate band reference. {}").format(e))
  78. return 1
  79. except OpenError as e:
  80. gs.error(_("Map <{}> not found in current mapset").format(name))
  81. return 1
  82. return 0
  83. def main():
  84. maps = options['map'].split(',')
  85. if options['operation'] == 'add':
  86. if not options['band']:
  87. gs.fatal(_("Operation {}: required parameter <{}> not set").format(
  88. options['operation'], 'band')
  89. )
  90. bands = options['band'].split(',')
  91. if len(bands) > 1 and len(bands) != len(maps):
  92. gs.fatal(_("Number of maps differs from number of bands"))
  93. else:
  94. bands = [None]
  95. if options['operation'] == 'print':
  96. from grass.bandref import BandReferenceReader
  97. band_reader = BandReferenceReader()
  98. else:
  99. band_reader = None
  100. multi_bands = len(bands) > 1
  101. ret = 0
  102. for i in range(len(maps)):
  103. band_ref = bands[i] if multi_bands else bands[0]
  104. if options['operation'] == 'print':
  105. print_map_band_reference(maps[i], band_reader)
  106. else:
  107. if manage_map_band_reference(maps[i], band_ref) != 0:
  108. ret = 1
  109. return ret
  110. if __name__ == "__main__":
  111. options, flags = gs.parser()
  112. sys.exit(
  113. main()
  114. )