qgis / QGIS

QGIS is a free, open source, cross platform (lin/win/mac) geographical information system (GIS)
https://qgis.org
GNU General Public License v2.0
10.62k stars 3.01k forks source link

GDAL "clip raster by mask layer" produces faulty command when mask layer is a PostGIS layer #47484

Open BartStikkers opened 2 years ago

BartStikkers commented 2 years ago

What is the bug or the crash?

When I use the "clip raster by mask layer" function, the generated GDAL command line code looks like this:

gdalwarp -overwrite -of GTiff -cutline "[connection string]'" -cl "(SELECT * FROM countries WHERE admin = 'Spain')" -crop_to_cutline C:/INPUT.tif C:/OUTPUT.tif

But this produces an error:

ERROR 1: Failed to identify source layer from datasource. Process returned error code 1

Testing it in the command line, this does work:

gdalwarp -overwrite -of GTiff -cutline "[connection string]'" -csql "(SELECT * FROM countries WHERE admin = 'Spain')" -crop_to_cutline C:/INPUT.tif C:/OUTPUT.tif

So when the mask layer is a postGIS layer the flag should be -csql instead of -cl.

In addition, the code that's responsible for pasting in the SQL statement isn't working properly. It works fine when I change the statement to be like what you see above, but when I write perfectly functional SQL like this:

SELECT * FROM "staging"."countries"
WHERE "admin" = 'Spain'

the resulting statement in the GDAL command looks like this:

gdalwarp -overwrite -of GTiff -cutline "[connection string]" -cl "(SELECT * FROM \\" -crop_to_cutline [etc]

So something about the way escaped characters are parsed isn't going right.

Steps to reproduce the issue

  1. Add vector layer from postGIS database
  2. Add raster layer to be clipped from file
  3. Alter SQL code of vector layer to include a WHERE clause (RMB on layer in panel, "Update SQL layer...")
  4. Leave the double quotes in the two part identifier for the parsing bug, otherwise remove them
  5. Go to Raster, Extraction, Clip Raster by Mask Layer
  6. Select the raster layer as Input layer and the vector layer as Mask layer
  7. Scroll down to GDAL/OGR console call to observe the incorrect statement
  8. Click Run to get an error message.

Versions

QGIS version | 3.20.3-Odense | QGIS code revision | 495fbaec -- | -- | -- | -- Qt version | 5.15.2 Python version | 3.9.5 GDAL/OGR version | 3.3.1 PROJ version | 8.1.1 EPSG Registry database version | v10.028 (2021-07-07) GEOS version | 3.9.1-CAPI-1.14.2 SQLite version | 3.35.2 PDAL version | 2.3.0 PostgreSQL client version | 13.0 SpatiaLite version | 5.0.1 QWT version | 6.1.3 QScintilla2 version | 2.11.5 OS version | Windows 10 Version 2009   |   |   |   Active Python plugins | ClipMultipleLayersee_pluginLDMPquick_map_servicesdb_managerMetaSearchprocessing QGIS version 3.20.3-Odense QGIS code revision [495fbaec](https://github.com/qgis/QGIS/commit/495fbaec) Qt version 5.15.2 Python version 3.9.5 GDAL/OGR version 3.3.1 PROJ version 8.1.1 EPSG Registry database version v10.028 (2021-07-07) GEOS version 3.9.1-CAPI-1.14.2 SQLite version 3.35.2 PDAL version 2.3.0 PostgreSQL client version 13.0 SpatiaLite version 5.0.1 QWT version 6.1.3 QScintilla2 version 2.11.5 OS version Windows 10 Version 2009 Active Python plugins ClipMultipleLayers ee_plugin LDMP quick_map_services db_manager MetaSearch processing ### Supported QGIS version - [X] I'm running a supported QGIS version according to the roadmap. ### New profile - [X] I tried with a new QGIS profile ### Additional context _No response_
gioman commented 2 years ago

Alter SQL code of vector layer to include a WHERE clause (RMB on layer in panel, "Update SQL layer...")

@BartStikkers I don't think that a filtering a layer with "update SQL layer" is something supported in Processing. For sure is supported the filter that you can add with right click > filter.

BartStikkers commented 2 years ago

I don't think that a filtering a layer with "update SQL layer" is something supported in Processing.

@gioman I'm not sure what you mean by that. It goes wrong because it enters the whole SQL statement if you change the query in any way beyond "SELECT * FROM [table]", not just if you add a WHERE statement. GDAL supports this just fine, the QGIS dialog just doesn't generate the console call properly. Since it does try to, I'm assuming it's intended to work.

gioman commented 2 years ago

Since it does try to, I'm assuming it's intended to work.

yes maybe.

BartStikkers commented 2 years ago

Also:

For sure is supported the filter that you can add with right click > filter.

This is not the case. When I try it, the filter is ignored and the entire vector layer is used as a mask. I can't use the "Selected features only" option either, it remains grayed out.

gioman commented 2 years ago

This is not the case. When I try it, the filter is ignored and the entire vector layer is used as a mask.

@BartStikkers can't confirm.

I can't use the "Selected features only" option either, it remains grayed out.

to use this option the selection must be done on map (manually, or selecting by value/expression/etc.).