FilterFunction_setCRS extends FunctionExpressionImpl { public Object evaluate(Object feature) { Geometry geom; CoordinateReferenceSystem crs; try { geom = (Geometry) getExpression(0).evaluate(feature, Geometry.class); } catch (Exception e) { throw new IllegalArgumentException( "Expected argument of type Geometry for argument #0"); } try { crs = getExpression(1).evaluate(feature, CoordinateReferenceSystem.class); if(crs == null) { String srs = getExpression(1).evaluate(feature, String.class); try { crs = CRS.decode(srs); } catch(FactoryException e) { crs = CRS.parseWKT(srs); } } } catch(Exception e) { throw new IllegalArgumentException( "Expected argument of type CoordinateReferenceSystem, WKT or valid EPSG code for argument #1"); } if(geom != null) { geom.setUserData(crs); } return geom; } FilterFunction_setCRS(); Object evaluate(Object feature); static FunctionName NAME; }
@Test public void setCRSObject() { Function f = ff.function("setCRS", ff.literal(g), ff.literal(DefaultGeographicCRS.WGS84)); Geometry sg = (Geometry) f.evaluate(null); assertEquals(DefaultGeographicCRS.WGS84, sg.getUserData()); }
@Test public void setCRSCode() throws Exception { Function f = ff.function("setCRS", ff.literal(g), ff.literal("EPSG:4326")); Geometry sg = (Geometry) f.evaluate(null); assertEquals(CRS.decode("EPSG:4326"), sg.getUserData()); }
@Test public void setCRSWkt() { Function f = ff.function("setCRS", ff.literal(g), ff.literal(DefaultGeographicCRS.WGS84.toWKT())); Geometry sg = (Geometry) f.evaluate(null); assertTrue(CRS.equalsIgnoreMetadata(DefaultGeographicCRS.WGS84, sg.getUserData())); }