-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add PolylinesNotOverlapping function
- Loading branch information
Showing
6 changed files
with
142 additions
and
21 deletions.
There are no files selected for viewing
79 changes: 79 additions & 0 deletions
79
...a/ch/geowerkstatt/ilivalidator/extensions/functions/PolylinesNotOverlappingIoxPlugin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package ch.geowerkstatt.ilivalidator.extensions.functions; | ||
|
||
import ch.interlis.ili2c.metamodel.PathEl; | ||
import ch.interlis.ili2c.metamodel.Viewable; | ||
import ch.interlis.iom.IomObject; | ||
import ch.interlis.iom_j.itf.impl.jtsext.geom.CompoundCurve; | ||
import ch.interlis.iox.IoxException; | ||
import ch.interlis.iox_j.jts.Iox2jtsext; | ||
import ch.interlis.iox_j.validator.Value; | ||
|
||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.stream.Collectors; | ||
|
||
public final class PolylinesNotOverlappingIoxPlugin extends BaseInterlisFunction { | ||
@Override | ||
public String getQualifiedIliName() { | ||
return "GeoW_FunctionsExt.PolylinesNotOverlapping"; | ||
} | ||
|
||
@Override | ||
protected Value evaluateInternal(String validationKind, String usageScope, IomObject contextObject, Value[] arguments) { | ||
Value argObjects = arguments[0]; | ||
Value argPath = arguments[1]; | ||
|
||
if (argObjects.isUndefined()) { | ||
return Value.createSkipEvaluation(); | ||
} | ||
|
||
if (argObjects.getComplexObjects() == null) { | ||
return Value.createUndefined(); | ||
} | ||
|
||
Collection<IomObject> polylineObjects; | ||
|
||
if (argPath.isUndefined()) { | ||
polylineObjects = argObjects.getComplexObjects(); | ||
} else { | ||
Viewable contextClass = EvaluationHelper.getContextClass(td, contextObject, argObjects); | ||
if (contextClass == null) { | ||
throw new IllegalStateException("unknown class in " + usageScope); | ||
} | ||
|
||
PathEl[] attributePath = EvaluationHelper.getAttributePathEl(validator, contextClass, argPath); | ||
polylineObjects = EvaluationHelper.evaluateAttributes(validator, argObjects, attributePath); | ||
} | ||
|
||
List<CompoundCurve> lines = convertToJTSLines(polylineObjects); | ||
boolean hasOverlap = hasLineOverlap(lines); | ||
return new Value(!hasOverlap); | ||
} | ||
|
||
private List<CompoundCurve> convertToJTSLines(Collection<IomObject> polylines) { | ||
return polylines.stream() | ||
.map(line -> { | ||
try { | ||
return Iox2jtsext.polyline2JTS(line, false, 0); | ||
} catch (IoxException e) { | ||
logger.addEvent(logger.logErrorMsg("Could not calculate {0}", getQualifiedIliName())); | ||
return null; | ||
} | ||
}) | ||
.filter(Objects::nonNull) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
private static boolean hasLineOverlap(List<CompoundCurve> lines) { | ||
for (int i = 0; i < lines.size(); i++) { | ||
for (int j = i + 1; j < lines.size(); j++) { | ||
if (lines.get(i).overlaps(lines.get(j))) { | ||
return true; | ||
} | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 0 additions & 21 deletions
21
src/test/data/DetectPolylineOverlay/DetectPolylineOverlay.ili
This file was deleted.
Oops, something went wrong.
28 changes: 28 additions & 0 deletions
28
src/test/data/PolylinesNotOverlapping/PolylinesNotOverlapping.ili
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
INTERLIS 2.4; | ||
|
||
MODEL TestSuite | ||
AT "mailto:[email protected]" VERSION "2023-12-14" = | ||
IMPORTS GeoW_FunctionsExt; | ||
|
||
DOMAIN | ||
!!@CRS=EPSG:2056 | ||
CHKoord = COORD 2460000.000 .. 2870000.000 [INTERLIS.m], | ||
1045000.000 .. 1310000.000 [INTERLIS.m], | ||
ROTATION 2 -> 1; | ||
|
||
TOPIC FunctionTestTopic = | ||
|
||
CLASS TestClass = | ||
geometry : POLYLINE WITH (STRAIGHTS) VERTEX CHKoord WITHOUT OVERLAPS > 0.001; | ||
type : (t1,t2,t3); | ||
|
||
SET CONSTRAINT setConstraintAll : GeoW_FunctionsExt.PolylinesNotOverlapping(ALL, "geometry"); | ||
SET CONSTRAINT setConstraintT1 : WHERE type == #t1 : GeoW_FunctionsExt.PolylinesNotOverlapping(ALL, "geometry"); | ||
SET CONSTRAINT setConstraintT2 : WHERE type == #t2 : GeoW_FunctionsExt.PolylinesNotOverlapping(ALL, "geometry"); | ||
SET CONSTRAINT setConstraintT3 : WHERE type == #t3 : GeoW_FunctionsExt.PolylinesNotOverlapping(ALL, "geometry"); | ||
SET CONSTRAINT setConstraintT2or3 : WHERE type == #t2 OR type == #t3 : GeoW_FunctionsExt.PolylinesNotOverlapping(ALL, "geometry"); | ||
END TestClass; | ||
|
||
END FunctionTestTopic; | ||
|
||
END TestSuite. |
File renamed without changes.
29 changes: 29 additions & 0 deletions
29
.../geowerkstatt/ilivalidator/extensions/functions/PolylinesNotOverlappingIoxPluginTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package ch.geowerkstatt.ilivalidator.extensions.functions; | ||
|
||
import ch.interlis.ili2c.Ili2cFailure; | ||
import ch.interlis.iox.IoxException; | ||
import com.vividsolutions.jts.util.Assert; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public final class PolylinesNotOverlappingIoxPluginTest { | ||
private static final String TEST_DATA = "PolylinesNotOverlapping/TestData.xtf"; | ||
private ValidationTestHelper vh = null; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
vh = new ValidationTestHelper(); | ||
vh.addFunction(new PolylinesNotOverlappingIoxPlugin()); | ||
} | ||
|
||
@Test | ||
void polylinesNotOverlapping() throws Ili2cFailure, IoxException { | ||
vh.runValidation(new String[]{TEST_DATA}, new String[]{"PolylinesNotOverlapping/PolylinesNotOverlapping.ili"}); | ||
Assert.equals(2, vh.getErrs().size()); | ||
AssertionHelper.assertConstraintErrors(vh, 1, "setConstraintAll"); | ||
AssertionHelper.assertConstraintErrors(vh, 1, "setConstraintT1"); | ||
AssertionHelper.assertNoConstraintError(vh, "setConstraintT2"); | ||
AssertionHelper.assertNoConstraintError(vh, "setConstraintT3"); | ||
AssertionHelper.assertNoConstraintError(vh, "setConstraintT2or3"); | ||
} | ||
} |