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());
}
Aggregations