comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/charts/CrossSectionApp.java @ 1651:a7def20539fb

flys/issue317: Removed dependency from cross section demo app. flys-artifacts/trunk@2840 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Tue, 27 Sep 2011 13:45:13 +0000
parents 111794adf285
children 595c404523a6
comparison
equal deleted inserted replaced
1650:aaf8d32f85bd 1651:a7def20539fb
16 import java.awt.event.ItemEvent; 16 import java.awt.event.ItemEvent;
17 import java.awt.event.ActionListener; 17 import java.awt.event.ActionListener;
18 import java.awt.event.ActionEvent; 18 import java.awt.event.ActionEvent;
19 19
20 import java.awt.geom.Point2D; 20 import java.awt.geom.Point2D;
21 import java.awt.geom.Line2D; 21
22
23 import java.util.ArrayList;
24 import java.util.List; 22 import java.util.List;
25 import java.util.Comparator;
26 import java.util.Collections;
27 import java.util.Iterator;
28 23
29 import java.io.File; 24 import java.io.File;
30 import java.io.IOException; 25 import java.io.IOException;
31 import java.io.FileWriter; 26 import java.io.FileWriter;
32 import java.io.PrintWriter; 27 import java.io.PrintWriter;
57 import de.intevation.flys.backend.SessionFactoryProvider; 52 import de.intevation.flys.backend.SessionFactoryProvider;
58 53
59 import org.hibernate.Session; 54 import org.hibernate.Session;
60 import org.hibernate.Query; 55 import org.hibernate.Query;
61 56
62 import gnu.trove.TDoubleArrayList;
63
64 public class CrossSectionApp 57 public class CrossSectionApp
65 extends ApplicationFrame 58 extends ApplicationFrame
66 { 59 {
67 public static final String RIVER = System.getProperty("river", "Saar"); 60 public static final String RIVER = System.getProperty("river", "Saar");
68 61
69 public static final double EPSILON = 1e-4; 62 public static final double EPSILON = 1e-4;
70
71 public static final double TOO_SMALL = 0.2;
72 public static final double TOO_BIG = 500;
73
74 public static final Comparator<CrossSectionPoint> COL_POS_CMP =
75 new Comparator<CrossSectionPoint>() {
76 @Override
77 public int compare(CrossSectionPoint a, CrossSectionPoint b) {
78 // TODO evaluate: isnt it enough to
79 // return (|d| > |EPS|) ? d : diff
80 double xa = a.getX().doubleValue();
81 double xb = b.getX().doubleValue();
82 double d = xa - xb;
83 if (d < -EPSILON) return -1;
84 if (d > +EPSILON) return +1;
85 int diff = a.getColPos() - b.getColPos();
86 return diff < 0 ? -1 : diff > 0 ? +1 : 0;
87 }
88 };
89
90 public static final boolean isValid(double x) {
91 x = Math.abs(x);
92 return x > TOO_SMALL && x < TOO_BIG;
93 }
94 63
95 protected Session session; 64 protected Session session;
96 65
97 protected JComboBox crossSectionsCB; 66 protected JComboBox crossSectionsCB;
98 protected JComboBox crossSectionLinesCB; 67 protected JComboBox crossSectionLinesCB;
318 CrossSectionLine line, 287 CrossSectionLine line,
319 Double waterlevel 288 Double waterlevel
320 ) { 289 ) {
321 DefaultXYDataset dataset = new DefaultXYDataset(); 290 DefaultXYDataset dataset = new DefaultXYDataset();
322 291
323 List<CrossSectionPoint> ps = line.getPoints(); 292 List<Point2D> points = line.fetchCrossSectionLinesPoints();
324
325 if (ps.isEmpty()) {
326 return dataset;
327 }
328
329 Collections.sort(ps, COL_POS_CMP);
330
331 List<Point2D> points = new ArrayList<Point2D>(ps.size());
332
333 for (CrossSectionPoint p: ps) {
334 double x = p.getX().doubleValue();
335 double y = p.getY().doubleValue();
336 if (isValid(x) && isValid(y)) {
337 points.add(new Point2D.Double(x, y));
338 }
339 }
340 293
341 if (points.isEmpty()) { 294 if (points.isEmpty()) {
342 return dataset; 295 return dataset;
343 } 296 }
344 297
345 double [] xs = new double[points.size()];
346 double [] ys = new double[xs.length];
347
348 xs[0] = points.get(0).getX();
349 ys[0] = points.get(0).getY();
350
351 for (int i = 1; i < xs.length; ++i) {
352 Point2D p = points.get(i);
353 double x = p.getX();
354 double y = p.getY();
355
356 if (x <= xs[i-1]) {
357 x = xs[i-1] + EPSILON;
358 }
359 xs[i] = x;
360 ys[i] = y;
361 }
362
363 if (waterlevel != null) { 298 if (waterlevel != null) {
364 double [][] data = createWaterLines(points, waterlevel); 299 double [][] data = Lines.createWaterLines(points, waterlevel);
365 dataset.addSeries(String.valueOf(waterlevel), data); 300 dataset.addSeries(String.valueOf(waterlevel), data);
366 } 301 }
367 302
368 CrossSection cs = line.getCrossSection(); 303 CrossSection cs = line.getCrossSection();
369 304
370 String legend = (cs != null ? cs.getDescription() : "???") 305 String legend = (cs != null ? cs.getDescription() : "???")
371 + " " + Math.round(line.getKm().doubleValue() * 1000d)/1000d; 306 + " " + Math.round(line.getKm().doubleValue() * 1000d)/1000d;
372 307
373 dataset.addSeries(legend, new double [][] { xs, ys }); 308 double [][] values = CrossSectionLine.fetchCrossSectionProfile(points);
309
310 dataset.addSeries(legend, values);
374 311
375 return dataset; 312 return dataset;
376 }
377
378 public static double [][] createWaterLines(
379 List<Point2D> points,
380 double waterlevel
381 ) {
382 List<Line2D> lines = Lines.fillWater(points, waterlevel);
383
384 TDoubleArrayList lxs = new TDoubleArrayList();
385 TDoubleArrayList lys = new TDoubleArrayList();
386
387 for (Iterator<Line2D> iter = lines.iterator(); iter.hasNext();) {
388 Line2D l = iter.next();
389 Point2D p1 = l.getP1();
390 Point2D p2 = l.getP2();
391 lxs.add(p1.getX());
392 lys.add(p1.getY());
393 lxs.add(p2.getX());
394 lys.add(p2.getY());
395 if (iter.hasNext()) {
396 lxs.add(Double.NaN);
397 lys.add(Double.NaN);
398 }
399 }
400
401 return new double [][] { lxs.toNativeArray(), lys.toNativeArray() };
402 } 313 }
403 314
404 protected void updateCrossSection(CrossSection crossSection) { 315 protected void updateCrossSection(CrossSection crossSection) {
405 Object [] cslis = createCrossSectionLineItems(crossSection); 316 Object [] cslis = createCrossSectionLineItems(crossSection);
406 DefaultComboBoxModel dcbm = new DefaultComboBoxModel(cslis); 317 DefaultComboBoxModel dcbm = new DefaultComboBoxModel(cslis);

http://dive4elements.wald.intevation.org