Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[processing][gdal] Fix layer paths are sometimes incorrect within com…
…mand preview

Fixes #19451

(cherry-picked from ed94b69)
  • Loading branch information
nyalldawson committed Jul 25, 2018
1 parent 952380c commit 9ba794c
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 5 deletions.
4 changes: 2 additions & 2 deletions python/plugins/processing/algs/gdal/GdalAlgorithm.py
Expand Up @@ -103,12 +103,12 @@ def getOgrCompatibleSource(self, parameter_name, parameters, context, feedback,
else:
#not executing - don't worry about 'selected features only' handling. It has no meaning
#for the command line preview since it has no meaning outside of a QGIS session!
ogr_data_path = GdalUtils.ogrConnectionString(input_layer.dataProvider().dataSourceUri(), context)[1:-1]
ogr_data_path = GdalUtils.ogrConnectionStringAndFormatFromLayer(input_layer)[0]
ogr_layer_name = GdalUtils.ogrLayerName(input_layer.dataProvider().dataSourceUri())
else:
# vector layer, but not OGR - get OGR compatible path
# TODO - handle "selected features only" mode!!
ogr_data_path = GdalUtils.ogrConnectionString(input_layer.dataProvider().dataSourceUri(), context)[1:-1]
ogr_data_path = GdalUtils.ogrConnectionString(input_layer.dataProvider().dataSourceUri(), context)
ogr_layer_name = GdalUtils.ogrLayerName(input_layer.dataProvider().dataSourceUri())
return ogr_data_path, ogr_layer_name

Expand Down
4 changes: 4 additions & 0 deletions python/plugins/processing/algs/gdal/GdalUtils.py
Expand Up @@ -278,6 +278,10 @@ def ogrConnectionStringAndFormat(uri, context):
format = QgsVectorFileWriter.driverForExtension(ext)
return uri, '"' + format + '"'

return GdalUtils.ogrConnectionStringAndFormatFromLayer(layer)

@staticmethod
def ogrConnectionStringAndFormatFromLayer(layer):
provider = layer.dataProvider().name()
if provider == 'spatialite':
# dbname='/geodata/osm_ch.sqlite' table="places" (Geometry) sql=
Expand Down
Expand Up @@ -130,7 +130,7 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):
uri = GeoDB(uri=uri).uri

inLayer = self.getParameterValue(self.INPUT_LAYER)
ogrLayer = GdalUtils.ogrConnectionString(inLayer, context)[1:-1]
ogrLayer = GdalUtils.ogrConnectionString(inLayer, context)
shapeEncoding = self.getParameterValue(self.SHAPE_ENCODING)
schema = str(self.getParameterValue(self.SCHEMA))
table = str(self.getParameterValue(self.TABLE))
Expand Down
4 changes: 2 additions & 2 deletions python/plugins/processing/algs/gdal/rasterize_over.py
Expand Up @@ -78,8 +78,8 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):
inLayer = QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.INPUT), context)
inRasterLayer = QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.INPUT_RASTER), context)

ogrLayer = GdalUtils.ogrConnectionString(inLayer, context)[1:-1]
ogrRasterLayer = GdalUtils.ogrConnectionString(inRasterLayer, context)[1:-1]
ogrLayer = GdalUtils.ogrConnectionString(inLayer, context)
ogrRasterLayer = GdalUtils.ogrConnectionString(inRasterLayer, context)

arguments = []
arguments.append('-a')
Expand Down
19 changes: 19 additions & 0 deletions python/plugins/processing/tests/GdalAlgorithmsTest.py
Expand Up @@ -60,6 +60,7 @@
QgsGeometry,
QgsPointXY,
QgsProject,
QgsVectorLayer,
QgsRectangle,
QgsProcessingException,
QgsProcessingFeatureSourceDefinition)
Expand Down Expand Up @@ -154,6 +155,24 @@ def testGetOgrCompatibleSourceFromMemoryLayer(self):

QgsProject.instance().removeMapLayer(layer)

def testGetOgrCompatibleSourceFromOgrLayer(self):
p = QgsProject()
source = os.path.join(testDataPath, 'points.gml')
vl = QgsVectorLayer(source)
self.assertTrue(vl.isValid())
p.addMapLayer(vl)

context = QgsProcessingContext()
context.setProject(p)
feedback = QgsProcessingFeedback()

alg = ogr2ogr()
alg.initAlgorithm()
path, layer = alg.getOgrCompatibleSource('INPUT', {'INPUT': vl.id()}, context, feedback, True)
self.assertEqual(path, source)
path, layer = alg.getOgrCompatibleSource('INPUT', {'INPUT': vl.id()}, context, feedback, False)
self.assertEqual(path, source)

def testGetOgrCompatibleSourceFromFeatureSource(self):
# create a memory layer and add to project and context
layer = QgsVectorLayer("Point?field=fldtxt:string&field=fldint:integer",
Expand Down

0 comments on commit 9ba794c

Please sign in to comment.