I use the GeoTools API to read and render a compressed JPEG GeoTiff file:

 AbstractGridFormat format = new GeoTiffFormat();
    //this is a bit hacky but does make more geotiffs work
    Hints hints = new Hints(Hints.FORCE_LONGITUDE_FIRST_AXIS_ORDER, Boolean.TRUE);

   // InputStream stream = new BufferedInputStream(new FileInputStream(rasterFile), 40*1024);

    reader = format.getReader(rasterFile, hints);

    // Initially display the raster in greyscale using the
    // data from the first image band
    Style rasterStyle = createGreyscaleStyle(1);

    // Connect to the shapefile
    FileDataStore dataStore = FileDataStoreFinder.getDataStore(shpFile);
    SimpleFeatureSource shapefileSource = dataStore

    // Create a basic style with yellow lines and no fill
    Style shpStyle = SLD.createPolygonStyle(Color.YELLOW, null, 0.0f);

    // Set up a MapContent with the two layers
    final MapContent map = new MapContent();

    Layer rasterLayer = new GridReaderLayer(reader, rasterStyle);

The issue is that for bigger GeoTiff Files (e.g. file size 30 MB) the rendering takes a couple of seconds on an up to date machine, this is especially problematic if the user uses functions, like zooming.

Is there any way to use the API differently to speed up the process ? I did a rough profiling but only have seen that the renderer works a couple of seconds if the image gets refreshed.

The GeoTiff was created with gdal

gdal_translate -of GTiff -co COMPRESS=JPEG -co PHOTOMETRIC=YCBCR -co TILED=yes -a_srs EPSG:4326'
  • 4
    Add also overviews with gdaladdo.
    – user30184
    Feb 21 '18 at 18:44
  • 1
    @user30184 cool, that's a game changer ...wanna put it as an answer ?
    – Marco
    Feb 21 '18 at 18:48

As discussed by Paul Ramsey as well as compressing and tiling the images you need to provide overviews to save GeoTools (and other programs) from having to read the whole file when you are zoomed out.

Use gdaladdo to add overviews:

gdaladdo \
  -r average \
  5255C_JPEG_YCBCR.tif \
  2 4 8 16

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.