MergePolygonCommand implements CommandHasRequest<MergePolygonRequest, MergePolygonResponse> { @Override public void execute(MergePolygonRequest request, MergePolygonResponse response) throws Exception { Polygon[] polygons = new Polygon[request.getPolygons().length]; for (int i = 0; i < request.getPolygons().length; i++) { try { polygons[i] = (Polygon) converter.toInternal(request.getPolygons()[i]); } catch (Exception e) { throw new GeomajasException(e, ExceptionCode.MERGE_NO_POLYGON); } } int precision = polygons[0].getPrecisionModel().getMaximumSignificantDigits() - 1; PrecisionModel precisionModel = new PrecisionModel(Math.pow(10.0, precision)); GeometryFactory factory = new GeometryFactory(precisionModel, polygons[0].getSRID()); Geometry temp = factory.createGeometry(polygons[0]); for (int i = 1; i < polygons.length; i++) { Geometry polygon = factory.createGeometry(polygons[i]); temp = temp.union(polygon.buffer(Math.pow(10.0, -(precision - 1)))); } if (temp instanceof Polygon) { MultiPolygon mp = factory.createMultiPolygon(new Polygon[] { (Polygon) temp }); response.setGeometry(converter.toDto(mp)); } else if (temp instanceof MultiPolygon && temp.getNumGeometries() != 0 && (request.isAllowMultiPolygon() || temp.getNumGeometries() == 1)) { response.setGeometry(converter.toDto(temp)); } else { throw new GeomajasException(ExceptionCode.MERGE_NO_POLYGON); } }  @Override MergePolygonRequest getEmptyCommandRequest(); @Override MergePolygonResponse getEmptyCommandResponse(); @Override void execute(MergePolygonRequest request, MergePolygonResponse response);  }
@Test public void testMergePolygon() throws Exception { MergePolygonRequest request = new MergePolygonRequest(); GeometryFactory factory = new GeometryFactory(); request.setPolygons(new Geometry[] { dtoConverter.toDto(geoService.createCircle(factory.createPoint(new Coordinate(0, 0)), 10, 10)), dtoConverter.toDto(geoService.createCircle(factory.createPoint(new Coordinate(5, 5)), 10, 10))}); MergePolygonResponse response = (MergePolygonResponse) dispatcher.execute( MergePolygonRequest.COMMAND, request, null, "en"); if (response.isError()) { response.getErrors().get(0).printStackTrace(); } Assert.assertFalse(response.isError()); Assert.assertNotNull(response.getGeometry()); }