Skip to content
Permalink
Browse files

[processing][gdal] Fix layer paths are sometimes incorrect within com…

…mand preview

Fixes #19451

(cherry-picked from ed94b69)
  • Loading branch information
nyalldawson committed Jul 24, 2018
1 parent 952380c commit 9ba794c12f919e0d61bc405abacddf0412b41765
@@ -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

@@ -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=
@@ -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))
@@ -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')
@@ -60,6 +60,7 @@
QgsGeometry,
QgsPointXY,
QgsProject,
QgsVectorLayer,
QgsRectangle,
QgsProcessingException,
QgsProcessingFeatureSourceDefinition)
@@ -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",

0 comments on commit 9ba794c

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