Improve r26705 and test it

git-svn-id: https://svn.osgeo.org/gdal/trunk/gdal@26707 f0d54148-0727-0410-94bb-9a71ac55c965