comparison flys-backend/src/main/java/de/intevation/flys/model/CrossSectionLine.java @ 3815:ecab7e7804a9 pre2.6-2012-01-04

merged flys-backend/pre2.6-2012-01-04
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:14:57 +0200
parents 3efc3942b765
children c5d83366d0b1
comparison
equal deleted inserted replaced
3814:8083f6384023 3815:ecab7e7804a9
1 package de.intevation.flys.model;
2
3 import java.io.Serializable;
4
5 import java.math.BigDecimal;
6
7 import java.util.ArrayList;
8 import java.util.List;
9 import java.util.Collections;
10 import java.util.Comparator;
11
12 import java.awt.geom.Point2D;
13
14 import javax.persistence.Entity;
15 import javax.persistence.Id;
16 import javax.persistence.Table;
17 import javax.persistence.GeneratedValue;
18 import javax.persistence.Column;
19 import javax.persistence.SequenceGenerator;
20 import javax.persistence.GenerationType;
21 import javax.persistence.OneToOne;
22 import javax.persistence.OneToMany;
23 import javax.persistence.JoinColumn;
24
25 import org.apache.log4j.Logger;
26
27 @Entity
28 @Table(name = "cross_section_lines")
29 public class CrossSectionLine
30 implements Serializable
31 {
32 private static Logger logger = Logger.getLogger(CrossSectionLine.class);
33
34 public static final double EPSILON = 1e-4;
35
36 public static final double TOO_SMALL = 0.2;
37 public static final double TOO_BIG = 500;
38
39 private Integer id;
40 private BigDecimal km;
41 private CrossSection crossSection;
42
43 private List<CrossSectionPoint> points;
44
45 public static final Comparator<CrossSectionPoint> COL_POS_CMP =
46 new Comparator<CrossSectionPoint>() {
47 @Override
48 public int compare(CrossSectionPoint a, CrossSectionPoint b) {
49 double xa = a.getX().doubleValue();
50 double xb = b.getX().doubleValue();
51 double d = xa - xb;
52 if (d < -EPSILON) return -1;
53 if (d > +EPSILON) return +1;
54 int diff = a.getColPos() - b.getColPos();
55 return diff < 0 ? -1 : diff > 0 ? +1 : 0;
56 }
57 };
58
59
60 public static final boolean isValid(double x) {
61 x = Math.abs(x);
62 return x > TOO_SMALL && x < TOO_BIG;
63 }
64
65
66 public CrossSectionLine() {
67 }
68
69 public CrossSectionLine(CrossSection crossSection, BigDecimal km) {
70 this.crossSection = crossSection;
71 this.km = km;
72 }
73
74 @Id
75 @SequenceGenerator(
76 name = "SEQUENCE_CROSS_SECTION_LINES_ID_SEQ",
77 sequenceName = "CROSS_SECTION_LINES_ID_SEQ",
78 allocationSize = 1)
79 @GeneratedValue(
80 strategy = GenerationType.SEQUENCE,
81 generator = "SEQUENCE_CROSS_SECTION_LINES_ID_SEQ")
82 @Column(name = "id")
83 public Integer getId() {
84 return id;
85 }
86
87 public void setId(Integer id) {
88 this.id = id;
89 }
90
91 @Column(name = "km")
92 public BigDecimal getKm() {
93 return km;
94 }
95
96 public void setKm(BigDecimal km) {
97 this.km = km;
98 }
99
100 @OneToOne
101 @JoinColumn(name = "cross_section_id")
102 public CrossSection getCrossSection() {
103 return crossSection;
104 }
105
106 public void setCrossSection(CrossSection CrossSection) {
107 this.crossSection = crossSection;
108 }
109
110 @OneToMany
111 @JoinColumn(name="cross_section_line_id")
112 public List<CrossSectionPoint> getPoints() {
113 return points;
114 }
115
116 public void setPoints(List<CrossSectionPoint> points) {
117 this.points = points;
118 }
119
120
121 public List<Point2D> fetchCrossSectionLinesPoints() {
122 List<Point2D> points = new ArrayList<Point2D>();
123
124 List<CrossSectionPoint> linePoints = getPoints();
125
126 if (linePoints.isEmpty()) {
127 logger.info("No points in selected CrossSectionLine.");
128 return points;
129 }
130
131 linePoints = new ArrayList(linePoints);
132 Collections.sort(linePoints, COL_POS_CMP);
133
134 for (CrossSectionPoint p: linePoints) {
135 double x = p.getX().doubleValue();
136 double y = p.getY().doubleValue();
137 if (isValid(x) && isValid(y)) {
138 points.add(new Point2D.Double(x, y));
139 }
140 }
141
142 return points;
143 }
144
145 public double [][] fetchCrossSectionProfile() {
146 return fetchCrossSectionProfile(fetchCrossSectionLinesPoints());
147 }
148
149 public static double [][] fetchCrossSectionProfile(List<Point2D> points) {
150
151 int P = points.size();
152
153 double [] xs = new double[P];
154 double [] ys = new double[P];
155
156 if (P > 0) {
157 xs[0] = points.get(0).getX();
158 ys[0] = points.get(0).getY();
159
160 for (int i = 1; i < P; i++) {
161 Point2D p = points.get(i);
162 double x = p.getX();
163 double y = p.getY();
164
165 if (x <= xs[i-1]) {
166 x = xs[i-1] + EPSILON;
167 }
168
169 xs[i] = x;
170 ys[i] = y;
171 }
172 }
173
174 return new double [][] { xs, ys };
175 }
176 }
177 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org