[PATCH 2 of 2] We expect that every river has only one RiverAxis of kind 1

Wald Commits scm-commit at wald.intevation.org
Fri Jun 5 16:59:03 CEST 2015


# HG changeset patch
# User Tom Gottfried <tom at intevation.de>
# Date 1433516207 -7200
# Node ID e9d912c97fa850d388c2273c179497cd53b20e08
# Parent  e701ef544ffa2586cf4384f4594d27c8ec29ce61
We expect that every river has only one RiverAxis of kind 1

    This allowed to reduce code duplication and some cleanups.

diff -r e701ef544ffa -r e9d912c97fa8 artifacts/src/main/java/org/dive4elements/river/artifacts/RiverAxisArtifact.java
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/RiverAxisArtifact.java	Fri Jun 05 16:53:07 2015 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/RiverAxisArtifact.java	Fri Jun 05 16:56:47 2015 +0200
@@ -113,10 +113,7 @@
 
             String kind = getIdPart(2);
 
-            if (kind != null && kind.equals("1")) {
-                axes = RiverAxis.getRiverAxis(river.getName(),
-                        Integer.parseInt(kind));
-            } else if (kind != null) {
+            if (kind != null && !kind.equals(RiverAxis.KIND_CURRENT)) {
                 axes = RiverAxis.getRiverAxis(river.getName(),
                         getName(), Integer.parseInt(kind));
             } else {
@@ -125,11 +122,8 @@
                     return GeometryUtils.transform(
                             GeometryUtils.getRiverBoundary(river.getName()),
                             getSrid());
-                } else {
-                    return GeometryUtils.transform(
-                            GeometryUtils.getRiverBoundary(river.getName()),
-                            "31467");
                 }
+                return GeometryUtils.getRiverBoundary(river.getName());
             }
 
             Envelope max = null;
@@ -153,14 +147,16 @@
         @Override
         protected String getFilter() {
             String kind = getIdPart(2);
-            if (kind != null && kind.equals("1")) {
+            if (kind != null && kind.equals(RiverAxis.KIND_CURRENT)) {
                 return "river_id=" + String.valueOf(getRiverId()) +
                     " AND kind_id=" + kind;
-            } else if (kind != null) {
+            }
+            if (kind != null) {
                 return "river_id=" + String.valueOf(getRiverId()) +
                     " AND kind_id=" + kind +
                     " AND name='" + getName() + "'";
-            } else if (getIdPart(1) != null) {
+            }
+            if (getIdPart(1) != null) {
                 return "river_id=" + String.valueOf(getRiverId()) +
                     " AND name='" + getName() + "'";
             }
@@ -173,9 +169,7 @@
             if (RiverUtils.isUsingOracle()) {
                 return "geom FROM river_axes USING SRID " + getSrid();
             }
-            else {
-                return "geom FROM river_axes USING UNIQUE id";
-            }
+            return "geom FROM river_axes USING UNIQUE id";
         }
 
         @Override
diff -r e701ef544ffa -r e9d912c97fa8 artifacts/src/main/java/org/dive4elements/river/artifacts/states/FloodMapState.java
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/states/FloodMapState.java	Fri Jun 05 16:53:07 2015 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/states/FloodMapState.java	Fri Jun 05 16:56:47 2015 +0200
@@ -787,15 +787,15 @@
         String srid  = String.valueOf(access.getDGM().getSrid());
         String srs   = "EPSG:" + srid;
 
-        List<RiverAxis> axes = null;
+        RiverAxis axis = null;
         try {
-            axes = RiverAxis.getRiverAxis(river);
+            axis = RiverAxis.getRiverAxis(river);
         }
         catch (HibernateException iae) {
             log.warn("No valid river axis found for " + river);
             return;
         }
-        if (axes == null || axes.isEmpty()) {
+        if (axis == null) {
             log.warn("Could not find river axis for: '" + river + "'");
             return;
         }
@@ -806,14 +806,8 @@
         SimpleFeatureBuilder builder = new SimpleFeatureBuilder(ft);
         FeatureCollection collection = FeatureCollections.newCollection();
 
-        for (int i = 0, n = axes.size(); i < n; i++) {
-            RiverAxis axis = axes.get(i);
-
-            builder.add(axis.getGeom());
-            collection.add(builder.buildFeature(String.valueOf(i)));
-
-            builder.reset();
-        }
+        builder.add(axis.getGeom());
+        collection.add(builder.buildFeature("0"));
 
         File axisShape = new File(dir, WSPLGEN_AXIS);
 
diff -r e701ef544ffa -r e9d912c97fa8 artifacts/src/main/java/org/dive4elements/river/utils/GeometryUtils.java
--- a/artifacts/src/main/java/org/dive4elements/river/utils/GeometryUtils.java	Fri Jun 05 16:53:07 2015 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/utils/GeometryUtils.java	Fri Jun 05 16:56:47 2015 +0200
@@ -60,24 +60,9 @@
 
     public static Envelope getRiverBoundary(String rivername) {
         try {
-            List<RiverAxis> axes = RiverAxis.getRiverAxis(rivername);
-            if (axes != null && axes.size() > 0) {
-                Envelope max = null;
-
-                for (RiverAxis axis: axes) {
-                    // TODO Take the correct EPSG into account. Maybe, we need to
-                    // reproject the geometry.
-                    Envelope env = axis.getGeom().getEnvelopeInternal();
-
-                    if (max == null) {
-                        max = env;
-                    }
-                    else {
-                        max.expandToInclude(env);
-                    }
-                }
-
-                return max;
+            RiverAxis axis = RiverAxis.getRiverAxis(rivername);
+            if (axis != null) {
+                return axis.getGeom().getEnvelopeInternal();
             }
         }
         catch(HibernateException iae) {
diff -r e701ef544ffa -r e9d912c97fa8 artifacts/src/main/java/org/dive4elements/river/utils/MapfileGenerator.java
--- a/artifacts/src/main/java/org/dive4elements/river/utils/MapfileGenerator.java	Fri Jun 05 16:53:07 2015 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/utils/MapfileGenerator.java	Fri Jun 05 16:56:47 2015 +0200
@@ -282,10 +282,9 @@
     /**
      * Creates a layer snippet which might be included in the mapfile.
      *
-     * @param layerinfo A LayerInfo object that contains all necessary
+     * @param layerInfo A LayerInfo object that contains all necessary
      * information to build a Mapserver LAYER section.
-     * @param dir The base dir for the LAYER snippet.
-     * @param filename The name of the file that is written.
+     * @param layerFile The file that is written.
      * @param tpl The Velocity template which is used to create the LAYER
      * section.
      */
diff -r e701ef544ffa -r e9d912c97fa8 artifacts/src/main/java/org/dive4elements/river/utils/RiverMapfileGenerator.java
--- a/artifacts/src/main/java/org/dive4elements/river/utils/RiverMapfileGenerator.java	Fri Jun 05 16:53:07 2015 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/utils/RiverMapfileGenerator.java	Fri Jun 05 16:56:47 2015 +0200
@@ -69,10 +69,7 @@
         List<String> riverFiles = new ArrayList<String>();
 
         for (River river : rivers) {
-            // We expect that every river has only one RiverAxis.
-            // This is not correct but currently the case here, see
-            // RiverAxis.java.
-            List<RiverAxis> riverAxis = null;
+            RiverAxis riverAxis = null;
             try {
                 riverAxis = RiverAxis.getRiverAxis(river.getName());
             }
@@ -85,12 +82,12 @@
                 log.warn("River " + river.getName() + " has no river axis!");
                 continue;
             }
-            if (riverAxis.get(0).getGeom() == null) {
+            if (riverAxis.getGeom() == null) {
                 log.warn("River " + river.getName() +
                     " has no riveraxis geometry!");
                 continue;
             }
-            MultiLineString geom = riverAxis.get(0).getGeom();
+            MultiLineString geom = riverAxis.getGeom();
             Envelope extent = geom.getEnvelopeInternal();
 
             createRiverAxisLayer(
diff -r e701ef544ffa -r e9d912c97fa8 backend/src/main/java/org/dive4elements/river/model/RiverAxis.java
--- a/backend/src/main/java/org/dive4elements/river/model/RiverAxis.java	Fri Jun 05 16:53:07 2015 +0200
+++ b/backend/src/main/java/org/dive4elements/river/model/RiverAxis.java	Fri Jun 05 16:56:47 2015 +0200
@@ -29,6 +29,8 @@
 
 import org.hibernate.annotations.Type;
 
+import org.apache.log4j.Logger;
+
 /**
  * A river has one axis that is used for calculation.
  * Additional axes of a river can be used to be painted int maps etc.
@@ -39,6 +41,8 @@
 public class RiverAxis
 implements   Serializable
 {
+    private static Logger log = Logger.getLogger(RiverAxis.class);
+
     private Integer    id;
     private AxisKind   kind;
     private River      river;
@@ -119,11 +123,6 @@
     }
 
 
-    public static List<RiverAxis> getRiverAxis(String river)
-    throws IllegalArgumentException {
-        return getRiverAxis(river, KIND_CURRENT);
-    }
-
     public static List<RiverAxis> getRiverAxis(String river, String name, int kind)
     throws HibernateException {
         Session session = SessionHolder.HOLDER.get();
@@ -139,16 +138,23 @@
         return list.isEmpty() ? null : list;
     }
 
-    public static List<RiverAxis> getRiverAxis(String river, int kind)
+    public static RiverAxis getRiverAxis(String river)
     throws HibernateException {
         Session session = SessionHolder.HOLDER.get();
         Query query = session.createQuery(
             "from RiverAxis where river.name =:river AND kind.id =:kind");
         query.setParameter("river", river);
-        query.setParameter("kind", kind);
+        query.setParameter("kind", KIND_CURRENT);
 
         List<RiverAxis> list = query.list();
-        return list.isEmpty() ? null : list;
+
+        /* We expect that every river has only one RiverAxis of kind 1
+           thow this is not currently enforced in database schema. */
+        if (list.size() > 1) {
+            log.error("River " + river + " has more than one current axis.");
+        }
+
+        return list.isEmpty() ? null : list.get(0);
     }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :


More information about the Dive4Elements-commits mailing list