DefineProjection.py 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. """
  2. ***************************************************************************
  3. DefineProjection.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. import re
  22. from qgis.core import (QgsProcessing,
  23. QgsProcessingAlgorithm,
  24. QgsProcessingParameterVectorLayer,
  25. QgsProcessingParameterCrs,
  26. QgsProcessingOutputVectorLayer,
  27. QgsCoordinateReferenceSystem,
  28. QgsProjUtils)
  29. from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
  30. pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
  31. class DefineProjection(QgisAlgorithm):
  32. INPUT = 'INPUT'
  33. CRS = 'CRS'
  34. def group(self):
  35. return self.tr('Vector general')
  36. def groupId(self):
  37. return 'vectorgeneral'
  38. def __init__(self):
  39. super().__init__()
  40. def initAlgorithm(self, config=None):
  41. self.addParameter(QgsProcessingParameterVectorLayer(self.INPUT,
  42. self.tr('Input Shapefile'), types=[QgsProcessing.TypeVectorAnyGeometry]))
  43. self.addParameter(QgsProcessingParameterCrs(self.CRS, 'CRS', 'EPSG:4326'))
  44. self.addOutput(QgsProcessingOutputVectorLayer(self.INPUT,
  45. self.tr('Layer with projection')))
  46. def name(self):
  47. return 'definecurrentprojection'
  48. def displayName(self):
  49. return self.tr('Define Shapefile projection')
  50. def tags(self):
  51. return self.tr('layer,shp,prj,qpj,change,alter').split(',')
  52. def shortDescription(self):
  53. return self.tr('Changes a Shapefile\'s projection to a new CRS without reprojecting features')
  54. def flags(self):
  55. return super().flags() | QgsProcessingAlgorithm.FlagNoThreading
  56. def processAlgorithm(self, parameters, context, feedback):
  57. layer = self.parameterAsVectorLayer(parameters, self.INPUT, context)
  58. crs = self.parameterAsCrs(parameters, self.CRS, context)
  59. provider = layer.dataProvider()
  60. ds = provider.dataSourceUri()
  61. p = re.compile(r'\|.*')
  62. dsPath = p.sub('', ds)
  63. if dsPath.lower().endswith('.shp'):
  64. dsPath = dsPath[:-4]
  65. wkt = crs.toWkt(QgsCoordinateReferenceSystem.WKT1_ESRI)
  66. with open(dsPath + '.prj', 'w', encoding='utf-8') as f:
  67. f.write(wkt)
  68. qpjFile = dsPath + '.qpj'
  69. if os.path.exists(qpjFile):
  70. os.remove(qpjFile)
  71. else:
  72. feedback.pushConsoleInfo(self.tr("Data source isn't a Shapefile, skipping .prj/.qpj creation"))
  73. layer.setCrs(crs)
  74. layer.triggerRepaint()
  75. return {self.INPUT: layer}