Port gdal assign projection to new api

This commit is contained in:
Nyall Dawson 2017-08-14 05:41:54 +10:00
parent 19e5886b87
commit ab2886dc63
3 changed files with 23 additions and 14 deletions

View File

@ -29,10 +29,10 @@ import os
from qgis.PyQt.QtGui import QIcon
from qgis.core import (QgsProcessingParameterRasterLayer,
QgsProcessingParameterCrs,
QgsProcessingOutputRasterLayer)
from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm
from processing.core.parameters import ParameterRaster
from processing.core.parameters import ParameterCrs
from processing.core.outputs import OutputRaster
from processing.algs.gdal.GdalUtils import GdalUtils
from processing.tools.system import isWindows
@ -50,11 +50,11 @@ class AssignProjection(GdalAlgorithm):
super().__init__()
def initAlgorithm(self, config=None):
self.addParameter(ParameterRaster(self.INPUT, self.tr('Input layer'), False))
self.addParameter(ParameterCrs(self.CRS,
self.tr('Desired CRS'), ''))
self.addParameter(QgsProcessingParameterRasterLayer(self.INPUT, self.tr('Input layer'), optional=False))
self.addParameter(QgsProcessingParameterCrs(self.CRS,
self.tr('Desired CRS')))
self.addOutput(OutputRaster(self.OUTPUT, self.tr('Layer with projection'), True))
self.addOutput(QgsProcessingOutputRasterLayer(self.OUTPUT, self.tr('Layer with projection')))
def name(self):
return 'assignprojection'
@ -69,9 +69,10 @@ class AssignProjection(GdalAlgorithm):
return self.tr('Raster projections')
def getConsoleCommands(self, parameters, context, feedback):
fileName = self.getParameterValue(self.INPUT)
crs = self.getParameterValue(self.CRS)
output = self.getOutputValue(self.OUTPUT) # NOQA
inLayer = self.parameterAsRasterLayer(parameters, self.INPUT, context)
fileName = inLayer.source()
crs = self.parameterAsCrs(parameters, self.CRS, context).authid()
arguments = []
arguments.append('-a_srs')

View File

@ -45,6 +45,7 @@ class GdalAlgorithm(QgsProcessingAlgorithm):
def __init__(self):
super().__init__()
self.output_values = {}
def icon(self):
return QgsApplication.getThemeIcon("/providerGdal.svg")
@ -89,6 +90,9 @@ class GdalAlgorithm(QgsProcessingAlgorithm):
ogr_layer_name = GdalUtils.ogrLayerName(input_layer.dataProvider().dataSourceUri())
return ogr_data_path, ogr_layer_name
def setOutputValue(self, name, value):
self.output_values[name] = value
def processAlgorithm(self, parameters, context, feedback):
commands = self.getConsoleCommands(parameters, context, feedback)
GdalUtils.runGdal(commands, feedback)
@ -96,7 +100,11 @@ class GdalAlgorithm(QgsProcessingAlgorithm):
# auto generate outputs
results = {}
for o in self.outputDefinitions():
results[o.name()] = parameters[o.name()]
if o.name() in parameters:
results[o.name()] = parameters[o.name()]
for k, v in self.output_values.items():
results[k]=v
return results
def helpUrl(self):

View File

@ -33,6 +33,7 @@ from qgis.core import (QgsApplication,
from processing.core.ProcessingConfig import ProcessingConfig, Setting
from .GdalUtils import GdalUtils
from .AssignProjection import AssignProjection
from .aspect import aspect
from .warp import warp
# from .nearblack import nearblack
@ -69,8 +70,7 @@ from .warp import warp
# from .rasterize_over import rasterize_over
# from .retile import retile
# from .gdal2tiles import gdal2tiles
# from .AssignProjection import AssignProjection
#
from .ogr2ogrpointsonlines import Ogr2OgrPointsOnLines
from .ogr2ogrtopostgis import Ogr2OgrToPostGis
@ -143,6 +143,7 @@ class GdalAlgorithmProvider(QgsProcessingProvider):
self.algs = [
# nearblack(),
# information(),
AssignProjection(),
aspect(),
warp(),
# translate(),
@ -176,7 +177,6 @@ class GdalAlgorithmProvider(QgsProcessingProvider):
# rasterize_over(),
# retile(),
# gdal2tiles(),
# AssignProjection(),
# ----- OGR tools -----
Ogr2OgrPointsOnLines(),
Ogr2OgrToPostGis(),