使用本体在Virtuoso中正确分配数据类型



我已经获取了Geonames RDF转储(https://download.geonames.org/all-geonames-rdf.zip)到一个Virtuoso实例中,我一直在针对它运行查询,并取得了不同程度的成功。但是,我发现某些对象的数据类型不正确。例如,population是使用xsd:string编码的,因此尝试按population排序最终会按字典顺序对结果进行排序:

PREFIX  xsd:  <http://www.w3.org/2001/XMLSchema#>
PREFIX  gn:   <http://www.geonames.org/ontology#>
SELECT ?country ?name ?population (datatype(?population) AS ?type)
WHERE {
?country a gn:Feature .
?country gn:name ?name .
# A.PCLI is feature  code for 'independent political entity'
?country gn:featureCode <https://www.geonames.org/ontology#A.PCLI> .
?country gn:population ?population .
}
ORDER BY DESC(?population)
LIMIT 10
类型http://www.w3.org/2001/XMLSchema#stringhttp://www.w3.org/2001/XMLSchema#stringhttp://www.w3.org/2001/XMLSchema#string
国家名称人口
https://sws.geonames.org/1814991/中国1330044000
https://sws.geonames.org/1269750/印度http://www.w3.org/2001/XMLSchema#string
https://sws.geonames.org/6252001/美国310232863
https://sws.geonames.org/1643084/印度尼西亚http://www.w3.org/2001/XMLSchema#string
https://sws.geonames.org/3469034/巴西201103330

另一个选项,基于您的查询(a(--

将您的CAST放入SELECT,作为--

SELECT                              ?cityName 
?countryName 
?population
(xsd:integer(?population) AS ?pop) 
(datatype(?population) AS ?PopDataType)

然后是ORDER BY DESC (4)(用于您选择的第4个变量(。这样可以保留插入的数据,这在某些情况下可能很有价值。

以下是完整的查询--

PREFIX  xsd:  <http://www.w3.org/2001/XMLSchema#>
PREFIX  gn:   <http://www.geonames.org/ontology#>
SELECT                              ?cityName 
?countryName 
?population
(xsd:integer(?population) AS ?pop) 
(datatype(?population) AS ?PopDataType)
WHERE
{
?city  gn:parentCountry ?country ;
gn:population    ?population ;
gn:name          ?cityName .
?country gn:name ?countryName .
{
# a) 
SELECT ?country (MAX(?population) AS ?population)
# b) SELECT ?country (MAX(xsd:integer(?population)) AS ?population)     
# c) SELECT ?country (xsd:string(MAX(xsd:integer(?population))) AS ?population)
WHERE 
{
?city   a                gn:Feature ;
gn:featureClass  <https://www.geonames.org/ontology#P> ;
gn:population    ?population ;
gn:parentCountry ?country .
}
GROUP BY ?country
}
}
ORDER BY DESC (4)

--以及结果。

你也可以考虑从6.1版(06.01.3127(升级你的Virtuoso,该版本自2010年2月左右以来一直没有更新(尽管你的二进制文件显然是在2019年编译的(,从最近的代码库升级到7版的当前版本。如果您打算执行GeoSPARQL查询,这一点至关重要,因为直到大约2018年的7.1版才添加此支持!

更新以添加:

为了从Virtuoso获得完整的GeoSPARQL功能;Virtuoso GeoSPARQL支持";关于开源GitHub项目和/或Virtuoso产品手册中的Virtuoso地理空间增强部分的指导。";Virtuoso GeoSPARQL演示服务器";社区论坛上的文章也很有帮助,在GeoSPARQLDemo实例上进行测试也是如此。

最新更新