http://git-wip-us.apache.org/repos/asf/marmotta/blob/df172223/libraries/kiwi/kiwi-geosparql/src/main/java/org/apache/marmotta/kiwi/sparql/geosparql/functions/Rcc8DCFunction.java ---------------------------------------------------------------------- diff --git a/libraries/kiwi/kiwi-geosparql/src/main/java/org/apache/marmotta/kiwi/sparql/geosparql/functions/Rcc8DCFunction.java b/libraries/kiwi/kiwi-geosparql/src/main/java/org/apache/marmotta/kiwi/sparql/geosparql/functions/Rcc8DCFunction.java new file mode 100644 index 0000000..729bca1 --- /dev/null +++ b/libraries/kiwi/kiwi-geosparql/src/main/java/org/apache/marmotta/kiwi/sparql/geosparql/functions/Rcc8DCFunction.java @@ -0,0 +1,152 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.marmotta.kiwi.sparql.geosparql.functions; + +import org.apache.marmotta.kiwi.persistence.KiWiDialect; +import org.apache.marmotta.kiwi.persistence.pgsql.PostgreSQLDialect; +import org.apache.marmotta.kiwi.sparql.builder.ValueType; +import org.apache.marmotta.kiwi.sparql.function.NativeFunction; +import org.apache.marmotta.kiwi.vocabulary.FN_GEOSPARQL; +import org.openrdf.model.Value; +import org.openrdf.model.ValueFactory; +import org.openrdf.query.algebra.evaluation.ValueExprEvaluationException; +import org.openrdf.query.algebra.evaluation.function.FunctionRegistry; + +/** + * A SPARQL function for analyze the DisconnecTion between two geometries. + * Should be implemented directly in the database, as the in-memory + * implementation is non-functional. Only support by postgres - POSTGIS + * <p/> + * The function can be called either as: + * <ul> + * <li>geof:rcc8dc(?geometryA, ?geometryB) </li> + * </ul> + * DisconnecTion is calculated with "DE-9IM Intersection Pattern": defined in + * "RCC8 Query Functions Table 7" from "DE-9IM Intersection Pattern" from OGC + * GEOSPARQL DOCUMENT ( + * https://portal.opengeospatial.org/files/?artifact_id=47664 ) + * + * @author Xavier Sumba ([email protected])) + */ +public class Rcc8DCFunction implements NativeFunction { + + // auto-register for SPARQL environment + static { + if (!FunctionRegistry.getInstance().has(FN_GEOSPARQL.RCC8_DC.toString())) { + FunctionRegistry.getInstance().add(new Rcc8DCFunction()); + } + } + + @Override + public Value evaluate(ValueFactory valueFactory, Value... args) throws ValueExprEvaluationException { + throw new UnsupportedOperationException("cannot evaluate in-memory, needs to be supported by the database"); + } + + @Override + public String getURI() { + return FN_GEOSPARQL.RCC8_DC.toString(); + } + + /** + * Return true if this function has available native support for the given + * dialect + * + * @param dialect + * @return + */ + @Override + public boolean isSupported(KiWiDialect dialect) { + return dialect instanceof PostgreSQLDialect; + } + + /** + * Return a string representing how this GeoSPARQL function is translated + * into SQL ( Postgis Function ) in the given dialect + * + * @param dialect + * @param args + * @return + */ + @Override + public String getNative(KiWiDialect dialect, String... args) { + if (dialect instanceof PostgreSQLDialect) { + if (args.length == 2) { + String geom1 = args[0]; + String geom2 = args[1]; + String SRID_default = "4326"; + /* + * The following condition is required to read WKT inserted directly into args[0] or args[1] and create a geometries with SRID + * POINT, MULTIPOINT, LINESTRING ... and MULTIPOLYGON conditions: + * example: geof:rcc8dc(?wkt, "POLYGON(( -7 43, -2 43, -2 38, -7 38, -7 43))"^^geo:wktLiteral) + * st_AsText condition: It is to use the geometry that is the result of another function geosparql. + * example: geof:rcc8dc(?wkt, geof:buffer(?wkt2, 50, units:meter)) + */ + if (args[0].contains("POINT") || args[0].contains("MULTIPOINT") || args[0].contains("LINESTRING") || args[0].contains("MULTILINESTRING") || args[0].contains("POLYGON") || args[0].contains("MULTIPOLYGON") || args[0].contains("ST_AsText")) { + geom1 = String.format("ST_GeomFromText(%s,%s)", args[0], SRID_default); + } + if (args[1].contains("POINT") || args[1].contains("MULTIPOINT") || args[1].contains("LINESTRING") || args[1].contains("MULTILINESTRING") || args[1].contains("POLYGON") || args[1].contains("MULTIPOLYGON") || args[1].contains("ST_AsText")) { + geom2 = String.format("ST_GeomFromText(%s,%s)", args[1], SRID_default); + } + return String.format("ST_Relate(%s, %s, 'FFTFFTTTT')", geom1, geom2); + } + } + throw new UnsupportedOperationException("rcc8dc function not supported by dialect " + dialect); + } + + /** + * Get the return type of the function. This is needed for SQL type casting + * inside KiWi. + * + * @return + */ + @Override + public ValueType getReturnType() { + return ValueType.BOOL; + } + + /** + * Get the argument type of the function for the arg'th argument (starting + * to count at 0). This is needed for SQL type casting inside KiWi. + * + * @param arg + * @return + */ + @Override + public ValueType getArgumentType(int arg) { + return ValueType.GEOMETRY; + } + + /** + * Return the minimum number of arguments this function requires. + * + * @return + */ + @Override + public int getMinArgs() { + return 2; + } + + /** + * Return the maximum number of arguments this function can take + * + * @return + */ + @Override + public int getMaxArgs() { + return 2; + } +}
http://git-wip-us.apache.org/repos/asf/marmotta/blob/df172223/libraries/kiwi/kiwi-geosparql/src/main/java/org/apache/marmotta/kiwi/sparql/geosparql/functions/Rcc8ECFunction.java ---------------------------------------------------------------------- diff --git a/libraries/kiwi/kiwi-geosparql/src/main/java/org/apache/marmotta/kiwi/sparql/geosparql/functions/Rcc8ECFunction.java b/libraries/kiwi/kiwi-geosparql/src/main/java/org/apache/marmotta/kiwi/sparql/geosparql/functions/Rcc8ECFunction.java new file mode 100644 index 0000000..66cae24 --- /dev/null +++ b/libraries/kiwi/kiwi-geosparql/src/main/java/org/apache/marmotta/kiwi/sparql/geosparql/functions/Rcc8ECFunction.java @@ -0,0 +1,152 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.marmotta.kiwi.sparql.geosparql.functions; + +import org.apache.marmotta.kiwi.persistence.KiWiDialect; +import org.apache.marmotta.kiwi.persistence.pgsql.PostgreSQLDialect; +import org.apache.marmotta.kiwi.sparql.builder.ValueType; +import org.apache.marmotta.kiwi.sparql.function.NativeFunction; +import org.apache.marmotta.kiwi.vocabulary.FN_GEOSPARQL; +import org.openrdf.model.Value; +import org.openrdf.model.ValueFactory; +import org.openrdf.query.algebra.evaluation.ValueExprEvaluationException; +import org.openrdf.query.algebra.evaluation.function.FunctionRegistry; + +/** + * A SPARQL function for analyze the External Connection between two geometries. + * Should be implemented directly in the database, as the in-memory + * implementation is non-functional. Only support by postgres - POSTGIS + * <p/> + * The function can be called either as: + * <ul> + * <li>geof:rcc8ec(?geometryA, ?geometryB) </li> + * </ul> + * External Connection is calculated with "DE-9IM Intersection Pattern": defined + * in "RCC8 Query Functions Table 7" from "DE-9IM Intersection Pattern" from OGC + * GEOSPARQL DOCUMENT ( + * https://portal.opengeospatial.org/files/?artifact_id=47664 ) + * + * @author Xavier Sumba ([email protected])) + */ +public class Rcc8ECFunction implements NativeFunction { + + // auto-register for SPARQL environment + static { + if (!FunctionRegistry.getInstance().has(FN_GEOSPARQL.RCC8_EC.toString())) { + FunctionRegistry.getInstance().add(new Rcc8ECFunction()); + } + } + + @Override + public Value evaluate(ValueFactory valueFactory, Value... args) throws ValueExprEvaluationException { + throw new UnsupportedOperationException("cannot evaluate in-memory, needs to be supported by the database"); + } + + @Override + public String getURI() { + return FN_GEOSPARQL.RCC8_EC.toString(); + } + + /** + * Return true if this function has available native support for the given + * dialect + * + * @param dialect + * @return + */ + @Override + public boolean isSupported(KiWiDialect dialect) { + return dialect instanceof PostgreSQLDialect; + } + + /** + * Return a string representing how this GeoSPARQL function is translated + * into SQL ( Postgis Function ) in the given dialect + * + * @param dialect + * @param args + * @return + */ + @Override + public String getNative(KiWiDialect dialect, String... args) { + if (dialect instanceof PostgreSQLDialect) { + if (args.length == 2) { + String geom1 = args[0]; + String geom2 = args[1]; + String SRID_default = "4326"; + /* + * The following condition is required to read WKT inserted directly into args[0] or args[1] and create a geometries with SRID + * POINT, MULTIPOINT, LINESTRING ... and MULTIPOLYGON conditions: + * example: geof:rcc8ec(?wkt, "POLYGON(( -7 43, -2 43, -2 38, -7 38, -7 43))"^^geo:wktLiteral) + * st_AsText condition: It is to use the geometry that is the result of another function geosparql. + * example: geof:rcc8ec(?wkt, geof:buffer(?wkt2, 50, units:meter)) + */ + if (args[0].contains("POINT") || args[0].contains("MULTIPOINT") || args[0].contains("LINESTRING") || args[0].contains("MULTILINESTRING") || args[0].contains("POLYGON") || args[0].contains("MULTIPOLYGON") || args[0].contains("ST_AsText")) { + geom1 = String.format("ST_GeomFromText(%s,%s)", args[0], SRID_default); + } + if (args[1].contains("POINT") || args[1].contains("MULTIPOINT") || args[1].contains("LINESTRING") || args[1].contains("MULTILINESTRING") || args[1].contains("POLYGON") || args[1].contains("MULTIPOLYGON") || args[1].contains("ST_AsText")) { + geom2 = String.format("ST_GeomFromText(%s,%s)", args[1], SRID_default); + } + return String.format("ST_Relate(%s, %s, 'FFTFTTTTT')", geom1, geom2); + } + } + throw new UnsupportedOperationException("rcc8ec function not supported by dialect " + dialect); + } + + /** + * Get the return type of the function. This is needed for SQL type casting + * inside KiWi. + * + * @return + */ + @Override + public ValueType getReturnType() { + return ValueType.BOOL; + } + + /** + * Get the argument type of the function for the arg'th argument (starting + * to count at 0). This is needed for SQL type casting inside KiWi. + * + * @param arg + * @return + */ + @Override + public ValueType getArgumentType(int arg) { + return ValueType.GEOMETRY; + } + + /** + * Return the minimum number of arguments this function requires. + * + * @return + */ + @Override + public int getMinArgs() { + return 2; + } + + /** + * Return the maximum number of arguments this function can take + * + * @return + */ + @Override + public int getMaxArgs() { + return 2; + } +} http://git-wip-us.apache.org/repos/asf/marmotta/blob/df172223/libraries/kiwi/kiwi-geosparql/src/main/java/org/apache/marmotta/kiwi/sparql/geosparql/functions/Rcc8EQFunction.java ---------------------------------------------------------------------- diff --git a/libraries/kiwi/kiwi-geosparql/src/main/java/org/apache/marmotta/kiwi/sparql/geosparql/functions/Rcc8EQFunction.java b/libraries/kiwi/kiwi-geosparql/src/main/java/org/apache/marmotta/kiwi/sparql/geosparql/functions/Rcc8EQFunction.java new file mode 100644 index 0000000..6d7849c --- /dev/null +++ b/libraries/kiwi/kiwi-geosparql/src/main/java/org/apache/marmotta/kiwi/sparql/geosparql/functions/Rcc8EQFunction.java @@ -0,0 +1,152 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.marmotta.kiwi.sparql.geosparql.functions; + +import org.apache.marmotta.kiwi.persistence.KiWiDialect; +import org.apache.marmotta.kiwi.persistence.pgsql.PostgreSQLDialect; +import org.apache.marmotta.kiwi.sparql.builder.ValueType; +import org.apache.marmotta.kiwi.sparql.function.NativeFunction; +import org.apache.marmotta.kiwi.vocabulary.FN_GEOSPARQL; +import org.openrdf.model.Value; +import org.openrdf.model.ValueFactory; +import org.openrdf.query.algebra.evaluation.ValueExprEvaluationException; +import org.openrdf.query.algebra.evaluation.function.FunctionRegistry; + +/** + * A SPARQL function for analyze the EQuality between two geometries. Should be + * implemented directly in the database, as the in-memory implementation is + * non-functional. Only support by postgres - POSTGIS + * <p/> + * The function can be called either as: + * <ul> + * <li>geof:rcc8eq(?geometryA, ?geometryB) </li> + * </ul> + * EQuals is calculated with "DE-9IM Intersection Pattern": defined in "RCC8 + * Query Functions Table 7" from "DE-9IM Intersection Pattern" from OGC + * GEOSPARQL DOCUMENT ( + * https://portal.opengeospatial.org/files/?artifact_id=47664 ) + * + * @author Xavier Sumba ([email protected])) + */ +public class Rcc8EQFunction implements NativeFunction { + + // auto-register for SPARQL environment + static { + if (!FunctionRegistry.getInstance().has(FN_GEOSPARQL.RCC8_EQ.toString())) { + FunctionRegistry.getInstance().add(new Rcc8EQFunction()); + } + } + + @Override + public Value evaluate(ValueFactory valueFactory, Value... args) throws ValueExprEvaluationException { + throw new UnsupportedOperationException("cannot evaluate in-memory, needs to be supported by the database"); + } + + @Override + public String getURI() { + return FN_GEOSPARQL.RCC8_EQ.toString(); + } + + /** + * Return true if this function has available native support for the given + * dialect + * + * @param dialect + * @return + */ + @Override + public boolean isSupported(KiWiDialect dialect) { + return dialect instanceof PostgreSQLDialect; + } + + /** + * Return a string representing how this GeoSPARQL function is translated + * into SQL ( Postgis Function ) in the given dialect + * + * @param dialect + * @param args + * @return + */ + @Override + public String getNative(KiWiDialect dialect, String... args) { + if (dialect instanceof PostgreSQLDialect) { + if (args.length == 2) { + String geom1 = args[0]; + String geom2 = args[1]; + String SRID_default = "4326"; + /* + * The following condition is required to read WKT inserted directly into args[0] or args[1] and create a geometries with SRID + * POINT, MULTIPOINT, LINESTRING ... and MULTIPOLYGON conditions: + * example: geof:rcc8eq(?wkt, "POLYGON(( -7 43, -2 43, -2 38, -7 38, -7 43))"^^geo:wktLiteral) + * st_AsText condition: It is to use the geometry that is the result of another function geosparql. + * example: geof:rcc8eq(?wkt, geof:buffer(?wkt2, 50, units:meter)) + */ + if (args[0].contains("POINT") || args[0].contains("MULTIPOINT") || args[0].contains("LINESTRING") || args[0].contains("MULTILINESTRING") || args[0].contains("POLYGON") || args[0].contains("MULTIPOLYGON") || args[0].contains("ST_AsText")) { + geom1 = String.format("ST_GeomFromText(%s,%s)", args[0], SRID_default); + } + if (args[1].contains("POINT") || args[1].contains("MULTIPOINT") || args[1].contains("LINESTRING") || args[1].contains("MULTILINESTRING") || args[1].contains("POLYGON") || args[1].contains("MULTIPOLYGON") || args[1].contains("ST_AsText")) { + geom2 = String.format("ST_GeomFromText(%s,%s)", args[1], SRID_default); + } + return String.format("ST_Relate(%s, %s, 'TFFFTFFFT')", geom1, geom2); + } + } + throw new UnsupportedOperationException("rcc8eq function not supported by dialect " + dialect); + } + + /** + * Get the return type of the function. This is needed for SQL type casting + * inside KiWi. + * + * @return + */ + @Override + public ValueType getReturnType() { + return ValueType.BOOL; + } + + /** + * Get the argument type of the function for the arg'th argument (starting + * to count at 0). This is needed for SQL type casting inside KiWi. + * + * @param arg + * @return + */ + @Override + public ValueType getArgumentType(int arg) { + return ValueType.GEOMETRY; + } + + /** + * Return the minimum number of arguments this function requires. + * + * @return + */ + @Override + public int getMinArgs() { + return 2; + } + + /** + * Return the maximum number of arguments this function can take + * + * @return + */ + @Override + public int getMaxArgs() { + return 2; + } +} http://git-wip-us.apache.org/repos/asf/marmotta/blob/df172223/libraries/kiwi/kiwi-geosparql/src/main/java/org/apache/marmotta/kiwi/sparql/geosparql/functions/Rcc8NTPPFunction.java ---------------------------------------------------------------------- diff --git a/libraries/kiwi/kiwi-geosparql/src/main/java/org/apache/marmotta/kiwi/sparql/geosparql/functions/Rcc8NTPPFunction.java b/libraries/kiwi/kiwi-geosparql/src/main/java/org/apache/marmotta/kiwi/sparql/geosparql/functions/Rcc8NTPPFunction.java new file mode 100644 index 0000000..b9f2e4e --- /dev/null +++ b/libraries/kiwi/kiwi-geosparql/src/main/java/org/apache/marmotta/kiwi/sparql/geosparql/functions/Rcc8NTPPFunction.java @@ -0,0 +1,152 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.marmotta.kiwi.sparql.geosparql.functions; + +import org.apache.marmotta.kiwi.persistence.KiWiDialect; +import org.apache.marmotta.kiwi.persistence.pgsql.PostgreSQLDialect; +import org.apache.marmotta.kiwi.sparql.builder.ValueType; +import org.apache.marmotta.kiwi.sparql.function.NativeFunction; +import org.apache.marmotta.kiwi.vocabulary.FN_GEOSPARQL; +import org.openrdf.model.Value; +import org.openrdf.model.ValueFactory; +import org.openrdf.query.algebra.evaluation.ValueExprEvaluationException; +import org.openrdf.query.algebra.evaluation.function.FunctionRegistry; + +/** + * A SPARQL function for analyze the Non-Tangential Proper Part between + * geometries. Should be implemented directly in the database, as the in-memory + * implementation is non-functional. Only support by postgres - POSTGIS + * <p/> + * The function can be called either as: + * <ul> + * <li>geof:rcc8ntpp(?geometryA, ?geometryB) </li> + * </ul> + * Non-Tangential Proper Part is calculated with "DE-9IM Intersection Pattern": + * defined in "RCC8 Query Functions Table 7" from "DE-9IM Intersection Pattern" + * from OGC GEOSPARQL DOCUMENT ( + * https://portal.opengeospatial.org/files/?artifact_id=47664 ) + * + * @author Xavier Sumba ([email protected])) + */ +public class Rcc8NTPPFunction implements NativeFunction { + + // auto-register for SPARQL environment + static { + if (!FunctionRegistry.getInstance().has(FN_GEOSPARQL.RCC8_NTPP.toString())) { + FunctionRegistry.getInstance().add(new Rcc8NTPPFunction()); + } + } + + @Override + public Value evaluate(ValueFactory valueFactory, Value... args) throws ValueExprEvaluationException { + throw new UnsupportedOperationException("cannot evaluate in-memory, needs to be supported by the database"); + } + + @Override + public String getURI() { + return FN_GEOSPARQL.RCC8_NTPP.toString(); + } + + /** + * Return true if this function has available native support for the given + * dialect + * + * @param dialect + * @return + */ + @Override + public boolean isSupported(KiWiDialect dialect) { + return dialect instanceof PostgreSQLDialect; + } + + /** + * Return a string representing how this GeoSPARQL function is translated + * into SQL ( Postgis Function ) in the given dialect + * + * @param dialect + * @param args + * @return + */ + @Override + public String getNative(KiWiDialect dialect, String... args) { + if (dialect instanceof PostgreSQLDialect) { + if (args.length == 2) { + String geom1 = args[0]; + String geom2 = args[1]; + String SRID_default = "4326"; + /* + * The following condition is required to read WKT inserted directly into args[0] or args[1] and create a geometries with SRID + * POINT, MULTIPOINT, LINESTRING ... and MULTIPOLYGON conditions: + * example: geof:rcc8ntpp(?wkt, "POLYGON(( -7 43, -2 43, -2 38, -7 38, -7 43))"^^geo:wktLiteral) + * st_AsText condition: It is to use the geometry that is the result of another function geosparql. + * example: geof:rcc8ntpp(?wkt, geof:buffer(?wkt2, 50, units:meter)) + */ + if (args[0].contains("POINT") || args[0].contains("MULTIPOINT") || args[0].contains("LINESTRING") || args[0].contains("MULTILINESTRING") || args[0].contains("POLYGON") || args[0].contains("MULTIPOLYGON") || args[0].contains("ST_AsText")) { + geom1 = String.format("ST_GeomFromText(%s,%s)", args[0], SRID_default); + } + if (args[1].contains("POINT") || args[1].contains("MULTIPOINT") || args[1].contains("LINESTRING") || args[1].contains("MULTILINESTRING") || args[1].contains("POLYGON") || args[1].contains("MULTIPOLYGON") || args[1].contains("ST_AsText")) { + geom2 = String.format("ST_GeomFromText(%s,%s)", args[1], SRID_default); + } + return String.format("ST_Relate(%s, %s, 'TFFTFFTTT')", geom1, geom2); + } + } + throw new UnsupportedOperationException("rcc8ntpp function not supported by dialect " + dialect); + } + + /** + * Get the return type of the function. This is needed for SQL type casting + * inside KiWi. + * + * @return + */ + @Override + public ValueType getReturnType() { + return ValueType.BOOL; + } + + /** + * Get the argument type of the function for the arg'th argument (starting + * to count at 0). This is needed for SQL type casting inside KiWi. + * + * @param arg + * @return + */ + @Override + public ValueType getArgumentType(int arg) { + return ValueType.GEOMETRY; + } + + /** + * Return the minimum number of arguments this function requires. + * + * @return + */ + @Override + public int getMinArgs() { + return 2; + } + + /** + * Return the maximum number of arguments this function can take + * + * @return + */ + @Override + public int getMaxArgs() { + return 2; + } +} http://git-wip-us.apache.org/repos/asf/marmotta/blob/df172223/libraries/kiwi/kiwi-geosparql/src/main/java/org/apache/marmotta/kiwi/sparql/geosparql/functions/Rcc8NTPPiFunction.java ---------------------------------------------------------------------- diff --git a/libraries/kiwi/kiwi-geosparql/src/main/java/org/apache/marmotta/kiwi/sparql/geosparql/functions/Rcc8NTPPiFunction.java b/libraries/kiwi/kiwi-geosparql/src/main/java/org/apache/marmotta/kiwi/sparql/geosparql/functions/Rcc8NTPPiFunction.java new file mode 100644 index 0000000..0ed690d --- /dev/null +++ b/libraries/kiwi/kiwi-geosparql/src/main/java/org/apache/marmotta/kiwi/sparql/geosparql/functions/Rcc8NTPPiFunction.java @@ -0,0 +1,152 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.marmotta.kiwi.sparql.geosparql.functions; + +import org.apache.marmotta.kiwi.persistence.KiWiDialect; +import org.apache.marmotta.kiwi.persistence.pgsql.PostgreSQLDialect; +import org.apache.marmotta.kiwi.sparql.builder.ValueType; +import org.apache.marmotta.kiwi.sparql.function.NativeFunction; +import org.apache.marmotta.kiwi.vocabulary.FN_GEOSPARQL; +import org.openrdf.model.Value; +import org.openrdf.model.ValueFactory; +import org.openrdf.query.algebra.evaluation.ValueExprEvaluationException; +import org.openrdf.query.algebra.evaluation.function.FunctionRegistry; + +/** + * A SPARQL function for analyze the Non-Tangential Proper Part Inverse between + * geometries. Should be implemented directly in the database, as the in-memory + * implementation is non-functional. Only support by postgres - POSTGIS + * <p/> + * The function can be called either as: + * <ul> + * <li>geof:rcc8ntppi(?geometryA, ?geometryB) </li> + * </ul> + * Non-Tangential Proper Part Inverse is calculated with "DE-9IM Intersection + * Pattern": defined in "RCC8 Query Functions Table 7" from "DE-9IM Intersection + * Pattern" from OGC GEOSPARQL DOCUMENT ( + * https://portal.opengeospatial.org/files/?artifact_id=47664 ) + * + * @author Xavier Sumba ([email protected])) + */ +public class Rcc8NTPPiFunction implements NativeFunction { + + // auto-register for SPARQL environment + static { + if (!FunctionRegistry.getInstance().has(FN_GEOSPARQL.RCC8_NTPPI.toString())) { + FunctionRegistry.getInstance().add(new Rcc8NTPPiFunction()); + } + } + + @Override + public Value evaluate(ValueFactory valueFactory, Value... args) throws ValueExprEvaluationException { + throw new UnsupportedOperationException("cannot evaluate in-memory, needs to be supported by the database"); + } + + @Override + public String getURI() { + return FN_GEOSPARQL.RCC8_NTPPI.toString(); + } + + /** + * Return true if this function has available native support for the given + * dialect + * + * @param dialect + * @return + */ + @Override + public boolean isSupported(KiWiDialect dialect) { + return dialect instanceof PostgreSQLDialect; + } + + /** + * Return a string representing how this GeoSPARQL function is translated + * into SQL ( Postgis Function ) in the given dialect + * + * @param dialect + * @param args + * @return + */ + @Override + public String getNative(KiWiDialect dialect, String... args) { + if (dialect instanceof PostgreSQLDialect) { + if (args.length == 2) { + String geom1 = args[0]; + String geom2 = args[1]; + String SRID_default = "4326"; + /* + * The following condition is required to read WKT inserted directly into args[0] or args[1] and create a geometries with SRID + * POINT, MULTIPOINT, LINESTRING ... and MULTIPOLYGON conditions: + * example: geof:rcc8ntppi(?wkt, "POLYGON(( -7 43, -2 43, -2 38, -7 38, -7 43))"^^geo:wktLiteral) + * st_AsText condition: It is to use the geometry that is the result of another function geosparql. + * example: geof:rcc8ntppi(?wkt, geof:buffer(?wkt2, 50, units:meter)) + */ + if (args[0].contains("POINT") || args[0].contains("MULTIPOINT") || args[0].contains("LINESTRING") || args[0].contains("MULTILINESTRING") || args[0].contains("POLYGON") || args[0].contains("MULTIPOLYGON") || args[0].contains("ST_AsText")) { + geom1 = String.format("ST_GeomFromText(%s,%s)", args[0], SRID_default); + } + if (args[1].contains("POINT") || args[1].contains("MULTIPOINT") || args[1].contains("LINESTRING") || args[1].contains("MULTILINESTRING") || args[1].contains("POLYGON") || args[1].contains("MULTIPOLYGON") || args[1].contains("ST_AsText")) { + geom2 = String.format("ST_GeomFromText(%s,%s)", args[1], SRID_default); + } + return String.format("ST_Relate(%s, %s, 'TTTFFTFFT')", geom1, geom2); + } + } + throw new UnsupportedOperationException("rcc8ntppi function not supported by dialect " + dialect); + } + + /** + * Get the return type of the function. This is needed for SQL type casting + * inside KiWi. + * + * @return + */ + @Override + public ValueType getReturnType() { + return ValueType.BOOL; + } + + /** + * Get the argument type of the function for the arg'th argument (starting + * to count at 0). This is needed for SQL type casting inside KiWi. + * + * @param arg + * @return + */ + @Override + public ValueType getArgumentType(int arg) { + return ValueType.GEOMETRY; + } + + /** + * Return the minimum number of arguments this function requires. + * + * @return + */ + @Override + public int getMinArgs() { + return 2; + } + + /** + * Return the maximum number of arguments this function can take + * + * @return + */ + @Override + public int getMaxArgs() { + return 2; + } +} http://git-wip-us.apache.org/repos/asf/marmotta/blob/df172223/libraries/kiwi/kiwi-geosparql/src/main/java/org/apache/marmotta/kiwi/sparql/geosparql/functions/Rcc8POFunction.java ---------------------------------------------------------------------- diff --git a/libraries/kiwi/kiwi-geosparql/src/main/java/org/apache/marmotta/kiwi/sparql/geosparql/functions/Rcc8POFunction.java b/libraries/kiwi/kiwi-geosparql/src/main/java/org/apache/marmotta/kiwi/sparql/geosparql/functions/Rcc8POFunction.java new file mode 100644 index 0000000..159a188 --- /dev/null +++ b/libraries/kiwi/kiwi-geosparql/src/main/java/org/apache/marmotta/kiwi/sparql/geosparql/functions/Rcc8POFunction.java @@ -0,0 +1,152 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.marmotta.kiwi.sparql.geosparql.functions; + +import org.apache.marmotta.kiwi.persistence.KiWiDialect; +import org.apache.marmotta.kiwi.persistence.pgsql.PostgreSQLDialect; +import org.apache.marmotta.kiwi.sparql.builder.ValueType; +import org.apache.marmotta.kiwi.sparql.function.NativeFunction; +import org.apache.marmotta.kiwi.vocabulary.FN_GEOSPARQL; +import org.openrdf.model.Value; +import org.openrdf.model.ValueFactory; +import org.openrdf.query.algebra.evaluation.ValueExprEvaluationException; +import org.openrdf.query.algebra.evaluation.function.FunctionRegistry; + +/** + * A SPARQL function for analyze the Partially Overlapping between geometries. + * Should be implemented directly in the database, as the in-memory + * implementation is non-functional. Only support by postgres - POSTGIS + * <p/> + * The function can be called either as: + * <ul> + * <li>geof:rcc8po(?geometryA, ?geometryB) </li> + * </ul> + * Define Partial Overlapping with "DE-9IM Intersection Pattern": defined in + * "RCC8 Query Functions Table 7" from "DE-9IM Intersection Pattern" from OGC + * GEOSPARQL DOCUMENT ( + * https://portal.opengeospatial.org/files/?artifact_id=47664 ) + * + * @author Xavier Sumba ([email protected])) + */ +public class Rcc8POFunction implements NativeFunction { + + // auto-register for SPARQL environment + static { + if (!FunctionRegistry.getInstance().has(FN_GEOSPARQL.RCC8_PO.toString())) { + FunctionRegistry.getInstance().add(new Rcc8POFunction()); + } + } + + @Override + public Value evaluate(ValueFactory valueFactory, Value... args) throws ValueExprEvaluationException { + throw new UnsupportedOperationException("cannot evaluate in-memory, needs to be supported by the database"); + } + + @Override + public String getURI() { + return FN_GEOSPARQL.RCC8_PO.toString(); + } + + /** + * Return true if this function has available native support for the given + * dialect + * + * @param dialect + * @return + */ + @Override + public boolean isSupported(KiWiDialect dialect) { + return dialect instanceof PostgreSQLDialect; + } + + /** + * Return a string representing how this GeoSPARQL function is translated + * into SQL ( Postgis Function ) in the given dialect + * + * @param dialect + * @param args + * @return + */ + @Override + public String getNative(KiWiDialect dialect, String... args) { + if (dialect instanceof PostgreSQLDialect) { + if (args.length == 2) { + String geom1 = args[0]; + String geom2 = args[1]; + String SRID_default = "4326"; + /* + * The following condition is required to read WKT inserted directly into args[0] or args[1] and create a geometries with SRID + * POINT, MULTIPOINT, LINESTRING ... and MULTIPOLYGON conditions: + * example: geof:rcc8po(?wkt, "POLYGON(( -7 43, -2 43, -2 38, -7 38, -7 43))"^^geo:wktLiteral) + * st_AsText condition: It is to use the geometry that is the result of another function geosparql. + * example: geof:rcc8po(?wkt, geof:buffer(?wkt2, 50, units:meter)) + */ + if (args[0].contains("POINT") || args[0].contains("MULTIPOINT") || args[0].contains("LINESTRING") || args[0].contains("MULTILINESTRING") || args[0].contains("POLYGON") || args[0].contains("MULTIPOLYGON") || args[0].contains("ST_AsText")) { + geom1 = String.format("ST_GeomFromText(%s,%s)", args[0], SRID_default); + } + if (args[1].contains("POINT") || args[1].contains("MULTIPOINT") || args[1].contains("LINESTRING") || args[1].contains("MULTILINESTRING") || args[1].contains("POLYGON") || args[1].contains("MULTIPOLYGON") || args[1].contains("ST_AsText")) { + geom2 = String.format("ST_GeomFromText(%s,%s)", args[1], SRID_default); + } + return String.format("ST_Relate(%s, %s, 'TTTTTTTTT')", geom1, geom2); + } + } + throw new UnsupportedOperationException("rcc8po function not supported by dialect " + dialect); + } + + /** + * Get the return type of the function. This is needed for SQL type casting + * inside KiWi. + * + * @return + */ + @Override + public ValueType getReturnType() { + return ValueType.BOOL; + } + + /** + * Get the argument type of the function for the arg'th argument (starting + * to count at 0). This is needed for SQL type casting inside KiWi. + * + * @param arg + * @return + */ + @Override + public ValueType getArgumentType(int arg) { + return ValueType.GEOMETRY; + } + + /** + * Return the minimum number of arguments this function requires. + * + * @return + */ + @Override + public int getMinArgs() { + return 2; + } + + /** + * Return the maximum number of arguments this function can take + * + * @return + */ + @Override + public int getMaxArgs() { + return 2; + } +} http://git-wip-us.apache.org/repos/asf/marmotta/blob/df172223/libraries/kiwi/kiwi-geosparql/src/main/java/org/apache/marmotta/kiwi/sparql/geosparql/functions/Rcc8TPPFunction.java ---------------------------------------------------------------------- diff --git a/libraries/kiwi/kiwi-geosparql/src/main/java/org/apache/marmotta/kiwi/sparql/geosparql/functions/Rcc8TPPFunction.java b/libraries/kiwi/kiwi-geosparql/src/main/java/org/apache/marmotta/kiwi/sparql/geosparql/functions/Rcc8TPPFunction.java new file mode 100644 index 0000000..ab02f92 --- /dev/null +++ b/libraries/kiwi/kiwi-geosparql/src/main/java/org/apache/marmotta/kiwi/sparql/geosparql/functions/Rcc8TPPFunction.java @@ -0,0 +1,152 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.marmotta.kiwi.sparql.geosparql.functions; + +import org.apache.marmotta.kiwi.persistence.KiWiDialect; +import org.apache.marmotta.kiwi.persistence.pgsql.PostgreSQLDialect; +import org.apache.marmotta.kiwi.sparql.builder.ValueType; +import org.apache.marmotta.kiwi.sparql.function.NativeFunction; +import org.apache.marmotta.kiwi.vocabulary.FN_GEOSPARQL; +import org.openrdf.model.Value; +import org.openrdf.model.ValueFactory; +import org.openrdf.query.algebra.evaluation.ValueExprEvaluationException; +import org.openrdf.query.algebra.evaluation.function.FunctionRegistry; + +/** + * A SPARQL function for analyze the Tangential Proper Part between geometries. + * Should be implemented directly in the database, as the in-memory + * implementation is non-functional. Only support by postgres - POSTGIS + * <p/> + * The function can be called either as: + * <ul> + * <li>geof:rcc8tpp(?geometryA, ?geometryB) </li> + * </ul> + * Tangential Proper Part is calculated with "DE-9IM Intersection Pattern": + * defined in "RCC8 Query Functions Table 7" from "DE-9IM Intersection Pattern" + * from OGC GEOSPARQL DOCUMENT ( + * https://portal.opengeospatial.org/files/?artifact_id=47664 ) + * + * @author Xavier Sumba ([email protected])) + */ +public class Rcc8TPPFunction implements NativeFunction { + + // auto-register for SPARQL environment + static { + if (!FunctionRegistry.getInstance().has(FN_GEOSPARQL.RCC8_TPP.toString())) { + FunctionRegistry.getInstance().add(new Rcc8TPPFunction()); + } + } + + @Override + public Value evaluate(ValueFactory valueFactory, Value... args) throws ValueExprEvaluationException { + throw new UnsupportedOperationException("cannot evaluate in-memory, needs to be supported by the database"); + } + + @Override + public String getURI() { + return FN_GEOSPARQL.RCC8_TPP.toString(); + } + + /** + * Return true if this function has available native support for the given + * dialect + * + * @param dialect + * @return + */ + @Override + public boolean isSupported(KiWiDialect dialect) { + return dialect instanceof PostgreSQLDialect; + } + + /** + * Return a string representing how this GeoSPARQL function is translated + * into SQL ( Postgis Function ) in the given dialect + * + * @param dialect + * @param args + * @return + */ + @Override + public String getNative(KiWiDialect dialect, String... args) { + if (dialect instanceof PostgreSQLDialect) { + if (args.length == 2) { + String geom1 = args[0]; + String geom2 = args[1]; + String SRID_default = "4326"; + /* + * The following condition is required to read WKT inserted directly into args[0] or args[1] and create a geometries with SRID + * POINT, MULTIPOINT, LINESTRING ... and MULTIPOLYGON conditions: + * example: geof:rcc8tpp(?wkt, "POLYGON(( -7 43, -2 43, -2 38, -7 38, -7 43))"^^geo:wktLiteral) + * st_AsText condition: It is to use the geometry that is the result of another function geosparql. + * example: geof:rcc8tpp(?wkt, geof:buffer(?wkt2, 50, units:meter)) + */ + if (args[0].contains("POINT") || args[0].contains("MULTIPOINT") || args[0].contains("LINESTRING") || args[0].contains("MULTILINESTRING") || args[0].contains("POLYGON") || args[0].contains("MULTIPOLYGON") || args[0].contains("ST_AsText")) { + geom1 = String.format("ST_GeomFromText(%s,%s)", args[0], SRID_default); + } + if (args[1].contains("POINT") || args[1].contains("MULTIPOINT") || args[1].contains("LINESTRING") || args[1].contains("MULTILINESTRING") || args[1].contains("POLYGON") || args[1].contains("MULTIPOLYGON") || args[1].contains("ST_AsText")) { + geom2 = String.format("ST_GeomFromText(%s,%s)", args[1], SRID_default); + } + return String.format("ST_Relate(%s, %s, 'TFFTTFTTT')", geom1, geom2); + } + } + throw new UnsupportedOperationException("rcc8tpp function not supported by dialect " + dialect); + } + + /** + * Get the return type of the function. This is needed for SQL type casting + * inside KiWi. + * + * @return + */ + @Override + public ValueType getReturnType() { + return ValueType.BOOL; + } + + /** + * Get the argument type of the function for the arg'th argument (starting + * to count at 0). This is needed for SQL type casting inside KiWi. + * + * @param arg + * @return + */ + @Override + public ValueType getArgumentType(int arg) { + return ValueType.GEOMETRY; + } + + /** + * Return the minimum number of arguments this function requires. + * + * @return + */ + @Override + public int getMinArgs() { + return 2; + } + + /** + * Return the maximum number of arguments this function can take + * + * @return + */ + @Override + public int getMaxArgs() { + return 2; + } +} http://git-wip-us.apache.org/repos/asf/marmotta/blob/df172223/libraries/kiwi/kiwi-geosparql/src/main/java/org/apache/marmotta/kiwi/sparql/geosparql/functions/Rcc8TPPiFunction.java ---------------------------------------------------------------------- diff --git a/libraries/kiwi/kiwi-geosparql/src/main/java/org/apache/marmotta/kiwi/sparql/geosparql/functions/Rcc8TPPiFunction.java b/libraries/kiwi/kiwi-geosparql/src/main/java/org/apache/marmotta/kiwi/sparql/geosparql/functions/Rcc8TPPiFunction.java new file mode 100644 index 0000000..5c86d53 --- /dev/null +++ b/libraries/kiwi/kiwi-geosparql/src/main/java/org/apache/marmotta/kiwi/sparql/geosparql/functions/Rcc8TPPiFunction.java @@ -0,0 +1,152 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.marmotta.kiwi.sparql.geosparql.functions; + +import org.apache.marmotta.kiwi.persistence.KiWiDialect; +import org.apache.marmotta.kiwi.persistence.pgsql.PostgreSQLDialect; +import org.apache.marmotta.kiwi.sparql.builder.ValueType; +import org.apache.marmotta.kiwi.sparql.function.NativeFunction; +import org.apache.marmotta.kiwi.vocabulary.FN_GEOSPARQL; +import org.openrdf.model.Value; +import org.openrdf.model.ValueFactory; +import org.openrdf.query.algebra.evaluation.ValueExprEvaluationException; +import org.openrdf.query.algebra.evaluation.function.FunctionRegistry; + +/** + * A SPARQL function for analyze the Tangential Proper Part Inverse between + * geometries. Should be implemented directly in the database, as the in-memory + * implementation is non-functional. Only support by postgres - POSTGIS + * <p/> + * The function can be called either as: + * <ul> + * <li>geof:rcc8tppi(?geometryA, ?geometryB) </li> + * </ul> + * Tangential Proper Part Inverse is calculated with "DE-9IM Intersection + * Pattern": defined in "RCC8 Query Functions Table 7" from "DE-9IM Intersection + * Pattern" from OGC GEOSPARQL DOCUMENT ( + * https://portal.opengeospatial.org/files/?artifact_id=47664 ) + * + * @author Xavier Sumba ([email protected])) + */ +public class Rcc8TPPiFunction implements NativeFunction { + + // auto-register for SPARQL environment + static { + if (!FunctionRegistry.getInstance().has(FN_GEOSPARQL.RCC8_TPPI.toString())) { + FunctionRegistry.getInstance().add(new Rcc8TPPiFunction()); + } + } + + @Override + public Value evaluate(ValueFactory valueFactory, Value... args) throws ValueExprEvaluationException { + throw new UnsupportedOperationException("cannot evaluate in-memory, needs to be supported by the database"); + } + + @Override + public String getURI() { + return FN_GEOSPARQL.RCC8_TPPI.toString(); + } + + /** + * Return true if this function has available native support for the given + * dialect + * + * @param dialect + * @return + */ + @Override + public boolean isSupported(KiWiDialect dialect) { + return dialect instanceof PostgreSQLDialect; + } + + /** + * Return a string representing how this GeoSPARQL function is translated + * into SQL ( Postgis Function ) in the given dialect + * + * @param dialect + * @param args + * @return + */ + @Override + public String getNative(KiWiDialect dialect, String... args) { + if (dialect instanceof PostgreSQLDialect) { + if (args.length == 2) { + String geom1 = args[0]; + String geom2 = args[1]; + String SRID_default = "4326"; + /* + * The following condition is required to read WKT inserted directly into args[0] or args[1] and create a geometries with SRID + * POINT, MULTIPOINT, LINESTRING ... and MULTIPOLYGON conditions: + * example: geof:rcc8tppi(?wkt, "POLYGON(( -7 43, -2 43, -2 38, -7 38, -7 43))"^^geo:wktLiteral) + * st_AsText condition: It is to use the geometry that is the result of another function geosparql. + * example: geof:rcc8tppi(?wkt, geof:buffer(?wkt2, 50, units:meter)) + */ + if (args[0].contains("POINT") || args[0].contains("MULTIPOINT") || args[0].contains("LINESTRING") || args[0].contains("MULTILINESTRING") || args[0].contains("POLYGON") || args[0].contains("MULTIPOLYGON") || args[0].contains("ST_AsText")) { + geom1 = String.format("ST_GeomFromText(%s,%s)", args[0], SRID_default); + } + if (args[1].contains("POINT") || args[1].contains("MULTIPOINT") || args[1].contains("LINESTRING") || args[1].contains("MULTILINESTRING") || args[1].contains("POLYGON") || args[1].contains("MULTIPOLYGON") || args[1].contains("ST_AsText")) { + geom2 = String.format("ST_GeomFromText(%s,%s)", args[1], SRID_default); + } + return String.format("ST_Relate(%s, %s, 'TTTFTTFFT')", geom1, geom2); + } + } + throw new UnsupportedOperationException("rcc8tppi function not supported by dialect " + dialect); + } + + /** + * Get the return type of the function. This is needed for SQL type casting + * inside KiWi. + * + * @return + */ + @Override + public ValueType getReturnType() { + return ValueType.BOOL; + } + + /** + * Get the argument type of the function for the arg'th argument (starting + * to count at 0). This is needed for SQL type casting inside KiWi. + * + * @param arg + * @return + */ + @Override + public ValueType getArgumentType(int arg) { + return ValueType.GEOMETRY; + } + + /** + * Return the minimum number of arguments this function requires. + * + * @return + */ + @Override + public int getMinArgs() { + return 2; + } + + /** + * Return the maximum number of arguments this function can take + * + * @return + */ + @Override + public int getMaxArgs() { + return 2; + } +} http://git-wip-us.apache.org/repos/asf/marmotta/blob/df172223/libraries/kiwi/kiwi-geosparql/src/main/java/org/apache/marmotta/kiwi/sparql/geosparql/functions/RelateFunction.java ---------------------------------------------------------------------- diff --git a/libraries/kiwi/kiwi-geosparql/src/main/java/org/apache/marmotta/kiwi/sparql/geosparql/functions/RelateFunction.java b/libraries/kiwi/kiwi-geosparql/src/main/java/org/apache/marmotta/kiwi/sparql/geosparql/functions/RelateFunction.java new file mode 100644 index 0000000..53d67d3 --- /dev/null +++ b/libraries/kiwi/kiwi-geosparql/src/main/java/org/apache/marmotta/kiwi/sparql/geosparql/functions/RelateFunction.java @@ -0,0 +1,152 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + *Â + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.marmotta.kiwi.sparql.geosparql.functions; + +import org.apache.marmotta.kiwi.persistence.KiWiDialect; +import org.apache.marmotta.kiwi.persistence.pgsql.PostgreSQLDialect; +import org.apache.marmotta.kiwi.sparql.builder.ValueType; +import org.apache.marmotta.kiwi.sparql.function.NativeFunction; +import org.apache.marmotta.kiwi.vocabulary.FN_GEOSPARQL; +import org.openrdf.model.Value; +import org.openrdf.model.ValueFactory; +import org.openrdf.query.algebra.evaluation.ValueExprEvaluationException; +import org.openrdf.query.algebra.evaluation.function.FunctionRegistry; + +/** + * A SPARQL function for doing a relate between two geometries. Should be + * implemented directly in the database, as the in-memory implementation is + * non-functional. Only support by postgres - POSTGIS + * <p/> + * The function can be called either as: + * <ul> + * <li>geof:relate (?geom1, ?geom2, ?pattern-matrix)</li> + * </ul> + * Its necesary enable postgis in your database with the next command "CREATE + * EXTENSION postgis;" Note that for performance reasons it might be preferrable + * to create a geometry index for your database. Please consult your database + * documentation on how to do this. + * + * @author Xavier Sumba ([email protected])) + */ +public class RelateFunction implements NativeFunction { + + // auto-register for SPARQL environment + static { + if (!FunctionRegistry.getInstance().has(FN_GEOSPARQL.RELATE.toString())) { + FunctionRegistry.getInstance().add(new RelateFunction()); + } + } + + @Override + public Value evaluate(ValueFactory valueFactory, Value... args) throws ValueExprEvaluationException { + throw new UnsupportedOperationException("cannot evaluate in-memory, needs to be supported by the database"); + } + + @Override + public String getURI() { + return FN_GEOSPARQL.RELATE.toString(); + } + + /** + * Return true if this function has available native support for the given + * dialect + * + * @param dialect + * @return + */ + @Override + public boolean isSupported(KiWiDialect dialect) { + return dialect instanceof PostgreSQLDialect; + } + + /** + * Return a string representing how this GeoSPARQL function is translated + * into SQL ( Postgis Function ) in the given dialect + * + * @param dialect + * @param args + * @return + */ + @Override + public String getNative(KiWiDialect dialect, String... args) { + if (dialect instanceof PostgreSQLDialect) { + if (args.length == 3) { + String geom1 = args[0]; + String geom2 = args[1]; + String SRID_default = "4326"; + /* + * The following condition is required to read WKT inserted directly into args[0] or args[1] and create a geometries with SRID + * POINT, MULTIPOINT, LINESTRING ... and MULTIPOLYGON conditions: + * example: geof:relate(?wkt, "POLYGON(( -7 43, -2 43, -2 38, -7 38, -7 43))"^^geo:wktLiteral), "F**T*****") + * st_AsText condition: It is to use the geometry that is the result of another function geosparql. + * example: geof:relate(?wkt, geof:buffer(?wkt2, 50, units:meter), "F**T*****") + */ + if (args[0].contains("POINT") || args[0].contains("MULTIPOINT") || args[0].contains("LINESTRING") || args[0].contains("MULTILINESTRING") || args[0].contains("POLYGON") || args[0].contains("MULTIPOLYGON") || args[0].contains("ST_AsText")) { + geom1 = String.format("ST_GeomFromText(%s,%s)", args[0], SRID_default); + } + if (args[1].contains("POINT") || args[1].contains("MULTIPOINT") || args[1].contains("LINESTRING") || args[1].contains("MULTILINESTRING") || args[1].contains("POLYGON") || args[1].contains("MULTIPOLYGON") || args[1].contains("ST_AsText")) { + geom2 = String.format("ST_GeomFromText(%s,%s)", args[1], SRID_default); + } + return String.format("ST_Relate(%s, %s , %s)", geom1, geom2, args[2]); + } + } + throw new UnsupportedOperationException("Relate function not supported by dialect " + dialect); + } + + /** + * Get the return type of the function. This is needed for SQL type casting + * inside KiWi. + * + * @return + */ + @Override + public ValueType getReturnType() { + return ValueType.BOOL; + } + + /** + * Get the argument type of the function for the arg'th argument (starting + * to count at 0). This is needed for SQL type casting inside KiWi. + * + * @param arg + * @return + */ + @Override + public ValueType getArgumentType(int arg) { + return ValueType.GEOMETRY; + } + + /** + * Return the minimum number of arguments this function requires. + * + * @return + */ + @Override + public int getMinArgs() { + return 3; + } + + /** + * Return the maximum number of arguments this function can take + * + * @return + */ + @Override + public int getMaxArgs() { + return 3; + } +} http://git-wip-us.apache.org/repos/asf/marmotta/blob/df172223/libraries/kiwi/kiwi-geosparql/src/main/java/org/apache/marmotta/kiwi/sparql/geosparql/functions/SfContainsFunction.java ---------------------------------------------------------------------- diff --git a/libraries/kiwi/kiwi-geosparql/src/main/java/org/apache/marmotta/kiwi/sparql/geosparql/functions/SfContainsFunction.java b/libraries/kiwi/kiwi-geosparql/src/main/java/org/apache/marmotta/kiwi/sparql/geosparql/functions/SfContainsFunction.java new file mode 100644 index 0000000..8d5bf7f --- /dev/null +++ b/libraries/kiwi/kiwi-geosparql/src/main/java/org/apache/marmotta/kiwi/sparql/geosparql/functions/SfContainsFunction.java @@ -0,0 +1,152 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.marmotta.kiwi.sparql.geosparql.functions; + +import org.apache.marmotta.kiwi.persistence.KiWiDialect; +import org.apache.marmotta.kiwi.persistence.pgsql.PostgreSQLDialect; +import org.apache.marmotta.kiwi.sparql.builder.ValueType; +import org.apache.marmotta.kiwi.sparql.function.NativeFunction; +import org.apache.marmotta.kiwi.vocabulary.FN_GEOSPARQL; +import org.openrdf.model.Value; +import org.openrdf.model.ValueFactory; +import org.openrdf.query.algebra.evaluation.ValueExprEvaluationException; +import org.openrdf.query.algebra.evaluation.function.FunctionRegistry; + +/** + * A SPARQL function for doing a geometry that contains another geometries. + * Should be implemented directly in the database, as the in-memory + * implementation is non-functional. Only support by postgres - POSTGIS + * <p/> + * The function can be called either as: + * <ul> + * <li>geof:sfContains(?geometryA, ?geometryB) </li> + * </ul> + * Its necesary enable postgis in your database with the next command "CREATE + * EXTENSION postgis;" Note that for performance reasons it might be preferrable + * to create a geometry index for your database. Please consult your database + * documentation on how to do this. + * + * @author Xavier Sumba ([email protected])) + */ +public class SfContainsFunction implements NativeFunction { + + // auto-register for SPARQL environment + static { + if (!FunctionRegistry.getInstance().has(FN_GEOSPARQL.SF_CONTAINS.toString())) { + FunctionRegistry.getInstance().add(new SfContainsFunction()); + } + } + + @Override + public Value evaluate(ValueFactory valueFactory, Value... args) throws ValueExprEvaluationException { + throw new UnsupportedOperationException("cannot evaluate in-memory, needs to be supported by the database"); + } + + @Override + public String getURI() { + return FN_GEOSPARQL.SF_CONTAINS.toString(); + } + + /** + * Return true if this function has available native support for the given + * dialect + * + * @param dialect + * @return + */ + @Override + public boolean isSupported(KiWiDialect dialect) { + return dialect instanceof PostgreSQLDialect; + } + + /** + * Return a string representing how this GeoSPARQL function is translated + * into SQL ( Postgis Function ) in the given dialect + * + * @param dialect + * @param args + * @return + */ + @Override + public String getNative(KiWiDialect dialect, String... args) { + if (dialect instanceof PostgreSQLDialect) { + if (args.length == 2) { + String geom1 = args[0]; + String geom2 = args[1]; + String SRID_default = "4326"; + /* + * The following condition is required to read WKT inserted directly into args[0] or args[1] and create a geometries with SRID + * POINT, MULTIPOINT, LINESTRING ... and MULTIPOLYGON conditions: + * example: geof:sfContains(?wkt, "POLYGON(( -7 43, -2 43, -2 38, -7 38, -7 43))"^^geo:wktLiteral)) + * st_AsText condition: It is to use the geometry that is the result of another function geosparql. + * example: geof:sfContains(?wkt, geof:buffer(?wkt2, 50, units:meter)) + */ + if (args[0].contains("POINT") || args[0].contains("MULTIPOINT") || args[0].contains("LINESTRING") || args[0].contains("MULTILINESTRING") || args[0].contains("POLYGON") || args[0].contains("MULTIPOLYGON") || args[0].contains("ST_AsText")) { + geom1 = String.format("ST_GeomFromText(%s,%s)", args[0], SRID_default); + } + if (args[1].contains("POINT") || args[1].contains("MULTIPOINT") || args[1].contains("LINESTRING") || args[1].contains("MULTILINESTRING") || args[1].contains("POLYGON") || args[1].contains("MULTIPOLYGON") || args[1].contains("ST_AsText")) { + geom2 = String.format("ST_GeomFromText(%s,%s)", args[1], SRID_default); + } + return String.format("st_Contains(%s , %s )", geom1, geom2); + } + } + throw new UnsupportedOperationException("Contains function not supported by dialect " + dialect); + } + + /** + * Get the return type of the function. This is needed for SQL type casting + * inside KiWi. + * + * @return + */ + @Override + public ValueType getReturnType() { + return ValueType.BOOL; + } + + /** + * Get the argument type of the function for the arg'th argument (starting + * to count at 0). This is needed for SQL type casting inside KiWi. + * + * @param arg + * @return + */ + @Override + public ValueType getArgumentType(int arg) { + return ValueType.GEOMETRY; + } + + /** + * Return the minimum number of arguments this function requires. + * + * @return + */ + @Override + public int getMinArgs() { + return 2; + } + + /** + * Return the maximum number of arguments this function can take + * + * @return + */ + @Override + public int getMaxArgs() { + return 2; + } +} http://git-wip-us.apache.org/repos/asf/marmotta/blob/df172223/libraries/kiwi/kiwi-geosparql/src/main/java/org/apache/marmotta/kiwi/sparql/geosparql/functions/SfCrossesFunction.java ---------------------------------------------------------------------- diff --git a/libraries/kiwi/kiwi-geosparql/src/main/java/org/apache/marmotta/kiwi/sparql/geosparql/functions/SfCrossesFunction.java b/libraries/kiwi/kiwi-geosparql/src/main/java/org/apache/marmotta/kiwi/sparql/geosparql/functions/SfCrossesFunction.java new file mode 100644 index 0000000..054e3a3 --- /dev/null +++ b/libraries/kiwi/kiwi-geosparql/src/main/java/org/apache/marmotta/kiwi/sparql/geosparql/functions/SfCrossesFunction.java @@ -0,0 +1,152 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.marmotta.kiwi.sparql.geosparql.functions; + +import org.apache.marmotta.kiwi.persistence.KiWiDialect; +import org.apache.marmotta.kiwi.persistence.pgsql.PostgreSQLDialect; +import org.apache.marmotta.kiwi.sparql.builder.ValueType; +import org.apache.marmotta.kiwi.sparql.function.NativeFunction; +import org.apache.marmotta.kiwi.vocabulary.FN_GEOSPARQL; +import org.openrdf.model.Value; +import org.openrdf.model.ValueFactory; +import org.openrdf.query.algebra.evaluation.ValueExprEvaluationException; +import org.openrdf.query.algebra.evaluation.function.FunctionRegistry; + +/** + * A SPARQL function for doing a crosses between two geometries. Should be + * implemented directly in the database, as the in-memory implementation is + * non-functional. Only support by postgres - POSTGIS + * <p/> + * The function can be called either as: + * <ul> + * <li>geof:sfCrosses(?geometryA, ?geometryB) </li> + * </ul> + * Its necesary enable postgis in your database with the next command "CREATE + * EXTENSION postgis;" Note that for performance reasons it might be preferrable + * to create a geometry index for your database. Please consult your database + * documentation on how to do this. + * + * @author Xavier Sumba ([email protected])) + */ +public class SfCrossesFunction implements NativeFunction { + + // auto-register for SPARQL environment + static { + if (!FunctionRegistry.getInstance().has(FN_GEOSPARQL.SF_CROSSES.toString())) { + FunctionRegistry.getInstance().add(new SfCrossesFunction()); + } + } + + @Override + public Value evaluate(ValueFactory valueFactory, Value... args) throws ValueExprEvaluationException { + throw new UnsupportedOperationException("cannot evaluate in-memory, needs to be supported by the database"); + } + + @Override + public String getURI() { + return FN_GEOSPARQL.SF_CROSSES.toString(); + } + + /** + * Return true if this function has available native support for the given + * dialect + * + * @param dialect + * @return + */ + @Override + public boolean isSupported(KiWiDialect dialect) { + return dialect instanceof PostgreSQLDialect; + } + + /** + * Return a string representing how this GeoSPARQL function is translated + * into SQL ( Postgis Function ) in the given dialect + * + * @param dialect + * @param args + * @return + */ + @Override + public String getNative(KiWiDialect dialect, String... args) { + if (dialect instanceof PostgreSQLDialect) { + if (args.length == 2) { + String geom1 = args[0]; + String geom2 = args[1]; + String SRID_default = "4326"; + /* + * The following condition is required to read WKT inserted directly into args[0] or args[1] and create a geometries with SRID + * POINT, MULTIPOINT, LINESTRING ... and MULTIPOLYGON conditions: + * example: geof:sfCrosses(?wkt, "POLYGON(( -7 43, -2 43, -2 38, -7 38, -7 43))"^^geo:wktLiteral)) + * st_AsText condition: It is to use the geometry that is the result of another function geosparql. + * example: geof:sfCrosses(?wkt, geof:buffer(?wkt2, 50, units:meter)) + */ + if (args[0].contains("POINT") || args[0].contains("MULTIPOINT") || args[0].contains("LINESTRING") || args[0].contains("MULTILINESTRING") || args[0].contains("POLYGON") || args[0].contains("MULTIPOLYGON") || args[0].contains("ST_AsText")) { + geom1 = String.format("ST_GeomFromText(%s,%s)", args[0], SRID_default); + } + if (args[1].contains("POINT") || args[1].contains("MULTIPOINT") || args[1].contains("LINESTRING") || args[1].contains("MULTILINESTRING") || args[1].contains("POLYGON") || args[1].contains("MULTIPOLYGON") || args[1].contains("ST_AsText")) { + geom2 = String.format("ST_GeomFromText(%s,%s)", args[1], SRID_default); + } + return String.format("st_Crosses(%s , %s )", geom1, geom2); + } + } + throw new UnsupportedOperationException("Crosses function not supported by dialect " + dialect); + } + + /** + * Get the return type of the function. This is needed for SQL type casting + * inside KiWi. + * + * @return + */ + @Override + public ValueType getReturnType() { + return ValueType.BOOL; + } + + /** + * Get the argument type of the function for the arg'th argument (starting + * to count at 0). This is needed for SQL type casting inside KiWi. + * + * @param arg + * @return + */ + @Override + public ValueType getArgumentType(int arg) { + return ValueType.GEOMETRY; + } + + /** + * Return the minimum number of arguments this function requires. + * + * @return + */ + @Override + public int getMinArgs() { + return 2; + } + + /** + * Return the maximum number of arguments this function can take + * + * @return + */ + @Override + public int getMaxArgs() { + return 2; + } +} http://git-wip-us.apache.org/repos/asf/marmotta/blob/df172223/libraries/kiwi/kiwi-geosparql/src/main/java/org/apache/marmotta/kiwi/sparql/geosparql/functions/SfDisjointFunction.java ---------------------------------------------------------------------- diff --git a/libraries/kiwi/kiwi-geosparql/src/main/java/org/apache/marmotta/kiwi/sparql/geosparql/functions/SfDisjointFunction.java b/libraries/kiwi/kiwi-geosparql/src/main/java/org/apache/marmotta/kiwi/sparql/geosparql/functions/SfDisjointFunction.java new file mode 100644 index 0000000..b460ea0 --- /dev/null +++ b/libraries/kiwi/kiwi-geosparql/src/main/java/org/apache/marmotta/kiwi/sparql/geosparql/functions/SfDisjointFunction.java @@ -0,0 +1,152 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.marmotta.kiwi.sparql.geosparql.functions; + +import org.apache.marmotta.kiwi.persistence.KiWiDialect; +import org.apache.marmotta.kiwi.persistence.pgsql.PostgreSQLDialect; +import org.apache.marmotta.kiwi.sparql.builder.ValueType; +import org.apache.marmotta.kiwi.sparql.function.NativeFunction; +import org.apache.marmotta.kiwi.vocabulary.FN_GEOSPARQL; +import org.openrdf.model.Value; +import org.openrdf.model.ValueFactory; +import org.openrdf.query.algebra.evaluation.ValueExprEvaluationException; +import org.openrdf.query.algebra.evaluation.function.FunctionRegistry; + +/** + * A SPARQL function for doing a Disjoint between two geometries. Should be + * implemented directly in the database, as the in-memory implementation is + * non-functional. Only support by postgres - POSTGIS + * <p/> + * The function can be called either as: + * <ul> + * <li>geof:sfDisjoint(?geometryA, ?geometryB) </li> + * </ul> + * Its necesary enable postgis in your database with the next command "CREATE + * EXTENSION postgis;" Note that for performance reasons it might be preferrable + * to create a geometry index for your database. Please consult your database + * documentation on how to do this. + * + * @author Xavier Sumba ([email protected])) + */ +public class SfDisjointFunction implements NativeFunction { + + // auto-register for SPARQL environment + static { + if (!FunctionRegistry.getInstance().has(FN_GEOSPARQL.SF_DISJOINT.toString())) { + FunctionRegistry.getInstance().add(new SfDisjointFunction()); + } + } + + @Override + public Value evaluate(ValueFactory valueFactory, Value... args) throws ValueExprEvaluationException { + throw new UnsupportedOperationException("cannot evaluate in-memory, needs to be supported by the database"); + } + + @Override + public String getURI() { + return FN_GEOSPARQL.SF_DISJOINT.toString(); + } + + /** + * Return true if this function has available native support for the given + * dialect + * + * @param dialect + * @return + */ + @Override + public boolean isSupported(KiWiDialect dialect) { + return dialect instanceof PostgreSQLDialect; + } + + /** + * Return a string representing how this GeoSPARQL function is translated + * into SQL ( Postgis Function ) in the given dialect + * + * @param dialect + * @param args + * @return + */ + @Override + public String getNative(KiWiDialect dialect, String... args) { + if (dialect instanceof PostgreSQLDialect) { + if (args.length == 2) { + String geom1 = args[0]; + String geom2 = args[1]; + String SRID_default = "4326"; + /* + * The following condition is required to read WKT inserted directly into args[0] or args[1] and create a geometries with SRID + * POINT, MULTIPOINT, LINESTRING ... and MULTIPOLYGON conditions: + * example: geof:sfDisjoint(?wkt, "POLYGON(( -7 43, -2 43, -2 38, -7 38, -7 43))"^^geo:wktLiteral)) + * st_AsText condition: It is to use the geometry that is the result of another function geosparql. + * example: geof:sfDisjoint(?wkt, geof:buffer(?wkt2, 50, units:meter)) + */ + if (args[0].contains("POINT") || args[0].contains("MULTIPOINT") || args[0].contains("LINESTRING") || args[0].contains("MULTILINESTRING") || args[0].contains("POLYGON") || args[0].contains("MULTIPOLYGON") || args[0].contains("ST_AsText")) { + geom1 = String.format("ST_GeomFromText(%s,%s)", args[0], SRID_default); + } + if (args[1].contains("POINT") || args[1].contains("MULTIPOINT") || args[1].contains("LINESTRING") || args[1].contains("MULTILINESTRING") || args[1].contains("POLYGON") || args[1].contains("MULTIPOLYGON") || args[1].contains("ST_AsText")) { + geom2 = String.format("ST_GeomFromText(%s,%s)", args[1], SRID_default); + } + return String.format("st_Disjoint(%s , %s )", geom1, geom2); + } + } + throw new UnsupportedOperationException("Disjoint function not supported by dialect " + dialect); + } + + /** + * Get the return type of the function. This is needed for SQL type casting + * inside KiWi. + * + * @return + */ + @Override + public ValueType getReturnType() { + return ValueType.BOOL; + } + + /** + * Get the argument type of the function for the arg'th argument (starting + * to count at 0). This is needed for SQL type casting inside KiWi. + * + * @param arg + * @return + */ + @Override + public ValueType getArgumentType(int arg) { + return ValueType.GEOMETRY; + } + + /** + * Return the minimum number of arguments this function requires. + * + * @return + */ + @Override + public int getMinArgs() { + return 2; + } + + /** + * Return the maximum number of arguments this function can take + * + * @return + */ + @Override + public int getMaxArgs() { + return 2; + } +} http://git-wip-us.apache.org/repos/asf/marmotta/blob/df172223/libraries/kiwi/kiwi-geosparql/src/main/java/org/apache/marmotta/kiwi/sparql/geosparql/functions/SfEqualsFunction.java ---------------------------------------------------------------------- diff --git a/libraries/kiwi/kiwi-geosparql/src/main/java/org/apache/marmotta/kiwi/sparql/geosparql/functions/SfEqualsFunction.java b/libraries/kiwi/kiwi-geosparql/src/main/java/org/apache/marmotta/kiwi/sparql/geosparql/functions/SfEqualsFunction.java new file mode 100644 index 0000000..1dff32e --- /dev/null +++ b/libraries/kiwi/kiwi-geosparql/src/main/java/org/apache/marmotta/kiwi/sparql/geosparql/functions/SfEqualsFunction.java @@ -0,0 +1,152 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.marmotta.kiwi.sparql.geosparql.functions; + +import org.apache.marmotta.kiwi.persistence.KiWiDialect; +import org.apache.marmotta.kiwi.persistence.pgsql.PostgreSQLDialect; +import org.apache.marmotta.kiwi.sparql.builder.ValueType; +import org.apache.marmotta.kiwi.sparql.function.NativeFunction; +import org.apache.marmotta.kiwi.vocabulary.FN_GEOSPARQL; +import org.openrdf.model.Value; +import org.openrdf.model.ValueFactory; +import org.openrdf.query.algebra.evaluation.ValueExprEvaluationException; +import org.openrdf.query.algebra.evaluation.function.FunctionRegistry; + +/** + * A SPARQL function for doing a equals between two geometries. Should be + * implemented directly in the database, as the in-memory implementation is + * non-functional. Only support by postgres - POSTGIS + * <p/> + * The function can be called either as: + * <ul> + * <li>geof:sfEquals(?geometryA, ?geometryB) </li> + * </ul> + * Its necesary enable postgis in your database with the next command "CREATE + * EXTENSION postgis;" Note that for performance reasons it might be preferrable + * to create a geometry index for your database. Please consult your database + * documentation on how to do this. + * + * @author Xavier Sumba ([email protected])) + */ +public class SfEqualsFunction implements NativeFunction { + + // auto-register for SPARQL environment + static { + if (!FunctionRegistry.getInstance().has(FN_GEOSPARQL.SF_EQUALS.toString())) { + FunctionRegistry.getInstance().add(new SfEqualsFunction()); + } + } + + @Override + public Value evaluate(ValueFactory valueFactory, Value... args) throws ValueExprEvaluationException { + throw new UnsupportedOperationException("cannot evaluate in-memory, needs to be supported by the database"); + } + + @Override + public String getURI() { + return FN_GEOSPARQL.SF_EQUALS.toString(); + } + + /** + * Return true if this function has available native support for the given + * dialect + * + * @param dialect + * @return + */ + @Override + public boolean isSupported(KiWiDialect dialect) { + return dialect instanceof PostgreSQLDialect; + } + + /** + * Return a string representing how this GeoSPARQL function is translated + * into SQL ( Postgis Function ) in the given dialect + * + * @param dialect + * @param args + * @return + */ + @Override + public String getNative(KiWiDialect dialect, String... args) { + if (dialect instanceof PostgreSQLDialect) { + if (args.length == 2) { + String geom1 = args[0]; + String geom2 = args[1]; + String SRID_default = "4326"; + /* + * The following condition is required to read WKT inserted directly into args[0] or args[1] and create a geometries with SRID + * POINT, MULTIPOINT, LINESTRING ... and MULTIPOLYGON conditions: + * example: geof:sfEquals(?wkt, "POLYGON(( -7 43, -2 43, -2 38, -7 38, -7 43))"^^geo:wktLiteral)) + * st_AsText condition: It is to use the geometry that is the result of another function geosparql. + * example: geof:sfEquals(?wkt, geof:buffer(?wkt2, 50, units:meter)) + */ + if (args[0].contains("POINT") || args[0].contains("MULTIPOINT") || args[0].contains("LINESTRING") || args[0].contains("MULTILINESTRING") || args[0].contains("POLYGON") || args[0].contains("MULTIPOLYGON") || args[0].contains("ST_AsText")) { + geom1 = String.format("ST_GeomFromText(%s,%s)", args[0], SRID_default); + } + if (args[1].contains("POINT") || args[1].contains("MULTIPOINT") || args[1].contains("LINESTRING") || args[1].contains("MULTILINESTRING") || args[1].contains("POLYGON") || args[1].contains("MULTIPOLYGON") || args[1].contains("ST_AsText")) { + geom2 = String.format("ST_GeomFromText(%s,%s)", args[1], SRID_default); + } + return String.format("st_Equals(%s , %s )", geom1, geom2); + } + } + throw new UnsupportedOperationException("Equals function not supported by dialect " + dialect); + } + + /** + * Get the return type of the function. This is needed for SQL type casting + * inside KiWi. + * + * @return + */ + @Override + public ValueType getReturnType() { + return ValueType.BOOL; + } + + /** + * Get the argument type of the function for the arg'th argument (starting + * to count at 0). This is needed for SQL type casting inside KiWi. + * + * @param arg + * @return + */ + @Override + public ValueType getArgumentType(int arg) { + return ValueType.GEOMETRY; + } + + /** + * Return the minimum number of arguments this function requires. + * + * @return + */ + @Override + public int getMinArgs() { + return 2; + } + + /** + * Return the maximum number of arguments this function can take + * + * @return + */ + @Override + public int getMaxArgs() { + return 2; + } +}
