diff gnv-artifacts/src/main/java/de/intevation/gnv/math/QueriedXYDepth.java @ 458:92d6cf448598

Improved raster tile based height evaluation. gnv-artifacts/trunk@512 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Wed, 06 Jan 2010 10:09:51 +0000
parents 47ad9721e692
children 912797826099
line wrap: on
line diff
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/math/QueriedXYDepth.java	Tue Jan 05 14:12:03 2010 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/math/QueriedXYDepth.java	Wed Jan 06 10:09:51 2010 +0000
@@ -7,6 +7,8 @@
 import java.util.Collection;
 import java.util.HashMap;
 
+import java.lang.ref.SoftReference;
+
 import org.apache.log4j.Logger;
 
 import com.vividsolutions.jts.geom.Coordinate;
@@ -17,10 +19,11 @@
 import de.intevation.gnv.geobackend.base.query.exception.QueryException;
 import de.intevation.gnv.geobackend.sde.datasources.RasterObject;
 
+import de.intevation.gnv.utils.WKTUtils;
 
 /**
- * @author Tim Englich <tim.englich@intevation.de>
- *
+ * @author Tim Englich         (tim.englich@intevation.de)
+ * @author Sascha L. Teichmann (sascha.teichmann@intevation.de)
  */
 public class QueriedXYDepth implements XYDepth {
 
@@ -29,62 +32,55 @@
      */
     private static Logger log = Logger.getLogger(QueriedXYDepth.class);
     
-    private String queryID = "rasterQuery";
+    private static final String queryID = "rasterQuery";
     
-    private QueryExecutor queryExecutor = null;
+    private QueryExecutor queryExecutor;
     
-    private ArrayList<RasterObject> rasterData = new ArrayList<RasterObject>();
+    private ArrayList<SoftReference<RasterObject>> rasterData;
     
     /**
      * Constructor
      */
     public QueriedXYDepth() {
-        this.queryExecutor = QueryExecutorFactory.getInstance().getQueryExecutor();
+        rasterData    = new ArrayList<SoftReference<RasterObject>>();
+        queryExecutor = QueryExecutorFactory.getInstance().getQueryExecutor();
     }
 
     /**
      * @see de.intevation.gnv.math.XYDepth#depth(com.vividsolutions.jts.geom.Coordinate)
      */
     public double depth(Coordinate coordinate) {
-        log.debug("QueriedXYDepth.depth for " + coordinate.x+" "+ coordinate.y);
         double resultValue = Double.NaN;
         
-        RasterObject ro = this.getRasterObject(coordinate);
+        RasterObject ro = getRasterObject(coordinate);
         
-        if (ro == null){
-            Collection<Result> result = null;
+        if (ro == null) {
             try {
-                String[] filterValues = new String[]{"Point ("+coordinate.x+" "+coordinate.y+")"};
-                result = this.queryExecutor.executeQuery(this.queryID,filterValues);
+                String[] filterValues = new String[] { WKTUtils.toWKT(coordinate) };
+                Collection<Result> result = queryExecutor.executeQuery(this.queryID,filterValues);
+                for (Result row: result) {
+                    if ((ro = (RasterObject)row.getObject(0)) != null) {
+                        rasterData.add(new SoftReference<RasterObject>(ro));
+                    }
+                    break;
+                }
             } catch (QueryException e) {
-                log.error(e,e);
-            }
-            
-            if (result != null){
-                if (!result.isEmpty()){
-                    Result row = result.iterator().next();
-                    ro = (RasterObject)row.getObject(0);
-                    synchronized (rasterData) {
-                        rasterData.add(ro);
-                    }
-                }
+                log.error(e, e);
             }
         }
-        if (ro != null){
-            resultValue = ro.getValue(coordinate); 
-        }
-        return resultValue;
+        return ro != null
+            ? ro.getValue(coordinate)
+            : Double.NaN; 
     }
     
     private RasterObject getRasterObject(Coordinate coordinate){
-        for (int i = 0; i < rasterData.size(); i++){
-            RasterObject ro = rasterData.get(i);
-            if (ro.getEnvelope().contains(coordinate)){
+        for (int i = rasterData.size()-1; i >= 0; --i) {
+            SoftReference<RasterObject> ref = rasterData.get(i);
+            RasterObject ro = ref.get();
+            if (ro != null && ro.getEnvelope().contains(coordinate)) {
                 return ro;
             }
         }
         return null;
-       
     }
-
 }

http://dive4elements.wald.intevation.org