如何使用geof:来自GeoSPARQL的距离函数

时间:2017-04-06 09:14:48

标签: sparql openstreetmap geosparql

我创建了一个小型本体,其中有一个名为node的类。它具有与其关联的数据属性has_latitudehas_longitude。我需要执行一个给出lat,长输入的查询,我应该能够找到20米范围内的节点。所以用harvesian公式写了一个查询。它工作但查询看起来非常复杂。以下是我的查询

PREFIX geof: <http://www.opengis.net/def/function/geosparql/>
PREFIX math: <http://www.w3.org/2005/xpath-functions/math#>
PREFIX leviathan: <http://www.dotnetrdf.org/leviathan#>
PREFIX afn: <http://jena.hpl.hp.com/ARQ/function#>
PREFIX openStreetMapWithData: <http://www.semanticweb.org/yogitas/ontologies/2017/2/openStreetMapWithData.owl#>
PREFIX ont: <http://www.co-ode.org/ontologies/ont.owl#>
PREFIX has_k_recycling: <http://www.semanticweb.org/yogitas/ontologies/2017/2/openStreetMapWithData.owl#
has_k_recycling:>
PREFIX tags: <https://raw.github.com/doroam/planning-do-roam/master/Ontology/tags.owl#>
PREFIX k_addr: <https://raw.github.com/doroam/planning-do-roam/master/Ontology/tags.owl#k_addr:>
PREFIX geo: <http://www.opengis.net/ont/geosparql#>
PREFIX uom: <http://www.opengis.net/def/uom/OGC/1.0/>

select  ?lat ?lon ?node ?way ?id
where {

  ?node openStreetMapWithData:node_has_latitude ?lat .
  ?node openStreetMapWithData:node_has_longitude ?lon . 
  filter ( 
  6371 * 2 * math:asin(math:sqrt(  
  math:pow( math:sin((("48.4321094"^^xsd:double - ?lat)*math:pi()/180)/2),2)
    +
    math:cos("48.4321094"^^xsd:double * math:pi() /180) *   math:cos(?lat * math:pi() / 180) * math:pow( math:sin(("10.0219117"^^xsd:double - ?lon)*math:pi()/180),2)))  < 0.02 )

 {?node openStreetMapWithData:node_has_id ?id .
    ?waynode openStreetMapWithData:waynode_has_reference_id ?id .
    ?way openStreetMapWithData:way_has_part ?waynode .
    ?waynode rdf:type openStreetMapWithData:way_node .
    ?way rdf:type openStreetMapWithData:way.}

 }

我遇到了GeoSPARQL并发现它具有函数geo:distance(),其原因与我上面尝试的相同。我在下面的查询中使用它来查找给定输入点的每个节点的距离

PREFIX geo: <http://www.opengis.net/ont/geosparql#>
PREFIX uom: <http://www.opengis.net/def/uom/OGC/1.0/>
PREFIX geof: <http://www.opengis.net/def/function/geosparql/> 

select  ?node ?lat1 ?lon1
where {
?node1 openStreetMapWithData:node_has_latitude ?lat1 .
?node1 openStreetMapWithData:node_has_longitude ?lon1 .
BIND (geof:distance(?lat1,?lon1, "48.4332423"^^xsd:double, "10.0198943"^^xsd:double,uom:metre) as ?x)
}

它没有给我任何错误,但甚至没有任何价值。对于GeoSPARQL,我需要做哪些更改?我是否需要将我的节点类作为GeoSPARQL中的某个类的subClass?我找不到任何有关如何使用GeoSPARQL以及您自己的本体的文档。

任何帮助将不胜感激!感谢。

1 个答案:

答案 0 :(得分:0)

要计算距离,您必须在几何图形下使用asWKT实例,而不是直接使用纬度和经度。例如:

SELECT ?p ?dist
   WHERE {
     ?p rdf:type my:MyPoint .
     ?p1 rdf:type my:MyPoint .
     ?p my:Label "alpha1" .
     ?p my:Label "alpha2" . .
     ?p my:hasExactGeometry ?geom .
     ?geom my:asWKT ?wkt .
     ?p1 my:hasExactGeometry ?geom1 .
     ?geom1 my:asWKT ?wkt1 .
     BIND (geof:distance(?wkt, ?wkt1, <http://www.opengis.net/def/uom/OGC/1.0/meter>) as ?dist)
}