Removing common imaging also "prodigiously" solved the problem on AsterDem
dimension that Michael described in a previous post

Il giorno mar 22 set 2020 alle ore 18:20 Giuseppe Aruta <
giuseppe.ar...@gmail.com> ha scritto:

> Hi Ede,
> The changes I did:
> a) remove the reading of overviews as you suggested
> b) removed the usage of commons imaging to compute some metadata
> the reason is that those metadata  are already computed by method
> GeoReferencedRaster getGeoReferencedRaster(File tiffFile), except NoData,
> I also removed dependency to common imaging to read NoData and used
> JAI for that purpose.
> TiFFField unofficially used by GDAL to store nodata is 42113 and it is
> basically an ASCII tag.
> https://gdal.org/drivers/raster/gtiff.html.
> I added (lefted) also the possibility that the tag is decode as byte or
> float to  prevent errors
> With the raster files that Roberto provided, only one throws a
> NumberFormatException (depit084) so I left a standard value (-99999.0D) in
> case the reading of NoData fails.
>
>
> Il giorno mar 22 set 2020 alle ore 18:07 jump-pilot-svn--- via
> Jump-pilot-devel <jump-pilot-devel@lists.sourceforge.net> ha scritto:
>
>> Revision: 6518
>>           http://sourceforge.net/p/jump-pilot/code/6518
>> Author:   ma15569
>> Date:     2020-09-22 16:07:41 +0000 (Tue, 22 Sep 2020)
>> Log Message:
>> -----------
>> Added a new method to read Image and Metadata for TIF.
>> It gets rid of using Commons Imaging that sometimes throws exception
>> on both reading resolution of the image. Metadata are now decoded
>> partially via loaded GeoRasterImage or by using JAI to read noData value.
>> Removed code to read overviews.
>>
>> Modified Paths:
>> --------------
>>     core/trunk/src/org/openjump/core/rasterimage/RasterImageIO.java
>>     core/trunk/src/org/openjump/core/rasterimage/TiffUtilsV2.java
>>
>> Modified: core/trunk/src/org/openjump/core/rasterimage/RasterImageIO.java
>> ===================================================================
>> --- core/trunk/src/org/openjump/core/rasterimage/RasterImageIO.java
>>  2020-09-22 14:56:54 UTC (rev 6517)
>> +++ core/trunk/src/org/openjump/core/rasterimage/RasterImageIO.java
>>  2020-09-22 16:07:41 UTC (rev 6518)
>> @@ -140,11 +140,14 @@
>>  //                     Overviews overviews =
>> OverviewsUtils.getOverviews(new File(
>>  //                                     fileNameOrURL), imageEnvelope);
>>
>> -      Envelope imageEnvelope = TiffUtilsV2.getEnvelope(tiffFile);
>> -      Overviews overviews = OverviewsUtils.getOverviews(new
>> File(fileNameOrURL), imageEnvelope);
>> +    //  Envelope imageEnvelope = TiffUtilsV2.getEnvelope(tiffFile);
>> +     // Overviews overviews = OverviewsUtils.getOverviews(new
>> File(fileNameOrURL), imageEnvelope);
>>
>> -      return TiffUtilsV2.readImage(tiffFile, viewPortEnvelope,
>> requestedRes, overviews, stats);
>> -
>> +    //  return TiffUtilsV2.readImage(tiffFile, viewPortEnvelope,
>> requestedRes, overviews, stats);
>> +
>> +      // [Giuseppe Aruta 2020-sept-22] added new method that remove
>> overviews and gets metadata
>> +      // directly from GeoRasterImage or using JAI
>> +      return TiffUtilsV2.readImageAndMetadata(tiffFile,
>> viewPortEnvelope, requestedRes, stats);
>>      } else if (fileNameOrURL.toLowerCase().endsWith(".flt")) {
>>
>>                         GridFloat gf = new GridFloat(fileNameOrURL);
>>
>> Modified: core/trunk/src/org/openjump/core/rasterimage/TiffUtilsV2.java
>> ===================================================================
>> --- core/trunk/src/org/openjump/core/rasterimage/TiffUtilsV2.java
>>  2020-09-22 14:56:54 UTC (rev 6517)
>> +++ core/trunk/src/org/openjump/core/rasterimage/TiffUtilsV2.java
>>  2020-09-22 16:07:41 UTC (rev 6518)
>> @@ -29,6 +29,9 @@
>>  import org.apache.commons.imaging.formats.tiff.TiffImageParser;
>>  import org.xml.sax.SAXException;
>>
>> +import com.sun.media.jai.codec.FileSeekableStream;
>> +import com.sun.media.jai.codec.TIFFDirectory;
>> +import com.sun.media.jai.codec.TIFFField;
>>  import com.vividsolutions.jts.geom.Coordinate;
>>  import com.vividsolutions.jts.geom.Envelope;
>>  import com.vividsolutions.jump.workbench.Logger;
>> @@ -90,6 +93,7 @@
>>      return JAI.create("scale", parameterBlock);
>>    }
>>
>> +
>>    /**
>>     * Method to build an ImageAndMetadata file
>>     * An ImageAndMetadata groups the Envelope, the Image, the Statistics
>> and
>> @@ -201,7 +205,134 @@
>>             }
>>
>>           }
>> +
>> +
>> +
>> +
>> +
>> +  /**
>> +   * New method to build an ImageAndMetadata file
>> +   * An ImageAndMetadata groups the Envelope, the Image, the Statistics
>> and
>> +   * NoData value of a TIF file
>> +   * Removed reading overviews and dependency to commons imaging library
>> +   * @param tiffFile
>> +   * @param viewportEnvelope
>> +   * @param requestedRes
>> +   * @param stats
>> +   * @return ImageAndMetadata
>> +   * @throws NoninvertibleTransformException
>> +   * @throws IOException
>> +   * @throws FileNotFoundException
>> +   * @throws TiffTags.TiffReadingException
>> +   * @throws Exception
>> +   */
>> +  public static ImageAndMetadata readImageAndMetadata(File tiffFile,
>> Envelope viewportEnvelope, Resolution requestedRes,
>> +            Stats stats) throws NoninvertibleTransformException,
>> IOException, FileNotFoundException,
>> +             TiffTags.TiffReadingException, Exception {
>> +         RenderedOp renderedOp1 = getRenderedOp(tiffFile);
>> +         Envelope wholeImageEnvelope = getEnvelope(tiffFile);
>> +
>> +         //[Giuseppe Aruta 2020-sept-22] Deactivated Commons Imaging
>> <TiffTag class>
>> +         // as it throws an error on computing Resolution.
>> +         // This value can be calculated from envelope and image.
>> +         //Deactivated code
>> +         // TiffTags.TiffMetadata tiffMetadata =
>> TiffTags.readMetadata(tiffFile);
>> +         //int originalImageWidth = tiffMetadata.getColsCount();
>> +         //int originalImageHeight = tiffMetadata.getRowsCount();
>> +         // Resolution cellSize = tiffMetadata.getResolution();
>> +         // Double noData = tiffMetadata.getNoData();
>> +
>> +          //[Giuseppe Aruta 2020-sept-22]
>> +          // Try to read geotiff noData tag using JAI.
>> +          // It also solves the  problem of the size of AsterDEM files
>> +          // @ TODO This part should be ported to
>> +          //
>> com.vividsolutions.jump.workbench.imagery.geoimg.GeoReferencedRaster
>> +          // as only NoData tag is used = 42113
>> +           final FileSeekableStream fileSeekableStream = new
>> FileSeekableStream(
>> +                       tiffFile.getAbsoluteFile());
>> +        final TIFFDirectory tiffDirectory = new TIFFDirectory(
>> +                fileSeekableStream, 0);
>> +        Double noData=Double.NEGATIVE_INFINITY;
>> +        final TIFFField[] availTags = tiffDirectory.getFields();
>> +        try {
>> +        for (final TIFFField availTag : availTags) {
>> +               if (availTag.getTag() == 42113) {
>> +                        String noDataString = "";
>> +                 if(availTag.getType()== TIFFField.TIFF_ASCII) {
>> +                     noDataString = availTag.toString();
>> +                     if(noDataString.equalsIgnoreCase("NaN")) {
>> +                         noDataString = "NaN";
>> +                     }
>> +                 } else if(availTag.getType()== TIFFField.TIFF_BYTE) {
>> +                     noDataString = new String(availTag.getAsBytes());
>> +                 }else if(availTag.getType()== TIFFField.TIFF_FLOAT) {
>> +                     noDataString =
>> Float.toString(availTag.getAsFloat(0));
>> +                 }
>> +                 noData = Double.valueOf(noDataString);
>> +               }
>> +          }
>> +        } catch (NumberFormatException e){
>> +          //[Giuseppe Aruta 2020-sept-22]
>> +           //Sometimes reading NoData fails with NumberFormatException
>> +           //Thus the rastee is not well displayed in the view
>> +           //This code sets a standard (Saga gis) noData value readable
>> for OpenJUMP
>> +               noData=-99999.0D;
>> +        }
>> +           /////End of NoData reading
>> +
>> +
>> +
>> +           double cellSizeX =
>> wholeImageEnvelope.getWidth()/renderedOp1.getWidth();
>> +           double cellSizeY =
>> wholeImageEnvelope.getHeight()/renderedOp1.getHeight();
>> +        if (requestedRes == null) {
>> +             requestedRes = new Resolution(cellSizeX, cellSizeY);
>> +           }
>> +        if (stats == null) {
>> +             // Statistics on all pixels
>> +             stats = calculateStats(tiffFile, noData, tiffFile);
>> +           }
>>
>> +             float xScale = (float) (cellSizeX / requestedRes.getX());
>> +             float yScale = (float) (cellSizeY  / requestedRes.getY());
>> +             xScale = Math.min(xScale, 1);
>> +             yScale = Math.min(yScale, 1);
>> +
>> +             RenderedOp renderedOp = readSubsampled(tiffFile, xScale,
>> yScale);
>> +
>> +
>> +             Resolution subsetResolution = new
>> Resolution(wholeImageEnvelope.getWidth() / renderedOp.getWidth(),
>> +                 wholeImageEnvelope.getHeight() /
>> renderedOp.getHeight());
>> +
>> +             Rectangle imageSubset =
>> RasterImageIO.getDrawingRectangle(renderedOp.getWidth(),
>> renderedOp.getHeight(),
>> +                 wholeImageEnvelope, viewportEnvelope, subsetResolution);
>> +
>> +             BufferedImage bufferedImage;
>> +             Envelope imagePartEnvelope;
>> +             int actualImageWidth;
>> +             int actualImageHeight;
>> +             if (imageSubset == null) {
>> +               bufferedImage = null;
>> +               imagePartEnvelope = null;
>> +               actualImageWidth = 0;
>> +               actualImageHeight = 0;
>> +             } else {
>> +               bufferedImage =
>> renderedOp.getAsBufferedImage(imageSubset, null);
>> +               imagePartEnvelope =
>> getImageSubsetEnvelope(wholeImageEnvelope, imageSubset, subsetResolution);
>> +               actualImageWidth = bufferedImage.getWidth();
>> +               actualImageHeight = bufferedImage.getHeight();
>> +             }
>> +
>> +             Metadata metadata = new Metadata(wholeImageEnvelope,
>> imagePartEnvelope,
>> +                 new Point(renderedOp1.getWidth(),
>> renderedOp1.getHeight()), new Point(actualImageWidth, actualImageHeight),
>> +                 (cellSizeX + cellSizeY) / 2, (subsetResolution.getX() +
>> subsetResolution.getY()) / 2, noData,
>> +                 stats);
>> +             return new ImageAndMetadata(bufferedImage, metadata);
>> +
>> +
>> +
>> +
>> +         }
>> +
>>    /**
>>     * Method to read overviews of a TIF from the file metadata or from an
>> external .ovr file
>>     * @param tiffFile
>>
>>
>>
>> _______________________________________________
>> Jump-pilot-devel mailing list
>> Jump-pilot-devel@lists.sourceforge.net
>> https://lists.sourceforge.net/lists/listinfo/jump-pilot-devel
>>
>
_______________________________________________
Jump-pilot-devel mailing list
Jump-pilot-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jump-pilot-devel

Reply via email to