diff gnv-artifacts/src/main/java/de/intevation/gnv/utils/WKTUtils.java @ 468:7ba4c7222265

Added ij-Index determination for horizontal-cross-sections. gnv-artifacts/trunk@531 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Tim Englich <tim.englich@intevation.de>
date Tue, 12 Jan 2010 11:34:25 +0000
parents eb2ac62e853a
children 06887e2e3f7a
line wrap: on
line diff
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/utils/WKTUtils.java	Tue Jan 12 08:49:51 2010 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/utils/WKTUtils.java	Tue Jan 12 11:34:25 2010 +0000
@@ -81,7 +81,53 @@
         return sb.toString();
     }
 
-
+    public static String worldEnvelopeCoordinatesToIndex(
+         Coordinate []       coords,
+         Collection<Result>  result,
+         String              meshid,
+         String              ijkQueryID
+    )
+    throws QueryException
+    {
+        List<java.awt.Point> points = new ArrayList<java.awt.Point>(coords.length);
+        ArrayList<Object []> missingPoints = new ArrayList<Object []>();
+        createIJKPoints(coords, meshid, ijkQueryID, points, missingPoints);
+        
+        String additionWhere = "FEATUREID=FEATUREID";
+        if (missingPoints.size() == coords.length) {
+            log.debug("cannot create index buffer");
+        }else {
+            if (points.get(0) != null && points.get(2) != null){
+                additionWhere = "(MEDIAN.MESHPOINT.IPOSITION >= "+points.get(0).x+" AND " +
+                                " MEDIAN.MESHPOINT.JPOSITION >= "+points.get(0).y+" AND " +
+                                " MEDIAN.MESHPOINT.IPOSITION <= "+points.get(2).x+" AND " +
+                                " MEDIAN.MESHPOINT.JPOSITION <= "+points.get(2).y+")";
+            }else if (points.get(1) != null && points.get(3) != null){
+                additionWhere = "(MEDIAN.MESHPOINT.IPOSITION >= "+points.get(1).x+" AND " +
+                                " MEDIAN.MESHPOINT.JPOSITION <= "+points.get(1).y+" AND " +
+                                " MEDIAN.MESHPOINT.IPOSITION <= "+points.get(3).x+" AND " +
+                                " MEDIAN.MESHPOINT.JPOSITION >= "+points.get(3).y+")";
+            }else{
+                // 3 Points are Missing or the Points one one Side of the Envelopes are Missing
+                
+                boolean remainsMissingPoints = calculateIJ4MissingPoints(coords,
+                                                            points, missingPoints);
+                
+                if (!remainsMissingPoints || (points.get(0) != null && points.get(2) != null)){
+                    additionWhere = "(MEDIAN.MESHPOINT.IPOSITION >= "+points.get(0).x+" AND " +
+                                    " MEDIAN.MESHPOINT.JPOSITION >= "+points.get(0).y+" AND " +
+                                    " MEDIAN.MESHPOINT.IPOSITION <= "+points.get(2).x+" AND " +
+                                    " MEDIAN.MESHPOINT.JPOSITION <= "+points.get(2).y+")";
+                }else if (points.get(1) != null && points.get(3) != null){
+                    additionWhere = "(MEDIAN.MESHPOINT.IPOSITION >= "+points.get(1).x+" AND " +
+                                    " MEDIAN.MESHPOINT.JPOSITION <= "+points.get(1).y+" AND " +
+                                    " MEDIAN.MESHPOINT.IPOSITION <= "+points.get(3).x+" AND " +
+                                    " MEDIAN.MESHPOINT.JPOSITION >= "+points.get(3).y+")";
+                }
+            }
+        }
+        return additionWhere;
+    }
     public static String worldCoordinatesToIndex(
 		Coordinate []       coords,
         Collection<Result>  result,
@@ -94,14 +140,110 @@
 
         List<java.awt.Point> points = new ArrayList<java.awt.Point>(coords.length);
 
-        QueryExecutor queryExecutor = QueryExecutorFactory
-                                        .getInstance()
-                                        .getQueryExecutor();
+        ArrayList<Object []> missingPoints = new ArrayList<Object []>();
 
-        ArrayList missingPoints = new ArrayList();
+        createIJKPoints(coords, meshid, ijkQueryID, points, missingPoints);
+        
+        String additionWhere = "FEATUREID=FEATUREID";
+        if (missingPoints.size() == coords.length) {
+            log.debug("cannot create index buffer");
+        }
+        else { // generate index filter
+            boolean remainsMissingPoints = calculateIJ4MissingPoints(coords,
+                                                        points, missingPoints);
 
-        String additionWhere = "FEATUREID=FEATUREID";
+            if (!remainsMissingPoints) {
+                // TODO: Make Tablenames and Columns Configurable
+                IndexBuffer ib = new IndexBuffer(
+                    points, 
+                    "MEDIAN.MESHPOINT.IPOSITION", 
+                    "MEDIAN.MESHPOINT.JPOSITION" );
+                additionWhere = ib.toWhereClause();
+                log.debug("Additional Where Clause = "+additionWhere);
+                // 2. Aus diesen Stuetzpunkten den Resultset generieren.
+            }
+        } // if generate index filter
 
+        return additionWhere;
+    }
+
+
+    /**
+     * @param coords
+     * @param points
+     * @param missingPoints
+     * @return
+     */
+    private static boolean calculateIJ4MissingPoints(
+                                                     Coordinate[] coords,
+                                                     List<java.awt.Point> points,
+                                                     ArrayList<Object[]> missingPoints) {
+        boolean remainsMissingPoints = !missingPoints.isEmpty();
+
+        if (remainsMissingPoints) {
+            // try to guess the missing (i, j)
+            CurveFitter iFitter = new CurveFitter(new GaussNewtonOptimizer(true));
+            CurveFitter jFitter = new CurveFitter(new GaussNewtonOptimizer(true));
+
+            for (int i = 0, N = points.size(); i < N; ++i) {
+                java.awt.Point p = (java.awt.Point)points.get(i);
+                if (p != null) {
+                    Coordinate coord = coords[i];
+                    iFitter.addObservedPoint(coord.x, p.x);
+                    jFitter.addObservedPoint(coord.y, p.y);
+                }
+            }
+            try {
+                // XXX: Assumption: (i, j) are created by componentwise linear function.
+                // This is surely not correct because (x, y) are in a ellipsoid projection.
+                // TODO: use ellipsoid functions and fit with Levenberg Marquardt.
+                double [] iParams = iFitter.fit(
+                    LinearFunction.INSTANCE, new double [] { 1d, 1d });
+
+                double [] jParams = jFitter.fit(
+                    LinearFunction.INSTANCE, new double [] { 1d, 1d });
+
+                for (int i = missingPoints.size()-1; i >= 0; --i) {
+                    Object [] a = (Object [])missingPoints.get(i);
+                    Coordinate coord = (Coordinate)a[1];
+                    int pi = (int)Math.round(iParams[0]*coord.x + iParams[1]);
+                    int pj = (int)Math.round(jParams[0]*coord.y + jParams[1]);
+                    points.set(
+                        ((Integer)a[0]).intValue(),
+                        new java.awt.Point(pi, pj));
+                }
+
+                remainsMissingPoints = false; // we filled the gaps
+            }
+            catch (FunctionEvaluationException fee) {
+                log.error(fee);
+            }
+            catch (OptimizationException oe) {
+                log.error(oe);
+            }
+        }
+        return remainsMissingPoints;
+    }
+
+
+    /**
+     * @param coords
+     * @param meshid
+     * @param ijkQueryID
+     * @param points
+     * @param missingPoints
+     * @throws QueryException
+     */
+    private static void createIJKPoints(Coordinate[] coords, String meshid,
+                                        String ijkQueryID,
+                                        List<java.awt.Point> points,
+                                        ArrayList<Object []> missingPoints)
+                                                                throws QueryException {
+        Collection<Result> result;
+        QueryExecutor queryExecutor = QueryExecutorFactory
+            .getInstance()
+            .getQueryExecutor();
+        
         for (int i = 0; i < coords.length; i++) {
 
             String wkt = toWKT(coords[i]);
@@ -121,69 +263,6 @@
                 // Special Case no i,j found for Coordinate
             }
         }
-
-        if (missingPoints.size() == coords.length) {
-            log.debug("cannot create index buffer");
-        }
-        else { // generate index filter
-            boolean remainsMissingPoints = !missingPoints.isEmpty();
-
-            if (remainsMissingPoints) {
-                // try to guess the missing (i, j)
-                CurveFitter iFitter = new CurveFitter(new GaussNewtonOptimizer(true));
-                CurveFitter jFitter = new CurveFitter(new GaussNewtonOptimizer(true));
-
-                for (int i = 0, N = points.size(); i < N; ++i) {
-                    java.awt.Point p = (java.awt.Point)points.get(i);
-                    if (p != null) {
-                        Coordinate coord = coords[i];
-                        iFitter.addObservedPoint(coord.x, p.x);
-                        jFitter.addObservedPoint(coord.y, p.y);
-                    }
-                }
-                try {
-                    // XXX: Assumption: (i, j) are created by componentwise linear function.
-                    // This is surely not correct because (x, y) are in a ellipsoid projection.
-                    // TODO: use ellipsoid functions and fit with Levenberg Marquardt.
-                    double [] iParams = iFitter.fit(
-                        LinearFunction.INSTANCE, new double [] { 1d, 1d });
-
-                    double [] jParams = jFitter.fit(
-                        LinearFunction.INSTANCE, new double [] { 1d, 1d });
-
-                    for (int i = missingPoints.size()-1; i >= 0; --i) {
-                        Object [] a = (Object [])missingPoints.get(i);
-                        Coordinate coord = (Coordinate)a[1];
-                        int pi = (int)Math.round(iParams[0]*coord.x + iParams[1]);
-                        int pj = (int)Math.round(jParams[0]*coord.y + jParams[1]);
-                        points.set(
-                            ((Integer)a[0]).intValue(),
-                            new java.awt.Point(pi, pj));
-                    }
-
-                    remainsMissingPoints = false; // we filled the gaps
-                }
-                catch (FunctionEvaluationException fee) {
-                    log.error(fee);
-                }
-                catch (OptimizationException oe) {
-                    log.error(oe);
-                }
-            }
-
-            if (!remainsMissingPoints) {
-                // TODO: Make Tablenames and Columns Configurable
-                IndexBuffer ib = new IndexBuffer(
-                    points, 
-                    "MEDIAN.MESHPOINT.IPOSITION", 
-                    "MEDIAN.MESHPOINT.JPOSITION" );
-                additionWhere = ib.toWhereClause();
-                log.debug("Additional Where Clause = "+additionWhere);
-                // 2. Aus diesen Stuetzpunkten den Resultset generieren.
-            }
-        } // if generate index filter
-
-        return additionWhere;
     }
 
 	public static Coordinate [] toCoordinates(String wkt) {

http://dive4elements.wald.intevation.org