extractprojection.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. """
  2. ***************************************************************************
  3. extractprojection.py
  4. ---------------------
  5. Date : September 2013
  6. Copyright : (C) 2013 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__ = 'September 2013'
  19. __copyright__ = '(C) 2013, Alexander Bruy'
  20. import os
  21. from qgis.PyQt.QtGui import QIcon
  22. from osgeo import gdal, osr
  23. from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm
  24. from qgis.core import QgsProcessingException
  25. from qgis.core import (QgsProcessingParameterRasterLayer,
  26. QgsProcessingParameterBoolean,
  27. QgsProcessingOutputFile)
  28. pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
  29. class ExtractProjection(GdalAlgorithm):
  30. INPUT = 'INPUT'
  31. PRJ_FILE_CREATE = 'PRJ_FILE_CREATE'
  32. WORLD_FILE = 'WORLD_FILE'
  33. PRJ_FILE = 'PRJ_FILE'
  34. def __init__(self):
  35. super().__init__()
  36. def initAlgorithm(self, config=None):
  37. self.addParameter(QgsProcessingParameterRasterLayer(
  38. self.INPUT,
  39. self.tr('Input file')))
  40. self.addParameter(QgsProcessingParameterBoolean(
  41. self.PRJ_FILE_CREATE,
  42. self.tr('Create also .prj file'), False))
  43. self.addOutput(QgsProcessingOutputFile(
  44. self.WORLD_FILE,
  45. self.tr('World file')))
  46. self.addOutput(QgsProcessingOutputFile(
  47. self.PRJ_FILE,
  48. self.tr('ESRI Shapefile prj file')))
  49. def name(self):
  50. return 'extractprojection'
  51. def displayName(self):
  52. return self.tr('Extract projection')
  53. def icon(self):
  54. return QIcon(os.path.join(pluginPath, 'images', 'gdaltools',
  55. 'projection-export.png'))
  56. def group(self):
  57. return self.tr('Raster projections')
  58. def groupId(self):
  59. return 'rasterprojections'
  60. def commandName(self):
  61. return 'extractprojection'
  62. def getConsoleCommands(self, parameters, context, feedback,
  63. executing=True):
  64. return [self.commandName()]
  65. def processAlgorithm(self, parameters, context, feedback):
  66. createPrj = self.parameterAsBoolean(parameters,
  67. self.PRJ_FILE_CREATE,
  68. context)
  69. raster = self.parameterAsRasterLayer(parameters, self.INPUT,
  70. context)
  71. if raster.dataProvider().name() != 'gdal':
  72. raise QgsProcessingException(self.tr('This algorithm can '
  73. 'only be used with '
  74. 'GDAL raster layers'))
  75. rasterPath = raster.source()
  76. rasterDS = gdal.Open(rasterPath, gdal.GA_ReadOnly)
  77. geotransform = rasterDS.GetGeoTransform()
  78. inputcrs = raster.crs()
  79. crs = inputcrs.toWkt()
  80. raster = None
  81. rasterDS = None
  82. inFileName = os.path.splitext(str(rasterPath))
  83. outFileName = inFileName[0]
  84. # this is not a good idea as it won't work with an extension like .jpeg
  85. # outFileExt = '.' + inFileName[1][1:4:2] + 'w'
  86. if (len(inFileName[1]) < 4):
  87. outFileExt = '.wld'
  88. else:
  89. outFileExt = inFileName[1][0:2] + inFileName[1][-1] + 'w'
  90. results = {}
  91. if crs != '' and createPrj:
  92. tmp = osr.SpatialReference()
  93. tmp.ImportFromWkt(crs)
  94. tmp.MorphToESRI()
  95. crs = tmp.ExportToWkt()
  96. tmp = None
  97. with open(outFileName + '.prj', 'w', encoding='utf-8') as prj:
  98. prj.write(crs)
  99. results[self.PRJ_FILE] = outFileName + '.prj'
  100. else:
  101. results[self.PRJ_FILE] = None
  102. with open(outFileName + outFileExt, 'w') as wld:
  103. wld.write('%0.8f\n' % geotransform[1])
  104. wld.write('%0.8f\n' % geotransform[4])
  105. wld.write('%0.8f\n' % geotransform[2])
  106. wld.write('%0.8f\n' % geotransform[5])
  107. wld.write('%0.8f\n' % (geotransform[0]
  108. + 0.5 * geotransform[1]
  109. + 0.5 * geotransform[2]))
  110. wld.write('%0.8f\n' % (geotransform[3]
  111. + 0.5 * geotransform[4]
  112. + 0.5 * geotransform[5]))
  113. results[self.WORLD_FILE] = outFileName + outFileExt
  114. return results