Mercurial > dive4elements > river
comparison 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 |
comparison
equal
deleted
inserted
replaced
2379:8be27b950dbe | 2380:e237a08acf6e |
---|---|
1 package de.intevation.flys.model; | 1 package de.intevation.flys.model; |
2 | 2 |
3 import java.io.Serializable; | 3 import java.io.Serializable; |
4 | 4 |
5 import java.util.List; | 5 import java.util.List; |
6 import java.util.ArrayList; | |
7 | |
8 import java.awt.geom.Point2D; | |
6 | 9 |
7 import javax.persistence.Entity; | 10 import javax.persistence.Entity; |
8 import javax.persistence.Id; | 11 import javax.persistence.Id; |
9 import javax.persistence.Table; | 12 import javax.persistence.Table; |
10 import javax.persistence.GeneratedValue; | 13 import javax.persistence.GeneratedValue; |
18 | 21 |
19 import java.math.MathContext; | 22 import java.math.MathContext; |
20 import java.math.BigDecimal; | 23 import java.math.BigDecimal; |
21 | 24 |
22 import org.hibernate.Session; | 25 import org.hibernate.Session; |
26 import org.hibernate.SQLQuery; | |
23 import org.hibernate.Query; | 27 import org.hibernate.Query; |
28 | |
29 import org.hibernate.type.StandardBasicTypes; | |
24 | 30 |
25 import de.intevation.flys.backend.SessionHolder; | 31 import de.intevation.flys.backend.SessionHolder; |
26 | 32 |
27 @Entity | 33 @Entity |
28 @Table(name = "cross_sections") | 34 @Table(name = "cross_sections") |
29 public class CrossSection | 35 public class CrossSection |
30 implements Serializable | 36 implements Serializable |
31 { | 37 { |
32 public static final MathContext PRECISION = new MathContext(6); | 38 public static final MathContext PRECISION = new MathContext(6); |
39 | |
40 public static final String SQL_FAST_CROSS_SECTION_LINES = | |
41 "SELECT km, x, y, csl.id AS csl_id " + | |
42 "FROM cross_section_lines csl JOIN cross_section_points csp " + | |
43 "ON csp.cross_section_line_id = csl.id " + | |
44 "WHERE csl.cross_section_id = :cs_id AND " + | |
45 "km between :from_km AND :to_km " + | |
46 "ORDER BY csl.id, csp.col_pos"; | |
33 | 47 |
34 private Integer id; | 48 private Integer id; |
35 private River river; | 49 private River river; |
36 private TimeInterval timeInterval; | 50 private TimeInterval timeInterval; |
37 private String description; | 51 private String description; |
116 query.setParameter("startKm", new BigDecimal(startKm, PRECISION)); | 130 query.setParameter("startKm", new BigDecimal(startKm, PRECISION)); |
117 query.setParameter("endKm", new BigDecimal(endKm, PRECISION)); | 131 query.setParameter("endKm", new BigDecimal(endKm, PRECISION)); |
118 | 132 |
119 return query.list(); | 133 return query.list(); |
120 } | 134 } |
135 | |
136 public List<FastCrossSectionLine> getFastLines( | |
137 double startKm, | |
138 double endKm | |
139 ) { | |
140 Session session = SessionHolder.HOLDER.get(); | |
141 | |
142 SQLQuery sqlQuery = session.createSQLQuery(SQL_FAST_CROSS_SECTION_LINES) | |
143 .addScalar("km", StandardBasicTypes.DOUBLE) | |
144 .addScalar("x", StandardBasicTypes.DOUBLE) | |
145 .addScalar("y", StandardBasicTypes.DOUBLE) | |
146 .addScalar("csl_id", StandardBasicTypes.INTEGER); | |
147 | |
148 sqlQuery | |
149 .setInteger("cs_id", getId()) | |
150 .setDouble("from_km", startKm) | |
151 .setDouble("to_km", endKm); | |
152 | |
153 List<Object []> results = sqlQuery.list(); | |
154 | |
155 ArrayList<Point2D> points = new ArrayList<Point2D>(500); | |
156 ArrayList<FastCrossSectionLine> lines = | |
157 new ArrayList<FastCrossSectionLine>(); | |
158 | |
159 Integer lastId = null; | |
160 Double lastKm = null; | |
161 | |
162 for (Object [] result: results) { | |
163 Double km = (Double)result[0]; | |
164 Double x = (Double)result[1]; | |
165 Double y = (Double)result[2]; | |
166 Integer id = (Integer)result[3]; | |
167 | |
168 if (lastId != null && !lastId.equals(id)) { | |
169 points.trimToSize(); | |
170 FastCrossSectionLine line = | |
171 new FastCrossSectionLine(lastKm, points); | |
172 lines.add(line); | |
173 points = new ArrayList<Point2D>(500); | |
174 } | |
175 | |
176 Point2D p = new Point2D.Double(x, y); | |
177 | |
178 if (CrossSectionLine.isValid(p)) { | |
179 points.add(p); | |
180 } | |
181 | |
182 lastKm = km; | |
183 lastId = id; | |
184 } | |
185 | |
186 if (lastId != null) { | |
187 points.trimToSize(); | |
188 FastCrossSectionLine line = | |
189 new FastCrossSectionLine(lastKm, points); | |
190 lines.add(line); | |
191 } | |
192 | |
193 lines.trimToSize(); | |
194 | |
195 return lines; | |
196 } | |
121 } | 197 } |
122 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : | 198 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |