ingo@298: package de.intevation.gnv.chart; ingo@298: ingo@298: import java.util.Collection; ingo@298: import java.util.Locale; ingo@298: ingo@298: import com.vividsolutions.jts.geom.Point; ingo@298: import com.vividsolutions.jts.io.WKTReader; ingo@298: import com.vividsolutions.jts.io.ParseException; ingo@298: ingo@298: import org.apache.log4j.Logger; ingo@298: ingo@298: import org.jfree.chart.ChartTheme; ingo@298: import org.jfree.chart.plot.PlotOrientation; ingo@298: import org.jfree.data.general.Series; ingo@298: import org.jfree.data.xy.XYSeries; ingo@298: ingo@298: import de.intevation.gnv.geobackend.base.Result; ingo@298: import de.intevation.gnv.utils.DistanceCalculator; ingo@298: ingo@298: ingo@298: /** ingo@298: * @author Ingo Weinzierl ingo@298: */ ingo@298: public class HorizontalProfileChart ingo@298: extends VerticalProfileChart ingo@298: { ingo@298: private static Logger log = Logger.getLogger(HorizontalProfileChart.class); ingo@298: ingo@298: private static WKTReader wktReader = new WKTReader(); ingo@298: private Point lastPoint; ingo@298: private double distance; ingo@298: ingo@298: ingo@298: public HorizontalProfileChart( ingo@298: ChartLabels labels, ingo@298: ChartTheme theme, ingo@298: Collection parameters, ingo@298: Collection measurements, ingo@310: Collection dates, ingo@298: Collection result, ingo@310: Collection timeGaps, ingo@327: Locale locale, ingo@327: boolean linesVisible, ingo@327: boolean shapesVisible ingo@298: ) { ingo@310: super( ingo@310: labels, ingo@310: theme, ingo@310: parameters, ingo@310: measurements, ingo@310: dates, ingo@310: result, ingo@310: timeGaps, ingo@327: locale, ingo@327: linesVisible, ingo@327: shapesVisible ingo@310: ); ingo@298: this.PLOT_ORIENTATION = PlotOrientation.VERTICAL; ingo@298: this.distance = 0; ingo@298: } ingo@298: ingo@298: ingo@340: protected void gapDetection( ingo@340: Result[] results, ingo@340: Series series, ingo@340: int startPos, ingo@340: int endPos ingo@340: ) { ingo@340: log.debug("Start gap detection."); ingo@340: try { ingo@340: Point startValue = getPoint(results[startPos]); ingo@340: Point endValue = getPoint(results[endPos-1]); ingo@340: if (results[0].getInteger("DATAID") == 2) ingo@340: addGapsOnGrid(results, series, startPos, endPos); ingo@340: else ingo@340: addGaps( ingo@340: results, ingo@340: series, ingo@340: startValue, ingo@340: endValue, ingo@340: startPos, ingo@340: endPos ingo@340: ); ingo@340: } ingo@340: catch (ParseException pe) { ingo@340: log.warn( ingo@340: "Error while parsing points for gap detection. " + ingo@340: "No gaps for current series will be detected." ingo@340: ); ingo@340: } ingo@340: ingo@340: log.debug("Gap detection finished."); ingo@340: } ingo@340: ingo@340: ingo@298: protected void addValue(Result row, Series series) { ingo@298: try { ingo@298: Point point = (Point) wktReader.read(row.getString("SHAPE")); ingo@298: if (lastPoint != null) ingo@298: distance = distance + DistanceCalculator.calculateDistance( ingo@298: lastPoint, point ingo@298: ); ingo@298: lastPoint = point; ingo@298: ingo@298: ((XYSeries) series).add( ingo@298: distance, ingo@298: row.getDouble("YORDINATE") ingo@298: ); ingo@298: } ingo@298: catch(ParseException pe) { ingo@298: log.warn("No data found while parsing."); ingo@298: } ingo@298: } ingo@298: ingo@298: ingo@334: protected void addSeries(Series series, String label, int idx) { ingo@334: super.addSeries(series, label, idx); ingo@298: ingo@298: // reset values used by current series for next series ingo@298: lastPoint = null; ingo@298: distance = 0; ingo@298: } ingo@298: ingo@298: ingo@298: protected String createSeriesName( ingo@298: String breakPoint1, ingo@298: String breakPoint2, ingo@298: String breakPoint3 ingo@298: ) { ingo@298: log.debug("create seriesname of horizontalprofile chart"); ingo@298: return super.createSeriesName( ingo@298: breakPoint1, ingo@298: breakPoint2, ingo@298: breakPoint3) + ingo@298: " " + ingo@298: findValueTitle(dates, breakPoint3); ingo@298: } ingo@340: ingo@340: ingo@340: protected void addGapsOnGrid( ingo@340: Result[] results, ingo@340: Series series, ingo@340: int startPos, ingo@340: int endPos ingo@340: ) { ingo@340: String axis = getDependendAxisName( ingo@340: results[startPos], ingo@340: results[startPos+1] ingo@340: ); ingo@340: ingo@340: double range = 0; ingo@340: double distance = 0; ingo@340: int last = 0; ingo@340: int current = 0; ingo@340: Point lastPoint = null; ingo@340: Point currentPoint = null; ingo@340: ingo@340: for (int i = startPos+1; i < endPos; i++) { ingo@340: try { ingo@340: last = results[i-1].getInteger(axis); ingo@340: lastPoint = getPoint(results[i-1]); ingo@340: current = results[i].getInteger(axis); ingo@340: currentPoint = getPoint(results[i]); ingo@340: distance = DistanceCalculator.calculateDistance( ingo@340: lastPoint, ingo@340: currentPoint ingo@340: ); ingo@340: ingo@340: boolean detected = gridDetection(last, current); ingo@340: ingo@340: if (detected) { ingo@340: log.debug( ingo@340: "Gap detected on grid between " + range + ingo@340: " and " + (range+distance) ingo@340: ); ingo@340: ingo@340: ((XYSeries) series).add(range+0.0001, null); ingo@340: } ingo@340: ingo@340: range += distance; ingo@340: } ingo@340: catch (ParseException pe) { ingo@340: log.warn("Error while parsing point for gap detection.", pe); ingo@340: } ingo@340: } ingo@340: } ingo@340: ingo@340: ingo@340: protected void addGaps( ingo@340: Result[] results, ingo@340: Series series, ingo@340: Point startValue, ingo@340: Point endValue, ingo@340: int startPos, ingo@340: int endPos ingo@340: ) { ingo@340: double range = 0; ingo@340: Point last = null; ingo@340: Point now = null; ingo@340: ingo@340: for (int i = startPos+1; i < endPos; i++) { ingo@340: boolean detected = false; ingo@340: ingo@340: try { ingo@340: last = (Point) getPoint(results[i-1]); ingo@340: now = (Point) getPoint(results[i]); ingo@340: ingo@340: // gap detection for more than GAP_MAX_VALUES values ingo@340: if (results.length > GAP_MAX_VALUES) ingo@340: detected = simpleDetection(startValue, endValue, last, now); ingo@340: // gap detection for less than GAP_MAX_VALUES values ingo@340: else ingo@340: detected = specialDetection( ingo@340: startValue, ingo@340: endValue, ingo@340: last, ingo@340: now, ingo@340: results.length ingo@340: ); ingo@340: ingo@340: // gap detected, insert null value to break line ingo@340: if (detected) { ingo@340: log.info("Gap after " + range); ingo@340: double x = range + 0.0001; ingo@340: ingo@340: ((XYSeries)series).add(x, null); ingo@340: } ingo@340: ingo@340: range += DistanceCalculator.calculateDistance(last,now); ingo@340: } ingo@340: catch (ParseException pe) { ingo@340: log.warn("Error while parsing point."); ingo@340: } ingo@340: ingo@340: } ingo@340: } ingo@340: ingo@340: ingo@340: protected boolean simpleDetection( ingo@340: Point start, ingo@340: Point end, ingo@340: Point last, ingo@340: Point current ingo@340: ) { ingo@340: double delta = DistanceCalculator.calculateDistance(start, end); ingo@340: double deltaSmall = DistanceCalculator.calculateDistance(last,current); ingo@340: ingo@340: return (deltaSmall > (delta / 100 * PERCENTAGE)); ingo@340: } ingo@340: ingo@340: ingo@340: protected boolean specialDetection( ingo@340: Point start, ingo@340: Point end, ingo@340: Point last, ingo@340: Point current, ingo@340: int count ingo@340: ) { ingo@340: double delta = Math.abs( ingo@340: DistanceCalculator.calculateDistance(end, start) ingo@340: ); ingo@340: double smallDelta = Math.abs( ingo@340: DistanceCalculator.calculateDistance(current, last) ingo@340: ); ingo@340: ingo@340: return (smallDelta > (3.0 / (count - 1) * delta)); ingo@340: } ingo@340: ingo@370: @Override ingo@370: protected String getDependendAxisName(Result first, Result second) { ingo@370: if (first.getInteger("IPOSITION") == second.getInteger("IPOSITION")) ingo@370: return "JPOSITION"; ingo@370: ingo@370: return "IPOSITION"; ingo@370: } ingo@340: ingo@340: private Point getPoint(Result result) ingo@340: throws ParseException ingo@340: { ingo@340: return (Point) wktReader.read(result.getString("SHAPE")); ingo@340: } ingo@298: } ingo@327: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :