用正则表达式做这类事情总是有点棘手,尤其是当我们看起来没有精确的语法可以使用时,但我认为以下方法可行:
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