create geotiff from jpg with jgw file using geotools?

i have an image in JPG also crated JGW file using Affine transformation now i want to convert this jpg file with jgw file into geotiff how can i do that? thanks

1 answer

  • answered 2019-08-13 12:33 Ian Turton

    First you'll need to make sure you have a dependency on gt-image and gt-geotiff:

    <dependency>
      <groupId>org.geotools</groupId>
      <artifactId>gt-image</artifactId>
      <version>${geotools.version}</version>
    </dependency>
    <dependency>
      <groupId>org.geotools</groupId>
      <artifactId>gt-geotiff</artifactId>
      <version>${geotools.version}</version>
    </dependency>
    

    Then you can simply open the JPG with WLD file (or any other image based file) and write it out as a GeoTIFF.

    AbstractGridFormat format = GridFormatFinder.findFormat(input);
    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);
    reader.dispose();
    File out = new File("test.tif");
    
    GeoTiffFormat outFormat = new GeoTiffFormat();
    GridCoverageWriter writer = outFormat.getWriter(out, hints);
    writer.write(grid, null);
    writer.dispose();