0

I have written a piece of Java code that accesses a large Geotiff. My code utilizes the GeoTiffReader method. When I run the code locally I simply pass GeoTiffReader the file name, and everything works fine. However, I am ultimately trying to have the code read the raster from an AWS S3 bucket. What I have tried to do is set up a local FileInputStream, and pass that to GeoTiffReader. It seems to work fine, up to a point. It correctly reads in the number of bands, the CRS, everything…except the actual data! When I try to read in that, it crashes. The following two lines are where it all goes south:

This works:

 GridCoverage2DReader reader = new GeoTiffReader(file);   //One result from the grid read
    this.grid = reader.read(null);

This does not:

GridCoverage2DReader reader = new GeoTiffReader(fileInputStream);   //One result from the grid read
    this.grid = reader.read(null);

When it is sent a FileInputStream, the read method gets as far as the AbstractGridCoverage2DReader class. Inside of the getRescaledRasterToModel method it crashes on the first line with

final int ssWidth = coverageRaster.getWidth();

I get this same error if I host the file locally or on the AWS s3 bucket. As I said, it opens the GeoTiff just fine, and accesses all of the positional data. It just fails with the final read of the actual data. Any help is HUGELY appreciated!

The key imports are as follows:

 import org.geotools.coverage.grid.io.GridCoverage2DReader;
 import org.geotools.gce.geotiff.GeoTiffReader;
 import org.geotools.coverage.grid.*;
 import org.opengis.coverage.grid.*;
 import org.geotools.geometry.DirectPosition2D;
 import org.geotools.geometry.jts.JTS;
 import org.geotools.referencing.CRS;
 import org.opengis.geometry.DirectPosition;
 import org.opengis.referencing.crs.CoordinateReferenceSystem;
 import org.opengis.referencing.operation.MathTransform;
 import org.opengis.referencing.operation.TransformException;

If I run with the Netbeans standard debugging setup, I get the following stack trace:

JPDA Listening Start...
JPDA Address: BruceXPS15:53848
Port:53848
cd C:\Users\Mount\OneDrive\Documents\GeoTiffTest; "JAVA_HOME=C:\\Program Files\\Java\\jdk1.8.0_77" cmd /c "\"\"C:\\Program Files\\NetBeans 8.2\\java\\maven\\bin\\mvn.bat\" -Dexec.args=\"-Xdebug -Xrunjdwp:transport=dt_socket,server=n,address=53848 -classpath %classpath GeoTIFF_Demo.GeoTIFF_Test\" -Dexec.executable=\"C:\\Program Files\\Java\\jdk1.8.0_77\\bin\\java.exe\" -Dexec.classpathScope=runtime -Djpda.listen=true -Djpda.address=53848 -Dmaven.ext.class.path=\"C:\\Program Files\\NetBeans 8.2\\java\\maven-nblib\\netbeans-eventspy.jar\" -Dfile.encoding=UTF-8 org.codehaus.mojo:exec-maven-plugin:1.2.1:exec\""
Running NetBeans Compile On Save execution. Phase execution is skipped and output directories of dependency projects (with Compile on Save turned on) will be used instead of their jar artifacts.
Scanning for projects...

------------------------------------------------------------------------
Building GeoTiffTest_Local 1.0-SNAPSHOT
------------------------------------------------------------------------

--- exec-maven-plugin:1.2.1:exec (default-cli) @ GeoTiffTest ---
Exception in thread "main" javax.media.jai.util.ImagingException: All factories fail for the operation "ImageRead"
    at javax.media.jai.OperationRegistry.invokeFactory(OperationRegistry.java:1687)
    at javax.media.jai.ThreadSafeOperationRegistry.invokeFactory(ThreadSafeOperationRegistry.java:473)
    at javax.media.jai.registry.RIFRegistry.create(RIFRegistry.java:332)
    at javax.media.jai.RenderedOp.createInstance(RenderedOp.java:819)
    at javax.media.jai.RenderedOp.createRendering(RenderedOp.java:867)
    at javax.media.jai.RenderedOp.getWidth(RenderedOp.java:2179)
    at org.geotools.coverage.grid.io.AbstractGridCoverage2DReader.getRescaledRasterToModel(AbstractGridCoverage2DReader.java:1231)
    at org.geotools.gce.geotiff.GeoTiffReader.read(GeoTiffReader.java:591)
    at GeoTIFF_Demo.GeoTifReader.<init>(GeoTifReader.java:33)
    at GeoTIFF_Demo.GeoTIFF_Test.main(GeoTIFF_Test.java:35)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at javax.media.jai.FactoryCache.invoke(FactoryCache.java:122)
    at javax.media.jai.OperationRegistry.invokeFactory(OperationRegistry.java:1674)
    ... 9 more
Caused by: java.lang.RuntimeException: javax.imageio.IIOException: I/O error reading header!
    at com.sun.media.jai.imageioimpl.ImageReadCRIF.create(ImageReadCRIF.java:317)
    ... 15 more
Caused by: javax.imageio.IIOException: I/O error reading header!
    at it.geosolutions.imageioimpl.plugins.tiff.TIFFImageReader.readHeader(TIFFImageReader.java:458)
    at it.geosolutions.imageioimpl.plugins.tiff.TIFFImageReader.locateImage(TIFFImageReader.java:465)
    at it.geosolutions.imageioimpl.plugins.tiff.TIFFImageReader.seekToImage(TIFFImageReader.java:589)
    at it.geosolutions.imageioimpl.plugins.tiff.TIFFImageReader.getImageTypes(TIFFImageReader.java:1104)
    at javax.imageio.ImageReader.getRawImageType(ImageReader.java:681)
    at com.sun.media.jai.imageioimpl.ImageReadOpImage.layoutHelper(ImageReadOpImage.java:228)
    at com.sun.media.jai.imageioimpl.ImageReadOpImage.<init>(ImageReadOpImage.java:473)
    at com.sun.media.jai.imageioimpl.ImageReadCRIF.create(ImageReadCRIF.java:309)
    ... 15 more
Caused by: java.io.EOFException
    at javax.imageio.stream.ImageInputStreamImpl.readShort(ImageInputStreamImpl.java:229)
    at javax.imageio.stream.ImageInputStreamImpl.readUnsignedShort(ImageInputStreamImpl.java:242)
    at it.geosolutions.imageioimpl.plugins.tiff.TIFFImageReader.readHeader(TIFFImageReader.java:404)
    ... 22 more
Caused by:
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at javax.media.jai.FactoryCache.invoke(FactoryCache.java:122)
    at javax.media.jai.OperationRegistry.invokeFactory(OperationRegistry.java:1674)
    at javax.media.jai.ThreadSafeOperationRegistry.invokeFactory(ThreadSafeOperationRegistry.java:473)
    at javax.media.jai.registry.RIFRegistry.create(RIFRegistry.java:332)
    at javax.media.jai.RenderedOp.createInstance(RenderedOp.java:819)
    at javax.media.jai.RenderedOp.createRendering(RenderedOp.java:867)
    at javax.media.jai.RenderedOp.getWidth(RenderedOp.java:2179)
    at org.geotools.coverage.grid.io.AbstractGridCoverage2DReader.getRescaledRasterToModel(AbstractGridCoverage2DReader.java:1231)
    at org.geotools.gce.geotiff.GeoTiffReader.read(GeoTiffReader.java:591)
    at GeoTIFF_Demo.GeoTifReader.<init>(GeoTifReader.java:33)
    at GeoTIFF_Demo.GeoTIFF_Test.main(GeoTIFF_Test.java:35)
Caused by: java.lang.RuntimeException: javax.imageio.IIOException: I/O error reading header!
    at com.sun.media.jai.imageioimpl.ImageReadCRIF.create(ImageReadCRIF.java:317)
    ... 15 more
Caused by: javax.imageio.IIOException: I/O error reading header!
    at it.geosolutions.imageioimpl.plugins.tiff.TIFFImageReader.readHeader(TIFFImageReader.java:458)
    at it.geosolutions.imageioimpl.plugins.tiff.TIFFImageReader.locateImage(TIFFImageReader.java:465)
    at it.geosolutions.imageioimpl.plugins.tiff.TIFFImageReader.seekToImage(TIFFImageReader.java:589)
    at it.geosolutions.imageioimpl.plugins.tiff.TIFFImageReader.getImageTypes(TIFFImageReader.java:1104)
    at javax.imageio.ImageReader.getRawImageType(ImageReader.java:681)
    at com.sun.media.jai.imageioimpl.ImageReadOpImage.layoutHelper(ImageReadOpImage.java:228)
    at com.sun.media.jai.imageioimpl.ImageReadOpImage.<init>(ImageReadOpImage.java:473)
    at com.sun.media.jai.imageioimpl.ImageReadCRIF.create(ImageReadCRIF.java:309)
    ... 15 more
Caused by: java.io.EOFException
    at javax.imageio.stream.ImageInputStreamImpl.readShort(ImageInputStreamImpl.java:229)
    at javax.imageio.stream.ImageInputStreamImpl.readUnsignedShort(ImageInputStreamImpl.java:242)
    at it.geosolutions.imageioimpl.plugins.tiff.TIFFImageReader.readHeader(TIFFImageReader.java:404)
    ... 22 more
------------------------------------------------------------------------
BUILD FAILURE
------------------------------------------------------------------------
Total time: 6.253s
Finished at: Mon Dec 12 15:48:47 EST 2016
Final Memory: 6M/153M
------------------------------------------------------------------------
Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.2.1:exec (default-cli) on project GeoTiffTest: Command execution failed. Process exited with an error: 1 (Exit value: 1) -> [Help 1]

To see the full stack trace of the errors, re-run Maven with the -e switch.
Re-run Maven using the -X switch to enable full debug logging.

For more information about the errors and possible solutions, please read the following articles:
[Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
  • What is the package? Is this a standard java library? Whenever you give code like this you should include the imports so the reader can see the full package name of the library. – Tyler Durden Dec 12 '16 at 20:29
  • 1
    Please post the full stack trace! And edit the questions, don't hide important details in comments! – Harald K Dec 12 '16 at 20:34
  • I did not add the commands for the full Maven output...would that help? I'm unfortunately a little bit of a Maven noob...and a stackoverflow noob too! – Bruce Ikelheimer Dec 12 '16 at 20:51
  • 1
    Which version of GeoTools are you using? It looks like the GeoTiffReader is having trouble with InputStreams. From a quick read of the code, it also accepts ImageInputStreams. Could you try wrapping your InputStream in a MemoryCacheImageInputStream? I posted to the GeoServer devel list about serving up GeoTiffs from HDFS/S3 here: http://osgeo-org.1560.x6.nabble.com/Reading-GeoTiffs-from-HDFS-td5261637.html. Some of that might be helpful if passing in the ImageInputStream directly doesn't work for you. – GeoJim Dec 13 '16 at 16:01
  • Looks like version 13.2. I will look to see if a more up to date version will improve things. – Bruce Ikelheimer Dec 13 '16 at 18:05
  • I just ran it with version 16.0, with the same result. I'll look into ImageInputStreams now. – Bruce Ikelheimer Dec 13 '16 at 18:13

0 Answers0