2

Does GeoSPARQL provide functionality to calculate the distance between two linestrings? (Edit: I'm using Jena Fuseki with the GeoSPARQL extension.)

The results are not working as expected.

Linestring query, assuming all of the geometries are also linestrings:

prefix geo: <http://www.opengis.net/ont/geosparql#> 
prefix geof: <http://www.opengis.net/def/function/geosparql/> 
prefix ogis: <http://www.opengis.net/def/uom/OGC/1.0/>
select * where {
  ?s geo:hasGeometry ?f .
  ?f geo:asWKT ?wkt.
  BIND(geof:distance(?wkt,
    "<http://www.opengis.net/def/crs/EPSG/0/27700> LINESTRING(41.8 -87.6,41.8 -87.5)"^^geo:wktLiteral,
    ogis:kilometre) as ?distance)
}
ORDER BY asc(abs(?distance))

If instead I calculate distance between points, then this works as expected.

Points, assuming all of the geometries are also points:

prefix geo: <http://www.opengis.net/ont/geosparql#> 
prefix geof: <http://www.opengis.net/def/function/geosparql/> 
prefix ogis: <http://www.opengis.net/def/uom/OGC/1.0/>
select * where {
  ?s geo:hasGeometry ?f .
  ?f geo:asWKT ?wkt .
  BIND(geof:distance(?wkt,
    "<http://www.opengis.net/def/crs/EPSG/0/27700> POINT(41.8 -87.6)"^^geo:wktLiteral,
    ogis:kilometre) as ?distance)
}
ORDER BY asc(abs(?distance))

And what about the distance from a point to line, and vice versa?

Edit: Here are the results for a line to line search given my dataset. The distances are obviously wrong. The lines are typically no closer than a few KMs, but all of these results are shown as less than 1KM. For example, the last result is shown as 0.35KM but it's in Louisiana whereas the original point is in Illinois.

Again, these results are from Jena Fuseki with the GeoSPARQL extension.

select ?wkt ?distance where {
  ?s a sosa:featureOfInterest .
  ?s geo:hasGeometry ?f .
  ?f geo:asWKT ?wkt.
  BIND(geof:distance(?wkt,
    "<http://www.opengis.net/def/crs/EPSG/0/27700> LINESTRING(41.85003 -87.65005,41.85 -87.65)"^^geo:wktLiteral,
    ogis:kilometre) as ?distance) .  
}
ORDER BY asc(abs(?distance))
limit 10
index wkt.value distance.value
0 <http://www.opengis.net/def/crs/EPSG/0/27700> LINESTRING(46.7082 -89.9785,46.7 -89.9892) 0.168957e0
1 <http://www.opengis.net/def/crs/EPSG/0/27700> LINESTRING(46.699902 -89.989067,46.669998 -90.046654) -0.186799e0
2 <http://www.opengis.net/def/crs/EPSG/0/27700> LINESTRING(41.84 -87.607,41.839 -87.605) 0.205283e0
3 <http://www.opengis.net/def/crs/EPSG/0/27700> LINESTRING(41.865 -87.607,41.863 -87.607) 0.208798e0
4 <http://www.opengis.net/def/crs/EPSG/0/27700> LINESTRING(46.708 -89.978,46.7096 -89.9749) 0.246481e0
5 <http://www.opengis.net/def/crs/EPSG/0/27700> LINESTRING(41.894 -87.614,41.894 -87.612) 0.26358e0
6 <http://www.opengis.net/def/crs/EPSG/0/27700> LINESTRING(41.904 -87.624,41.902 -87.622) 0.272661e0
7 <http://www.opengis.net/def/crs/EPSG/0/27700> LINESTRING(41.8234 -87.5966,41.8215 -87.595) 0.278589e0
8 <http://www.opengis.net/def/crs/EPSG/0/27700> LINESTRING(41.924 -87.63,41.914 -87.62) 0.326035e0
9 <http://www.opengis.net/def/crs/EPSG/0/27700> LINESTRING(29.226625 -90.001852,29.211947 -90.026484) -0.353463e0
There
  • 498
  • 6
  • 18
  • 1
    what means "not working as expected"? And yes, it's possible to compute the distance of linestrings or any geometry. Try `prefix geo: prefix geof: prefix ogis: select * where { VALUES ?s {"LINESTRING(40.8 -87.6, 43.8 -87.5)"^^geo:wktLiteral} BIND(geof:distance(?s, "LINESTRING(41.8 -89.6, 41.8 -89.5)"^^geo:wktLiteral, ogis:kilometre) as ?distance) } ORDER BY asc(abs(?distance))`, works as epxected – UninformedUser Feb 05 '22 at 08:03
  • maybe you can give more details, data, Jena version etc? I can say it "should" work, but nobody knows if there's a bug or not without being able to reproduce which needs more information from your side – UninformedUser Feb 05 '22 at 11:02
  • @UninformedUser Question updated... – There Feb 05 '22 at 17:10
  • 1
    I see, I tried to reproduce and now I'm wondering why you use 27700 as EPSG, this is "EPSG:27700 OSGB 1936 / British National Grid" isn't it? Isn't this just for UK? I tried to map a line to the map, but if I use https://epsg.io/transform#s_srs=27700&t_srs=4326 you points are somehere else but not Wisconsin nor Illinois. Can you explain this to me? The unit is meters not degrees for this CRS, no? – UninformedUser Feb 06 '22 at 08:25
  • 1
    Also, to verify you result I took the the last line as inline value: `prefix geo: prefix geof: prefix ogis: select * where { VALUES ?s {" LINESTRING(29.226625 -90.001852,29.211947 -90.026484)"^^geo:wktLiteral} BIND(geof:distance(?s, " LINESTRING(41.85003 -87.65005,41.85 -87.65)"^^geo:wktLiteral, ogis:kilometre) as ?distance) } ORDER BY asc(abs(?distance))` – UninformedUser Feb 06 '22 at 08:26
  • 1
    I'm getting a different distance value of `0.012767e0` compared to your `-0.353463e0` – UninformedUser Feb 06 '22 at 08:27
  • 1
    you can also try to use `` as an alternative distance function: `BIND((?wkt, " LINESTRING(41.85003 -87.65005,41.85 -87.65)"^^geo:wktLiteral, ogis:kilometre)` – UninformedUser Feb 06 '22 at 08:49
  • 1
    though I'm still not sure if your EPSG is correct here. For me this is a bug in your data but I might be wrong - looks more like you're using CRS84 as the coordinates seem to be degrees – UninformedUser Feb 06 '22 at 08:52
  • Aha, got it. That makes sense. Thanks! Using appears to produce correct distances as expected. – There Feb 06 '22 at 16:24

0 Answers0