我正在使用包含地理参考的东西的RDF数据,例如具有指定位置的POI:

@prefix ogc:   <http://www.opengis.net/ont/geosparql#> .

:poi  ogc:hasGeometry  :geo
:geo  ogc:asWKT        "POINT(48.5 11.7)"^^ogc:wktLiteral .

因此,有一种POI位于(48.5,11.7)。
我可以使用GeoSPARQL -queries处理这些位置,但是现在我想分别提取纬度和经度,因此可以将其输入不支持WKT的其他应用程序中。
SELECT ?lat ?lon
WHERE {
    # how do I get lat and lon from "POINT(48.5 11.7)"^^ogc:wktLiteral?
}

我没有在OGC's GeoSPARQL specification中找到任何有用的东西,所以我想知道在SPARQL查询中手动提取此类数据的最佳方法是什么。

最佳答案

使用正则表达式执行此类操作总是有些棘手,尤其是在看起来我们无法使用精确的语法的情况下,但是我认为以下方法可以工作:

prefix ogc: <urn:ex:>

select ?lat ?long where {
  values ?point { "POINT(48.5 11.7)"^^ogc:wktLiteral }
  bind( replace( str(?point), "^[^0-9\\.]*([0-9\\.]+) .*$", "$1" ) as ?long )
  bind( replace( str(?point), "^.* ([0-9\\.]+)[^0-9\\.]*$", "$1" ) as ?lat )
}
-------------------
| lat    | long   |
===================
| "11.7" | "48.5" |
-------------------

关键在正则表达式中
"^[^0-9\\.]*([0-9\\.]+) .*$" === <non-number>(number) <anything>
"^.* ([0-9\\.]+)[^0-9\\.]*$" === <anything> (number)<non-number>

当然,这实际上是number的近似值,因为它将匹配具有多个点的事物,但是如果数据很好,那么您应该不会有问题。如果需要将这些值转换为数字类型,则也可以进行这种转换:
prefix ogc: <urn:ex:>
prefix xsd: <http://www.w3.org/2001/XMLSchema#>

select ?lat ?long where {
  values ?point { "POINT(48.5 11.7)"^^ogc:wktLiteral }
  bind( xsd:decimal( replace( str(?point), "^[^0-9\\.]*([0-9\\.]+) .*$", "$1" )) as ?long )
  bind( xsd:decimal( replace( str(?point), "^.* ([0-9\\.]+)[^0-9\\.]*$", "$1" )) as ?lat )
}
---------------
| lat  | long |
===============
| 11.7 | 48.5 |  # note: no quotation marks; these are numbers
---------------

请注意,还有其他类型的WKT点,并且此代码无法正确处理它们。例如,来自Wikipedia的Well-known text文章的一些示例:
POINT ZM (1 1 5 60)
POINT M (1 1 80)
POINT EMPTY

关于rdf - 如何从geosparql的wktLiteral中检索纬度和经度?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22536467/

10-08 21:50