Skip to content

Commit

Permalink
Add and expose stubMinLength
Browse files Browse the repository at this point in the history
  • Loading branch information
wipfli committed Nov 17, 2024
1 parent dd0f572 commit a7cf4f6
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,69 @@ public static List<VectorTile.Feature> mergeLineStrings(List<VectorTile.Feature>
return result;
}

public static List<VectorTile.Feature> mergeLineStringsAllParams(List<VectorTile.Feature> features,
Function<Map<String, Object>, Double> lengthLimitCalculator, double tolerance, double buffer, boolean resimplify, double loopMinLength, double stubMinLength) {
List<VectorTile.Feature> result = new ArrayList<>(features.size());
var groupedByAttrs = groupByAttrs(features, result, GeometryType.LINE);
for (List<VectorTile.Feature> groupedFeatures : groupedByAttrs) {
VectorTile.Feature feature1 = groupedFeatures.getFirst();
double lengthLimit = lengthLimitCalculator.apply(feature1.tags());

// as a shortcut, can skip line merging only if:
// - only 1 element in the group
// - it doesn't need to be clipped
// - and it can't possibly be filtered out for being too short
// - and it does not need to be simplified
if (groupedFeatures.size() == 1 && buffer == 0d && lengthLimit == 0 && (!resimplify || tolerance == 0)) {
result.add(feature1);
} else {
LoopLineMerger merger = new LoopLineMerger()
.setTolerance(tolerance)
.setMergeStrokes(true)
.setMinLength(lengthLimit)
.setLoopMinLength(loopMinLength)
.setStubMinLength(stubMinLength);
for (VectorTile.Feature feature : groupedFeatures) {
try {
merger.add(feature.geometry().decode());
} catch (GeometryException e) {
e.log("Error decoding vector tile feature for line merge: " + feature);
}
}
List<LineString> outputSegments = new ArrayList<>();
var i = 0;
for (var line : merger.getMergedLineStrings()) {
// TODO remove debug features comment
// Map<String, Object> attrs = new HashMap<>();
// attrs.put("idx", i++);
// result.add(feature1.copyWithNewGeometry(line.getStartPoint()).copyWithExtraAttrs(attrs));
// result.add(feature1.copyWithNewGeometry(line).copyWithExtraAttrs(attrs));
// attrs.put("end", "yes");
// result.add(feature1.copyWithNewGeometry(line.getEndPoint()).copyWithExtraAttrs(attrs));

if (buffer >= 0) {
removeDetailOutsideTile(line, buffer, outputSegments);
} else {
outputSegments.add(line);
}
}

if (!outputSegments.isEmpty()) {
outputSegments = sortByHilbertIndex(outputSegments);
Geometry newGeometry = GeoUtils.combineLineStrings(outputSegments);
result.add(feature1.copyWithNewGeometry(newGeometry));
// i = 0;
// for (var outputSegment : outputSegments) {
// Map<String, Object> attrs = new HashMap<>();
// attrs.put("idx", ++i);
// result.add(feature1.copyWithNewGeometry(outputSegment).copyWithExtraAttrs(attrs));
// }
}
}
}
return result;
}

/**
* Removes any segments from {@code input} where both the start and end are outside the tile boundary (plus {@code
* buffer}) and puts the resulting segments into {@code output}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class LoopLineMerger {
private GeometryFactory factory = new GeometryFactory(precisionModel);
private double minLength = 0.0;
private double loopMinLength = 0.0;
private double stubMinLength = 0.0;
private double tolerance = 0.0;
private boolean mergeStrokes = false;

Expand All @@ -46,6 +47,11 @@ public LoopLineMerger setLoopMinLength(double loopMinLength) {
return this;
}

public LoopLineMerger setStubMinLength(double stubMinLength) {
this.stubMinLength = stubMinLength;
return this;
}

public LoopLineMerger setTolerance(double tolerance) {
this.tolerance = tolerance;
return this;
Expand Down Expand Up @@ -238,13 +244,13 @@ public List<LineString> getMergedLineStrings() {
degreeTwoMerge();
}

if (minLength > 0.0) {
if (stubMinLength > 0.0) {
double step = 1.0 / precisionModel.getScale();
for (double stubMinLength = 0.0; stubMinLength < minLength; stubMinLength += step) {
removeShortStubEdges(stubMinLength);
for (double current = step; current < stubMinLength; current += step) {
removeShortStubEdges(current);
degreeTwoMerge();
}
removeShortStubEdges(minLength);
removeShortStubEdges(stubMinLength);
degreeTwoMerge();
}

Expand Down

0 comments on commit a7cf4f6

Please sign in to comment.