fix: merge line if it needs to be simplified (#244)

pull/273/head
farfromrefuge 2022-06-10 10:45:12 +00:00 zatwierdzone przez GitHub
rodzic c9682d5bee
commit b9d91e47cf
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
2 zmienionych plików z 62 dodań i 9 usunięć

Wyświetl plik

@ -63,24 +63,43 @@ public class FeatureMerge {
* <p>
* Orders grouped output multilinestring by the index of the first element in that group from the input list.
*
* @param features all features in a layer
* @param minLength minimum tile pixel length of features to emit, or 0 to emit all merged linestrings
* @param tolerance after merging, simplify linestrings using this pixel tolerance, or -1 to skip simplification step
* @param buffer number of pixels outside the visible tile area to include detail for, or -1 to skip clipping step
* @param features all features in a layer
* @param minLength minimum tile pixel length of features to emit, or 0 to emit all merged linestrings
* @param tolerance after merging, simplify linestrings using this pixel tolerance, or -1 to skip simplification step
* @param buffer number of pixels outside the visible tile area to include detail for, or -1 to skip clipping step
* @param resimplify True if linestrings should be simplified even if they don't get merged with another
* @return a new list containing all unaltered features in their original order, then each of the merged groups
* ordered by the index of the first element in that group from the input list.
*/
public static List<VectorTile.Feature> mergeLineStrings(List<VectorTile.Feature> features,
double minLength, double tolerance, double buffer) {
return mergeLineStrings(features, attrs -> minLength, tolerance, buffer);
double minLength, double tolerance, double buffer, boolean resimplify) {
return mergeLineStrings(features, attrs -> minLength, tolerance, buffer, resimplify);
}
/**
* Merges linestrings with the same attributes as {@link #mergeLineStrings(List, double, double, double)} except with
* a dynamic length limit computed by {@code lengthLimitCalculator} for the attributes of each group.
* Merges linestrings with the same attributes as {@link #mergeLineStrings(List, double, double, double, boolean)}
* except sets {@code resimplify=false} by default.
*/
public static List<VectorTile.Feature> mergeLineStrings(List<VectorTile.Feature> features,
double minLength, double tolerance, double buffer) {
return mergeLineStrings(features, minLength, tolerance, buffer, false);
}
/**
* Merges linestrings with the same attributes as {@link #mergeLineStrings(List, Function, double, double, boolean)}
* except sets {@code resimplify=false} by default.
*/
public static List<VectorTile.Feature> mergeLineStrings(List<VectorTile.Feature> features,
Function<Map<String, Object>, Double> lengthLimitCalculator, double tolerance, double buffer) {
return mergeLineStrings(features, lengthLimitCalculator, tolerance, buffer, false);
}
/**
* Merges linestrings with the same attributes as {@link #mergeLineStrings(List, double, double, double, boolean)}
* except with a dynamic length limit computed by {@code lengthLimitCalculator} for the attributes of each group.
*/
public static List<VectorTile.Feature> mergeLineStrings(List<VectorTile.Feature> features,
Function<Map<String, Object>, Double> lengthLimitCalculator, double tolerance, double buffer, boolean resimplify) {
List<VectorTile.Feature> result = new ArrayList<>(features.size());
var groupedByAttrs = groupByAttrs(features, result, GeometryType.LINE);
for (List<VectorTile.Feature> groupedFeatures : groupedByAttrs) {
@ -91,7 +110,8 @@ public class FeatureMerge {
// - 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
if (groupedFeatures.size() == 1 && buffer == 0d && lengthLimit == 0) {
// - and it does not need to be simplified
if (groupedFeatures.size() == 1 && buffer == 0d && lengthLimit == 0 && (!resimplify || tolerance == 0)) {
result.add(feature1);
} else {
LineMerger merger = new LineMerger();

Wyświetl plik

@ -121,6 +121,39 @@ class FeatureMergeTest {
);
}
@Test
void simplifyLineStringIfToleranceIsSet() {
// does not resimplify by default
assertEquals(
List.of(
feature(1, newLineString(10, 10, 20, 20, 30, 30), Map.of("a", 1))
),
FeatureMerge.mergeLineStrings(
List.of(
feature(1, newLineString(10, 10, 20, 20, 30, 30), Map.of("a", 1))
),
0,
1,
0
)
);
// but does resimplify when resimplify=true
assertEquals(
List.of(
feature(1, newLineString(10, 10, 30, 30), Map.of("a", 1))
),
FeatureMerge.mergeLineStrings(
List.of(
feature(1, newLineString(10, 10, 20, 20, 30, 30), Map.of("a", 1))
),
0,
1,
0,
true
)
);
}
@Test
void mergeMultiLineString() {
assertEquals(