6
6
import com .onthegomap .planetiler .collection .Hppc ;
7
7
import com .onthegomap .planetiler .geo .GeoUtils ;
8
8
import com .onthegomap .planetiler .geo .GeometryException ;
9
+ import com .onthegomap .planetiler .geo .GeometryPipeline ;
9
10
import com .onthegomap .planetiler .geo .GeometryType ;
10
11
import com .onthegomap .planetiler .geo .MutableCoordinateSequence ;
11
12
import com .onthegomap .planetiler .stats .DefaultStats ;
@@ -81,7 +82,7 @@ private FeatureMerge() {}
81
82
*/
82
83
public static List <VectorTile .Feature > mergeLineStrings (List <VectorTile .Feature > features ,
83
84
double minLength , double tolerance , double buffer , boolean resimplify ) {
84
- return mergeLineStrings (features , attrs -> minLength , tolerance , buffer , resimplify );
85
+ return mergeLineStrings (features , attrs -> minLength , tolerance , buffer , resimplify , null );
85
86
}
86
87
87
88
/**
@@ -143,20 +144,22 @@ private static List<VectorTile.Feature> mergeGeometries(
143
144
}
144
145
145
146
/**
146
- * Merges linestrings with the same attributes as {@link #mergeLineStrings(List, Function, double, double, boolean)}
147
- * except sets {@code resimplify=false} by default.
147
+ * Merges linestrings with the same attributes as
148
+ * {@link #mergeLineStrings(List, Function, double, double, boolean, GeometryPipeline)} except sets
149
+ * {@code resimplify=false} by default.
148
150
*/
149
151
public static List <VectorTile .Feature > mergeLineStrings (List <VectorTile .Feature > features ,
150
152
Function <Map <String , Object >, Double > lengthLimitCalculator , double tolerance , double buffer ) {
151
- return mergeLineStrings (features , lengthLimitCalculator , tolerance , buffer , false );
153
+ return mergeLineStrings (features , lengthLimitCalculator , tolerance , buffer , false , null );
152
154
}
153
155
154
156
/**
155
157
* Merges linestrings with the same attributes as {@link #mergeLineStrings(List, double, double, double, boolean)}
156
158
* except with a dynamic length limit computed by {@code lengthLimitCalculator} for the attributes of each group.
157
159
*/
158
160
public static List <VectorTile .Feature > mergeLineStrings (List <VectorTile .Feature > features ,
159
- Function <Map <String , Object >, Double > lengthLimitCalculator , double tolerance , double buffer , boolean resimplify ) {
161
+ Function <Map <String , Object >, Double > lengthLimitCalculator , double tolerance , double buffer , boolean resimplify ,
162
+ GeometryPipeline pipeline ) {
160
163
List <VectorTile .Feature > result = new ArrayList <>(features .size ());
161
164
var groupedByAttrs = groupByAttrs (features , result , GeometryType .LINE );
162
165
for (List <VectorTile .Feature > groupedFeatures : groupedByAttrs ) {
@@ -176,7 +179,8 @@ public static List<VectorTile.Feature> mergeLineStrings(List<VectorTile.Feature>
176
179
.setMergeStrokes (true )
177
180
.setMinLength (lengthLimit )
178
181
.setLoopMinLength (lengthLimit )
179
- .setStubMinLength (0.5 );
182
+ .setStubMinLength (0.5 )
183
+ .setSegmentTransform (pipeline );
180
184
for (VectorTile .Feature feature : groupedFeatures ) {
181
185
try {
182
186
merger .add (feature .geometry ().decode ());
@@ -287,12 +291,15 @@ public static List<VectorTile.Feature> mergeOverlappingPolygons(List<VectorTile.
287
291
* @param buffer the amount (in tile pixels) to expand then contract polygons by in order to combine
288
292
* almost-touching polygons
289
293
* @param stats for counting data errors
294
+ * @param pipeline a transform that should be applied to each merged polygon in tile pixel coordinates where
295
+ * {@code 0,0} is the top-left and {@code 256,256} is the bottom-right corner of the tile
290
296
* @return a new list containing all unaltered features in their original order, then each of the merged groups
291
297
* ordered by the index of the first element in that group from the input list.
292
298
* @throws GeometryException if an error occurs encoding the combined geometry
293
299
*/
294
300
public static List <VectorTile .Feature > mergeNearbyPolygons (List <VectorTile .Feature > features , double minArea ,
295
- double minHoleArea , double minDist , double buffer , Stats stats ) throws GeometryException {
301
+ double minHoleArea , double minDist , double buffer , Stats stats , GeometryPipeline pipeline )
302
+ throws GeometryException {
296
303
List <VectorTile .Feature > result = new ArrayList <>(features .size ());
297
304
Collection <List <VectorTile .Feature >> groupedByAttrs = groupByAttrs (features , result , GeometryType .POLYGON );
298
305
for (List <VectorTile .Feature > groupedFeatures : groupedByAttrs ) {
@@ -326,12 +333,26 @@ public static List<VectorTile.Feature> mergeNearbyPolygons(List<VectorTile.Featu
326
333
if (!(merged instanceof Polygonal ) || merged .getEnvelopeInternal ().getArea () < minArea ) {
327
334
continue ;
328
335
}
336
+ if (pipeline != null ) {
337
+ merged = pipeline .apply (merged );
338
+ if (!(merged instanceof Polygonal )) {
339
+ continue ;
340
+ }
341
+ }
329
342
merged = GeoUtils .snapAndFixPolygon (merged , stats , "merge" ).reverse ();
330
343
} else {
331
344
merged = polygonGroup .getFirst ();
332
345
if (!(merged instanceof Polygonal ) || merged .getEnvelopeInternal ().getArea () < minArea ) {
333
346
continue ;
334
347
}
348
+ if (pipeline != null ) {
349
+ Geometry after = pipeline .apply (merged );
350
+ if (!(after instanceof Polygonal )) {
351
+ continue ;
352
+ } else if (after != merged ) {
353
+ merged = GeoUtils .snapAndFixPolygon (after , stats , "merge_after_pipeline" ).reverse ();
354
+ }
355
+ }
335
356
}
336
357
extractPolygons (merged , outPolygons , minArea , minHoleArea );
337
358
}
@@ -354,7 +375,7 @@ private static <G extends Geometry> List<G> sortByHilbertIndex(List<G> geometrie
354
375
355
376
public static List <VectorTile .Feature > mergeNearbyPolygons (List <VectorTile .Feature > features , double minArea ,
356
377
double minHoleArea , double minDist , double buffer ) throws GeometryException {
357
- return mergeNearbyPolygons (features , minArea , minHoleArea , minDist , buffer , DefaultStats .get ());
378
+ return mergeNearbyPolygons (features , minArea , minHoleArea , minDist , buffer , DefaultStats .get (), null );
358
379
}
359
380
360
381
0 commit comments