BufferFF extends FunctionBase3 { @Override public NodeValue exec(NodeValue v1, NodeValue v2, NodeValue v3) { try { GeometryWrapper geometry = GeometryWrapper.extract(v1); if (!v2.isDouble()) { throw new ExprEvalException("Not a Double: " + FmtUtils.stringForNode(v2.asNode())); } Node node2 = v2.asNode(); double radius = Double.parseDouble(node2.getLiteralLexicalForm()); if (!v3.isIRI()) { throw new ExprEvalException("Not a IRI: " + FmtUtils.stringForNode(v3.asNode())); } String unitsURI = v3.getNode().getURI(); GeometryWrapper buffer = geometry.buffer(radius, unitsURI); return buffer.asNodeValue(); } catch (DatatypeFormatException ex) { throw new ExprEvalException(ex.getMessage(), ex); } catch (FactoryException | MismatchedDimensionException | TransformException ex) { throw new ExprEvalException(ex.getMessage() + ": " + FmtUtils.stringForNode(v1.asNode()) + ", " + FmtUtils.stringForNode(v2.asNode()) + ", " + FmtUtils.stringForNode(v3.asNode()), ex); } }  @Override NodeValue exec(NodeValue v1, NodeValue v2, NodeValue v3);  }
@Test public void testExec_Projection_Linear() { NodeValue v1 = NodeValue.makeNode("<http: NodeValue v2 = NodeValue.makeDecimal(20); NodeValue v3 = NodeValue.makeNode(NodeFactory.createURI(Unit_URI.METRE_URL)); BufferFF instance = new BufferFF(); NodeValue expResult = NodeValue.makeNode("<http: NodeValue result = instance.exec(v1, v2, v3); assertEquals(expResult, result); }
@Test public void testExec_Projection_NonLinear() { NodeValue v1 = NodeValue.makeNode("<http: NodeValue v2 = NodeValue.makeDecimal(0.0002); NodeValue v3 = NodeValue.makeNode(NodeFactory.createURI(Unit_URI.DEGREE_URL)); BufferFF instance = new BufferFF(); NodeValue expResult = NodeValue.makeNode("<http: NodeValue result = instance.exec(v1, v2, v3); assertEquals(expResult, result); }
@Test public void testExec_Geographic_Linear() { NodeValue v1 = NodeValue.makeNode("<http: NodeValue v2 = NodeValue.makeDecimal(20); NodeValue v3 = NodeValue.makeNode(NodeFactory.createURI(Unit_URI.METRE_URL)); BufferFF instance = new BufferFF(); NodeValue expResult = NodeValue.makeNode("<http: NodeValue result = instance.exec(v1, v2, v3); assertEquals(expResult, result); }
@Test public void testExec_Geographic_Linear2() throws MismatchedDimensionException, TransformException, FactoryException { GeometryWrapper originalGeometryWrapper = GeometryWrapper.extract("<http: NodeValue v1a = originalGeometryWrapper.transform(SRS_URI.WGS84_CRS).asNodeValue(); NodeValue v2 = NodeValue.makeDecimal(20); NodeValue v3 = NodeValue.makeNode(NodeFactory.createURI(Unit_URI.METRE_URL)); BufferFF instance = new BufferFF(); NodeValue bufferedGeographicNodeValue = instance.exec(v1a, v2, v3); GeometryWrapper bufferedGeographic = GeometryWrapper.extract(bufferedGeographicNodeValue); String resultLexicalForm = bufferedGeographic.transform(SRS_URI.OSGB36_CRS).asLiteral().getLexicalForm(); NodeValue v1b = originalGeometryWrapper.asNodeValue(); NodeValue bufferedProjectedNodeValue = instance.exec(v1b, v2, v3); GeometryWrapper bufferedProjected = GeometryWrapper.extract(bufferedProjectedNodeValue); String expResultLexicalForm = bufferedProjected.asLiteral().getLexicalForm(); String[] resultsStr = resultLexicalForm.substring(resultLexicalForm.indexOf("((") + 2, resultLexicalForm.indexOf("))")).split(", "); String[] expResultsStr = expResultLexicalForm.substring(expResultLexicalForm.indexOf("((") + 2, expResultLexicalForm.indexOf("))")).split(", "); double[] results = new double[resultsStr.length * 2]; int i = 0; for (String result : resultsStr) { String[] res = result.split(" "); results[i] = Double.parseDouble(res[0]); i++; results[i] = Double.parseDouble(res[1]); i++; } double[] expResults = new double[expResultsStr.length * 2]; int j = 0; for (String result : expResultsStr) { String[] res = result.split(" "); expResults[j] = Double.parseDouble(res[0]); j++; expResults[j] = Double.parseDouble(res[1]); j++; } Assert.assertArrayEquals(expResults, results, 0.12); }
@Test public void testExec_Geographic_NonLinear() { NodeValue v1 = NodeValue.makeNode("<http: NodeValue v2 = NodeValue.makeDecimal(0.0002); NodeValue v3 = NodeValue.makeNode(NodeFactory.createURI(Unit_URI.DEGREE_URL)); BufferFF instance = new BufferFF(); NodeValue expResult = NodeValue.makeNode("<http: NodeValue result = instance.exec(v1, v2, v3); assertEquals(expResult, result); }