A question about geospatial queries with variable bindings

Here is my query:

prefix geof: <http://www.opengis.net/def/function/geosparql/> 
prefix geosparql: <http://www.opengis.net/ont/geosparql#> 
prefix unit: <http://qudt.org/vocab/unit#>  
prefix wgs: <http://www.w3.org/2003/01/geo/wgs84_pos#> 
prefix : <urn:logx:ontology:> 
SELECT ?prim_id ?name ?country ?portLat ?portLong ?distanceKms 
WHERE { ?port a geosparql:Geometry ; 
                     geof:nearby (?latitude ?longitude ?maxDistanceKms unit:Kilometer) ; 
                    :label ?name ; :prim_id ?prim_id ; 
                    wgs:lat ?portLat ; wgs:long ?portLong . 
               ?geoCountry :hasComponent ?port ; :isoAlpha2 ?country . 
               BIND(CONCAT("\\"POINT(", CONCAT(" ", CONCAT(?longitude, 
                  CONCAT(?latitude, ")\\"^^geosparql:wktLiteral\")))) as ?geoPoint) . 
              BIND(geof:distance(?port, ?geoPoint, unit:Kilometer) as ?distanceKms) } 
              ORDER BY ?distanceKms

I pass in ?latitude ?longitude and ?maxDistanceKms using a query binding.

But ... How can I encode a geo:wktLiteral as a variable? I have tried multiple things (one is shown above) but nothing works.
I want:
BIND(geof:distance(?port, "POINT(-118.2 33.75)"^^geosparql:wktLiteral, unit:Kilometer) as ?distanceKms)


My first guess would be that you can’t concatenate the data type in there. You’ll get a literal with the data type included inside the literal.

OK, so how do I do that since I want to variable bindings and not have SPARQL injections? :slight_smile:


You’d use the STRDT function. Sorry, I’m not quite sure what you mean by encode it as a variable here.

So, I want to encode the final BIND of the query as:

BIND(geof:distance(?port, ?geoPoint, unit:Kilometer) as ?distanceKms) } 

And pass a query variable binding for geoPoint directly as the "POINT(y x)" string or construct it from the variable bindings for ?longitude and ?latitude.


Any reply on this?


BIND(STRDT(CONCAT("POINT(", ?longitude, ",", ?latitude, ")"), geosparql:wktLiteral)) AS ?geoPoint) . 
BIND(geof:distance(?port, ?geoPoint, unit:Kilometer) as ?distanceKms)

Can you try the above?

Tried your recommendation and got 0 results.

If I hard code the entry, I get results.

BIND(geof:distance(?port, "POINT(-118.2165 33.7542)"^^geosparql:wktLiteral,
unit:Kilometer) as ?distanceKms)


It looks like I accidentally put a comma between lat and long. Can you try it one more time

BIND(STRDT(CONCAT("POINT(", ?longitude, " ", ?latitude, ")"), geosparql:wktLiteral)) AS ?geoPoint) . 
BIND(geof:distance(?port, ?geoPoint, unit:Kilometer) as ?distanceKms)

If it still doesn't work can you send the query plan for both queries with and without the var?

BIND(STRDT(CONCAT("POINT(", STR(?longitude), " ", STR(?latitude), ")"), geosparql:wktLiteral)) AS ?geoPoint) . 
BIND(geof:distance(?port, ?geoPoint, unit:Kilometer) as ?distanceKms)

It was pointed out to me that concat might be throwing an error on floating point values and they might need to be wrapped in STR()

No dice. Still no results returned.

Did you try this yourself? (There is an extra closing parentheses in the first BIND - but after fixing that still 0 results.)

I only get results by hard-coding "POINT(xxx yyy)"^^geosparql:wktLiteral.


This is the right idea but I don't think it's going to work due to Stardog bug #7543.

So we have to hardcode for now. When might #7543 be fixed??


This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.