Skip to content

Commit

Permalink
[processing][gdal] Don't double quote output paths
Browse files Browse the repository at this point in the history
Breaks for some drivers, e.g. KML

Fixes #18264
  • Loading branch information
nyalldawson committed Jun 1, 2018
1 parent 30ecb33 commit bd65fc6
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 13 deletions.
4 changes: 2 additions & 2 deletions python/plugins/processing/algs/gdal/GdalUtils.py
Expand Up @@ -276,7 +276,7 @@ def ogrConnectionStringAndFormat(uri, context):
if layer is None: if layer is None:
path, ext = os.path.splitext(uri) path, ext = os.path.splitext(uri)
format = QgsVectorFileWriter.driverForExtension(ext) format = QgsVectorFileWriter.driverForExtension(ext)
return '"' + uri + '"', '"' + format + '"' return uri, '"' + format + '"'


provider = layer.dataProvider().name() provider = layer.dataProvider().name()
if provider == 'spatialite': if provider == 'spatialite':
Expand Down Expand Up @@ -348,7 +348,7 @@ def ogrConnectionStringAndFormat(uri, context):
path, ext = os.path.splitext(ogrstr) path, ext = os.path.splitext(ogrstr)
format = QgsVectorFileWriter.driverForExtension(ext) format = QgsVectorFileWriter.driverForExtension(ext)


return '"' + ogrstr + '"', '"' + format + '"' return ogrstr, '"' + format + '"'


@staticmethod @staticmethod
def ogrLayerName(uri): def ogrLayerName(uri):
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/gdal/ogr2ogr.py
Expand Up @@ -82,7 +82,7 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):
output, outputFormat = GdalUtils.ogrConnectionStringAndFormat(outFile, context) output, outputFormat = GdalUtils.ogrConnectionStringAndFormat(outFile, context)


if outputFormat == 'SQLite' and os.path.isfile(output): if outputFormat == 'SQLite' and os.path.isfile(output):
raise QgsProcessinException(self.tr('Output file "{}" already exists.'.format(output))) raise QgsProcessingException(self.tr('Output file "{}" already exists.'.format(output)))


arguments = [] arguments = []
if outputFormat: if outputFormat:
Expand Down
2 changes: 2 additions & 0 deletions python/plugins/processing/gui/BatchAlgorithmDialog.py
Expand Up @@ -33,6 +33,7 @@


from qgis.core import (QgsProcessingParameterDefinition, from qgis.core import (QgsProcessingParameterDefinition,
QgsProcessingParameterRasterDestination, QgsProcessingParameterRasterDestination,
QgsProcessingParameterVectorDestination,
QgsProcessingParameterFeatureSink, QgsProcessingParameterFeatureSink,
QgsProcessingOutputLayerDefinition, QgsProcessingOutputLayerDefinition,
QgsProcessingOutputHtml, QgsProcessingOutputHtml,
Expand Down Expand Up @@ -99,6 +100,7 @@ def accept(self):
text = widget.getValue() text = widget.getValue()
if out.checkValueIsAcceptable(text): if out.checkValueIsAcceptable(text):
if isinstance(out, (QgsProcessingParameterRasterDestination, if isinstance(out, (QgsProcessingParameterRasterDestination,
QgsProcessingParameterVectorDestination,
QgsProcessingParameterFeatureSink)): QgsProcessingParameterFeatureSink)):
# load rasters and sinks on completion # load rasters and sinks on completion
parameters[out.name()] = QgsProcessingOutputLayerDefinition(text, project) parameters[out.name()] = QgsProcessingOutputLayerDefinition(text, project)
Expand Down
49 changes: 39 additions & 10 deletions python/plugins/processing/tests/GdalAlgorithmsTest.py
Expand Up @@ -41,6 +41,7 @@
from processing.algs.gdal.GridInverseDistanceNearestNeighbor import GridInverseDistanceNearestNeighbor from processing.algs.gdal.GridInverseDistanceNearestNeighbor import GridInverseDistanceNearestNeighbor
from processing.algs.gdal.GridLinear import GridLinear from processing.algs.gdal.GridLinear import GridLinear
from processing.algs.gdal.GridNearestNeighbor import GridNearestNeighbor from processing.algs.gdal.GridNearestNeighbor import GridNearestNeighbor
from processing.algs.gdal.ogr2ogr import ogr2ogr
from processing.algs.gdal.proximity import proximity from processing.algs.gdal.proximity import proximity
from processing.algs.gdal.rasterize import rasterize from processing.algs.gdal.rasterize import rasterize
from processing.algs.gdal.retile import retile from processing.algs.gdal.retile import retile
Expand Down Expand Up @@ -233,10 +234,10 @@ def _copyFile(dst):
def testOgrConnectionStringAndFormat(self): def testOgrConnectionStringAndFormat(self):
context = QgsProcessingContext() context = QgsProcessingContext()
output, outputFormat = GdalUtils.ogrConnectionStringAndFormat('d:/test/test.shp', context) output, outputFormat = GdalUtils.ogrConnectionStringAndFormat('d:/test/test.shp', context)
self.assertEqual(output, '"d:/test/test.shp"') self.assertEqual(output, 'd:/test/test.shp')
self.assertEqual(outputFormat, '"ESRI Shapefile"') self.assertEqual(outputFormat, '"ESRI Shapefile"')
output, outputFormat = GdalUtils.ogrConnectionStringAndFormat('d:/test/test.mif', context) output, outputFormat = GdalUtils.ogrConnectionStringAndFormat('d:/test/test.mif', context)
self.assertEqual(output, '"d:/test/test.mif"') self.assertEqual(output, 'd:/test/test.mif')
self.assertEqual(outputFormat, '"MapInfo File"') self.assertEqual(outputFormat, '"MapInfo File"')


def testCrsConversion(self): def testCrsConversion(self):
Expand Down Expand Up @@ -479,7 +480,7 @@ def testContour(self):
['gdal_contour', ['gdal_contour',
'-b 1 -a elev -i 5.0 -f "ESRI Shapefile" ' + '-b 1 -a elev -i 5.0 -f "ESRI Shapefile" ' +
source + ' ' + source + ' ' +
'"d:/temp/check.shp"']) 'd:/temp/check.shp'])
# with NODATA value # with NODATA value
self.assertEqual( self.assertEqual(
alg.getConsoleCommands({'INPUT': source, alg.getConsoleCommands({'INPUT': source,
Expand All @@ -491,7 +492,7 @@ def testContour(self):
['gdal_contour', ['gdal_contour',
'-b 1 -a elev -i 5.0 -snodata 9999.0 -f "ESRI Shapefile" ' + '-b 1 -a elev -i 5.0 -snodata 9999.0 -f "ESRI Shapefile" ' +
source + ' ' + source + ' ' +
'"d:/temp/check.shp"']) 'd:/temp/check.shp'])
# with "0" NODATA value # with "0" NODATA value
self.assertEqual( self.assertEqual(
alg.getConsoleCommands({'INPUT': source, alg.getConsoleCommands({'INPUT': source,
Expand All @@ -503,7 +504,7 @@ def testContour(self):
['gdal_contour', ['gdal_contour',
'-b 1 -a elev -i 5.0 -snodata 0.0 -f "GPKG" ' + '-b 1 -a elev -i 5.0 -snodata 0.0 -f "GPKG" ' +
source + ' ' + source + ' ' +
'"d:/temp/check.gpkg"']) 'd:/temp/check.gpkg'])


def testGdal2Tiles(self): def testGdal2Tiles(self):
context = QgsProcessingContext() context = QgsProcessingContext()
Expand Down Expand Up @@ -648,7 +649,7 @@ def testGdalTindex(self):
'OUTPUT': 'd:/temp/test.shp'}, context, feedback), 'OUTPUT': 'd:/temp/test.shp'}, context, feedback),
['gdaltindex', ['gdaltindex',
'-tileindex location -f "ESRI Shapefile" ' + '-tileindex location -f "ESRI Shapefile" ' +
'"d:/temp/test.shp" ' + 'd:/temp/test.shp ' +
source]) source])


# with input srs # with input srs
Expand All @@ -658,7 +659,7 @@ def testGdalTindex(self):
'OUTPUT': 'd:/temp/test.shp'}, context, feedback), 'OUTPUT': 'd:/temp/test.shp'}, context, feedback),
['gdaltindex', ['gdaltindex',
'-tileindex location -t_srs EPSG:3111 -f "ESRI Shapefile" ' + '-tileindex location -t_srs EPSG:3111 -f "ESRI Shapefile" ' +
'"d:/temp/test.shp" ' + 'd:/temp/test.shp ' +
source]) source])


# with target using proj string # with target using proj string
Expand All @@ -669,7 +670,7 @@ def testGdalTindex(self):
'OUTPUT': 'd:/temp/test.shp'}, context, feedback), 'OUTPUT': 'd:/temp/test.shp'}, context, feedback),
['gdaltindex', ['gdaltindex',
'-tileindex location -t_srs EPSG:20936 -f "ESRI Shapefile" ' + '-tileindex location -t_srs EPSG:20936 -f "ESRI Shapefile" ' +
'"d:/temp/test.shp" ' + 'd:/temp/test.shp ' +
source]) source])


# with target using custom projection # with target using custom projection
Expand All @@ -680,7 +681,7 @@ def testGdalTindex(self):
'OUTPUT': 'd:/temp/test.shp'}, context, feedback), 'OUTPUT': 'd:/temp/test.shp'}, context, feedback),
['gdaltindex', ['gdaltindex',
'-tileindex location -t_srs "+proj=utm +zone=36 +south +a=63785 +b=6357 +towgs84=-143,-90,-294,0,0,0,0 +units=m +no_defs" -f "ESRI Shapefile" ' + '-tileindex location -t_srs "+proj=utm +zone=36 +south +a=63785 +b=6357 +towgs84=-143,-90,-294,0,0,0,0 +units=m +no_defs" -f "ESRI Shapefile" ' +
'"d:/temp/test.shp" ' + 'd:/temp/test.shp ' +
source]) source])


# with non-EPSG crs code # with non-EPSG crs code
Expand All @@ -690,7 +691,7 @@ def testGdalTindex(self):
'OUTPUT': 'd:/temp/test.shp'}, context, feedback), 'OUTPUT': 'd:/temp/test.shp'}, context, feedback),
['gdaltindex', ['gdaltindex',
'-tileindex location -t_srs EPSG:3111 -f "ESRI Shapefile" ' + '-tileindex location -t_srs EPSG:3111 -f "ESRI Shapefile" ' +
'"d:/temp/test.shp" ' + 'd:/temp/test.shp ' +
source]) source])


def testGridAverage(self): def testGridAverage(self):
Expand Down Expand Up @@ -897,6 +898,34 @@ def testGridNearestNeighbour(self):
source + ' ' + source + ' ' +
'd:/temp/check.jpg']) 'd:/temp/check.jpg'])


def testOgr2Ogr(self):
context = QgsProcessingContext()
feedback = QgsProcessingFeedback()
source = os.path.join(testDataPath, 'polys.gml')
alg = ogr2ogr()
alg.initAlgorithm()

self.assertEqual(
alg.getConsoleCommands({'INPUT': source,
'OUTPUT': 'd:/temp/check.shp'}, context, feedback),
['ogr2ogr',
'-f "ESRI Shapefile" d:/temp/check.shp ' +
source + ' polys2'])

self.assertEqual(
alg.getConsoleCommands({'INPUT': source,
'OUTPUT': 'd:/temp/check.kml'}, context, feedback),
['ogr2ogr',
'-f "LIBKML" d:/temp/check.kml ' +
source + ' polys2'])

self.assertEqual(
alg.getConsoleCommands({'INPUT': source,
'OUTPUT': 'd:/temp/my out/check.kml'}, context, feedback),
['ogr2ogr',
'-f "LIBKML" "d:/temp/my out/check.kml" ' +
source + ' polys2'])

def testProximity(self): def testProximity(self):
context = QgsProcessingContext() context = QgsProcessingContext()
feedback = QgsProcessingFeedback() feedback = QgsProcessingFeedback()
Expand Down

0 comments on commit bd65fc6

Please sign in to comment.