i.band.py 4.1 KB

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