view geo-backend/src/main/java/de/intevation/gnv/geobackend/sde/datasources/RasterObject.java @ 544:33f93898cbbf

Added RasterObject for caching the Rastertiles to get a better performance geo-backend/trunk@508 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Tim Englich <tim.englich@intevation.de>
date Tue, 05 Jan 2010 14:09:53 +0000
parents
children 347c84467478
line wrap: on
line source
/**
 *
 */
package de.intevation.gnv.geobackend.sde.datasources;

import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Envelope;

/**
 * @author Tim Englich <tim.englich@intevation.de>
 *
 */
public class RasterObject {

    private int rasterSize;
    private int rowIndex;
    private int columnIndex;
    private int rasterWidth;
    private int rasterHeight;
    private Envelope envelope = null;
    private Envelope rasterEnvelope = null;
    
    private double[] rasterData = null;
    /**
     * Constructor
     */
    public RasterObject(Envelope envelope , double[] rasterData, 
                        int rasterSize, int rowIndex, 
                        int columnIndex, int rasterWidth, int rasterHeight ){
        this.envelope = envelope;
        this.rasterData = rasterData;
        this.rasterSize = rasterSize;
        this.rowIndex = rowIndex;
        this.columnIndex = columnIndex;
        this.rasterHeight = rasterHeight;
        this.rasterWidth = rasterWidth;
    }
    public synchronized Envelope getEnvelope() {
        if (this.rasterEnvelope == null){
            double minX = (columnIndex* rasterSize * 
                          (envelope.getWidth() / rasterWidth)+ envelope.getMinX());
            double maxX = minX + (rasterSize * (envelope.getWidth() / rasterWidth));
            
            double maxY = envelope.getMaxY() - 
                          (rowIndex * rasterSize * 
                          (envelope.getHeight() / rasterHeight));
            double minY = maxY - 
                          (rasterSize * (envelope.getHeight() / rasterHeight));
            this.rasterEnvelope = new Envelope(
                    new Coordinate(minX, minY), 
                    new Coordinate(maxX, maxY));
        }
        return this.rasterEnvelope;
    }
    
    public synchronized double getValue(Coordinate coordinate){
        double dxNature = coordinate.x - envelope.getMinX();
        double dyNature = envelope.getMaxY() - coordinate.y;
        double widthNature = envelope.getMaxX()-envelope.getMinX(); 
        double heightNature = envelope.getMaxY()-envelope.getMinY();
        
        int pixelX = (int)Math.round(dxNature * rasterWidth / widthNature);
        int pixelY = (int)Math.round(dyNature * rasterHeight/ heightNature);
        
        
        int localPixelX = pixelX - (columnIndex*rasterSize);
        int localPixelY = pixelY - (rowIndex*rasterSize);
        
        int pos = localPixelY * rasterSize + localPixelX;
        
        
        if (pos < rasterData.length){
            return this.rasterData[pos];
        }
        
        return Double.NaN;
    }

}

http://dive4elements.wald.intevation.org