Bug report #20415

Clip raster by mask layer

Added by Paul Day almost 2 years ago. Updated almost 2 years ago.

Category:GDAL Tools
Affected QGIS version:3.4.1 Regression?:No
Operating System: Easy fix?:No
Pull Request or Patch supplied:Yes Resolution:
Crashes QGIS or corrupts data:No Copied to github as #:28235


I generated a 10m raster (32-bit geoTIFF) using 'Rasterize (vector to raster)' [gdal_rasterize] with cells centred at whole 10m grid values - this gives raster extents to the outer edges with grid values ending in 5.

I clipped the raster using 'Clip raster by mask layer' [gdalwarp], but the results were shifted by half a cell (5m). I tried both with and without matching the extent of the clipped raster to the mask layer and got the same result, i.e. the data is offset NW by 5m and extents showing grid values ending in 0.

I have seen the same in v2.18.19 'Raster > Extraction > Clipper...' unless I force the extents using the -te option, which is possible using the Edit button to alter the command string, but this has disappeared from v3.x.

So, two points to note:

  • Clipping using gdalwarp (without -te) seems to shift the input data, which is not good
  • I see no way to specify output extents when using the tool in v3.x

Test Data files attached and processing results below...

Algorithm 'Rasterize (vector to raster)' starting…
Input parameters: { 'BURN' : 0, 'DATA_TYPE' : 5, 'EXTENT' : '502375, 505905, 106045, 108155', 'FIELD' : 'ogc_fid', 'HEIGHT' : 10, 'INIT' : 0, 'INPUT' : '_<filepath>_\\Input_Polygon.shp|layername=Input_Polygon', 'INVERT' : False, 'NODATA' : 0, 'OPTIONS' : 'COMPRESS=PACKBITS', 'OUTPUT' : '_<filepath>_/B_10m_Raster_Polygon.tif', 'UNITS' : 1, 'WIDTH' : 10 }

GDAL command:
gdal_rasterize -l Input_Polygon -a ogc_fid -tr 10.0 10.0 -a_nodata 0.0 -te 502375.0 106045.0 505905.0 108155.0 -ot Float32 -of GTiff -co COMPRESS=PACKBITS <filepath>\Input_Polygon.shp <filepath>/B_10m_Raster_Polygon.tif
GDAL command output:
0...10...20...30...40...50...60...70...80...90...100 - done.

Execution completed in 0.85 seconds
Results: {'OUTPUT': <QgsProcessingOutputLayerDefinition {'sink':_<filepath>_/B_10m_Raster_Polygon.tif, 'createOptions': {'fileEncoding': 'System'}}>}

Loading resulting layers
Algorithm 'Rasterize (vector to raster)' finished

Algorithm 'Clip raster by mask layer' starting…
Input parameters: { 'ALPHA_BAND' : False, 'CROP_TO_CUTLINE' : False, 'DATA_TYPE' : 0, 'INPUT' : 'S:/GIS_Expert_Services_PostAward/GIS/WorkingGIS/20180411_HabConn/99_GDAL_ClipTests/B_10m_Raster_Polygon.tif', 'KEEP_RESOLUTION' : True, 'MASK' : '_<filepath>_\\C_Clip_Polygon.shp', 'NODATA' : None, 'OPTIONS' : 'COMPRESS=PACKBITS', 'OUTPUT' : '_<filepath>_/D_10m_Raster_Clip.tif' }

GDAL command:
gdalwarp -of GTiff -tr 10.0 -10.0 -tap -cutline <filepath>\C_Clip_Polygon.shp -co COMPRESS=PACKBITS <filepath>/B_10m_Raster_Polygon.tif <filepath>/D_10m_Raster_Clip.tif
GDAL command output:
Creating output file that is 354P x 212L.

Processing <filepath>/B_10m_Raster_Polygon.tif [1/1] : 0Using internal nodata values (e.g. 0) for image <filepath>/B_10m_Raster_Polygon.tif.

Copying nodata values from source <filepath>/B_10m_Raster_Polygon.tif to destination <filepath>/D_10m_Raster_Clip.tif.

...10...20...30...40...50...60...70...80...90...100 - done.

Execution completed in 0.51 seconds
Results: {'OUTPUT': <QgsProcessingOutputLayerDefinition {'sink':_<filepath>_/D_10m_Raster_Clip.tif, 'createOptions': {'fileEncoding': 'System'}}>}

Loading resulting layers
Algorithm 'Clip raster by mask layer' finished

GDAL_ClipTest_DataFiles.zip (17.2 KB) Paul Day, 2018-11-08 02:41 PM


#1 Updated by Giovanni Manghi almost 2 years ago

  • Pull Request or Patch supplied changed from No to Yes
  • Operating System deleted (Windows 7)

#2 Updated by Luigi Pirelli almost 2 years ago

  • Status changed from Open to Closed

merged, btw would need to add unit test to the process, and would be backported

Also available in: Atom PDF