32
32
from osgeo import gdal , osr
33
33
34
34
from processing .algs .gdal .GdalAlgorithm import GdalAlgorithm
35
+ from qgis .core import QgsProcessingException
35
36
from qgis .core import (QgsProcessingParameterRasterLayer ,
36
- QgsProcessingParameterBoolean )
37
+ QgsProcessingParameterBoolean ,
38
+ QgsProcessingOutputFile )
37
39
38
40
pluginPath = os .path .split (os .path .split (os .path .dirname (__file__ ))[0 ])[0 ]
39
41
40
42
41
43
class ExtractProjection (GdalAlgorithm ):
42
44
43
45
INPUT = 'INPUT'
46
+ PRJ_FILE_CREATE = 'PRJ_FILE_CREATE'
47
+ WORLD_FILE = 'WORLD_FILE'
44
48
PRJ_FILE = 'PRJ_FILE'
45
49
46
50
def __init__ (self ):
47
51
super ().__init__ ()
48
52
49
53
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
+
54
66
def name (self ):
55
67
return 'extractprojection'
56
68
57
69
def displayName (self ):
58
70
return self .tr ('Extract projection' )
59
71
60
72
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' ))
62
75
63
76
def group (self ):
64
77
return self .tr ('Raster projections' )
@@ -69,12 +82,19 @@ def groupId(self):
69
82
def commandName (self ):
70
83
return 'extractprojection'
71
84
72
- def getConsoleCommands (self , parameters , context , feedback , executing = True ):
85
+ def getConsoleCommands (self , parameters , context , feedback ,
86
+ executing = True ):
73
87
return [self .commandName ()]
74
88
75
89
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' )
78
98
rasterPath = raster .source ()
79
99
rasterDS = gdal .Open (rasterPath , gdal .GA_ReadOnly )
80
100
geotransform = rasterDS .GetGeoTransform ()
@@ -85,6 +105,7 @@ def processAlgorithm(self, parameters, context, feedback):
85
105
86
106
outFileName = os .path .splitext (str (rasterPath ))[0 ]
87
107
108
+ results = {}
88
109
if crs != '' and createPrj :
89
110
tmp = osr .SpatialReference ()
90
111
tmp .ImportFromWkt (crs )
@@ -94,6 +115,9 @@ def processAlgorithm(self, parameters, context, feedback):
94
115
95
116
with open (outFileName + '.prj' , 'wt' ) as prj :
96
117
prj .write (crs )
118
+ results [self .PRJ_FILE ] = outFileName + '.prj'
119
+ else :
120
+ results [self .PRJ_FILE ] = None
97
121
98
122
with open (outFileName + '.wld' , 'wt' ) as wld :
99
123
wld .write ('%0.8f\n ' % geotransform [1 ])
@@ -106,5 +130,6 @@ def processAlgorithm(self, parameters, context, feedback):
106
130
wld .write ('%0.8f\n ' % (geotransform [3 ] +
107
131
0.5 * geotransform [4 ] +
108
132
0.5 * geotransform [5 ]))
109
- return {}
133
+ results [ self . WORLD_FILE ] = outFileName + '.wld'
110
134
135
+ return results
0 commit comments