使用公共可用数据中的SPARQL下载GeoJSON边界

时间:2016-02-26 16:51:27

标签: sparql rdf geojson linked-data opendata

我有兴趣从statistics.gov.scot下载一些边界文件,这是一个用于共享利用SPARQL查询的统计数据的官方统计存储库。

背景

Statistics.gov.scot可以为多个管理和统计地理位置提供对GeoJSON边界的访问权限,例如local authority administrative boundarieshealth boards。在我的特定情况下,我有兴趣下载与数据区相关的GeoJSON边界的数据集。 数据区是为在小范围内传播生命结果数据而开发的统计地理区域。通过statistics.gov.scot示例数据区访问时,看起来像是:

Sample data zone

可以访问地理位置和相关数据here。相应的GeoJSON数据可用here

问题

数据区有两次迭代,2004年制作,另一次最近更新。我想下载 2004 中生成的第一次迭代。根据{{​​3}}上的信息,我起草了以下查询:

PREFIX entity: <http://statistics.data.gov.uk/def/statistical-entity#>
PREFIX boundaries: <http://statistics.gov.scot/boundaries/>

SELECT ?boundary 
    WHERE {
        entity:introduced <http://reference.data.gov.uk/id/day/2004-02-01>
  }

LIMIT 1000

返回以下错误消息:

Error There was a syntax error in your query: Encountered " "}" "} "" at line 7,
column 3. Was expecting one of: <IRIref> ... <PNAME_NS> ... <PNAME_LN> ...
<BLANK_NODE_LABEL> ... <VAR1> ... <VAR2> ... "true" ... "false" ... <INTEGER> ...
<DECIMAL> ... <DOUBLE> ... <INTEGER_POSITIVE> ... <DECIMAL_POSITIVE> ...
<DOUBLE_POSITIVE> ... <INTEGER_NEGATIVE> ... <DECIMAL_NEGATIVE> ...
<DOUBLE_NEGATIVE> ... <STRING_LITERAL1> ... <STRING_LITERAL2> ...
<STRING_LITERAL_LONG1> ... <STRING_LITERAL_LONG2> ... "(" ... <NIL> ... "[" ...
<ANON> ... "+" ... "*" ... "/" ... "|" ... "?" ...

通过端点进行测试时:statistical entities

评论

理想情况下,我想开发其他查询,使我能够使用entity:前缀来寻找其他统计地理位置。这应该是可能的,因为entity:将包含有关可用地理位置的信息(名称,首字母缩写词,创建日期)。

查询:

PREFIX entity: <http://statistics.data.gov.uk/def/statistical-entity#>
PREFIX boundaries: <http://statistics.gov.scot/boundaries/>

SELECT DISTINCT ?boundary ?shape WHERE {
  ?shape entity:firstcode ?boundary
}

LIMIT 1000

让我看到一些类似于所需地理位置的内容,但我正在努力寻找GeoJSON边界。

2 个答案:

答案 0 :(得分:5)

第一个查询缺少主题。 SPARQL查询定义了一组三元模式 - 主题,谓词和对象 - 以匹配RDF图。要将WHERE子句转换为SPARQL三元模式,请尝试:

?boundary entity:introduced <http://reference.data.gov.uk/id/day/2004-02-01>

答案 1 :(得分:1)

statistics.gov.scot statistics.data.gov.uk 都不包含WKT或字符串文字的数据区边界。

但是,通过以下查询,可以轻松构建资源页面上使用的GeoJSON文件的URL:

PREFIX pref1: <http://statistics.data.gov.uk/def/statistical-entity#>
PREFIX pref2: <http://statistics.gov.scot/id/statistical-entity/>
PREFIX pref3: <http://statistics.data.gov.uk/def/boundary-change/>
PREFIX pref4: <http://reference.data.gov.uk/id/day/>
PREFIX pref5: <http://statistics.data.gov.uk/def/statistical-geography#>
PREFIX pref6: <http://statistics.gov.scot/id/statistical-geography/>
PREFIX pref7: <http://statistics.gov.scot/boundaries/>

SELECT ?zone ?name ?json {
   ?zone pref1:code pref2:S01 .
   ?zone pref3:operativedate pref4:2004-02-01
   OPTIONAL { ?zone pref5:officialname ?name }
   BIND (CONCAT(REPLACE(STR(?zone), STR(pref6:), STR(pref7:)), ".json") AS ?json)
} ORDER BY (!bound(?name)) ASC(?name)

之后,可以使用wget -i或类似的东西轻松检索GeoJSON文件。

一些解释

您应该使用<http://statistics.data.gov.uk/def/boundary-change/operativedate>而不是<http://statistics.data.gov.uk/def/statistical-entity#introduced>,后者属性是类属性:

SELECT * WHERE {
    ?S <http://statistics.data.gov.uk/def/statistical-entity#introduced> ?date .
    ?S <http://www.w3.org/2000/01/rdf-schema#label> ?label
}

第二代数据区的日期为2014-11-06

SELECT ?date (COUNT(?zone) AS ?count) WHERE {
    ?zone
        <http://statistics.data.gov.uk/def/statistical-entity#code>
            <http://statistics.gov.scot/id/statistical-entity/S01> ;
        <http://statistics.data.gov.uk/def/boundary-change/operativedate>
            ?date 
} GROUP BY ?date

类似地,如果您需要相应GeoJSON文件的URL,则查询应为:

SELECT ?zone ?name ?json {
   ?zone pref1:code pref2:S01 .
   ?zone pref3:operativedate pref4:2014-11-06 .
   ?zone pref5:officialname ?name 
   BIND (CONCAT(REPLACE(STR(?zone), STR(pref6:), STR(pref7:)), ".json") AS ?json)
} ORDER BY ASC(?name)

您不需要OPTIONAL,因为所有第二代数据区都有“官方名称”。

data.gov.uk 上的this page可能对您有意义。
对于与开放数据相关的问题,也存在opendata.stackexchange.com

<强>更新

截至2018年5月,人们可以检索数据区边界为WKT:

PREFIX pref1: <http://statistics.data.gov.uk/def/statistical-entity#>
PREFIX pref2: <http://statistics.gov.scot/id/statistical-entity/>
PREFIX pref3: <http://statistics.data.gov.uk/def/boundary-change/>
PREFIX pref4: <http://reference.data.gov.uk/id/day/>
PREFIX pref5: <http://statistics.data.gov.uk/def/statistical-geography#>
PREFIX pref6: <http://www.opengis.net/ont/geosparql#>


SELECT ?zone ?name ?geometry {
   ?zone pref1:code pref2:S01 .
   ?zone pref3:operativedate pref4:2014-11-06 .
   ?zone pref5:officialname ?name .
   ?zone pref6:hasGeometry/pref6:asWKT ?geometry .
} ORDER BY ASC(?name)
相关问题