Mercurial > dive4elements > river
changeset 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 | 03246a8b3869 |
children | 92a08725bc63 |
files | flys-backend/src/main/java/de/intevation/flys/importer/parsers/W80Parser.java flys-backend/src/main/java/de/intevation/flys/importer/parsers/tim/Coordinate.java |
diffstat | 2 files changed, 24 insertions(+), 17 deletions(-) [+] |
line wrap: on
line diff
--- a/flys-backend/src/main/java/de/intevation/flys/importer/parsers/W80Parser.java Fri Jan 04 11:17:40 2013 +0100 +++ b/flys-backend/src/main/java/de/intevation/flys/importer/parsers/W80Parser.java Fri Jan 04 15:35:51 2013 +0100 @@ -4,10 +4,10 @@ import de.intevation.flys.importer.XY; -import de.intevation.flys.utils.EpsilonComparator; import de.intevation.flys.importer.parsers.tim.Coordinate; import de.intevation.flys.utils.DateGuesser; +import de.intevation.flys.utils.EpsilonComparator; import java.io.File; import java.io.IOException; @@ -19,8 +19,6 @@ import java.util.Map; import java.util.TreeMap; -import java.util.regex.Pattern; - import org.apache.log4j.Logger; @@ -43,18 +41,19 @@ /** Anchor to project to. */ - private class Anchor { - private Coordinate coordinate; + private static class Anchor extends Coordinate { + + private static final double EPSILON = 1e-5; + private double station; - public Anchor(Coordinate anchor, double station) { - this.coordinate = anchor; + + public Anchor(double x, double y, double z, double station) { + super(x, y, z); this.station = station; } - public double getStation() { - return station; - } - public Coordinate getCoordinate() { - return coordinate; + + public boolean sameStation(double station) { + return Math.abs(this.station - station) < EPSILON; } } @@ -159,9 +158,7 @@ */ private boolean addPoint(double gkr, double gkh, double height, String idx) { // Calculate distance between this and anchor-point. - double dx = gkr - anchor.getCoordinate().getX(); - double dy = gkh - anchor.getCoordinate().getY(); - double d = Math.sqrt(dx * dx + dy * dy); + double d = anchor.distance(gkr, gkh); // TODO: Scale to have "x==0" e.g. at axis of river. // TODO: Handle "not straight lines." @@ -220,8 +217,8 @@ double heightM = Double.parseDouble(height); // New (or first) line. - if (anchor == null || anchor.getStation() != stationKm) { - this.anchor = new Anchor(new Coordinate(gkRightKm, gkHighKm, heightM), stationKm); + if (anchor == null || !anchor.sameStation(stationKm)) { + anchor = new Anchor(gkRightKm, gkHighKm, heightM, stationKm); currentLine = new ArrayList<XY>(); data.put(stationKm, currentLine); currentLine.add(new XY(0d, heightM,0));
--- a/flys-backend/src/main/java/de/intevation/flys/importer/parsers/tim/Coordinate.java Fri Jan 04 11:17:40 2013 +0100 +++ b/flys-backend/src/main/java/de/intevation/flys/importer/parsers/tim/Coordinate.java Fri Jan 04 15:35:51 2013 +0100 @@ -31,5 +31,15 @@ public double getY() { return this.y; } + + public final double distanceSqr(double ox, double oy) { + double dx = x - ox; + double dy = y - oy; + return dx*dx + dy*dy; + } + + public final double distance(double xo, double yo) { + return Math.sqrt(distanceSqr(xo, yo)); + } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :