Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix running gdal algs with virtual layers #59985

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 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,15 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):
source_details = self.getOgrCompatibleSource(
self.INPUT, parameters, context, feedback, executing
)
if not source_details.layer_name:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these changes (and all the others below, except the virtual layers part) related to the PR title "fix running gdal algs with virtual layers" ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

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 Down Expand Up @@ -167,6 +174,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
4 changes: 4 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,10 @@ 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
5 changes: 5 additions & 0 deletions python/plugins/processing/algs/gdal/ClipVectorByMask.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
QgsProcessingParameterDefinition,
QgsProcessingParameterString,
QgsProcessingParameterFeatureSource,
QgsProcessingException,
QgsProcessingParameterVectorDestination,
)
from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm
Expand Down Expand Up @@ -94,6 +95,10 @@ 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
11 changes: 9 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,16 @@ 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
4 changes: 4 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,10 @@ 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
7 changes: 6 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,11 @@ 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 +112,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
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/gdal/GdalAlgorithmDialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def parametersHaveChanged(self):

try:
commands = self.algorithm().getConsoleCommands(
parameters, context, feedback, executing=False
parameters, context, feedback, executing=True
)
commands = [c for c in commands if c not in ["cmd.exe", "/C "]]
self.text.setPlainText(" ".join(commands))
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
9 changes: 8 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,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)
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
4 changes: 4 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,10 @@ 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
9 changes: 8 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,10 @@ 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 +377,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
Loading