How to calculate distance between two points using geosparql - sparql

I'm trying to calculate the distance between two points using geosparql. I have objects like the following image (same properties, different values):
And I'm executing that query in sparql:
PREFIX geos: <http://www.opengis.net/ont/geosparql#>
PREFIX geosf: <http://www.opengis.net/def/function/geosparql/>
PREFIX : <http://www.semanticweb.org/frubi/ontologies/2017/10/puntsWIFI#>
SELECT ?distance
WHERE {
?wifipoint1 :hasGeometry ?geo1 .
?geo1 geos:asWKT ?wpoint1 .
FILTER sameterm(?wifipoint1, <http://www.semanticweb.org/frubi/ontologies/2017/10/puntsWIFI#NYWifiFree103>)
?wifipoint2 :hasGeometry ?geo2 .
?geo2 geos:asWKT ?wpoint2 .
FILTER sameterm(?wifipoint2, <http://www.semanticweb.org/frubi/ontologies/2017/10/puntsWIFI#NYWifiFree105>) .
?distance geosf:distance(?wpoint1 ?wpoint2 <http://qudt.org/vocab/unit#Kilometer>)
}
Without adding the distance, I'm able to get the following result:
But at the moment I add the distance I get empty rows. Any idea?
Notes:
I need to calculate the distance between two wifipoints (NYWifiFree103 and NYWifiFree105) which have each one a point.
I'm executing that queries in stardog.
** EDIT **
I simplified the query:
PREFIX geos: <http://www.opengis.net/ont/geosparql#>
PREFIX geof: <http://www.opengis.net/def/function/geosparql/>
PREFIX : <http://www.semanticweb.org/frubi/ontologies/2017/10/puntsWIFI#>
SELECT (geof:distance(?wpoint1, ?wpoint2, <http://qudt.org/vocab/unit#Kilometer>) as ?distance)
WHERE {
?wifipoint1 :hasGeometry ?geo1 .
?geo1 geos:asWKT ?wpoint1 .
FILTER sameterm(?wifipoint1, <http://www.semanticweb.org/frubi/ontologies/2017/10/puntsWIFI#NYWifiFree103>) .
?wifipoint2 :hasGeometry ?geo2 .
?geo2 geos:asWKT ?wpoint2 .
FILTER sameterm(?wifipoint2, <http://www.semanticweb.org/frubi/ontologies/2017/10/puntsWIFI#NYWifiFree105>)
}
When I set in geof:distance two harcoded wktLiteral returns me correct distance, but using Points does not return nothing.

The geof:distance function accepts Geometries as its first two parameters. So with your simplified query, using geof:distance(?geo1, ?geo2, unit:Kilometer) should give you the result you desire. The Stardog Blog has a geospatial primer post that you may find useful.

AKSW is correct, you should use the built in geof:distance function for the calculation.

I have the same problem with GraphDB. instead of distance it returns 2 fields empty and I have already enabled geosparql
Here is my code:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX : <http://www.semanticweb.org/evangelos/ontologies/2019/2/untitled-ontology-2#>
prefix geo: <http://www.opengis.net/ont/geosparql#>
prefix geof: <http://www.opengis.net/def/function/geosparql/>
prefix unit: <http://qudt.org/vocab/unit#>
prefix sf: <http://www.opengis.net/ont/sf#>
prefix test24: <http://www.semanticweb.org/evangelos/ontologies/2019/2/untitled-ontology-2#>
prefix omgeo: <http://www.ontotext.com/owlim/geo#>
SELECT (geof:distance(?point, ?point2, <http://qudt.org/vocab/unit#Kilometer>) as ?distance)
WHERE
{
:MouseioMetaksisSoufliou :hasGPSCoordinates ?geom2.
?geom2 geo:asWKT ?point.
?geom3 geo:asWKT ?point2.
FILTER (?geom2 != ?geom3)
}

Related

Display by ObjectProperty in SPARQL

I have a problem in displaying the result using SPARQL in protege.
I have two classes Panne and Solution and an ObjectProperty hasSolution .
I want to display the Panne that has A Solution ex: GODEX hasSolution SOLGODEX.
I tried
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX panne:<http://www.semanticweb.org/rahma/ontologies/2020/pannesOnto.owl#>
SELECT ?Panne ?Solution
WHERE {
?Panne panne:hasSolution ?Solution}
Without knowing your specific ontology structure, the following will likely need some adaptations.
Currently your query selects all subjects and objects involved in a tuple using panne:hasSolution.
If you want to filter down to only those of a specific solution, you have to include that in the query:
SELECT ?Panne
WHERE {
?Panne panne:hasSolution ex:SOLGODEX .
}
If you want all pairs of panne:Panne and panne:Solution, add the respective patterns to select only members ot those classes:
SELECT ?Panne ?Solution
WHERE {
?Panne a panne:Panne .
?Solution a panne:Solution .
?Panne panne:hasSolution ?Solution .
}

How to get all organisations from DBPedia?

How can I get a list of all organisations from DBpedia?
By "organisation", I mean a entity of any type that is either a organisation or any subclass of organisation.
I found the question How to get all companies from DBPedia? but this doesn't work in the current DBpedia SPARQL web version and I wasn't able to adapt the query.
To simply get all resources that are an instance of dbo:Organization or its subclass:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dbo: <http://dbpedia.org/ontology/>
SELECT DISTINCT ?org { ?org a/rdfs:subClassOf* dbo:Organisation . }
However, as the question you linked shows, DBpedia has a cap on how many results are returned. So, as in the answer to said question, you can use a subquery with LIMIT and OFFSET to get all the results in chunks:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dbo: <http://dbpedia.org/ontology/>
SELECT ?org {
SELECT DISTINCT ?org {
?org a/rdfs:subClassOf* dbo:Organisation .
} ORDER BY ?org
}
LIMIT 10000 OFFSET 0
This would get you the first 10000 results. To get the next 10000, just add 10000 to the offset: LIMIT 10000 OFFSET 10000. Then, the next 10000 with OFFSET 20000, and so on.
You can get all organisations with a query like this, giving you English label and Wikipedia page for those resources that have it:
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX o: <http://dbpedia.org/ontology/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT DISTINCT ?orgURI ?orgName ?Wikipedia_page
WHERE {
?orgURI a o:Organisation .
OPTIONAL { ?orgURI rdfs:label ?orgName .
FILTER (lang(?orgName) = "en") }
OPTIONAL { ?orgURI ^foaf:primaryTopic ?Wikipedia_page }
}
ORDER BY ?orgName
This will currently return 350033 results for those resources that are classified as http://dbpedia.org/ontology/Organisation.
To get also the members of subclasses of http://dbpedia.org/ontology/Organisation, you can change the first pattern by turning the property into a property path going though zero or more rdfs:subClassOf:
?orgURI a/rdfs:subClassOf* o:Organisation

Common super category between two wikipedia categories using DBpedia

I have the following SPARQL query that supposed to retrieve the common categroies between Category:Computer_science category and Category:German_scientists
SELECT DISTINCT ?subject WHERE {
?subject <http://purl.org/dc/terms/subject> ?cat1, ?cat2 .
?cat1 skos:broader? / skos:broader? / skos:broader?
<http://dbpedia.org/resource/Category:Computer_science> .
?cat2 skos:broader? / skos:broader? / skos:broader?
<http://dbpedia.org/resource/Category:German_scientists>.
} LIMIT 10
I need to get the closest category to them or who has the shortest path to both categories , How Can I do that ? How to get the lenght between each common categroy between (computer_science) and (German_scientists) for example ?
Virtuoso, the endpoint that serves the DBpedia data supports a non-standard SPARQL extension to property paths: instead of just p*, p+, and p?, to mean a path of length "zero or more", "one or more", and "zero or one", respectively, Virtuoso also supports p{m,n}, meaning a path of length "at least m and at most n". By trying the following query with n=0 and increasing values of m, I started getting results with m=4:
prefix category: <http://dbpedia.org/resource/Category:>
select distinct ?super where {
?super (^skos:broader){0,4} category:Computer_science, category:German_scientists
}
super
-----------------------------------------------
http://dbpedia.org/resource/Category:Technology
http://dbpedia.org/resource/Category:Science
For the clarification:: The query is the PREFIX version of the below answer which follows current norms :
PREFIX dbpedia: <http://dbpedia.org/resource/>
PREFIX dcterms: <http://purl.org/dc/terms/>
PREFIX dbc: <http://dbpedia.org/resource/Category:>
select distinct ?super where {
?super (^skos:broader){0,4} dbc:Computer_science, dbc:German_scientists
}
SPARQL RESULTS
super
--------------------------------------------
http://dbpedia.org/resource/Category:Science_and_technology

SPARQL date range

I'm trying to obtain all records between certain dates. The date field has appears in this format: 2012-01-31. I think it is of type: <http://www.w3.org/2001/XMLSchema#date>
How would I modify the query below to extract records with date greater than 2012-01-31 please?
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX lrppi: <http://landregistry.data.gov.uk/def/ppi/>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX lrcommon: <http://landregistry.data.gov.uk/def/common/>
SELECT ?county ?postcode ?amount ?date
WHERE
{
?transx lrppi:pricePaid ?amount .
?transx lrppi:transactionDate ?date .
?transx lrppi:propertyAddress ?addr.
?addr lrcommon:postcode "PL6 8RU"^^xsd:string .
?addr lrcommon:postcode ?postcode .
# Cant get this line to work
# ?date lrppi:transactionDate ?date . FILTER ( ?date >= "1327968000"^^xsd:date )
OPTIONAL {?addr lrcommon:county ?county .}
}
ORDER BY ?postcode
If you want to play with this, you can enter your query here:
http://landregistry.data.gov.uk/landregistry/sparql/sparql.html
This is what the FILTER clause is designed for.
The Expressions and Testing Values section of the SPARQL specification covers this in detail, pretty much the first example in that section covers filtering on dates.
Edit
If you are new to SPARQL then I would recommend you read a good SPARQL tutorial like SPARQL by Example which is written by one of the specification authors. This will walk you through various parts of SPARQL and should help you get your head around the RDF data model and query language better.
In terms of dates they are represented using XML schema datatypes, for example expressing today as a date would be the following:
"2013-03-22"^^xsd:date
The linked specification covers the lexical form of various datatypes.
So for your example it would be the following:
FILTER ( ?date >= "2012-01-31"^^xsd:date )
If you are starting from a UNIX timestamp and trying to get to an xsd:date see Generating an xsd:dateTime in shell script which may provide a useful starting point.

Can't execute a specific SPARQL query on DBPedia :/

On this site, for example, take the first SPARQL query and make something very similar:
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX p: <http://dbpedia.org/property/>
SELECT *
WHERE {
?name p:name <http://dbpedia.org/resource/Olivier_Theyskens> .
}
Try to execute it: here
And I get no results. However, modify the query to the following:
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX p: <http://dbpedia.org/property/>
SELECT *
WHERE {
?name p:name ?otherthing.
}
And I get results, even though they're not the results I want.
Why doesn't the first query work -- what am I doing wrong? :/
In this case, I think it's because you're ordering your query statement backwards.
The DBpedia resource (<http://dbpedia.org/resource/Olivier_Theyskens>) is the Entity or Subject (?s), the property (p:name) is the Attribute or Predicate (?p), and the value of that property (?name) is the Value or Object (?o).
SPARQL expects all statements to be { ?s ?p ?o }, but yours seems to be written as { ?o ?p ?s }...
To sum up, if you try this query --
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX p: <http://dbpedia.org/property/>
SELECT *
WHERE
{
<http://dbpedia.org/resource/Olivier_Theyskens> p:name ?name .
}
-- you'll get the results I think you want.
The problem with your first query is that p:name links to Literal and you try to match a URI.
If you want your first query to work you have to to use the property http://dbpedia.org/ontology/artist that links to the URI and not the literal:
SELECT *
WHERE {
?s <http://dbpedia.org/ontology/artist> <http://dbpedia.org/resource/The_Velvet_Underground> .
}
Notice the different name space for the property <http://dbpedia.org/ontology/artist> this namespace contains ontology instead of property - ontology is the one used for object properties.