使用 SPARQL 从公开数据下载 GeoJSON 边界

Downloading GeoJSON boundaries using SPARQL from publicly available data

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

背景

Statistics.gov.scot 提供对多个行政和统计地理区域的 GeoJSON 边界的访问,例如 local当局行政边界 健康委员会 。在我的特殊情况下,我有兴趣下载具有与 数据区 有关的 GeoJSON 边界的数据集。 数据区 是为了在小范围内传播生活成果数据而开发的统计地理区域。通过 statistics.gov.scot 示例数据区访问时如下所示:

可以访问地理和相关数据here. The corresponding GeoJSON data is available here

问题

数据区有两个迭代版本,一个是 2004 年制作的,另一个是最近更新的。我想下载 2004 中制作的第一版。根据 statistical entities 上的信息,我起草了以下查询:

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

其中returns以下错误信息:

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> ... "+" ... "*" ... "/" ... "|" ... "?" ...

通过端点测试时:http://statistics.gov.scot/sparql

评论

理想情况下,我想开发其他查询,使我能够使用 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 边界。

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

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

statistics.gov.scotstatistics.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>,后者属性更像是一个class 属性:

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,因为所有二代数据区都有"official names".


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)