AssignProjection.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. """
  2. ***************************************************************************
  3. self.py
  4. ---------------------
  5. Date : January 2016
  6. Copyright : (C) 2016 by Alexander Bruy
  7. Email : alexander dot bruy at gmail dot com
  8. ***************************************************************************
  9. * *
  10. * This program is free software; you can redistribute it and/or modify *
  11. * it under the terms of the GNU General Public License as published by *
  12. * the Free Software Foundation; either version 2 of the License, or *
  13. * (at your option) any later version. *
  14. * *
  15. ***************************************************************************
  16. """
  17. __author__ = 'Alexander Bruy'
  18. __date__ = 'January 2016'
  19. __copyright__ = '(C) 2016, Alexander Bruy'
  20. import os
  21. from qgis.PyQt.QtGui import QIcon
  22. from qgis.core import (QgsProcessingException,
  23. QgsProcessingParameterRasterLayer,
  24. QgsProcessingParameterCrs,
  25. QgsProcessingOutputRasterLayer,
  26. QgsProcessingContext)
  27. from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm
  28. from processing.algs.gdal.GdalUtils import GdalUtils
  29. from processing.tools.system import isWindows
  30. pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
  31. class AssignProjection(GdalAlgorithm):
  32. INPUT = 'INPUT'
  33. CRS = 'CRS'
  34. OUTPUT = 'OUTPUT'
  35. def __init__(self):
  36. super().__init__()
  37. def initAlgorithm(self, config=None):
  38. self.addParameter(QgsProcessingParameterRasterLayer(self.INPUT,
  39. self.tr('Input layer')))
  40. self.addParameter(QgsProcessingParameterCrs(self.CRS,
  41. self.tr('Desired CRS')))
  42. self.addOutput(QgsProcessingOutputRasterLayer(self.OUTPUT,
  43. self.tr('Layer with projection')))
  44. def name(self):
  45. return 'assignprojection'
  46. def displayName(self):
  47. return self.tr('栅格数据重投影')
  48. def icon(self):
  49. return QIcon(os.path.join(pluginPath, 'images', 'dbms', 'imageproject.png'))
  50. def tags(self):
  51. tags = self.tr('assign,set,transform,reproject,crs,srs').split(',')
  52. tags.extend(super().tags())
  53. return tags
  54. def group(self):
  55. return self.tr('数据处理')
  56. def groupId(self):
  57. return 'datahandle'
  58. def commandName(self):
  59. return 'gdal_edit'
  60. def getConsoleCommands(self, parameters, context, feedback, executing=True):
  61. inLayer = self.parameterAsRasterLayer(parameters, self.INPUT, context)
  62. if inLayer is None:
  63. raise QgsProcessingException(self.invalidRasterError(parameters, self.INPUT))
  64. fileName = inLayer.source()
  65. crs = self.parameterAsCrs(parameters, self.CRS, context)
  66. arguments = [
  67. '-a_srs',
  68. GdalUtils.gdal_crs_string(crs),
  69. fileName
  70. ]
  71. self.setOutputValue(self.OUTPUT, fileName)
  72. return [self.commandName() + ('.bat' if isWindows() else '.py'), GdalUtils.escapeAndJoin(arguments)]
  73. def postProcessAlgorithm(self, context, feedback):
  74. # get output value
  75. fileName = self.output_values.get(self.OUTPUT)
  76. if not fileName:
  77. return {}
  78. # search in context project's layers
  79. if context.project():
  80. for l in context.project().mapLayers().values():
  81. # check the source
  82. if l.source() != fileName:
  83. continue
  84. # reload provider's data
  85. l.dataProvider().reloadData()
  86. l.setCrs(l.dataProvider().crs())
  87. l.triggerRepaint()
  88. # search in context temporary layer store
  89. for l in context.temporaryLayerStore().mapLayers().values():
  90. # check the source
  91. if l.source() != fileName:
  92. continue
  93. # reload provider's data
  94. l.dataProvider().reloadData()
  95. l.setCrs(l.dataProvider().crs())
  96. context.temporaryLayerStore().addMapLayer(l)
  97. return {}