MapRenderer { public static Geometry computeGeomInDomainOfValidity(Geometry geom, CoordinateReferenceSystem mapCRS) { final GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(null); final Extent domainOfValidity = mapCRS.getDomainOfValidity(); Geometry adjustedGeom = geom; if (domainOfValidity != null) { for (final GeographicExtent extent : domainOfValidity.getGeographicElements()) { if (Boolean.FALSE.equals(extent.getInclusion())) { continue; } if (extent instanceof GeographicBoundingBox) { if (extent != null) { GeographicBoundingBox box = (GeographicBoundingBox) extent; Envelope env = new Envelope( box.getWestBoundLongitude(), box.getEastBoundLongitude(), box.getSouthBoundLatitude(), box.getNorthBoundLatitude()); if (env.contains(geom.getEnvelopeInternal())) { return geom; } else { Geometry extentPolygon = JTS.toGeometry(env); adjustedGeom = geom.intersection(extentPolygon); } } } } return adjustedGeom; } else { return geom; } } MapRenderer(ServiceContext context); static AffineTransform worldToScreenTransform(Envelope mapExtent, Dimension screenSize); static Geometry computeGeomInDomainOfValidity(Geometry geom, CoordinateReferenceSystem mapCRS); BufferedImage render(String id, String srs, Integer width, Integer height,
                                String background, String geomParam, String geomType, String geomSrs, String fillColor, String strokeColor);  }
@Test public void domainContainsBoundingBox() throws Exception { String test = "POLYGON ((40 -63, 40 -20, 70 -20, 70 -63, 40 -63))"; Geometry bbox = wktReader.read(test); bbox.setSRID(4326); String result = wktWriter.write(MapRenderer.computeGeomInDomainOfValidity(bbox, Region.decodeCRS("EPSG:3857"))); assertEquals(test, result); }
@Test public void domainIntersectsBoundingBox() throws Exception { String test = "POLYGON ((40 -86, 40 -20, 70 -20, 70 -86, 40 -86))"; Geometry bbox = wktReader.read(test); bbox.setSRID(4326); String result = wktWriter.write(MapRenderer.computeGeomInDomainOfValidity(bbox, Region.decodeCRS("EPSG:3857"))); assertEquals("POLYGON ((40 -85.06, 40 -20, 70 -20, 70 -85.06, 40 -85.06))", result); }
@Test public void domainContainsBoundingPolygon() throws Exception { String test = "POLYGON ((165 -83, 135 -76, 161 -76, 153 -81, 165 -83))"; Geometry bbox = wktReader.read(test); bbox.setSRID(4326); String result = wktWriter.write(MapRenderer.computeGeomInDomainOfValidity(bbox, Region.decodeCRS("EPSG:3857"))); assertEquals("POLYGON ((165 -83, 135 -76, 161 -76, 153 -81, 165 -83))", result); }
@Test public void domainIntersectsBoundingPolygon() throws Exception { String test = "POLYGON ((165 -87, 135 -76, 161 -76, 153 -81, 165 -87))"; Geometry bbox = wktReader.read(test); bbox.setSRID(4326); String result = wktWriter.write(MapRenderer.computeGeomInDomainOfValidity(bbox, Region.decodeCRS("EPSG:3857"))); assertEquals("POLYGON ((159.70909090909092 -85.06, 135 -76, 161 -76, 153 -81, 161.12 -85.06, 159.70909090909092 -85.06))", result); }