-
Notifications
You must be signed in to change notification settings - Fork 7
COG Optimization
If you're working with a small dataset, you might get away without optimizing your Cloud Optimized GeoTIFFs (COGs). But when you're dealing with larger datasets, optimization can significantly boost performance.
To ensure that CTOD returns tiles correctly for viewing in Cesium, the data needs to be in EPSG:4326 projection. While CTOD can handle COGs in any projection on the fly (thanks to the magic of GDAL working behind the scenes), this can be a bottleneck, especially with a large dataset like the entirety of a country. So, to keep things smooth and speedy, it's best to reproject our GeoTIFF files to EPSG:4326 before creating the COGs.
Here's how you can do that:
gdalwarp -s_srs 'EPSG:YOUR_DATASET_EPSG_CODE' -t_srs 'EPSG:4326' input.tif output.tif
This command warps your GeoTIFF from the original projection to the required EPSG:4326 projection.
Cesium viewers request tiles using the WorldCRS84Quad tiling schema. When a tile request comes into CTOD, it fetches the relevant part of a COG using an HTTP range request. By creating our COGs with the WorldCRS84Quad tiling schema in mind, we can boost performance. This setup divides the image into tiles, each representing a small, fixed-size block of the raster data, perfectly matching the tiling schema used for requesting terrain tiles. This can be done by supplying the schema (--tms schema.json
) to the tool rio-cogeo
which creates the COG. You can find an example of this at the end.
Overviews are essentially zoomed-out, lower-resolution versions of the original image, a process known as downsampling. By increasing the size of the grid cells, you make the image smaller and less detailed. GeoTIFF files often contain multiple overviews at different zoom levels. Without these overviews, a tile request might end up pulling a massive amount of data from the COG, which then has to be downsampled to fit into a 256x256 tile.
For most datasets, having 10 overviews works well, as the lowest overview level fits neatly within a single 256x256 tile. You can check the sizes of images at different overview levels using the gdalinfo tool. Make sure the highest level size fits inside 256x256. The amount of overviews can also be set using rio-cogeo
which can be found at the end of this page.
gdalinfo -json my_tif.tif
-------------------
"overviews":[
{
"size":[
7680,
5632
]
},
{
"size":[
3840,
2816
]
},
{
"size":[
1920,
1408
]
},
.....
To squeeze out a bit more performance, we can align the dataset precisely with the tiling scheme. This can be achieved by using the --aligned-levels flag with the rio-cogeo
tool when creating the COG. Aligned levels refer to the number of overview levels where the GeoTIFF tiles perfectly match the tiles defined in the tiling scheme.
However, there's a catch: setting too many aligned levels can introduce a significant "nodata" border around the dataset. This is especially problematic when using multiple COGs through a .vrt file. If there's too much overlap, CTOD/GDAL will need to fetch multiple tiles from all overlapping files and merge them, which can slow things down.
Here's an example of how much nodata (highlighted in red) can be added when setting aligned levels to 8.
With aligned levels 2 we have a middle ground between aligning and added nodata around the data
When the size of the COG is very large (multiple GB's) it results in GDAL needing to first get a very large header which can be some substantial data which costs precious time, in this case it's better to split up the COG into multiple files and use a .vrt
file. You can read more about this on the wiki page VRT and Mosaid (At the moment of writing this is an empty page)
Example to warp and convert a tiff, the WGS1984Quad.json can be found HERE
gdalwarp -multi -r bilinear -s_srs EPSG:<code> -t_srs EPSG:4326 in.tif out.tif
rio cogeo create out.tif my_cog.tif --cog-profile deflate --blocksize 256 --overview-blocksize 256 -w --overview-level 10 --aligned-levels 2 --tms ./WGS1984Quad.json