如何在 Sparql 的 Marklogic 中创建和使用 GeoSpatial 索引
How to create and use GeoSpatial indexes in Marklogic from Sparql
我已经使用 RDF 导入将地理空间数据从 geonames.org 加载到 Marklogic 中。
使用查询控制台浏览数据时,我看到数据已加载到 xml 文档中,如下所示:
<sem:triple>
<sem:subject>http://sws.geonames.org/2736540/</sem:subject>
<sem:predicate>http://www.w3.org/2003/01/geo/wgs84_pos#lat</sem:predicate>
<sem:object datatype="http://www.w3.org/2001/XMLSchema#string">40.41476</sem:object>
</sem:triple>
<sem:triple>
<sem:subject>http://sws.geonames.org/2736540/</sem:subject>
<sem:predicate>http://www.w3.org/2003/01/geo/wgs84_pos#long</sem:predicate>
<sem:object datatype="http://www.w3.org/2001/XMLSchema#string">-8.54304</sem:object>
</sem:triple>
我能够执行 SPARQL DESCRIBE 并查看数据。这是一个例子。
@prefix geonames: <http://www.geonames.org/ontology#> .
@prefix xs: <http://www.w3.org/2001/XMLSchema#> .
@prefix p0: <http://www.w3.org/2003/01/geo/wgs84_pos#> .
<http://sws.geonames.org/2736540/> geonames:parentCountry <http://sws.geonames.org/2264397/> ;
geonames:countryCode "PT"^^xs:string ;
p0:long "-8.54304"^^xs:string ;
geonames:featureCode <http://www.geonames.org/ontology#P.PPL> ;
geonames:parentADM1 <http://sws.geonames.org/2742610/> ;
geonames:parentFeature <http://sws.geonames.org/2742610/> ;
<http://www.w3.org/2000/01/rdf-schema#isDefinedBy> "http://sws.geonames.org/2736540/about.rdf"^^xs:string ;
a geonames:Feature ;
geonames:locationMap <http://www.geonames.org/2736540/pedreira-de-vilarinho.html> ;
geonames:name "Pedreira de Vilarinho"^^xs:string ;
geonames:nearbyFeatures <http://sws.geonames.org/2736540/nearby.rdf> ;
geonames:featureClass geonames:P ;
p0:lat "40.41476"^^xs:string .
我想使用 SPARQL QUERY 作为我的查询类型来查询此数据,以便我可以利用 MarkLogic 可以创建的地理空间索引。
我在这两个方面遇到了麻烦。
- 如何为 wgs84_pos#lat 和 wgs84_pos#long 谓词正确创建地理空间索引?
- 如何从 SPARQL QUERY 访问这些索引?
我想要一个 sparql 查询,它能够在点的某个范围内找到主题。
=====================================
跟进:
按照 David Ennis 的回答(效果很好,谢谢!)我最终得到了这个示例 Xquery,它能够通过地理搜索 select 从文档中提取数据,然后在 sparql 值查询中使用这些 IRI .
示例:
xquery version "1.0-ml";
import module namespace sem = "http://marklogic.com/semantics"
at "/MarkLogic/semantics.xqy";
let $matches := cts:search(//rdf:RDF,
cts:element-pair-geospatial-query (
fn:QName("http://www.geonames.org/ontology#","Feature"),
fn:QName("http://www.w3.org/2003/01/geo/wgs84_pos#", "lat"),
fn:QName ("http://www.w3.org/2003/01/geo/wgs84_pos#","long"),
cts:circle(10, cts:point(19.8,99.8))))
let $iris := sem:iri($matches//@rdf:about)
let $bindings := (fn:map(function($n) { map:entry("featureIRI", $n) }, $iris))
let $sparql := '
PREFIX wgs: <http://www.w3.org/2003/01/geo/wgs84_pos#>
SELECT *
WHERE {
?featureIRI wgs:lat ?lat;
wgs:long ?long.
}
'
return sem:sparql-values($sparql, $bindings)
此 xquery 查询地理空间索引,找到匹配的文档,然后 selects xml 文档的 rdf:about
属性中的 IRI。
然后它映射所有这些 IRI 并创建可以在 sem:sparql-values
函数的绑定参数中传递的映射条目。
我不相信你可以通过本机 SPARQL 做你想做的事。任何 SPARQL 实现中的地理空间查询都是扩展,例如 geoSPARQL、Apache Jena 地理空间查询等
我在 MarkLogic 中建议的方法:
- 将 geonames 主题作为非托管三元组插入 MarkLogic(XML 或 JSON 文档,每个文档都有嵌入式三元组)
- 在同一文档中,以可接受的 MarkLogic 格式之一包含地理空间数据。这实质上将地理空间元数据添加到三元组中,因为它在同一个片段中。
- 为地理空间数据添加地理空间路径范围索引。
- 在带有 cts 查询限制的 MarkLogic 内部使用 SPARQL。
上面的构建基块:
- 了解unmanaged triples
- 了解Geo-spacial Region Types
- 了解Geo-spacial Indexes
- 了解Geo-spacial Queries
- 了解Semantics with cts search
最终查询的另一种方法可能是 Optic API,但我看不出它如何否定执行步骤 1-3 的需要
我已经使用 RDF 导入将地理空间数据从 geonames.org 加载到 Marklogic 中。
使用查询控制台浏览数据时,我看到数据已加载到 xml 文档中,如下所示:
<sem:triple>
<sem:subject>http://sws.geonames.org/2736540/</sem:subject>
<sem:predicate>http://www.w3.org/2003/01/geo/wgs84_pos#lat</sem:predicate>
<sem:object datatype="http://www.w3.org/2001/XMLSchema#string">40.41476</sem:object>
</sem:triple>
<sem:triple>
<sem:subject>http://sws.geonames.org/2736540/</sem:subject>
<sem:predicate>http://www.w3.org/2003/01/geo/wgs84_pos#long</sem:predicate>
<sem:object datatype="http://www.w3.org/2001/XMLSchema#string">-8.54304</sem:object>
</sem:triple>
我能够执行 SPARQL DESCRIBE 并查看数据。这是一个例子。
@prefix geonames: <http://www.geonames.org/ontology#> .
@prefix xs: <http://www.w3.org/2001/XMLSchema#> .
@prefix p0: <http://www.w3.org/2003/01/geo/wgs84_pos#> .
<http://sws.geonames.org/2736540/> geonames:parentCountry <http://sws.geonames.org/2264397/> ;
geonames:countryCode "PT"^^xs:string ;
p0:long "-8.54304"^^xs:string ;
geonames:featureCode <http://www.geonames.org/ontology#P.PPL> ;
geonames:parentADM1 <http://sws.geonames.org/2742610/> ;
geonames:parentFeature <http://sws.geonames.org/2742610/> ;
<http://www.w3.org/2000/01/rdf-schema#isDefinedBy> "http://sws.geonames.org/2736540/about.rdf"^^xs:string ;
a geonames:Feature ;
geonames:locationMap <http://www.geonames.org/2736540/pedreira-de-vilarinho.html> ;
geonames:name "Pedreira de Vilarinho"^^xs:string ;
geonames:nearbyFeatures <http://sws.geonames.org/2736540/nearby.rdf> ;
geonames:featureClass geonames:P ;
p0:lat "40.41476"^^xs:string .
我想使用 SPARQL QUERY 作为我的查询类型来查询此数据,以便我可以利用 MarkLogic 可以创建的地理空间索引。
我在这两个方面遇到了麻烦。
- 如何为 wgs84_pos#lat 和 wgs84_pos#long 谓词正确创建地理空间索引?
- 如何从 SPARQL QUERY 访问这些索引?
我想要一个 sparql 查询,它能够在点的某个范围内找到主题。
=====================================
跟进:
按照 David Ennis 的回答(效果很好,谢谢!)我最终得到了这个示例 Xquery,它能够通过地理搜索 select 从文档中提取数据,然后在 sparql 值查询中使用这些 IRI .
示例:
xquery version "1.0-ml";
import module namespace sem = "http://marklogic.com/semantics"
at "/MarkLogic/semantics.xqy";
let $matches := cts:search(//rdf:RDF,
cts:element-pair-geospatial-query (
fn:QName("http://www.geonames.org/ontology#","Feature"),
fn:QName("http://www.w3.org/2003/01/geo/wgs84_pos#", "lat"),
fn:QName ("http://www.w3.org/2003/01/geo/wgs84_pos#","long"),
cts:circle(10, cts:point(19.8,99.8))))
let $iris := sem:iri($matches//@rdf:about)
let $bindings := (fn:map(function($n) { map:entry("featureIRI", $n) }, $iris))
let $sparql := '
PREFIX wgs: <http://www.w3.org/2003/01/geo/wgs84_pos#>
SELECT *
WHERE {
?featureIRI wgs:lat ?lat;
wgs:long ?long.
}
'
return sem:sparql-values($sparql, $bindings)
此 xquery 查询地理空间索引,找到匹配的文档,然后 selects xml 文档的 rdf:about
属性中的 IRI。
然后它映射所有这些 IRI 并创建可以在 sem:sparql-values
函数的绑定参数中传递的映射条目。
我不相信你可以通过本机 SPARQL 做你想做的事。任何 SPARQL 实现中的地理空间查询都是扩展,例如 geoSPARQL、Apache Jena 地理空间查询等
我在 MarkLogic 中建议的方法:
- 将 geonames 主题作为非托管三元组插入 MarkLogic(XML 或 JSON 文档,每个文档都有嵌入式三元组)
- 在同一文档中,以可接受的 MarkLogic 格式之一包含地理空间数据。这实质上将地理空间元数据添加到三元组中,因为它在同一个片段中。
- 为地理空间数据添加地理空间路径范围索引。
- 在带有 cts 查询限制的 MarkLogic 内部使用 SPARQL。
上面的构建基块:
- 了解unmanaged triples
- 了解Geo-spacial Region Types
- 了解Geo-spacial Indexes
- 了解Geo-spacial Queries
- 了解Semantics with cts search
最终查询的另一种方法可能是 Optic API,但我看不出它如何否定执行步骤 1-3 的需要