changeset 471:06887e2e3f7a

Some minor code cleanup gnv-artifacts/trunk@534 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Tue, 12 Jan 2010 17:41:59 +0000 (2010-01-12)
parents b7bb66440cc8
children d6a100d5f74a
files gnv-artifacts/ChangeLog gnv-artifacts/src/main/java/de/intevation/gnv/state/profile/horizontalcrosssection/HorizontalCrossSectionMeshOutputState.java gnv-artifacts/src/main/java/de/intevation/gnv/utils/WKTUtils.java
diffstat 3 files changed, 143 insertions(+), 75 deletions(-) [+]
line wrap: on
line diff
--- a/gnv-artifacts/ChangeLog	Tue Jan 12 15:25:32 2010 +0000
+++ b/gnv-artifacts/ChangeLog	Tue Jan 12 17:41:59 2010 +0000
@@ -1,3 +1,9 @@
+2010-01-12	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
+
+	* src/main/java/de/intevation/gnv/utils/WKTUtils.java,
+	  src/main/java/de/intevation/gnv/state/profile/horizontalcrosssection/HorizontalCrossSectionMeshOutputState.java:
+	  Code cleanup
+
 2010-01-12  Ingo Weinzierl <ingo.weinzierl@intevation.de>
 
 	* src/main/java/de/intevation/gnv/state/SingleInputState.java,
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/state/profile/horizontalcrosssection/HorizontalCrossSectionMeshOutputState.java	Tue Jan 12 15:25:32 2010 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/state/profile/horizontalcrosssection/HorizontalCrossSectionMeshOutputState.java	Tue Jan 12 17:41:59 2010 +0000
@@ -151,15 +151,22 @@
                     log.error("meshid is not defined");
                     throw new IllegalStateException("missing meshid");
                 }
+
+                Polygon p = WKTUtils.toPolygon(meshPolygon.getValue());
+
+                if (p == null) {
+                    log.error("no valid polygon");
+                    throw new IllegalStateException("no valid polygon");
+                }
+
                 try {
-                    // TODO: Refactore in own Method to WKTUtils?
-                    Polygon p = (Polygon)new WKTReader().read(meshPolygon.getValue());
                     Envelope env  = p.getEnvelopeInternal();
-                    Coordinate[] coords = new Coordinate[4];
-                    coords[0] = new Coordinate(env.getMinX(), env.getMinY());
-                    coords[1] = new Coordinate(env.getMinX(), env.getMaxY());
-                    coords[2] = new Coordinate(env.getMaxX(), env.getMaxY());
-                    coords[2] = new Coordinate(env.getMaxX(), env.getMinY());
+
+                    Coordinate [] coords = new Coordinate [] {
+                        new Coordinate(env.getMinX(), env.getMinY()),
+                        new Coordinate(env.getMinX(), env.getMaxY()),
+                        new Coordinate(env.getMaxX(), env.getMaxY()),
+                        new Coordinate(env.getMaxX(), env.getMinY()) };
 
                     String additionWhere =
                         WKTUtils.worldEnvelopeCoordinatesToIndex(
@@ -180,24 +187,20 @@
                         Arrays.asList(coords),
                         numSamples(callContext),
                         queryExecutor.executeQuery(
-                        queryID,
-                        addedFilterValues),
-                        p);
-                } catch (QueryException e) {
-                    log.error(e,e);
-                } catch (ParseException e) {
+                            queryID,
+                            addedFilterValues),
+                            p);
+                }
+                catch (QueryException e) {
                     log.error(e,e);
                 }
 
                 if (CacheFactory.getInstance().isInitialized()) {
                     CacheFactory.getInstance().getCache().put(new net.sf.ehcache.Element(key, result));
                 }
-
             }
         }
         return result;
-        
-        
     }
     
     public static Collection<Result> process(
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/utils/WKTUtils.java	Tue Jan 12 15:25:32 2010 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/utils/WKTUtils.java	Tue Jan 12 17:41:59 2010 +0000
@@ -2,8 +2,10 @@
 
 import com.vividsolutions.jts.geom.Coordinate;
 import com.vividsolutions.jts.geom.Point;
+import com.vividsolutions.jts.geom.Polygon;
 import com.vividsolutions.jts.geom.LineString;
 import com.vividsolutions.jts.io.ParseException;
+
 import com.vividsolutions.jts.io.WKTReader;
 
 import de.intevation.gnv.geobackend.base.Result;
@@ -31,6 +33,11 @@
     public static final double NAUTICAL_MILE = 1852.216d;
     public static final double KILOMETER     = 1000d;
 
+    public static final String I_NAME = "MEDIAN.MESHPOINT.IPOSITION";
+    public static final String J_NAME = "MEDIAN.MESHPOINT.JPOSITION";
+
+    public static final String TRUE_EXPRESSION = "FEATUREID=FEATUREID";
+
     public static boolean different(Result a, Result b, int [] indices) {
         for (int i = 0; i < indices.length; ++i) {
             String oa = a.getString(indices[i]);
@@ -45,17 +52,12 @@
             }
 
             if (!oa.equals(ob)) {
-                if (log.isDebugEnabled()) {
-                    log.debug("+++++++++++++++ differs ++++++++++++++");
-                    log.debug("   " + oa + " != " + ob);
-                }
                 return true;
             }
         }
         return false;
     }
 
-
     public static Coordinate toCoordinate(String shape) {
         try {
             return ((Point)(new WKTReader().read(shape))).getCoordinate();
@@ -63,9 +65,24 @@
         catch (ParseException pe) {
             log.error(pe);
         }
+		catch (ClassCastException cce) {
+			log.error("cannot read WKT point", cce);
+		}
         return null;
     }
 
+    public static Polygon toPolygon(String shape) {
+        try {
+            return (Polygon)new WKTReader().read(shape);
+        }
+        catch (ParseException pe) {
+            log.error(pe);
+        }
+		catch (ClassCastException cce) {
+			log.error("cannot read WKT polygon", cce);
+		}
+        return null;
+    }
 
     public static final double toKM(double distance) {
         return (distance * NAUTICAL_MILE) / KILOMETER;
@@ -81,6 +98,44 @@
         return sb.toString();
     }
 
+    public static final String indexBox(
+        java.awt.Point a, 
+        java.awt.Point b,
+        String         iName,
+        String         jName
+    ) {
+        int minI = Math.min(a.x, b.x) - 1;
+        int maxI = Math.max(a.x, b.x) + 1;
+        int minJ = Math.min(a.y, b.y) - 1;
+        int maxJ = Math.max(a.y, b.y) + 1;
+        StringBuilder sb = new StringBuilder("(")
+                          .append(iName).append(" >= ").append(minI)
+          .append(" AND ").append(iName).append(" <= ").append(maxI)
+          .append(" AND ").append(jName).append(" >= ").append(minJ)
+          .append(" AND ").append(jName).append(" <= ").append(maxJ)
+          .append(')');
+        return sb.toString();
+    }
+
+    public static final String diagonalBox(List<java.awt.Point> points) {
+
+        if (points.get(0) != null && points.get(2) != null) {
+            return indexBox(
+                points.get(0), points.get(2),
+                I_NAME,
+                J_NAME);
+        }
+
+        if (points.get(1) != null && points.get(3) != null) {
+            return indexBox(
+                points.get(1), points.get(3),
+                I_NAME,
+                J_NAME);
+        }
+
+        return null;
+    }
+
     public static String worldEnvelopeCoordinatesToIndex(
          Coordinate []       coords,
          Collection<Result>  result,
@@ -89,45 +144,38 @@
     )
     throws QueryException
     {
-        List<java.awt.Point> points = new ArrayList<java.awt.Point>(coords.length);
-        ArrayList<Object []> missingPoints = new ArrayList<Object []>();
+        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";
+        String additionWhere = null;
+
         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+")";
+        }
+        else {
+            additionWhere = diagonalBox(points);
+
+            if (additionWhere == null) {
+                // 3 Points are missing or are on one side of the envelope
+                boolean remainsMissingPoints = calculateIJ4MissingPoints(
+                    coords, points, missingPoints);
+
+                if (!remainsMissingPoints) {
+                    additionWhere = diagonalBox(points);
                 }
             }
         }
-        return additionWhere;
+
+        return additionWhere != null
+            ? additionWhere
+            : TRUE_EXPRESSION;
     }
+
     public static String worldCoordinatesToIndex(
 		Coordinate []       coords,
         Collection<Result>  result,
@@ -136,31 +184,29 @@
     )
 	throws QueryException
     {
-        // 1. IJK Anfragen für Stuetzpunkte im Netz ausführen.
-
         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";
+        String additionWhere = TRUE_EXPRESSION;
+
         if (missingPoints.size() == coords.length) {
             log.debug("cannot create index buffer");
         }
         else { // generate index filter
-            boolean remainsMissingPoints = calculateIJ4MissingPoints(coords,
-                                                        points, missingPoints);
+            boolean remainsMissingPoints = calculateIJ4MissingPoints(
+                coords, points, missingPoints);
 
             if (!remainsMissingPoints) {
                 // TODO: Make Tablenames and Columns Configurable
                 IndexBuffer ib = new IndexBuffer(
                     points, 
-                    "MEDIAN.MESHPOINT.IPOSITION", 
-                    "MEDIAN.MESHPOINT.JPOSITION" );
+                    I_NAME, 
+                    J_NAME );
                 additionWhere = ib.toWhereClause();
                 log.debug("Additional Where Clause = "+additionWhere);
-                // 2. Aus diesen Stuetzpunkten den Resultset generieren.
             }
         } // if generate index filter
 
@@ -175,9 +221,10 @@
      * @return
      */
     private static boolean calculateIJ4MissingPoints(
-                                                     Coordinate[] coords,
-                                                     List<java.awt.Point> points,
-                                                     ArrayList<Object[]> missingPoints) {
+        Coordinate []        coords,
+        List<java.awt.Point> points,
+        ArrayList<Object[]>  missingPoints
+    ) {
         boolean remainsMissingPoints = !missingPoints.isEmpty();
 
         if (remainsMissingPoints) {
@@ -234,12 +281,17 @@
      * @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;
+    private static void createIJKPoints(
+        Coordinate[]         coords,
+        String               meshid,
+        String               ijkQueryID,
+        List<java.awt.Point> points,
+        ArrayList<Object []> missingPoints
+    )
+    throws QueryException 
+    {
+        boolean debug = log.isDebugEnabled();
+
         QueryExecutor queryExecutor = QueryExecutorFactory
             .getInstance()
             .getQueryExecutor();
@@ -248,16 +300,23 @@
 
             String wkt = toWKT(coords[i]);
 
-            result = queryExecutor.executeQuery(ijkQueryID,
-                                               new String[]{meshid,wkt});
-            if (!result.isEmpty()){
+            Collection<Result> result = queryExecutor.executeQuery(
+                ijkQueryID,
+                new String [] {meshid, wkt});
+
+            if (!result.isEmpty()) {
                 Result resultValue = result.iterator().next();
                 int iPos = resultValue.getInteger(1);
                 int jPos = resultValue.getInteger(0);
-                log.debug("Found Pos "+iPos+"/"+jPos +" for "+wkt);
+                if (debug) {
+                    log.debug("Found Pos "+iPos+"/"+jPos +" for "+wkt);
+                }
                 points.add(i, new java.awt.Point(iPos,jPos));
-            }else{
-                log.debug("No i/j Pos found for "+wkt);
+            }
+            else {
+                if (debug) {
+                    log.debug("No i/j Pos found for "+wkt);
+                }
                 missingPoints.add(new Object [] { Integer.valueOf(i), coords[i] });
                 points.add(i, null);
                 // Special Case no i,j found for Coordinate

http://dive4elements.wald.intevation.org