Mercurial > dive4elements > river
comparison flys-backend/src/main/java/de/intevation/flys/importer/parsers/W80Parser.java @ 4768:99f9e371371b
Move distance calculation to Coordinate class. Use inheritance instead of composition in Anchor class. Made Anchor class static. Use epsilon equal comparision when checking for same station: Boy, do you ever learn that sharp equal comparison of doubles is not a clever idea!?
author | Sascha L. Teichmann <teichmann@intevation.de> |
---|---|
date | Fri, 04 Jan 2013 15:35:51 +0100 |
parents | c6654a19b00f |
children | 92a08725bc63 |
comparison
equal
deleted
inserted
replaced
4767:03246a8b3869 | 4768:99f9e371371b |
---|---|
2 | 2 |
3 import de.intevation.artifacts.common.utils.FileTools; | 3 import de.intevation.artifacts.common.utils.FileTools; |
4 | 4 |
5 import de.intevation.flys.importer.XY; | 5 import de.intevation.flys.importer.XY; |
6 | 6 |
7 import de.intevation.flys.importer.parsers.tim.Coordinate; | |
8 | |
9 import de.intevation.flys.utils.DateGuesser; | |
7 import de.intevation.flys.utils.EpsilonComparator; | 10 import de.intevation.flys.utils.EpsilonComparator; |
8 import de.intevation.flys.importer.parsers.tim.Coordinate; | |
9 | |
10 import de.intevation.flys.utils.DateGuesser; | |
11 | 11 |
12 import java.io.File; | 12 import java.io.File; |
13 import java.io.IOException; | 13 import java.io.IOException; |
14 | 14 |
15 import java.util.ArrayList; | 15 import java.util.ArrayList; |
16 import java.util.Calendar; | 16 import java.util.Calendar; |
17 import java.util.Date; | 17 import java.util.Date; |
18 import java.util.List; | 18 import java.util.List; |
19 import java.util.Map; | 19 import java.util.Map; |
20 import java.util.TreeMap; | 20 import java.util.TreeMap; |
21 | |
22 import java.util.regex.Pattern; | |
23 | 21 |
24 import org.apache.log4j.Logger; | 22 import org.apache.log4j.Logger; |
25 | 23 |
26 | 24 |
27 /** | 25 /** |
41 /** Data collected so far, last element will be currentLine. */ | 39 /** Data collected so far, last element will be currentLine. */ |
42 protected Map<Double, List<XY>> data; | 40 protected Map<Double, List<XY>> data; |
43 | 41 |
44 | 42 |
45 /** Anchor to project to. */ | 43 /** Anchor to project to. */ |
46 private class Anchor { | 44 private static class Anchor extends Coordinate { |
47 private Coordinate coordinate; | 45 |
46 private static final double EPSILON = 1e-5; | |
47 | |
48 private double station; | 48 private double station; |
49 public Anchor(Coordinate anchor, double station) { | 49 |
50 this.coordinate = anchor; | 50 public Anchor(double x, double y, double z, double station) { |
51 super(x, y, z); | |
51 this.station = station; | 52 this.station = station; |
52 } | 53 } |
53 public double getStation() { | 54 |
54 return station; | 55 public boolean sameStation(double station) { |
55 } | 56 return Math.abs(this.station - station) < EPSILON; |
56 public Coordinate getCoordinate() { | |
57 return coordinate; | |
58 } | 57 } |
59 } | 58 } |
60 | 59 |
61 | 60 |
62 /** Reference point for simple projection. */ | 61 /** Reference point for simple projection. */ |
157 * @return true if point could been added, false otherwise (e.g. not | 156 * @return true if point could been added, false otherwise (e.g. not |
158 * parsable y or z values. | 157 * parsable y or z values. |
159 */ | 158 */ |
160 private boolean addPoint(double gkr, double gkh, double height, String idx) { | 159 private boolean addPoint(double gkr, double gkh, double height, String idx) { |
161 // Calculate distance between this and anchor-point. | 160 // Calculate distance between this and anchor-point. |
162 double dx = gkr - anchor.getCoordinate().getX(); | 161 double d = anchor.distance(gkr, gkh); |
163 double dy = gkh - anchor.getCoordinate().getY(); | |
164 double d = Math.sqrt(dx * dx + dy * dy); | |
165 | 162 |
166 // TODO: Scale to have "x==0" e.g. at axis of river. | 163 // TODO: Scale to have "x==0" e.g. at axis of river. |
167 // TODO: Handle "not straight lines." | 164 // TODO: Handle "not straight lines." |
168 | 165 |
169 // We ignore idx, and increment instead. | 166 // We ignore idx, and increment instead. |
218 double gkRightKm = Double.parseDouble(gkRight.substring(0,7)); | 215 double gkRightKm = Double.parseDouble(gkRight.substring(0,7)); |
219 double gkHighKm = Double.parseDouble(gkHigh.substring(0,7)); | 216 double gkHighKm = Double.parseDouble(gkHigh.substring(0,7)); |
220 double heightM = Double.parseDouble(height); | 217 double heightM = Double.parseDouble(height); |
221 | 218 |
222 // New (or first) line. | 219 // New (or first) line. |
223 if (anchor == null || anchor.getStation() != stationKm) { | 220 if (anchor == null || !anchor.sameStation(stationKm)) { |
224 this.anchor = new Anchor(new Coordinate(gkRightKm, gkHighKm, heightM), stationKm); | 221 anchor = new Anchor(gkRightKm, gkHighKm, heightM, stationKm); |
225 currentLine = new ArrayList<XY>(); | 222 currentLine = new ArrayList<XY>(); |
226 data.put(stationKm, currentLine); | 223 data.put(stationKm, currentLine); |
227 currentLine.add(new XY(0d, heightM,0)); | 224 currentLine.add(new XY(0d, heightM,0)); |
228 anchorDate = DateGuesser.guessDate(date); | 225 anchorDate = DateGuesser.guessDate(date); |
229 } | 226 } |