COG¶
This module reads Cloud Optimized GeoTIFF (COG) files through tileverse-storage. It verifies the layout of a file when it opens it, then reads pixel windows of the full resolution image and of its overviews.
The module reads files and does not write them. To create a COG, use the GDAL COG driver. For example, gdal_translate -of COG input.tif output.tif converts a GeoTIFF.
Features¶
- Cloud-optimized reads: opening a file reads its directories and none of its tiles. Reading a pixel window of up to 1,024 tiles fetches the tiles under it in one batched read.
- Storage agnostic: works with the
RangeReaderbackends oftileverse-storage: local file, HTTP, S3, Azure Blob, and GCS. - Overviews and masks: read windows of the full resolution image or of an overview, along with the transparency mask of that level.
- Layout verification: opening a file checks the COG layout rules. When the file breaks one, the exception tells which.
- Collections: list the COGs of a directory or a glob up to a bound, and find one by its id.
- Few dependencies: the module compiles against the JDK, Tileverse and the SLF4J API, and against no TIFF library. It leaves decoding to a TIFF
ImageReaderofjavax.imageio, looked up at run time.
Core concepts¶
A COG is a tiled TIFF with rules about where its parts go. The image file directories come first, each with the index of its tiles, and the tile data follows. The head of the file is then enough to learn where the tiles are, and a reader fetches only the needed tiles with range requests.
The file holds one image per level. Level 0 is the full resolution image, and the levels after it are its overviews, each a smaller copy of the image. Levels can have a transparency mask: an image of one bit per pixel telling where the level has data.
CogImage opens a file. It checks the layout rules, reads the directories, and reports the levels. You then ask it for a CogWindow, a rectangle of pixels of one level. To read the window, the module builds a small TIFF header and directory in memory. They declare only the tiles under the window, and the decoder sees them ahead of the bytes of the real file. The TIFF ImageReader decodes the window from that stream. It never parses the tile index of the real file, and that index can hold hundreds of thousands of tiles.
That stream is a BigTIFF, because a tile may lie past the first 4 GB of the real file, and the decoder must be able to read BigTIFF. The imageio-ext TIFF reader can. The TIFF reader of the JDK cannot.
Installation¶
<dependency>
<groupId>io.tileverse.cog</groupId>
<artifactId>tileverse-cog</artifactId>
</dependency>
The version comes from tileverse-bom, as described in Getting Started. The module ships with Tileverse 2.2 and later. Local files and HTTP work out of the box. S3, Azure and GCS URIs need tileverse-storage-all, or the matching tileverse-storage-s3, tileverse-storage-azure or tileverse-storage-gcs module, next to tileverse-cog.
The decoder¶
To read pixels you need the imageio-ext TIFF reader, or another TIFF ImageReader able to read BigTIFF. imageio-ext is an optional dependency of the module, and your application has to add it:
<dependency>
<groupId>it.geosolutions.imageio-ext</groupId>
<artifactId>imageio-ext-tiff</artifactId>
<version>2.1.4</version>
<scope>runtime</scope>
</dependency>
imageio-ext is not published to Maven Central. Declare the OSGeo repository to resolve it:
<repositories>
<repository>
<id>osgeo-releases</id>
<url>https://repo.osgeo.org/repository/release/</url>
</repository>
</repositories>
imageio-ext brings other jars with it: ImageN, a JAXB runtime and aircompressor. When it registers, it also removes the TIFF reader of the JDK from the ImageIO registry of the process. GeoTools applications have imageio-ext already.
Usage¶
import io.tileverse.cog.CogImage;
import java.awt.Rectangle;
import java.awt.image.Raster;
import java.util.Properties;
try (CogImage image = CogImage.open("https://example.com/scenes/scene.tif", new Properties())) {
for (CogImage.Level level : image.levels()) {
System.out.printf("level %d: %d x %d pixels%n", level.index(), level.width(), level.height());
}
// 512 x 512 pixels of the full resolution level
Raster pixels = image.window(0, new Rectangle(256, 256, 512, 512)).read();
}
CogImage.open(String, Properties) accepts a local path, or any URI supported by tileverse-storage, such as s3://bucket/scene.tif.
Next steps¶
- Read COGs: open files in cloud storage, read windows and masks, use your own decoder, and list a directory or a glob.
- Format support: accepted and rejected files, the URL grammar, caches, and the cost of reads.