# HG changeset patch # User mschaefer # Date 1534237560 -7200 # Node ID 8e100593aec301af71f305c965ed30ad3d523b2d # Parent 77367e8da74d5077a5ff30670acba87a8ab78498 Missing class added diff -r 77367e8da74d -r 8e100593aec3 artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/tkhstate/BedHeightsUtils.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/tkhstate/BedHeightsUtils.java Tue Aug 14 11:06:00 2018 +0200 @@ -0,0 +1,92 @@ +/** Copyright (C) 2017 by Bundesanstalt für Gewässerkunde + * Software engineering by + * Björnsen Beratende Ingenieure GmbH + * Dr. Schumacher Ingenieurbüro für Wasser und Umwelt + * + * This file is Free Software under the GNU AGPL (>=v3) + * and comes with ABSOLUTELY NO WARRANTY! Check out the + * documentation coming with Dive4Elements River for details. + */ +package org.dive4elements.river.artifacts.sinfo.tkhstate; + +import java.util.Collection; +import java.util.TreeSet; + +import org.apache.commons.lang.ArrayUtils; +import org.apache.commons.lang.math.DoubleRange; + +/** + * Helper functions for bed height calculations + */ +public final class BedHeightsUtils { + + /** + * Get a union of all stations in a collection of bed heights + */ + public static double[] extractStations(final Collection bedHeightFinders) { + + return extractStations(bedHeightFinders, false); + } + + /** + * Get a union of all stations in a collection of bed heights + */ + public static double[] extractStations(final Collection bedHeightFinders, final boolean minMaxAware) { + + final Collection allStations = extractStationCollection(bedHeightFinders, minMaxAware); + return ArrayUtils.toPrimitive(allStations.toArray(new Double[allStations.size()])); + } + + /** + * Get a union of all stations in a collection of bed heights + */ + public static Collection extractStationCollection(final Collection bedHeightFinders, final boolean minMaxAware) { + + if (!minMaxAware) + return extractStationCollection(bedHeightFinders); + + final DoubleRange kmRange = extractMinimumStationRange(bedHeightFinders); + + final Collection allStations = new TreeSet<>(); + for (final BedHeightsFinder bhf : bedHeightFinders) { + for (final Double station : bhf.getStations()) + if ((station.doubleValue() >= kmRange.getMinimumDouble() - 0.0001) && (station.doubleValue() <= kmRange.getMaximumDouble() + 0.0001)) + allStations.add(station.doubleValue()); + } + return allStations; + } + + /** + * Get a union of all stations in a collection of bed heights + */ + public static Collection extractStationCollection(final Collection bedHeights) { + + final Collection allStations = new TreeSet<>(); + + for (final BedHeightsFinder bedHeight : bedHeights) + allStations.addAll(bedHeight.getStations()); + + return allStations; + } + + /** + * Intersects all km ranges of a collection of bed heights + */ + public static DoubleRange extractMinimumStationRange(final Collection bedHeightFinders) { + DoubleRange kmRange = null; + for (final BedHeightsFinder bhf : bedHeightFinders) + kmRange = intersectRanges(kmRange, bhf.getKmRange()); + return new DoubleRange(kmRange.getMinimumDouble() - 0.0001, kmRange.getMaximumDouble() + 0.0001); + } + + /** + * Creates a range with the minimum overlapping of two ranges + */ + public static DoubleRange intersectRanges(final DoubleRange a, final DoubleRange b) { + if (a == null) + return new DoubleRange(b.getMinimumDouble(), b.getMaximumDouble()); + if (b == null) + return new DoubleRange(a.getMinimumDouble(), a.getMaximumDouble()); + return new DoubleRange(Math.max(a.getMinimumDouble(), b.getMinimumDouble()), Math.min(a.getMaximumDouble(), b.getMaximumDouble())); + } +}