diff --git a/deegree-core/deegree-core-geometry/src/main/java/org/deegree/geometry/standard/primitive/DefaultSurface.java b/deegree-core/deegree-core-geometry/src/main/java/org/deegree/geometry/standard/primitive/DefaultSurface.java index 2b05263573..143323027f 100644 --- a/deegree-core/deegree-core-geometry/src/main/java/org/deegree/geometry/standard/primitive/DefaultSurface.java +++ b/deegree-core/deegree-core-geometry/src/main/java/org/deegree/geometry/standard/primitive/DefaultSurface.java @@ -37,6 +37,8 @@ import java.math.BigDecimal; import java.util.ArrayList; import java.util.List; +import java.util.Optional; +import java.util.stream.Collectors; import org.deegree.commons.uom.Measure; import org.deegree.commons.uom.Unit; @@ -55,6 +57,7 @@ import org.locationtech.jts.algorithm.InteriorPointArea; import org.locationtech.jts.geom.Coordinate; import org.locationtech.jts.geom.LinearRing; +import org.locationtech.jts.geom.Polygon; /** * Default implementation of {@link Surface}. @@ -155,13 +158,28 @@ public List getInteriorRingsCoordinates() { @Override protected org.locationtech.jts.geom.Geometry buildJTSGeometry() { - - if (patches.size() < 1 || !(patches.get(0) instanceof PolygonPatch)) { + if (patches.size() < 1 || containsNonPolygonPatches()) { throw new IllegalArgumentException(Messages.getMessage("SURFACE_NOT_EQUIVALENT_TO_POLYGON")); } + if (patches.size() == 1) { + PolygonPatch patch = (PolygonPatch) patches.get(0); + return createJtsPolygon(patch); + } + Polygon[] jtsPolygons = patches.stream() + .map(patch -> createJtsPolygon((PolygonPatch) patch)) + .collect(Collectors.toList()) + .toArray(Polygon[]::new); + return jtsFactory.createMultiPolygon(jtsPolygons); + } + + private boolean containsNonPolygonPatches() { + Optional nonPolygonPatch = patches.stream() + .filter(patch -> !(patch instanceof PolygonPatch)) + .findAny(); + return nonPolygonPatch.isPresent(); + } - // TODO handle the other patches as well - PolygonPatch patch = (PolygonPatch) patches.get(0); + private static Polygon createJtsPolygon(PolygonPatch patch) { Ring exteriorRing = patch.getExteriorRing(); List interiorRings = patch.getInteriorRings();