3232from osgeo import gdal , osr
3333
3434from processing .algs .gdal .GdalAlgorithm import GdalAlgorithm
35+ from qgis .core import QgsProcessingException
3536from qgis .core import (QgsProcessingParameterRasterLayer ,
36- QgsProcessingParameterBoolean )
37+ QgsProcessingParameterBoolean ,
38+ QgsProcessingOutputFile )
3739
3840pluginPath = os .path .split (os .path .split (os .path .dirname (__file__ ))[0 ])[0 ]
3941
4042
4143class ExtractProjection (GdalAlgorithm ):
4244
4345 INPUT = 'INPUT'
46+ PRJ_FILE_CREATE = 'PRJ_FILE_CREATE'
47+ WORLD_FILE = 'WORLD_FILE'
4448 PRJ_FILE = 'PRJ_FILE'
4549
4650 def __init__ (self ):
4751 super ().__init__ ()
4852
4953 def initAlgorithm (self , config = None ):
50- self .addParameter (QgsProcessingParameterRasterLayer (self .INPUT , self .tr ('Input file' )))
51- self .addParameter (QgsProcessingParameterBoolean (self .PRJ_FILE ,
52- self .tr ('Create also .prj file' ), False ))
53-
54+ self .addParameter (QgsProcessingParameterRasterLayer (
55+ self .INPUT ,
56+ self .tr ('Input file' )))
57+ self .addParameter (QgsProcessingParameterBoolean (
58+ self .PRJ_FILE_CREATE ,
59+ self .tr ('Create also .prj file' ), False ))
60+ self .addOutput (QgsProcessingOutputFile (self .WORLD_FILE ,
61+ self .tr ('World file' )))
62+ self .addOutput (QgsProcessingOutputFile (
63+ self .PRJ_FILE ,
64+ self .tr ('ESRI Shapefile prj file' )))
65+
5466 def name (self ):
5567 return 'extractprojection'
5668
5769 def displayName (self ):
5870 return self .tr ('Extract projection' )
5971
6072 def icon (self ):
61- return QIcon (os .path .join (pluginPath , 'images' , 'gdaltools' , 'projection-export.png' ))
73+ return QIcon (os .path .join (pluginPath , 'images' , 'gdaltools' ,
74+ 'projection-export.png' ))
6275
6376 def group (self ):
6477 return self .tr ('Raster projections' )
@@ -69,12 +82,19 @@ def groupId(self):
6982 def commandName (self ):
7083 return 'extractprojection'
7184
72- def getConsoleCommands (self , parameters , context , feedback , executing = True ):
85+ def getConsoleCommands (self , parameters , context , feedback ,
86+ executing = True ):
7387 return [self .commandName ()]
7488
7589 def processAlgorithm (self , parameters , context , feedback ):
76- createPrj = QgsProcessingParameterBoolean (self .PRJ_FILE )
77- raster = self .parameterAsRasterLayer (parameters , self .INPUT , context )
90+ createPrj = self .parameterAsBool (parameters ,
91+ self .PRJ_FILE_CREATE ,
92+ context )
93+ raster = self .parameterAsRasterLayer (parameters , self .INPUT ,
94+ context )
95+ if not raster .dataProvider ().name () == 'gdal' :
96+ raise QgsProcessingException ('This algorithm can only ' \
97+ 'be used with GDAL raster layers' )
7898 rasterPath = raster .source ()
7999 rasterDS = gdal .Open (rasterPath , gdal .GA_ReadOnly )
80100 geotransform = rasterDS .GetGeoTransform ()
@@ -85,6 +105,7 @@ def processAlgorithm(self, parameters, context, feedback):
85105
86106 outFileName = os .path .splitext (str (rasterPath ))[0 ]
87107
108+ results = {}
88109 if crs != '' and createPrj :
89110 tmp = osr .SpatialReference ()
90111 tmp .ImportFromWkt (crs )
@@ -94,6 +115,9 @@ def processAlgorithm(self, parameters, context, feedback):
94115
95116 with open (outFileName + '.prj' , 'wt' ) as prj :
96117 prj .write (crs )
118+ results [self .PRJ_FILE ] = outFileName + '.prj'
119+ else :
120+ results [self .PRJ_FILE ] = None
97121
98122 with open (outFileName + '.wld' , 'wt' ) as wld :
99123 wld .write ('%0.8f\n ' % geotransform [1 ])
@@ -106,5 +130,6 @@ def processAlgorithm(self, parameters, context, feedback):
106130 wld .write ('%0.8f\n ' % (geotransform [3 ] +
107131 0.5 * geotransform [4 ] +
108132 0.5 * geotransform [5 ]))
109- return {}
133+ results [ self . WORLD_FILE ] = outFileName + '.wld'
110134
135+ return results
0 commit comments