diff flys-backend/src/main/java/de/intevation/flys/model/CrossSection.java @ 2380:e237a08acf6e

Create fast cross section lines in the backend now. flys-backend/trunk@3696 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Tue, 17 Jan 2012 16:56:38 +0000
parents 8be27b950dbe
children 2ae732e2c65c
line wrap: on
line diff
--- a/flys-backend/src/main/java/de/intevation/flys/model/CrossSection.java	Mon Jan 16 14:52:40 2012 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/model/CrossSection.java	Tue Jan 17 16:56:38 2012 +0000
@@ -3,6 +3,9 @@
 import java.io.Serializable;
 
 import java.util.List;
+import java.util.ArrayList;
+
+import java.awt.geom.Point2D;
 
 import javax.persistence.Entity;
 import javax.persistence.Id;
@@ -20,8 +23,11 @@
 import java.math.BigDecimal;
 
 import org.hibernate.Session;
+import org.hibernate.SQLQuery;
 import org.hibernate.Query;
 
+import org.hibernate.type.StandardBasicTypes;
+
 import de.intevation.flys.backend.SessionHolder;
 
 @Entity
@@ -31,6 +37,14 @@
 {
     public static final MathContext PRECISION = new MathContext(6);
 
+    public static final String SQL_FAST_CROSS_SECTION_LINES =
+        "SELECT km, x, y, csl.id AS csl_id " +
+        "FROM cross_section_lines csl JOIN cross_section_points csp " +
+        "ON csp.cross_section_line_id = csl.id " +
+        "WHERE csl.cross_section_id = :cs_id AND " +
+        "km between :from_km AND :to_km " + 
+        "ORDER BY csl.id, csp.col_pos";
+
     private Integer                id;
     private River                  river;
     private TimeInterval           timeInterval;
@@ -118,5 +132,67 @@
 
         return query.list();
     }
+
+    public List<FastCrossSectionLine> getFastLines(
+        double startKm,
+        double endKm
+    ) {
+        Session session = SessionHolder.HOLDER.get();
+
+        SQLQuery sqlQuery = session.createSQLQuery(SQL_FAST_CROSS_SECTION_LINES)
+            .addScalar("km",     StandardBasicTypes.DOUBLE)
+            .addScalar("x",      StandardBasicTypes.DOUBLE)
+            .addScalar("y",      StandardBasicTypes.DOUBLE)
+            .addScalar("csl_id", StandardBasicTypes.INTEGER);
+
+        sqlQuery
+            .setInteger("cs_id",  getId())
+            .setDouble("from_km", startKm)
+            .setDouble("to_km",   endKm);
+
+        List<Object []> results = sqlQuery.list();
+
+        ArrayList<Point2D> points = new ArrayList<Point2D>(500);
+        ArrayList<FastCrossSectionLine> lines =
+            new ArrayList<FastCrossSectionLine>();
+
+        Integer lastId = null;
+        Double  lastKm = null;
+
+        for (Object [] result: results) {
+            Double  km = (Double)result[0];
+            Double  x  = (Double)result[1];
+            Double  y  = (Double)result[2];
+            Integer id = (Integer)result[3];
+
+            if (lastId != null && !lastId.equals(id)) {
+                points.trimToSize();
+                FastCrossSectionLine line =
+                    new FastCrossSectionLine(lastKm, points);
+                lines.add(line);
+                points = new ArrayList<Point2D>(500);
+            }
+
+            Point2D p = new Point2D.Double(x, y);
+
+            if (CrossSectionLine.isValid(p)) {
+                points.add(p);
+            }
+
+            lastKm = km;
+            lastId = id;
+        }
+
+        if (lastId != null) {
+            points.trimToSize();
+            FastCrossSectionLine line =
+                new FastCrossSectionLine(lastKm, points);
+            lines.add(line);
+        }
+
+        lines.trimToSize();
+
+        return lines;
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org