Bbox implements Serializable { public static Bbox copyToCRS(Bbox source, String targetCRS) { Bbox newBox = copyValues(source, new Bbox()); CoordinateReferenceSystem sourceCRS = DefaultGeographicCRS.WGS84; Pair<Pair<Double,Double>,Pair<Double,Double>> points = getPoints(source); if(points != null) { try { MathTransform transform = CRS.findMathTransform(sourceCRS, CRS.decode(targetCRS)); DirectPosition minPoint = new DirectPosition2D(sourceCRS, points.getLeft().getLeft(), points.getLeft().getRight()); minPoint = transform.transform(minPoint, null); DirectPosition maxPoint = new DirectPosition2D(sourceCRS, points.getRight().getLeft(), points.getRight().getRight()); maxPoint = transform.transform(maxPoint, null); newBox.setBbox(minPoint.getCoordinate()[0], minPoint.getCoordinate()[1], maxPoint.getCoordinate()[0], maxPoint.getCoordinate()[1]); } catch(Exception e) { log.error("Failed to transform BBOX points from EPSG:4326 to " + targetCRS); newBox = new Bbox(); } log.debug("Transformed box: " + newBox.getBbox()); } return newBox; }  @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "id") int getId(); void setId(int id); @Column(name = "bbox", columnDefinition = "box2d") String getBbox(); void setBbox(String bbox); void setBbox(double minX, double minY, double maxX, double maxY); Envelope makeEnvelope(); static Pair<Pair<Double,Double>,Pair<Double,Double>> getPoints(Bbox source); static Bbox copyValues(Bbox from, Bbox to); static Polygon envelopeToPolygon(Envelope e); static Bbox copyToSquareBox(Bbox source); static Bbox copyToCRS(Bbox source, String targetCRS); static final int SRID; static final String BOX_FORMAT; }
@Test public void testCopyToCRS() { Bbox testBox = new Bbox(); testBox.setBbox(1, 1, 3, 2); Bbox resultBox = Bbox.copyToCRS(testBox, "EPSG:3857"); Pair<Pair<Double,Double>,Pair<Double,Double>> points = Bbox.getPoints(resultBox); assertEquals(new Double(111319.490793), points.getLeft().getLeft()); assertEquals(new Double(111325.142866), points.getLeft().getRight()); assertEquals(new Double(333958.47238), points.getRight().getLeft()); assertEquals(new Double(222684.208506), points.getRight().getRight()); }