0

After searching through GeoTools documentation and following their tutorials, I have failed to find a class that can convert images to different format (for example translate a .tif to .jpg).

Does GeoTools have a Translate function, like GDAL, in some other git repository?

  • No, you just read them in and then write them out in the new format – Ian Turton Aug 9 '17 at 21:40
0

You can use something like:

   AbstractGridFormat format = GridFormatFinder.findFormat(input);
    //working around a bug/quirk in geotiff loading via format.getReader which doesn't set this
    //correctly
    Hints hints = null;
    if (format instanceof GeoTiffFormat) {
        hints = new Hints(Hints.FORCE_LONGITUDE_FIRST_AXIS_ORDER,Boolean.TRUE);
    }

    AbstractGridCoverage2DReader reader = format.getReader(input,hints);
    GridCoverage2D grid = reader.read(null);

    File out = new File("test.jpg");
    Iterator<ImageWriter> writers = ImageIO.getImageWritersBySuffix("jpg");
    ImageWriter writer = null;
    while(writers.hasNext()) {

        writer=writers.next();
        break;
    }
    writer.setOutput(new ImageOutputStreamAdapter(new FileOutputStream(out)));
    writer.write(grid.getRenderedImage());

Note JPG isn't a geographic format so you need to drop down to the default ImageIO to write them out.

  • Ian Turton: can you please elaborate over 'you need to drop down to the default ImageIO'? As you guess, your snipped does not find any suitable ImageWriter for 'jpg' images. – mcfoi May 8 at 13:35
  • works fine on my machine. The comment is just that you won't be using the Enhanced ImageIO that GeoTools uses. – Ian Turton May 8 at 16:52
  • Ok.. ..so mine is not a lib issue. I'm getting Process failed during execution javax.imageio.IIOException: Illegal band size: should be 0 < size <= 8 The GridCoverage2D I'm trying to dump is a float32 that fits perfectly in a GeoTiff.. ..but doesn't fit in a 'jpg'. Any clue? – mcfoi May 8 at 22:33
  • I think you need to ask a new question - make sure to add the gdalinfo output on the input file to the question as well as your code – Ian Turton May 9 at 6:45

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.