Search in sources :

Example 1 with DependencyOrderedList

use of eu.esdihumboldt.util.DependencyOrderedList in project hale by halestudio.

the class CurveHelper method combineCurve.

/**
 * Combine the given {@link LineString}s using the given builder.
 *
 * @param lineStrings the line strings
 * @param strict if it should be checked if the geometry fulfills the strict
 *            requirements of a curve
 * @param builder the builder function creating a combination of the
 *            individual {@link LineString}s
 * @return the combined {@link MultiLineString} or <code>null</code> if the
 *         geometry did not meet the requirements of the strict mode
 */
@Nullable
public static <T> T combineCurve(List<? extends LineString> lineStrings, boolean strict, Function<List<LineString>, T> builder) {
    // try to order by start/end point (e.g. for composite curves)
    Map<Coordinate, LineString> endPoints = new HashMap<>();
    for (LineString element : lineStrings) {
        endPoints.put(element.getEndPoint().getCoordinate(), element);
    }
    Map<LineString, Set<LineString>> dependencies = new HashMap<>();
    for (LineString element : lineStrings) {
        // check if there is another line that ends at this line's start
        // and build the dependency map accordingly
        LineString dependsOn = endPoints.get(element.getStartPoint().getCoordinate());
        @SuppressWarnings("unchecked") Set<LineString> deps = (Set<LineString>) ((dependsOn == null) ? Collections.emptySet() : Collections.singleton(dependsOn));
        dependencies.put(element, deps);
    }
    // use dependency ordered list to achieve sorting
    // will only yield a perfect result if all lines can be combined into
    // one
    DependencyOrderedList<LineString> ordered = new DependencyOrderedList<>(dependencies);
    if (strict) {
        Coordinate lastEndPoint = null;
        for (LineString lineString : ordered.getInternalList()) {
            if (lastEndPoint != null) {
                // start point must be equal to last end point
                if (!lineString.getStartPoint().getCoordinate().equals(lastEndPoint)) {
                    // not a strict curve
                    return null;
                }
            }
            lastEndPoint = lineString.getEndPoint().getCoordinate();
        }
    } else {
    // "best effort"
    }
    return builder.apply(ordered.getInternalList());
}
Also used : Set(java.util.Set) Coordinate(com.vividsolutions.jts.geom.Coordinate) LineString(com.vividsolutions.jts.geom.LineString) MultiLineString(com.vividsolutions.jts.geom.MultiLineString) HashMap(java.util.HashMap) DependencyOrderedList(eu.esdihumboldt.util.DependencyOrderedList) Nullable(javax.annotation.Nullable)

Aggregations

Coordinate (com.vividsolutions.jts.geom.Coordinate)1 LineString (com.vividsolutions.jts.geom.LineString)1 MultiLineString (com.vividsolutions.jts.geom.MultiLineString)1 DependencyOrderedList (eu.esdihumboldt.util.DependencyOrderedList)1 HashMap (java.util.HashMap)1 Set (java.util.Set)1 Nullable (javax.annotation.Nullable)1