Skip to content
Permalink
Browse files

[processing][gdal] Fix conversion of non-EPSG crs parameters

to GDAL compatible strings

(cherry-picked from 0d52045)
  • Loading branch information
nyalldawson committed May 11, 2018
1 parent cd6a14b commit 15c2c9b3e10c5a94d4a742a443a11d4280fdf5d3
@@ -80,11 +80,11 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):
inLayer = self.parameterAsRasterLayer(parameters, self.INPUT, context)
fileName = inLayer.source()

crs = self.parameterAsCrs(parameters, self.CRS, context).authid()
crs = self.parameterAsCrs(parameters, self.CRS, context)

arguments = []
arguments.append('-a_srs')
arguments.append(crs)
arguments.append(GdalUtils.gdal_crs_string(crs))

arguments.append(fileName)

@@ -37,6 +37,7 @@

from qgis.core import (Qgis,
QgsApplication,
QgsCoordinateReferenceSystem,
QgsVectorFileWriter,
QgsProcessingFeedback,
QgsProcessingUtils,
@@ -411,3 +412,17 @@ def writeLayerParameterToTextFile(filename, alg, parameters, parameter_name, con
layers.append(l.source())
f.write('\n'.join(layers))
return listFile

@staticmethod
def gdal_crs_string(crs: QgsCoordinateReferenceSystem) -> str:
"""
Converts a QgsCoordinateReferenceSystem to a string understandable
by GDAL
:param crs: crs to convert
:return: gdal friendly string
"""
if crs.authid().upper().startswith('EPSG:'):
return crs.authid()

# fallback to proj4 string
return crs.toProj4()
@@ -190,9 +190,9 @@ def getConnectionString(self, parameters, context):
def getConsoleCommands(self, parameters, context, feedback, executing=True):
ogrLayer, layername = self.getOgrCompatibleSource(self.INPUT, parameters, context, feedback, executing)
shapeEncoding = self.parameterAsString(parameters, self.SHAPE_ENCODING, context)
ssrs = self.parameterAsCrs(parameters, self.S_SRS, context).authid()
tsrs = self.parameterAsCrs(parameters, self.T_SRS, context).authid()
asrs = self.parameterAsCrs(parameters, self.A_SRS, context).authid()
ssrs = self.parameterAsCrs(parameters, self.S_SRS, context)
tsrs = self.parameterAsCrs(parameters, self.T_SRS, context)
asrs = self.parameterAsCrs(parameters, self.A_SRS, context)
table = self.parameterAsString(parameters, self.TABLE, context)
schema = self.parameterAsString(parameters, self.SCHEMA, context)
pk = self.parameterAsString(parameters, self.PK, context)
@@ -261,15 +261,15 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):
table = '{}.{}'.format(schema, table)
arguments.append('-nln')
arguments.append(table)
if len(ssrs) > 0:
if ssrs.isValid():
arguments.append('-s_srs')
arguments.append(ssrs)
if len(tsrs) > 0:
arguments.append(GdalUtils.gdal_crs_string(ssrs))
if tsrs.isValid():
arguments.append('-t_srs')
arguments.append(tsrs)
if len(asrs) > 0:
arguments.append(GdalUtils.gdal_crs_string(tsrs))
if asrs.isValid():
arguments.append('-a_srs')
arguments.append(asrs)
arguments.append(GdalUtils.gdal_crs_string(asrs))
if not spat.isNull():
arguments.append('-spat')
arguments.append(spat.xMinimum())
@@ -187,7 +187,7 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):
crs = self.parameterAsCrs(parameters, self.SOURCE_CRS, context)
if crs.isValid():
arguments.append('-s')
arguments.append(crs.authid())
arguments.append(GdalUtils.gdal_crs_string(crs))

if self.NODATA in parameters and parameters[self.NODATA] is not None:
nodata = self.parameterAsDouble(parameters, self.NODATA, context)
@@ -153,7 +153,8 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):
arguments.append('-src_srs_format {}'.format(self.modes[crs_format][1]))

if target_crs.isValid():
arguments.append('-t_srs {}'.format(target_crs.authid()))
arguments.append('-t_srs')
arguments.append(GdalUtils.gdal_crs_string(target_crs))

if outFormat:
arguments.append('-f {}'.format(outFormat))
@@ -188,9 +188,9 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):

ogrLayer, layername = self.getOgrCompatibleSource(self.INPUT, parameters, context, feedback, executing)
shapeEncoding = self.parameterAsString(parameters, self.SHAPE_ENCODING, context)
ssrs = self.parameterAsCrs(parameters, self.S_SRS, context).authid()
tsrs = self.parameterAsCrs(parameters, self.T_SRS, context).authid()
asrs = self.parameterAsCrs(parameters, self.A_SRS, context).authid()
ssrs = self.parameterAsCrs(parameters, self.S_SRS, context)
tsrs = self.parameterAsCrs(parameters, self.T_SRS, context)
asrs = self.parameterAsCrs(parameters, self.A_SRS, context)
table = self.parameterAsString(parameters, self.TABLE, context)
schema = self.parameterAsString(parameters, self.SCHEMA, context)
pk = self.parameterAsString(parameters, self.PK, context)
@@ -261,15 +261,15 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):
table = '{}.{}'.format(schema, table)
arguments.append('-nln')
arguments.append(table)
if len(ssrs) > 0:
if ssrs.isValid():
arguments.append('-s_srs')
arguments.append(ssrs)
if len(tsrs) > 0:
arguments.append(GdalUtils.gdal_crs_string(ssrs))
if tsrs.isValid():
arguments.append('-t_srs')
arguments.append(tsrs)
if len(asrs) > 0:
arguments.append(GdalUtils.gdal_crs_string(tsrs))
if asrs.isValid():
arguments.append('-a_srs')
arguments.append(asrs)
arguments.append(GdalUtils.gdal_crs_string(asrs))
if not spat.isNull():
arguments.append('-spat')
arguments.append(spat.xMinimum())
@@ -177,7 +177,7 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):
crs = self.parameterAsCrs(parameters, self.SOURCE_CRS, context)
if crs.isValid():
arguments.append('-s_srs')
arguments.append(crs.authid())
arguments.append(GdalUtils.gdal_crs_string(crs))

arguments.append('-r')
arguments.append(self.methods[self.parameterAsEnum(parameters, self.RESAMPLING, context)][1])
@@ -126,7 +126,7 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):
crs = self.parameterAsCrs(parameters, self.TARGET_CRS, context)
if crs.isValid():
arguments.append('-a_srs')
arguments.append(crs.authid())
arguments.append(GdalUtils.gdal_crs_string(crs))

if nodata is not None:
arguments.append('-a_nodata')
@@ -175,11 +175,11 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):
arguments = []
if sourceCrs.isValid():
arguments.append('-s_srs')
arguments.append(sourceCrs.authid())
arguments.append(GdalUtils.gdal_crs_string(sourceCrs))

if targetCrs.isValid():
arguments.append('-t_srs')
arguments.append(targetCrs.authid())
arguments.append(GdalUtils.gdal_crs_string(targetCrs))

if nodata is not None:
arguments.append('-dstnodata')

0 comments on commit 15c2c9b

Please sign in to comment.
You can’t perform that action at this time.