diff geo-backend/src/main/java/de/intevation/gnv/geobackend/sde/datasources/RasterObject.java @ 547:23d5cc37dd5b

Fixed access to raster data. geo-backend/trunk@518 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Sat, 09 Jan 2010 12:41:26 +0000
parents 347c84467478
children ccd976fc0f7b
line wrap: on
line diff
--- a/geo-backend/src/main/java/de/intevation/gnv/geobackend/sde/datasources/RasterObject.java	Fri Jan 08 14:40:08 2010 +0000
+++ b/geo-backend/src/main/java/de/intevation/gnv/geobackend/sde/datasources/RasterObject.java	Sat Jan 09 12:41:26 2010 +0000
@@ -8,75 +8,105 @@
 
 import java.io.Serializable;
 
+
+import org.apache.log4j.Logger;
+
 /**
- * @author Tim Englich <tim.englich@intevation.de>
- *
+ * @author Tim Englich (tim.englich@intevation.de)
+ * @author Sascha L. Teichmann (sascha.teichmann@intevation.de)
  */
 public class RasterObject 
 implements   Serializable
 {
-    private int rasterSize;
-    private int rowIndex;
+    private static Logger log = Logger.getLogger(RasterObject.class);
+
+    public static final int NEAREST_NEIGHBOR = 0;
+    public static final int BILINEAR         = 1;
+
+    private double mx;
+    private double bx;
+    private double my;
+    private double by;
+
     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;
+    private int rowIndex;
+
+    private int tileWidth;
+    private int tileHeight;
+
+    private double [] rasterData;
+
+    public RasterObject() {
+    }
+
+    public RasterObject(
+        double mx, double bx,
+        double my, double by,
+        int       columnIndex, 
+        int       rowIndex,
+        double [] rasterData,
+        int       tileWidth,
+        int       tileHeight
+    ){
+        this.mx          = mx;
+        this.bx          = bx;
+        this.my          = my;
+        this.by          = by;
         this.columnIndex = columnIndex;
-        this.rasterHeight = rasterHeight;
-        this.rasterWidth = rasterWidth;
+        this.rowIndex    = rowIndex;
+        this.rasterData  = rasterData;
+        this.tileWidth   = tileWidth;
+        this.tileHeight  = tileHeight;
     }
-    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 boolean contains(Coordinate coordinate) {
+        double px = mx*coordinate.x + bx;
+        double py = my*coordinate.y + by;
+        return px >= 0d && py >= 0d && px < tileWidth && py < tileHeight;
+    }
+
+    public final double get(int posX, int posY) {
+        return rasterData[posY*tileWidth + posX];
+    }
+
+    public int getTileWidth() {
+        return tileWidth;
+    }
+
+    public int getTileHeight() {
+        return tileWidth;
+    }
+
+    public int getColumnIndex() {
+        return columnIndex;
+    }
+
+    public int getRowIndex() {
+        return rowIndex;
     }
     
-    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;
+    public double getValue(Coordinate coordinate) {
+        return getValue(coordinate, NEAREST_NEIGHBOR);
     }
 
+    public double getValue(Coordinate coordinate, int interpolationType) {
+
+        double px = mx*coordinate.x + bx;
+        double py = my*coordinate.y + by;
+
+        if (px < 0d || py < 0d || px >= tileWidth || py >= tileHeight) {
+            return Double.NaN;
+        }
+
+        /*
+        if (interpolationType == BILINEAR) {
+            // TODO: implement me!
+        }
+        */
+
+        int posX = Math.min(tileWidth-1,  (int)Math.round(px));
+        int posY = Math.min(tileHeight-1, (int)Math.round(py));
+
+        return get(posX, posY);
+    }
 }

http://dive4elements.wald.intevation.org