jenkins-bot has submitted this change and it was merged.

Change subject: Add geof:distance
......................................................................


Add geof:distance

Bug: T133357
Change-Id: I94b2392ec05b6d0a9cfd154d06a795bc06bcfc43
---
M 
blazegraph/src/main/java/org/wikidata/query/rdf/blazegraph/WikibaseContextListener.java
A 
blazegraph/src/main/java/org/wikidata/query/rdf/blazegraph/constraints/WikibaseDistanceBOp.java
M common/src/main/java/org/wikidata/query/rdf/common/uri/GeoSparql.java
M dist/src/assembly/dist.xml
4 files changed, 129 insertions(+), 1 deletion(-)

Approvals:
  Smalyshev: Looks good to me, approved
  jenkins-bot: Verified



diff --git 
a/blazegraph/src/main/java/org/wikidata/query/rdf/blazegraph/WikibaseContextListener.java
 
b/blazegraph/src/main/java/org/wikidata/query/rdf/blazegraph/WikibaseContextListener.java
index 0cd9293..1c93488 100644
--- 
a/blazegraph/src/main/java/org/wikidata/query/rdf/blazegraph/WikibaseContextListener.java
+++ 
b/blazegraph/src/main/java/org/wikidata/query/rdf/blazegraph/WikibaseContextListener.java
@@ -4,9 +4,11 @@
 
 import javax.servlet.ServletContextEvent;
 
+import org.openrdf.model.impl.URIImpl;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.wikidata.query.rdf.blazegraph.constraints.WikibaseDateBOp;
+import org.wikidata.query.rdf.blazegraph.constraints.WikibaseDistanceBOp;
 import org.wikidata.query.rdf.blazegraph.constraints.WikibaseNowBOp;
 import org.wikidata.query.rdf.blazegraph.geo.GeoService;
 import org.wikidata.query.rdf.blazegraph.label.LabelService;
@@ -84,6 +86,10 @@
                 return new WikibaseNowBOp(globals);
             }
         });
+
+        // Geospatial distance function
+        FunctionRegistry.add(new URIImpl(GeoSparql.FUNCTION_NAMESPACE + 
"distance"), getDistanceBOPFactory());
+
         addPrefixes(WikibaseUris.getURISystem());
 
         log.warn("Wikibase services initialized.");
@@ -110,6 +116,7 @@
         defaultDecls.put("skos", SKOS.NAMESPACE);
         defaultDecls.put("owl", OWL.NAMESPACE);
         defaultDecls.put("geo", GeoSparql.NAMESPACE);
+        defaultDecls.put("geof", GeoSparql.FUNCTION_NAMESPACE);
     }
 
     @Override
@@ -140,6 +147,30 @@
     }
 
     /**
+     * Get WikibaseDistanceBOp factory.
+     * @return Factory to create WikibaseDistanceBOp
+     */
+    private static Factory getDistanceBOPFactory() {
+        return new Factory() {
+            public IValueExpression<? extends IV> create(final BOpContextBase 
context, final GlobalAnnotations globals,
+                    Map<String, Object> scalarValues, final 
ValueExpressionNode... args) {
+
+                if (args.length != 2) {
+                    throw new IllegalArgumentException("wrong # of args");
+                }
+
+                final IValueExpression<? extends IV> left = 
AST2BOpUtility.toVE(context,
+                        globals, args[0]);
+
+                final IValueExpression<? extends IV> right = AST2BOpUtility
+                            .toVE(context, globals, args[1]);
+
+                return new WikibaseDistanceBOp(left, right, globals);
+            }
+        };
+    }
+
+    /**
      * Service factory that disables remote access.
      */
     private static final class DisableRemotesServiceFactory extends 
AbstractServiceFactoryBase {
diff --git 
a/blazegraph/src/main/java/org/wikidata/query/rdf/blazegraph/constraints/WikibaseDistanceBOp.java
 
b/blazegraph/src/main/java/org/wikidata/query/rdf/blazegraph/constraints/WikibaseDistanceBOp.java
new file mode 100644
index 0000000..f064b3d
--- /dev/null
+++ 
b/blazegraph/src/main/java/org/wikidata/query/rdf/blazegraph/constraints/WikibaseDistanceBOp.java
@@ -0,0 +1,86 @@
+package org.wikidata.query.rdf.blazegraph.constraints;
+
+import java.util.Map;
+
+import org.wikidata.query.rdf.common.WikibasePoint;
+
+import com.bigdata.bop.BOp;
+import com.bigdata.bop.IBindingSet;
+import com.bigdata.bop.IValueExpression;
+import com.bigdata.rdf.internal.IV;
+import com.bigdata.rdf.internal.constraints.INeedsMaterialization;
+import com.bigdata.rdf.internal.constraints.IVValueExpression;
+import com.bigdata.rdf.internal.gis.CoordinateDD;
+import com.bigdata.rdf.internal.gis.ICoordinate.UNITS;
+import com.bigdata.rdf.model.BigdataLiteral;
+import com.bigdata.rdf.sparql.ast.GlobalAnnotations;
+
+/**
+ * Implementation of geof:distance function.
+ */
+@SuppressWarnings("rawtypes")
+public class WikibaseDistanceBOp extends IVValueExpression<IV> implements 
INeedsMaterialization {
+
+    /**
+     *
+     */
+    private static final long serialVersionUID = 2909300288279424402L;
+
+    /**
+     * Required shallow copy constructor.
+     */
+    public WikibaseDistanceBOp(final BOp[] args,
+            final Map<String, Object> anns) {
+        super(args, anns);
+
+        if (args.length < 2 || args[0] == null || args[1] == null)
+            throw new IllegalArgumentException();
+    }
+
+    @SuppressWarnings("rawtypes")
+    public WikibaseDistanceBOp(//
+            final IValueExpression<? extends IV> left,
+            final IValueExpression<? extends IV> right,
+            final GlobalAnnotations globals) {
+        this(new BOp[] {left, right}, anns(globals));
+
+    }
+
+    /**
+     * Constructor required for {@link 
com.bigdata.bop.BOpUtility#deepCopy(FilterNode)}.
+     */
+    public WikibaseDistanceBOp(final WikibaseDistanceBOp op) {
+        super(op);
+    }
+
+    @Override
+    public IV get(IBindingSet bindingSet) {
+        final IV left = get(0).get(bindingSet);
+        final IV right = get(1).get(bindingSet);
+
+        final CoordinateDD leftPoint = getCoordinateFromIV(left);
+        final CoordinateDD rightPoint = getCoordinateFromIV(right);
+        // TODO: allow to supply Units
+        final double distance = leftPoint.distance(rightPoint, 
UNITS.Kilometers);
+
+        final BigdataLiteral dist = getValueFactory().createLiteral(distance);
+        return super.asIV(dist, bindingSet);
+    }
+
+    /**
+     * Get coordinate from IV value.
+     * @param iv
+     * @return Coordinate
+     */
+    protected CoordinateDD getCoordinateFromIV(IV iv) {
+        final WikibasePoint point = new 
WikibasePoint(asLiteral(iv).stringValue());
+        return new CoordinateDD(Double.parseDouble(point.getLatitude()),
+                                Double.parseDouble(point.getLongitude()));
+    }
+
+    @Override
+    public Requirement getRequirement() {
+        return Requirement.ALWAYS;
+    }
+
+}
diff --git 
a/common/src/main/java/org/wikidata/query/rdf/common/uri/GeoSparql.java 
b/common/src/main/java/org/wikidata/query/rdf/common/uri/GeoSparql.java
index 02c58f1..9e47adb 100644
--- a/common/src/main/java/org/wikidata/query/rdf/common/uri/GeoSparql.java
+++ b/common/src/main/java/org/wikidata/query/rdf/common/uri/GeoSparql.java
@@ -10,6 +10,11 @@
     public static final String NAMESPACE = 
"http://www.opengis.net/ont/geosparql#";;
 
     /**
+     * geof: namespace.
+     */
+    public static final String FUNCTION_NAMESPACE = 
"http://www.opengis.net/def/geosparql/function/";;
+
+    /**
      * WKT literal type.
      */
     public static final String WKT_LITERAL = NAMESPACE + "wktLiteral";
diff --git a/dist/src/assembly/dist.xml b/dist/src/assembly/dist.xml
index 4540fc9..e413eed 100644
--- a/dist/src/assembly/dist.xml
+++ b/dist/src/assembly/dist.xml
@@ -35,9 +35,15 @@
       <directory>${project.basedir}/../gui</directory>
       <excludes>
         <exclude>Gruntfile.js</exclude>
-        <exclude>.git</exclude>
+        <exclude>.git*</exclude>
         <exclude>.js*</exclude>
+        <exclude>.settings</exclude>
+        <exclude>.project</exclude>
+        <exclude>package.json</exclude>
+        <exclude>jsduck.json</exclude>
         <exclude>README.md</exclude>
+        <exclude>node_modules/**</exclude>
+        <exclude>wikibase/tests/**</exclude>
       </excludes>
     </fileSet>
 

-- 
To view, visit https://gerrit.wikimedia.org/r/284853
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I94b2392ec05b6d0a9cfd154d06a795bc06bcfc43
Gerrit-PatchSet: 3
Gerrit-Project: wikidata/query/rdf
Gerrit-Branch: master
Gerrit-Owner: Smalyshev <smalys...@wikimedia.org>
Gerrit-Reviewer: Smalyshev <smalys...@wikimedia.org>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to