1

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?

1

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.

| improve this answer | |
0

A workaround with gdal2tiles could be to create a virtual mosaic file that covers your area of interest with gdal_translate, and then run gdal2tiles for the .VRT file.

Documentation is in https://gdal.org/programs/gdal_translate.html and you probably would like to use parameter -projwin

-projwin <ulx> <uly> <lrx> <lry>

Selects a subwindow from the source image for copying (like -srcwin) but with the corners given in georeferenced coordinates (by default expressed in the SRS of the dataset. Can be changed with -projwin_srs).

You can also create the vrt file with Java by using the GDAL Java bindings if you prefer it to calling the gdal_translate program.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.