Skip to content

Commit e563bb1

Browse files
authored
Merge pull request #49 from ElectronicChartCentre/github-48
separate overridable method for command validation and repair. #48
2 parents ef427ee + 53198e4 commit e563bb1

File tree

1 file changed

+41
-17
lines changed

1 file changed

+41
-17
lines changed

src/main/java/no/ecc/vectortile/VectorTileEncoder.java

+41-17
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,44 @@ protected Geometry clipGeometry(Geometry geometry) {
313313
}
314314
}
315315

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+
316354
/**
317355
* @return a byte array with the vector tile
318356
*/
@@ -373,27 +411,13 @@ public byte[] encode() {
373411
y = 0;
374412
List<Integer> commands = commands(geometry);
375413

414+
// Extra step to parse and check validity and try to repair.
415+
commands = validateAndRepairCommands(commands, geometry);
416+
376417
// skip features with no geometry commands
377418
if (commands.isEmpty()) {
378419
continue;
379420
}
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-
}
397421

398422
featureBuilder.setType(geomType);
399423
featureBuilder.addAllGeometry(commands);

0 commit comments

Comments
 (0)