GeoTIFF – How to Create Tiles from GeoTIFF Using GeoTools

geotiff-tiffgeotoolstiles

I have a big GeoTIFF(25GB) and I want to create tiles from it.

I know GeoServer have the feature, but I can't use it. I found GeoTools example for cropping tif files here, but it just create some tiffs from a tiff file, not creating png/jpeg tiles. I found JAI-EXT, but there is no example.
I found gdal2tiles.py and use it successfully in java, but we need to manage creating tiles(for example some times, we didn't need to create all tiles, just we want to create 20% of them in special area)

Can anyone give an example to create a special tile from GeoTIFF file?

Best Answer

GeoServer creates tiles of rendered WMS maps using GeoWebCache where as the GeoTools tutorial you found is splitting up an existing GeoTiff. These are different processes with as you note different outputs.

However, there is no difficulty in using GeoTools to split a GeoTiff into png or jpg images using the tutorial code. You simply need to change the output format when writing the tiles back to disk.

At the end of the tile method are the output lines:

File tileFile = new File(tileDirectory, i + "_" + j + "." + fileExtension);
format.getWriter(tileFile).write(finalCoverage, null);

format is the same object as was used to read in the coverage (a GeoTiff) but you can get other formats, for example a WorldImageWriter will output georeferenced png or jpg tiles:

fileExtension = "png";
File tileFile = new File(tileDirectory, i + "_" + j + "." + fileExtension);
final WorldImageWriter wiWriter = new WorldImageWriter(tileFile);

// writing parameters for png
final Format oFormat = wiWriter.getFormat();
((AbstractGridFormat) oFormat).getWriter(tileFile).write(finalCoverage, null);

Or you can simply use an ImageWriter

Iterator<ImageWriter> writers = ImageIO.getImageWritersBySuffix("jpg");
ImageWriter writer = null;
while (writers.hasNext()) {
  writer = writers.next();
  System.out.println(writer);
  // break;
}
writer.setOutput(new ImageOutputStreamAdapter(new FileOutputStream(tileFile)));
writer.write(grid.getRenderedImage());

In both these cases you are writing out the raw data values with no styling applied, this may or may not be what you want. If you need to apply styling then you should look at the image tutorial for an example of how to style a raster and draw it to screen, you would need to provide an Image to draw on instead of a screen and pass that Graphics2D object to the renderer.

Related Question