Skip to content

Commit

Permalink
fix running algs with virtual layers
Browse files Browse the repository at this point in the history
  • Loading branch information
Admire Nyakudya authored and Admire Nyakudya committed Dec 20, 2024
1 parent 8edbf86 commit 73e2be4
Show file tree
Hide file tree
Showing 12 changed files with 52 additions and 8 deletions.
11 changes: 10 additions & 1 deletion python/plugins/processing/algs/gdal/Buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,13 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):
source_details = self.getOgrCompatibleSource(
self.INPUT, parameters, context, feedback, executing
)
if not source_details.layer_name:
raise QgsProcessingException(self.invalidSourceError(parameters, self.INPUT))

geometry = self.parameterAsString(parameters, self.GEOMETRY, context)
if source_details.geometry_column_name:
geometry = source_details.geometry_column_name
else:
geometry = self.parameterAsString(parameters, self.GEOMETRY, context)
distance = self.parameterAsDouble(parameters, self.DISTANCE, context)
fieldName = self.parameterAsString(parameters, self.FIELD, context)
dissolve = self.parameterAsBoolean(parameters, self.DISSOLVE, context)
Expand All @@ -149,6 +154,7 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):

other_fields = ",*" if other_fields_exist else ""


arguments = [
output_details.connection_string,
source_details.connection_string,
Expand All @@ -167,6 +173,9 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):

arguments.append(sql)

if source_details.geometry_column_name:
arguments.append('-nlt PROMOTE_TO_MULTI')

if self.parameterAsBoolean(parameters, self.EXPLODE_COLLECTIONS, context):
arguments.append("-explodecollections")

Expand Down
2 changes: 2 additions & 0 deletions python/plugins/processing/algs/gdal/ClipVectorByExtent.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):
input_details = self.getOgrCompatibleSource(
self.INPUT, parameters, context, feedback, executing
)
if not input_details.layer_name:
raise QgsProcessingException(self.invalidSourceError(parameters, self.INPUT))
source = self.parameterAsSource(parameters, self.INPUT, context)
if source is None:
raise QgsProcessingException(
Expand Down
2 changes: 2 additions & 0 deletions python/plugins/processing/algs/gdal/ClipVectorByMask.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):
input_details = self.getOgrCompatibleSource(
self.INPUT, parameters, context, feedback, executing
)
if not input_details.layer_name:
raise QgsProcessingException(self.invalidSourceError(parameters, self.INPUT))
mask_details = self.getOgrCompatibleSource(
self.MASK, parameters, context, feedback, executing
)
Expand Down
9 changes: 7 additions & 2 deletions python/plugins/processing/algs/gdal/Dissolve.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,14 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):
input_details = self.getOgrCompatibleSource(
self.INPUT, parameters, context, feedback, executing
)
geometry = self.parameterAsString(parameters, self.GEOMETRY, context)
fieldName = self.parameterAsString(parameters, self.FIELD, context)
if not input_details.layer_name:
raise QgsProcessingException(self.invalidSourceError(parameters, self.INPUT))
if input_details.geometry_column_name:
geometry = input_details.geometry_column_name
else:
geometry = self.parameterAsString(parameters, self.GEOMETRY, context)

fieldName = self.parameterAsString(parameters, self.FIELD, context)
options = self.parameterAsString(parameters, self.OPTIONS, context)
outFile = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
self.setOutputValue(self.OUTPUT, outFile)
Expand Down
2 changes: 2 additions & 0 deletions python/plugins/processing/algs/gdal/ExecuteSql.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):
input_details = self.getOgrCompatibleSource(
self.INPUT, parameters, context, feedback, executing
)
if not input_details.layer_name:
raise QgsProcessingException(self.invalidSourceError(parameters, self.INPUT))
sql = self.parameterAsString(parameters, self.SQL, context)
options = self.parameterAsString(parameters, self.OPTIONS, context)
outFile = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
Expand Down
3 changes: 2 additions & 1 deletion python/plugins/processing/algs/gdal/GdalAlgorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def getOgrCompatibleSource(
parameters = {parameter_name: parameters[parameter_name].source}

input_layer = self.parameterAsVectorLayer(parameters, parameter_name, context)
if input_layer is None or input_layer.providerType() in ("memory", "grass"):
if input_layer is None or input_layer.providerType() in ('virtual', 'memory', 'grass'):
if executing:
# parameter is not a vector layer - try to convert to a source compatible with OGR
# and extract selection if required
Expand All @@ -108,6 +108,7 @@ def getOgrCompatibleSource(
return GdalConnectionDetails(
connection_string=ogr_data_path,
layer_name=GdalUtils.ogrLayerName(ogr_data_path),
geometry_column_name='geom',
)
else:
# not executing - don't waste time converting incompatible sources, just return dummy strings
Expand Down
1 change: 1 addition & 0 deletions python/plugins/processing/algs/gdal/GdalUtils.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class GdalConnectionDetails:
open_options: Optional[list[str]] = None
layer_name: Optional[str] = None
credential_options: Optional[dict] = None
geometry_column_name: Optional[str] = None

def open_options_as_arguments(self) -> list[str]:
"""
Expand Down
5 changes: 4 additions & 1 deletion python/plugins/processing/algs/gdal/OgrToPostGis.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,10 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):
pk = self.parameterAsString(parameters, self.PK, context)
pkstring = "-lco FID=" + pk
primary_key = self.parameterAsString(parameters, self.PRIMARY_KEY, context)
geocolumn = self.parameterAsString(parameters, self.GEOCOLUMN, context)
if input_details.geometry_column_name:
geocolumn = input_details.geometry_column_name
else:
geocolumn = self.parameterAsString(parameters, self.GEOCOLUMN, context)
geocolumnstring = "-lco GEOMETRY_NAME=" + geocolumn
dim = self.DIMLIST[self.parameterAsEnum(parameters, self.DIM, context)]
dimstring = "-lco DIM=" + dim
Expand Down
7 changes: 6 additions & 1 deletion python/plugins/processing/algs/gdal/OneSideBuffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,12 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):
input_details = self.getOgrCompatibleSource(
self.INPUT, parameters, context, feedback, executing
)
geometry = self.parameterAsString(parameters, self.GEOMETRY, context)
if not input_details.layer_name:
raise QgsProcessingException(self.invalidSourceError(parameters, self.INPUT))
if input_details.geometry_column_name:
geometry = input_details.geometry_column_name
else:
geometry = self.parameterAsString(parameters, self.GEOMETRY, context)
distance = self.parameterAsDouble(parameters, self.DISTANCE, context)
side = self.parameterAsEnum(parameters, self.BUFFER_SIDE, context)
fieldName = self.parameterAsString(parameters, self.FIELD, context)
Expand Down
9 changes: 8 additions & 1 deletion python/plugins/processing/algs/gdal/PointsAlongLines.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,15 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):
input_details = self.getOgrCompatibleSource(
self.INPUT, parameters, context, feedback, executing
)
if not input_details.layer_name:
raise QgsProcessingException(
self.invalidSourceError(parameters, self.INPUT)
)
distance = self.parameterAsDouble(parameters, self.DISTANCE, context)
geometry = self.parameterAsString(parameters, self.GEOMETRY, context)
if input_details.geometry_column_name:
geometry = input_details.geometry_column_name
else:
geometry = self.parameterAsString(parameters, self.GEOMETRY, context)
outFile = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
self.setOutputValue(self.OUTPUT, outFile)
options = self.parameterAsString(parameters, self.OPTIONS, context)
Expand Down
2 changes: 2 additions & 0 deletions python/plugins/processing/algs/gdal/ogr2ogr.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):
input_details = self.getOgrCompatibleSource(
self.INPUT, parameters, context, feedback, executing
)
if not input_details.layer_name:
raise QgsProcessingException(self.invalidSourceError(parameters, self.INPUT))
convertAllLayers = self.parameterAsBoolean(
parameters, self.CONVERT_ALL_LAYERS, context
)
Expand Down
7 changes: 6 additions & 1 deletion python/plugins/processing/algs/gdal/ogr2ogrtopostgislist.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,8 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):
input_details = self.getOgrCompatibleSource(
self.INPUT, parameters, context, feedback, executing
)
if not input_details.layer_name:
raise QgsProcessingException(self.invalidSourceError(parameters, self.INPUT))
shapeEncoding = self.parameterAsString(parameters, self.SHAPE_ENCODING, context)
ssrs = self.parameterAsCrs(parameters, self.S_SRS, context)
tsrs = self.parameterAsCrs(parameters, self.T_SRS, context)
Expand All @@ -373,7 +375,10 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):
pk = self.parameterAsString(parameters, self.PK, context)
pkstring = "-lco FID=" + pk
primary_key = self.parameterAsString(parameters, self.PRIMARY_KEY, context)
geocolumn = self.parameterAsString(parameters, self.GEOCOLUMN, context)
if input_details.geometry_column_name:
geocolumn = input_details.geometry_column_name
else:
geocolumn = self.parameterAsString(parameters, self.GEOCOLUMN, context)
geocolumnstring = "-lco GEOMETRY_NAME=" + geocolumn
dim = self.DIMLIST[self.parameterAsEnum(parameters, self.DIM, context)]
dimstring = "-lco DIM=" + dim
Expand Down

0 comments on commit 73e2be4

Please sign in to comment.