-
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.
Merge pull request #35 Add Union function
- Loading branch information
Showing
10 changed files
with
439 additions
and
19 deletions.
There are no files selected for viewing
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
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
108 changes: 108 additions & 0 deletions
108
src/main/java/ch/geowerkstatt/ilivalidator/extensions/functions/UnionIoxPlugin.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,108 @@ | ||
package ch.geowerkstatt.ilivalidator.extensions.functions; | ||
|
||
import ch.ehi.basics.types.OutParam; | ||
import ch.interlis.iom.IomObject; | ||
import ch.interlis.iox.IoxException; | ||
import ch.interlis.iox_j.jts.Iox2jtsException; | ||
import ch.interlis.iox_j.jts.Iox2jtsext; | ||
import ch.interlis.iox_j.jts.Jts2iox; | ||
import ch.interlis.iox_j.jts.Jtsext2iox; | ||
import ch.interlis.iox_j.validator.Value; | ||
import com.vividsolutions.jts.geom.Geometry; | ||
import com.vividsolutions.jts.geom.GeometryFactory; | ||
import com.vividsolutions.jts.geom.MultiPolygon; | ||
import com.vividsolutions.jts.geom.Polygon; | ||
|
||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
|
||
public final class UnionIoxPlugin extends BaseInterlisFunction { | ||
private static final Map<UnionSurfaceKey, IomObject> UNION_SURFACE_CACHE = new HashMap<>(); | ||
|
||
@Override | ||
public String getQualifiedIliName() { | ||
return "GeoW_FunctionsExt.Union"; | ||
} | ||
|
||
@Override | ||
protected Value evaluateInternal(String validationKind, String usageScope, IomObject contextObject, Value[] actualArguments) { | ||
Value argGeometries = actualArguments[0]; | ||
|
||
if (argGeometries.isUndefined()) { | ||
return Value.createSkipEvaluation(); | ||
} | ||
|
||
Collection<IomObject> surfaces = argGeometries.getComplexObjects(); | ||
if (surfaces == null) { | ||
return Value.createUndefined(); | ||
} | ||
|
||
UnionSurfaceKey key = new UnionSurfaceKey(surfaces); | ||
IomObject unionSurface = UNION_SURFACE_CACHE.computeIfAbsent(key, this::union); | ||
if (unionSurface == null) { | ||
return Value.createUndefined(); | ||
} | ||
return new Value(Collections.singletonList(unionSurface)); | ||
} | ||
|
||
private IomObject union(UnionSurfaceKey key) { | ||
Collection<IomObject> surfaces = key.surfaces; | ||
|
||
MultiPolygon[] polygons = surfaces.stream() | ||
.map(surface -> { | ||
try { | ||
return Iox2jtsext.multisurface2JTS(surface, 0, new OutParam<>(), logger, 0, "warning"); | ||
} catch (IoxException e) { | ||
logger.addEvent(logger.logErrorMsg("Could not convert surface to JTS")); | ||
return null; | ||
} | ||
}) | ||
.filter(Objects::nonNull) | ||
.toArray(MultiPolygon[]::new); | ||
|
||
Geometry geometryCollection = new GeometryFactory().createGeometryCollection(polygons); | ||
Geometry unionGeometry = geometryCollection.union(); | ||
|
||
try { | ||
if (unionGeometry instanceof Polygon) { | ||
return Jtsext2iox.JTS2surface((Polygon) unionGeometry); | ||
} else if (unionGeometry instanceof MultiPolygon) { | ||
return Jts2iox.JTS2multisurface((MultiPolygon) unionGeometry); | ||
} else { | ||
logger.addEvent(logger.logErrorMsg("Expected {0} or {1} but was {2}", Polygon.class.toString(), MultiPolygon.class.toString(), unionGeometry.getClass().toString())); | ||
return null; | ||
} | ||
} catch (Iox2jtsException e) { | ||
logger.addEvent(logger.logErrorMsg("Could not calculate {0}", this.getQualifiedIliName())); | ||
return null; | ||
} | ||
} | ||
|
||
private static final class UnionSurfaceKey { | ||
private final Collection<IomObject> surfaces; | ||
|
||
private UnionSurfaceKey(Collection<IomObject> surfaces) { | ||
this.surfaces = surfaces; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (!(o instanceof UnionSurfaceKey)) { | ||
return false; | ||
} | ||
UnionSurfaceKey that = (UnionSurfaceKey) o; | ||
return Objects.equals(surfaces, that.surfaces); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(surfaces); | ||
} | ||
} | ||
} |
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
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
31 changes: 31 additions & 0 deletions
31
src/test/data/Union/MandatoryConstraintMergedInnerRings.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,31 @@ | ||
INTERLIS 2.4; | ||
|
||
MODEL TestSuite | ||
AT "mailto:[email protected]" VERSION "2023-12-13" = | ||
IMPORTS GeoW_FunctionsExt; | ||
|
||
DOMAIN | ||
Coord = COORD 0 .. 10 [INTERLIS.m], | ||
0 .. 10 [INTERLIS.m], | ||
ROTATION 2 -> 1; | ||
|
||
Surface = SURFACE WITH (STRAIGHTS) VERTEX Coord WITHOUT OVERLAPS > 0.1; | ||
|
||
TOPIC FunctionTestTopic = | ||
|
||
CLASS BaseClass = | ||
surfaceAttribute : BAG OF Surface; | ||
|
||
!! Surfaces evaluated separately | ||
MANDATORY CONSTRAINT innerRings: | ||
GeoW_FunctionsExt.GetInnerRingsCount(THIS, "surfaceAttribute") == 2; | ||
|
||
!! Surfaces evaluated as union (inner rings overlap in test data) | ||
MANDATORY CONSTRAINT innerRingsUnion: | ||
GeoW_FunctionsExt.GetInnerRingsCount(GeoW_FunctionsExt.Union(THIS->surfaceAttribute), UNDEFINED) == 1; | ||
|
||
END BaseClass; | ||
|
||
END FunctionTestTopic; | ||
|
||
END TestSuite. |
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,30 @@ | ||
INTERLIS 2.4; | ||
|
||
MODEL TestSuite | ||
AT "mailto:[email protected]" VERSION "2023-12-13" = | ||
IMPORTS GeoW_FunctionsExt; | ||
|
||
DOMAIN | ||
!!@CRS=EPSG:2056 | ||
CHKoord = COORD 2460000.000 .. 2870000.000 [INTERLIS.m], | ||
1045000.000 .. 1310000.000 [INTERLIS.m], | ||
ROTATION 2 -> 1; | ||
|
||
Surface = SURFACE WITH (STRAIGHTS) VERTEX CHKoord WITHOUT OVERLAPS > 0.1; | ||
|
||
TOPIC FunctionTestTopic = | ||
|
||
CLASS BaseClass = | ||
surfaceAttribute : BAG OF Surface; | ||
|
||
MANDATORY CONSTRAINT falseConstraint: | ||
GeoW_FunctionsExt.GetInnerRingsCount(GeoW_FunctionsExt.Union(THIS->surfaceAttribute), UNDEFINED) > 3; | ||
|
||
MANDATORY CONSTRAINT oneInnerRingConstraint: | ||
GeoW_FunctionsExt.GetInnerRingsCount(GeoW_FunctionsExt.Union(THIS->surfaceAttribute), UNDEFINED) == 1; | ||
|
||
END BaseClass; | ||
|
||
END FunctionTestTopic; | ||
|
||
END TestSuite. |
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,101 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<ili:transfer xmlns:ili="http://www.interlis.ch/xtf/2.4/INTERLIS" xmlns:geom="http://www.interlis.ch/geometry/1.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xmlns:GeoW_FunctionsExt="http://www.interlis.ch/xtf/2.4/GeoW_FunctionsExt" | ||
xmlns:TestSuite="http://www.interlis.ch/xtf/2.4/TestSuite"> | ||
<ili:headersection> | ||
<ili:models> | ||
<ili:model>GeoW_FunctionsExt</ili:model> | ||
<ili:model>TestSuite</ili:model> | ||
</ili:models> | ||
<ili:sender>ili2gpkg-4.6.1-63db90def1260a503f0f2d4cb846686cd4851184</ili:sender> | ||
</ili:headersection> | ||
<ili:datasection> | ||
<TestSuite:FunctionTestTopic ili:bid="TestSuite.FunctionTestTopic"> | ||
<TestSuite:BaseClass ili:tid="0"> | ||
<TestSuite:surfaceAttribute> <!-- Surface with 1 inner ring --> | ||
<geom:surface> | ||
<geom:exterior> | ||
<geom:polyline> | ||
<geom:coord> | ||
<geom:c1>2530000</geom:c1> | ||
<geom:c2>1150000</geom:c2> | ||
</geom:coord> | ||
<geom:coord> | ||
<geom:c1>2530010</geom:c1> | ||
<geom:c2>1150000</geom:c2> | ||
</geom:coord> | ||
<geom:coord> | ||
<geom:c1>2530010</geom:c1> | ||
<geom:c2>1150010</geom:c2> | ||
</geom:coord> | ||
<geom:coord> | ||
<geom:c1>2530000</geom:c1> | ||
<geom:c2>1150010</geom:c2> | ||
</geom:coord> | ||
<geom:coord> | ||
<geom:c1>2530000</geom:c1> | ||
<geom:c2>1150000</geom:c2> | ||
</geom:coord> | ||
</geom:polyline> | ||
</geom:exterior> | ||
<geom:interior> | ||
<geom:polyline> | ||
<geom:coord> | ||
<geom:c1>2530003</geom:c1> | ||
<geom:c2>1150003</geom:c2> | ||
</geom:coord> | ||
<geom:coord> | ||
<geom:c1>2530006</geom:c1> | ||
<geom:c2>1150003</geom:c2> | ||
</geom:coord> | ||
<geom:coord> | ||
<geom:c1>2530006</geom:c1> | ||
<geom:c2>1150006</geom:c2> | ||
</geom:coord> | ||
<geom:coord> | ||
<geom:c1>2530003</geom:c1> | ||
<geom:c2>1150006</geom:c2> | ||
</geom:coord> | ||
<geom:coord> | ||
<geom:c1>2530003</geom:c1> | ||
<geom:c2>1150003</geom:c2> | ||
</geom:coord> | ||
</geom:polyline> | ||
</geom:interior> | ||
</geom:surface> | ||
</TestSuite:surfaceAttribute> | ||
</TestSuite:BaseClass> | ||
<TestSuite:BaseClass ili:tid="1"> | ||
<TestSuite:surfaceAttribute> <!-- Surface without inner ring --> | ||
<geom:surface> | ||
<geom:exterior> | ||
<geom:polyline> | ||
<geom:coord> | ||
<geom:c1>2646349.508</geom:c1> | ||
<geom:c2>1249022.931</geom:c2> | ||
</geom:coord> | ||
<geom:coord> | ||
<geom:c1>2646353.508</geom:c1> | ||
<geom:c2>1249022.926</geom:c2> | ||
</geom:coord> | ||
<geom:coord> | ||
<geom:c1>2646353.503</geom:c1> | ||
<geom:c2>1249018.926</geom:c2> | ||
</geom:coord> | ||
<geom:coord> | ||
<geom:c1>2646349.503</geom:c1> | ||
<geom:c2>1249018.931</geom:c2> | ||
</geom:coord> | ||
<geom:coord> | ||
<geom:c1>2646349.508</geom:c1> | ||
<geom:c2>1249022.931</geom:c2> | ||
</geom:coord> | ||
</geom:polyline> | ||
</geom:exterior> | ||
</geom:surface> | ||
</TestSuite:surfaceAttribute> | ||
</TestSuite:BaseClass> | ||
</TestSuite:FunctionTestTopic> | ||
</ili:datasection> | ||
</ili:transfer> |
Oops, something went wrong.