4

I have a project that references several raster files.

I've grouped and styled them in QGIS 2.18, saved and closed the project, and then changed these raster files on the disk, with a larger extent.1

When opening the QGIS project again, the rasters were still being displayed cropped to their previous extents. Examining the *.qgs file in a text editor, I see that the extent of each layer is explicitly stored.

How can I update these extents in the QGIS project to the ones of the actual rasters in the raster files on disk?

I could of course add the rasters to the project anew, group and style them again (or copy the styles from the existing references) and then remove the old references. However, I'd like to avoid this repetition of effort, if possible.

Or I could just edit the *.qgs XML in a text editor, but this seems rather error-prone.

Is there a way to do this in QGIS, with the existing references?


1 actually, re-generated them with the same file names. I had originally generated them with a Python script using gdal.Warp() with cutlineDSName=... and cropToCutline=True and later without cropToCutline.

  • Perhaps try the following in the Python Console: layer = iface.activeLayer(); layer.dataProvider().updateExtents() – Joseph Feb 8 '18 at 11:07
  • It seems like updateExtents() only works for vector layers. It's callable on raster layers, but doesn't seem to have any effect there. – das-g Feb 8 '18 at 13:55
  • The button mentioned at gis.stackexchange.com/a/99897/51574 also only appears for vector layers, it seems, not for raster layers. – das-g Feb 8 '18 at 14:16
  • Yes, I wasn't sure if updateExtents() would work for rasters because of that missing button. But glad you found a workaround at least :) – Joseph Feb 8 '18 at 14:18
2

I've found a feasible workaround:

Simply delete the <extent>...</extent> XML elements of the respective layers from the *.qgs file. QGIS will recalculate them.

(Be careful not to delete the <extent>...</extent> XML element of the <mapcanvas> if you need to preserve your current view (zoom/pan)! Best make a backup before attempting this.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.