@@ -313,6 +313,44 @@ protected Geometry clipGeometry(Geometry geometry) {
313
313
}
314
314
}
315
315
316
+ /**
317
+ * Validate and potentially repair the given {@link List} of commands for the
318
+ * given {@link Geometry}. Will return a {@link List} of the validated and/or
319
+ * repaired commands.
320
+ * <p>
321
+ * This can be overridden to change behavior. By returning just the incoming
322
+ * {@link List} of commands instead, the encoding will be faster, but
323
+ * potentially less safe.
324
+ *
325
+ * @param commands
326
+ * @param geometry
327
+ * @return
328
+ */
329
+ protected List <Integer > validateAndRepairCommands (List <Integer > commands , Geometry geometry ) {
330
+ if (commands .isEmpty ()) {
331
+ return commands ;
332
+ }
333
+
334
+ GeomType geomType = toGeomType (geometry );
335
+ if (simplificationDistanceTolerance > 0.0 && geomType == GeomType .POLYGON ) {
336
+ double scale = autoScale ? (extent / 256.0 ) : 1.0 ;
337
+ Geometry decodedGeometry = VectorTileDecoder .decodeGeometry (gf , geomType , commands , scale );
338
+ if (!isValid (decodedGeometry )) {
339
+ // Invalid. Try more simplification and without preserving topology.
340
+ geometry = DouglasPeuckerSimplifier .simplify (geometry , simplificationDistanceTolerance * 2.0 );
341
+ if (geometry .isEmpty ()) {
342
+ Collections .emptyList ();
343
+ }
344
+ geomType = toGeomType (geometry );
345
+ x = 0 ;
346
+ y = 0 ;
347
+ return commands (geometry );
348
+ }
349
+ }
350
+
351
+ return commands ;
352
+ }
353
+
316
354
/**
317
355
* @return a byte array with the vector tile
318
356
*/
@@ -373,27 +411,13 @@ public byte[] encode() {
373
411
y = 0 ;
374
412
List <Integer > commands = commands (geometry );
375
413
414
+ // Extra step to parse and check validity and try to repair.
415
+ commands = validateAndRepairCommands (commands , geometry );
416
+
376
417
// skip features with no geometry commands
377
418
if (commands .isEmpty ()) {
378
419
continue ;
379
420
}
380
-
381
- // Extra step to parse and check validity and try to repair. Probably expensive.
382
- if (simplificationDistanceTolerance > 0.0 && geomType == GeomType .POLYGON ) {
383
- double scale = autoScale ? (extent / 256.0 ) : 1.0 ;
384
- Geometry decodedGeometry = VectorTileDecoder .decodeGeometry (gf , geomType , commands , scale );
385
- if (!isValid (decodedGeometry )) {
386
- // Invalid. Try more simplification and without preserving topology.
387
- geometry = DouglasPeuckerSimplifier .simplify (geometry , simplificationDistanceTolerance * 2.0 );
388
- if (geometry .isEmpty ()) {
389
- continue ;
390
- }
391
- geomType = toGeomType (geometry );
392
- x = 0 ;
393
- y = 0 ;
394
- commands = commands (geometry );
395
- }
396
- }
397
421
398
422
featureBuilder .setType (geomType );
399
423
featureBuilder .addAllGeometry (commands );
0 commit comments