Andrea, Simone,
I still don't have a solution, but I think I do have some hints
on what's happening. I've attached a working app (with the same
JAI initialization code used by Geoserver).

If I try to renderer the indexed file as is, I get an OOM.
If I create an alternate version that does not use compression using:
gdal_translate km054086.tif alternate.tif
and create alternate.prj and alternate.tfw, I get an OOM.

If I force the creation of a proper geotiff using:
gdal_translate -of GTiff -a_srs "EPSG:2003" km054086.tif indexed.tiff
and use the geotiff driver to render it, it works like a charm.

What does this mean? Well, it seems to me the Geotiff reader
is using some optimized reading path that's not used by the
plain image+world reader. That may explain why Geoserver is slow
at rendering the original image (thought I miss the secret sauce
that makes Geoserver render it without OOM....)

If this is true, all we need to do is to make the world image reader
smarter when it hits a tiff image.

Simone, comments?
Cheeers
Andrea



package org.vfny.geoserver.jetty;

import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.HashMap;

import javax.imageio.ImageIO;
import javax.media.jai.JAI;
import javax.media.jai.RecyclingTileFactory;

import org.geotools.gce.geotiff.GeoTiffFormat;
import org.geotools.gce.geotiff.GeoTiffReader;
import org.geotools.gce.image.WorldImageFormat;
import org.geotools.gce.image.WorldImageReader;
import org.geotools.geometry.jts.ReferencedEnvelope;
import org.geotools.map.DefaultMapContext;
import org.geotools.map.MapContext;
import org.geotools.referencing.CRS;
import org.geotools.renderer.GTRenderer;
import org.geotools.renderer.lite.StreamingRenderer;
import org.geotools.styling.RasterSymbolizer;
import org.geotools.styling.StyleBuilder;
import org.opengis.coverage.grid.GridCoverage;
import org.opengis.referencing.FactoryException;
import org.opengis.referencing.NoSuchAuthorityCodeException;
import org.opengis.referencing.crs.CoordinateReferenceSystem;

import com.sun.media.jai.util.SunTileCache;
import com.vividsolutions.jts.geom.Envelope;

public class GeotiffRenderer {
    public static void main(String[] args) throws NoSuchAuthorityCodeException, 
FactoryException {
        JAI jaiDef = JAI.getDefaultInstance();
        jaiDef.setRenderingHint(JAI.KEY_CACHED_TILE_RECYCLING_ENABLED,
                new Boolean (true));
        // tile factory and recycler
        final RecyclingTileFactory recyclingFactory = new 
RecyclingTileFactory();
        jaiDef.setRenderingHint(JAI.KEY_TILE_FACTORY, recyclingFactory);
        jaiDef.setRenderingHint(JAI.KEY_TILE_RECYCLER, recyclingFactory);
        
        // Setting up Cache Capacity
        SunTileCache jaiCache = (SunTileCache) jaiDef.getTileCache();
        jaiCache.setMemoryCapacity(50 * 1024 * 1024);
        
        // Setting up Cahce Threshold
        jaiCache.setMemoryThreshold((float) 0.75);
        
        jaiDef.getTileScheduler().setParallelism(7);
        jaiDef.getTileScheduler().setPrefetchParallelism(7);
        jaiDef.getTileScheduler().setPriority(5);
        jaiDef.getTileScheduler().setPrefetchPriority(5);
        
        ImageIO.setUseCache(true);
        
//        String rasterPath = 
"C:\\progetti\\gisData\\tiffIndexed\\km054086.tif";
//        File tiffFile = new File(rasterPath);
//        WorldImageReader wiR = (WorldImageReader) ((new 
WorldImageFormat()).getReader(tiffFile));
//        GridCoverage imgCov = null;
//        try {
//            imgCov = wiR.read(null);
//
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
        
//        String rasterPath = 
"C:\\progetti\\gisData\\tiffIndexed\\indexed.tiff";
//        File tiffFile = new File(rasterPath);
//        GeoTiffReader wiR = (GeoTiffReader) ((new 
GeoTiffFormat()).getReader(tiffFile));
//        GridCoverage imgCov = null;
//        try {
//            imgCov = wiR.read(null);
//
//        } catch (Exception e) {
//            e.printStackTrace();
//        } 
        
      String rasterPath = "C:\\progetti\\gisData\\tiffIndexed\\alternate.tif";
      File tiffFile = new File(rasterPath);
      WorldImageReader wiR = (WorldImageReader) ((new 
WorldImageFormat()).getReader(tiffFile));
      GridCoverage imgCov = null;
      try {
          imgCov = wiR.read(null);

      } catch (Exception e) {
          e.printStackTrace();
      }

        StyleBuilder sb = new StyleBuilder();
        RasterSymbolizer rsDem = sb.createRasterSymbolizer();
        org.geotools.styling.Style demStyle = sb.createStyle(rsDem);
        CoordinateReferenceSystem cr = CRS.decode("EPSG:3003");
        MapContext map = new DefaultMapContext(cr);
        map.addLayer(imgCov, demStyle);
        org.opengis.spatialschema.geometry.Envelope env = imgCov.getEnvelope();
        Envelope envelope = new Envelope(env.getUpperCorner().getOrdinate(0), 
                env.getLowerCorner().getOrdinate(0),
                env.getUpperCorner().getOrdinate(1),
                env.getLowerCorner().getOrdinate(1));

        ReferencedEnvelope area = new ReferencedEnvelope(envelope, map
                .getCoordinateReferenceSystem());
        map.setAreaOfInterest(area);
        
        GTRenderer renderer;
        if (false) {
            renderer = new StreamingRenderer();
            HashMap hints = new HashMap();
            hints.put("memoryPreloadingEnabled", Boolean.TRUE);
            renderer.setRendererHints(hints);
        } else {
            renderer = new StreamingRenderer();
        }
        
        BufferedImage bi = new BufferedImage(1024, 768, 
BufferedImage.TYPE_4BYTE_ABGR);
        renderer.setContext(map);
        renderer.paint(bi.createGraphics(), new Rectangle(1024, 768), area);
    }
}
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Geotools-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/geotools-devel

Reply via email to