SplitPolygonCommand implements CommandHasRequest<SplitPolygonRequest, SplitPolygonResponse> { @Override public void execute(SplitPolygonRequest request, SplitPolygonResponse response) throws Exception { com.vividsolutions.jts.geom.Geometry jtsGeometry = converter.toInternal(request.getGeometry()); if (!(jtsGeometry instanceof Polygon)) { throw new GeomajasException(ExceptionCode.UNEXPECTED_PROBLEM, "geometry has to be a Polygon"); } Polygon polygon = (Polygon) converter.toInternal(request.getGeometry()); jtsGeometry = converter.toInternal(request.getSplitter()); if (!(jtsGeometry instanceof LineString)) { throw new GeomajasException(ExceptionCode.UNEXPECTED_PROBLEM, "splitter has to be a LineString"); } LineString preciseLine = (LineString) jtsGeometry; int precision = polygon.getPrecisionModel().getMaximumSignificantDigits() - 1; com.vividsolutions.jts.geom.Geometry bufferedLine = preciseLine.buffer(Math.pow(10.0, -precision)); com.vividsolutions.jts.geom.Geometry diff = polygon.difference(bufferedLine); if (diff instanceof Polygon) { response.setGeometries(new Geometry[] { converter.toDto(diff) }); } else if (diff instanceof MultiPolygon) { Geometry[] polygons = new Geometry[diff.getNumGeometries()]; for (int i = 0; i < diff.getNumGeometries(); i++) { polygons[i] = converter.toDto(diff.getGeometryN(i)); } response.setGeometries(polygons); } }  @Override SplitPolygonRequest getEmptyCommandRequest(); @Override SplitPolygonResponse getEmptyCommandResponse(); @Override void execute(SplitPolygonRequest request, SplitPolygonResponse response);  }
@Test public void testSplitPolygon() throws Exception { SplitPolygonRequest request = new SplitPolygonRequest(); GeometryFactory factory = new GeometryFactory(); request.setGeometry( dtoConverter.toDto(geoService.createCircle(factory.createPoint(new Coordinate(0, 0)), 10, 10))); request.setSplitter( dtoConverter.toDto(factory.createLineString(new Coordinate[] { new Coordinate(-10, -10), new Coordinate(10, 10) }))); SplitPolygonResponse response = (SplitPolygonResponse) dispatcher.execute( SplitPolygonRequest.COMMAND, request, null, "en"); if (response.isError()) { response.getErrors().get(0).printStackTrace(); } Assert.assertFalse(response.isError()); Assert.assertNotNull(response.getGeometries()); }