The development version of GDAL has lately received a few long awaited improvements in the area of warping and overview computation.
For those non familiar with GDAL, warping is mainly used for reprojecting datasets from one source coordinate system to a target one, or to create a "north-up" image from a rotated image or an image that has ground control points. Overviews in GDAL are also called pyramids in other GIS software and are sub-sampled (i.e. with coarser resolution) versions of full-resolution datasets, that are mainly used for fast display in zooming out operations. Depending on the utility (warper or overview computation), different resampling methods are available : bilinear, cubic, cubicspline, lanczos, average, etc..
Cubic resampling
Up to now, the bi-cubic resampling algorithm used when computing warped images and overviews was a 4x4 convolution kernel. This was appropriate for warping, when the dimensions of the target dataset are of the same order as the source dataset. However if the target dataset was downsized (which is the nominal case of overview computation), the result was sub-optimal, not to say plainly bad, because not enough source pixels were captured, leading to a result close to what nearest neighbour would give. Now, the convolution kernel dynamically uses the subsampling ratio to take into account all source pixels that have an influence on each target pixel, so e.g 8x8 pixels if subsampling by a factor of 2.
Of course, this involves more computation and could be slower. Fortunately, for 64 bit builds, Intel SSE2 intrinsics are at the rescue to compute convolutions in a very efficient way.
For example in GDAL 2.0dev, computing 5 overview levels on a 10474x4951 RGB raster with cubic resampling takes 2.4 seconds on a Core i5-750, to be compared with 3.8s with GDAL 1.11
$ gdaladdo -ro -r cubic world_4326.tif 2 4 8 16 32
To compare both results, we can select the 5th overview level with the fresh new open option OVERVIEW_LEVEL=4 (index are 0 based)
$ gdal_translate world_4326.tif out.tif -oo OVERVIEW_LEVEL=4
5th overview generated by GDAL 2.0dev |
5th overview generated by GDAL 1.11.1 |
So yes, faster (a bit) and better (a lot) !
Similar result can also be obtained with :
$ gdalwarp -r cubic world_4326.tif out.tif -ts 328 155
The "-oo OVERVIEW_LEVEL=xxx" option can be used with gdalinfo, gdal_translate and gdalwarp, or with the new GDALOpenEx() API.
Related work could involve adding resampling method selection in the RasterIO() API that currently only does nearest neighbour sampling. If that might interest you, please contact me.
Overviews in warping
Related to the OVERVIEW_LEVEL open option, another long due improvement was the selection of the appropriate overview level when warping. A typical use case is to start with a WMS or tiled dataset, e.g the OpenStreetMap tiles, and wanting to reproject full or partial extent to an image with reasonably small dimensions. Up to now, GDAL would alway use the most precision dataset (typically zoom level 18 for OpenStreetMap), which would make the operation terribly slow and unpractical.
Now, the following will run in just a few seconds :
$ gdalwarp frmt_wms_openstreetmap_tms.xml out.tif -t_srs EPSG:4326 \
-r cubic -te -10 35 10 55 -overwrite -ts 1000 1000
With the -ovr flag, you can modify the overview selection strategy, and for example specify you want to use the overview if the level immediately before the one that would have been automatically selected (i.e. with bigger dimensions, more precise)
$ gdalwarp frmt_wms_openstreetmap_tms.xml out.tif -t_srs EPSG:4326 \
-r cubic -te -10 35 10 55 -overwrite -ts 1000 1000 -ovr AUTO-1
You can also specify a precise overview level to control the level of details, which is particuarly relevant in the case of OSM since the rendering depends on the scale :
$ gdalwarp frmt_wms_openstreetmap_tms.xml out.tif -t_srs EPSG:4326 \
-r cubic -te -10 35 10 55 -overwrite -ts 1000 1000 -ovr 9
(Note: -ovr 9 is equivalent to OSM zoom level 8, since GDAL_overview_level = OSM_max_zoom_level - 1 - OSM_level, 9 = 18 - 1 - 8. )
With -ovr 9 (zoom level 8) |
With -ovr 10 (zoom level 7) |
With -ovr 11 (zoom level 6) or without any -ovr parameter |
With -ovr 12 (zoom level 5) |
Overviews in warped VRT
GDAL advanced users will perhaps know the Virtual Raster (.vrt) format. There are several flavors of VRT files, one of them is the so-called "warped VRT", which can be produced by "gdalwarp -of VRT". This is an XML file that captures the name of the source dataset being warped and the parameters of the warping: output resolution, extent, dimensions, transformer used, etc... This can be convenient to do on-the-fly reprojection without needing to store the result of the reprojection. Similarly to regular warping, warped VRT can now make use of overviews of the source dataset to expose "implicit" overviews in the warped VRT dataset. Which make it possible to use warped VRT in a GIS viewer ith decent performance when zooming out. Among others, this will be beneficial to QGIS that use the "auto-warped-VRT" mechanism when opening a raster that is not a "north-up" dataset.
Still playing with our OpenStreetMap dataset, let's create a warped VRT around western Europe :
$ gdalwarp frmt_wms_openstreetmap_tms.xml out.vrt -t_srs EPSG:4326 \
-r cubic -te -10 35 10 55 -overwrite -of VRT
We can see that the VRT now advertizes overviews :
$ gdalinfo out.vrt
[...]
Size is 4767192, 4767192
[...]
Band 1 Block=512x128 Type=Byte, ColorInterp=Red
Overviews: 2383596x2383596, 1191798x1191798, 595899x595899,
297950x297950, 148975x148975, 74487x74487,
37244x37244, 18622x18622, 9311x9311, 4655x4655,
2328x2328, 1164x1164, 582x582, 291x291, 145x145,
73x73, 36x36, 18x18
I'd like to thank Koordinates and Land Information New Zealand for funding those improvements.
Great work Even, as usual. I was looking for overview support in Warped VRT for a long time and you did it. Thanks.
RépondreSupprimerGood article
RépondreSupprimerIf I wont to down sample map with average algorithm of 4x4 pixels how do I do it with gdal utils?
NOTE: I dont wont an overview BUT new map with less resolution
Thanks1