Implemented intersection function.

Project: http://git-wip-us.apache.org/repos/asf/marmotta/repo
Commit: http://git-wip-us.apache.org/repos/asf/marmotta/commit/2e14b1a8
Tree: http://git-wip-us.apache.org/repos/asf/marmotta/tree/2e14b1a8
Diff: http://git-wip-us.apache.org/repos/asf/marmotta/diff/2e14b1a8

Branch: refs/heads/MARMOTTA-584
Commit: 2e14b1a802c148e67e478b875911bec8ad75ef10
Parents: 4a68e35
Author: cuent <[email protected]>
Authored: Thu Jun 25 00:30:38 2015 -0500
Committer: cuent <[email protected]>
Committed: Thu Jun 25 00:30:38 2015 -0500

----------------------------------------------------------------------
 .../geosparql/IntersectionFunction.java         | 136 +++++++++++++++++++
 ...marmotta.kiwi.sparql.function.NativeFunction |   1 +
 ...f.query.algebra.evaluation.function.Function |   3 +-
 .../marmotta/kiwi/vocabulary/FN_GEOSPARQL.java  |   2 +
 4 files changed, 141 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/marmotta/blob/2e14b1a8/libraries/kiwi/kiwi-sparql/src/main/java/org/apache/marmotta/kiwi/sparql/function/geosparql/IntersectionFunction.java
----------------------------------------------------------------------
diff --git 
a/libraries/kiwi/kiwi-sparql/src/main/java/org/apache/marmotta/kiwi/sparql/function/geosparql/IntersectionFunction.java
 
b/libraries/kiwi/kiwi-sparql/src/main/java/org/apache/marmotta/kiwi/sparql/function/geosparql/IntersectionFunction.java
new file mode 100644
index 0000000..1f523c2
--- /dev/null
+++ 
b/libraries/kiwi/kiwi-sparql/src/main/java/org/apache/marmotta/kiwi/sparql/function/geosparql/IntersectionFunction.java
@@ -0,0 +1,136 @@
+/*
+ * 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.function.geosparql;
+
+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 intersection 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:intersection(?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 IntersectionFunction implements NativeFunction {
+
+    // auto-register for SPARQL environment
+    static {
+        
if(!FunctionRegistry.getInstance().has(FN_GEOSPARQL.INTERSECTION.toString())) {
+            FunctionRegistry.getInstance().add(new IntersectionFunction());
+        }
+    }
+
+    @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.INTERSECTION.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) {
+                if (args[1].contains(FN_GEOSPARQL.MULTIPOLYGON)|| 
args[1].contains(FN_GEOSPARQL.MULTILINESTRING) || 
args[1].contains(FN_GEOSPARQL.POINT)){  
+                    //If users insert Direct the WKT  Geometry 
+                    return String.format("ST_AsText(st_Intersection(substring( 
%s from position(' ' in %s) + 1 for char_length( %s ) ), %s ) )", args[0], 
args[0], args[0], args[1]);
+                }        
+                return String.format("ST_AsText(st_Intersection(substring( %s 
from position(' ' in %s) + 1 for char_length( %s ) ), substring( %s from 
position(' ' in %s) + 1 for char_length( %s ) ) ) )", args[0], args[0], 
args[0], args[1], args[1], args[1]);
+            } 
+        }
+        throw new UnsupportedOperationException("intersection 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.GEOMETRY;
+    }
+
+    /**
+     * 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.STRING;
+    }
+
+    /**
+     * 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/2e14b1a8/libraries/kiwi/kiwi-sparql/src/main/resources/META-INF/services/org.apache.marmotta.kiwi.sparql.function.NativeFunction
----------------------------------------------------------------------
diff --git 
a/libraries/kiwi/kiwi-sparql/src/main/resources/META-INF/services/org.apache.marmotta.kiwi.sparql.function.NativeFunction
 
b/libraries/kiwi/kiwi-sparql/src/main/resources/META-INF/services/org.apache.marmotta.kiwi.sparql.function.NativeFunction
index 2bb353b..0234578 100644
--- 
a/libraries/kiwi/kiwi-sparql/src/main/resources/META-INF/services/org.apache.marmotta.kiwi.sparql.function.NativeFunction
+++ 
b/libraries/kiwi/kiwi-sparql/src/main/resources/META-INF/services/org.apache.marmotta.kiwi.sparql.function.NativeFunction
@@ -26,6 +26,7 @@ 
org.apache.marmotta.kiwi.sparql.function.geosparql.SfOverlapsFunction
 
 org.apache.marmotta.kiwi.sparql.function.geosparql.SfBufferFunction
 org.apache.marmotta.kiwi.sparql.function.geosparql.SfConvexHullFunction
+org.apache.marmotta.kiwi.sparql.function.geosparql.IntersectionFunction
 
 org.apache.marmotta.kiwi.sparql.function.cast.NBooleanCast
 org.apache.marmotta.kiwi.sparql.function.cast.NDateTimeCast

http://git-wip-us.apache.org/repos/asf/marmotta/blob/2e14b1a8/libraries/kiwi/kiwi-sparql/src/main/resources/META-INF/services/org.openrdf.query.algebra.evaluation.function.Function
----------------------------------------------------------------------
diff --git 
a/libraries/kiwi/kiwi-sparql/src/main/resources/META-INF/services/org.openrdf.query.algebra.evaluation.function.Function
 
b/libraries/kiwi/kiwi-sparql/src/main/resources/META-INF/services/org.openrdf.query.algebra.evaluation.function.Function
index cc8bcb7..f445a12 100644
--- 
a/libraries/kiwi/kiwi-sparql/src/main/resources/META-INF/services/org.openrdf.query.algebra.evaluation.function.Function
+++ 
b/libraries/kiwi/kiwi-sparql/src/main/resources/META-INF/services/org.openrdf.query.algebra.evaluation.function.Function
@@ -13,4 +13,5 @@ 
org.apache.marmotta.kiwi.sparql.function.geosparql.SfEqualsFunction
 org.apache.marmotta.kiwi.sparql.function.geosparql.SfOverlapsFunction
 
 org.apache.marmotta.kiwi.sparql.function.geosparql.SfBufferFunction
-org.apache.marmotta.kiwi.sparql.function.geosparql.SfConvexHullFunction
\ No newline at end of file
+org.apache.marmotta.kiwi.sparql.function.geosparql.SfConvexHullFunction
+org.apache.marmotta.kiwi.sparql.function.geosparql.IntersectionFunction
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/marmotta/blob/2e14b1a8/libraries/kiwi/kiwi-triplestore/src/main/java/org/apache/marmotta/kiwi/vocabulary/FN_GEOSPARQL.java
----------------------------------------------------------------------
diff --git 
a/libraries/kiwi/kiwi-triplestore/src/main/java/org/apache/marmotta/kiwi/vocabulary/FN_GEOSPARQL.java
 
b/libraries/kiwi/kiwi-triplestore/src/main/java/org/apache/marmotta/kiwi/vocabulary/FN_GEOSPARQL.java
index 17e38c7..2e33796 100644
--- 
a/libraries/kiwi/kiwi-triplestore/src/main/java/org/apache/marmotta/kiwi/vocabulary/FN_GEOSPARQL.java
+++ 
b/libraries/kiwi/kiwi-triplestore/src/main/java/org/apache/marmotta/kiwi/vocabulary/FN_GEOSPARQL.java
@@ -61,6 +61,7 @@ public class FN_GEOSPARQL {
     //Non-Topological  FUNCTION 
     public static final URI BUFFER;
     public static final URI CONVEX_HULL;
+    public static final URI INTERSECTION;
     
     static {
         ValueFactory f = new ValueFactoryImpl();
@@ -76,6 +77,7 @@ public class FN_GEOSPARQL {
         
         BUFFER = f.createURI(NAMESPACE,"buffer");
         CONVEX_HULL = f.createURI(NAMESPACE,"convexHull");
+        INTERSECTION = f.createURI(NAMESPACE,"intersection");
         
         
         POINT = "POINT";

Reply via email to