0

I'm trying to read my .tiff image using GeoTools. After reading I intend to perform georeferencing of the image using the four coordinates of the image. Following is my code.

public static void main(String[] args) throws IOException, FactoryException {
        AbstractGridCoverage2DReader data = new GeoTiffReader(new FileInputStream(new File("a.tif")));
        GridCoverageFactory gcf = new GridCoverageFactory();
        CoordinateReferenceSystem crs = CRS.decode("EPSG:29901");// null;//null;

        int llx = 500000;
        int lly = 105000;
        int urx = 501000;
        int ury = 106000;

        ReferencedEnvelope referencedEnvelope = new ReferencedEnvelope(llx, urx, lly, ury, crs);
        GridCoverage2D gc = gcf.create("name", (RenderedImage) data, referencedEnvelope);
        String url = "random.tif";
        File file = new File(url);
        GeoTiffWriter writer = null;
        try {
            writer = new GeoTiffWriter(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        writer.write(gc, null);
        writer.dispose();
    }

I am getting the following exceptions and warnings.

WARNING: Coordinate Reference System is not available
Exception in thread "main" org.geotools.data.DataSourceException: Raster to Model Transformation is not available for: null
    at org.geotools.gce.geotiff.GeoTiffReader.<init>(GeoTiffReader.java:279)
    at org.geotools.gce.geotiff.GeoTiffReader.<init>(GeoTiffReader.java:192)
    at IrishGridConverter.main(IrishGridConverter.java:26)
Caused by: org.geotools.data.DataSourceException: Raster to Model Transformation is not available for: null
    at org.geotools.gce.geotiff.GeoTiffReader.getHRInfo(GeoTiffReader.java:539)
    at org.geotools.gce.geotiff.GeoTiffReader.<init>(GeoTiffReader.java:265)
    ... 2 more

what could be the potential reasons, and how can I solve?

10
  • What is the value of crs? do you have a referencing jar in your classpath?
    – Ian Turton
    Commented May 13, 2021 at 14:42
  • I have gt-epsg-hsql dependency set in my pom.xml.
    – 0xMinCha
    Commented May 13, 2021 at 14:46
  • is a.tif a GeoTiff or just a tiff?
    – Ian Turton
    Commented May 13, 2021 at 14:50
  • I'm reading a .tif. I have the coordinate values with respect to the four corners of the image. I want to transform this .tif to a GeoTiff image.
    – 0xMinCha
    Commented May 13, 2021 at 14:52
  • 1
    I don't think you can read in a basic tif file with the geotiff reader as it is expecting to find referencing information in there. Try BufferedImage image = ImageIO.read(inputFile); instead
    – Ian Turton
    Commented May 13, 2021 at 15:25

1 Answer 1

1

This is caused by trying to read in a "normal" tiff file with no spatial data in it. GeoTools is trying to find the required parts of a GeoTiff and not finding them.

You should read in your tiff using BufferedImage image = ImageIO.read(inputFile); and you can then create a coverage using GridCoverage2D gc = gcf.create("name", image, referencedEnvelope);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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