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

12

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

背景

Statistics.gov.scot提供访问各种行政和统计地理位置的GeoJSON边界数据,例如地方政府行政边界卫生委员会 。在我的特定情况下,我有兴趣下载与数据区域相关的GeoJSON边界数据集。 数据区域是为了传播小区域生活结果数据而开发的统计地理位置。通过statistics.gov.scot访问样本数据区域如下:

Sample data zone

可以在此处访问地理位置及相关数据此处。相应的GeoJSON数据位于此处

问题

数据区域有两个版本,一个是在2004年制作的,另一个是最近更新的。我想要下载制作于2004年的第一版。根据统计实体上的信息,我起草了以下查询:

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

会返回以下错误信息:

which returns the following error message:
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边界。


似乎 statistics.gov.scotstatistics.data.gov.uk 都不包含数据区域边界的 wkt- 或字符串文字。但是,可以使用以下查询轻松构建 geojson 文件的 URI。 - Stanislav Kralin
@StanislavKralin 为什么不让它回答呢,这似乎是一个好的方法。 - Konrad
2个回答

5

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

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

感谢您对我的问题表示关注。运行此查询后,我得到了一个包含一行数据的表格,其中的值为:http://statistics.gov.scot/id/statistical-entity/S01 - Konrad
1
好的,如果您想查看有关该实体的信息,那么请添加三元模式 {?boundary ?p ?o},这将为您提供所有属性/对象对,您可以选择要查询哪些属性。 - scotthenninger
请返回已翻译的文本:http://statistics.data.gov.uk/def/boundary-change/operativedate,而不是http://statistics.data.gov.uk/def/statistical-entity#introduced。 - Stanislav Kralin

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,因为所有第二代数据区都有“官方名称”。

也许您会对数据英国政府网站上的这个页面感兴趣。
还有一个专门解答与开放数据相关问题的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)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接