# A question about geospatial queries with variable bindings

**URL:** <https://community.stardog.com/t/a-question-about-geospatial-queries-with-variable-bindings/2528>\
**Category:** Support\
**Created:** [June 16, 2020, 3:18pm UTC](https://community.stardog.com/t/a-question-about-geospatial-queries-with-variable-bindings/2528 "2020-06-16T15:18:16Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![arwesterinen](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/arwesterinen/32/40_2.png) [@arwesterinen](https://community.stardog.com/u/arwesterinen)\
**Post date:** [June 16, 2020, 3:18pm UTC](https://community.stardog.com/t/a-question-about-geospatial-queries-with-variable-bindings/2528/1 "2020-06-16T15:18:16Z")

</div>

Here is my query:

```auto
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)

Thanks.  
Andrea

---

<div class="post-metadata">

**Author:** ![zachary.whitley](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/zachary.whitley/32/94_2.png) [@zachary.whitley](https://community.stardog.com/u/zachary.whitley)\
**Post date:** [June 16, 2020, 4:37pm UTC](https://community.stardog.com/t/a-question-about-geospatial-queries-with-variable-bindings/2528/2 "2020-06-16T16:37:48Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![arwesterinen](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/arwesterinen/32/40_2.png) [@arwesterinen](https://community.stardog.com/u/arwesterinen)\
**Post date:** [June 16, 2020, 5:30pm UTC](https://community.stardog.com/t/a-question-about-geospatial-queries-with-variable-bindings/2528/3 "2020-06-16T17:30:04Z")

</div>

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

Andrea

---

<div class="post-metadata">

**Author:** ![zachary.whitley](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/zachary.whitley/32/94_2.png) [@zachary.whitley](https://community.stardog.com/u/zachary.whitley)\
**Post date:** [June 16, 2020, 5:52pm UTC](https://community.stardog.com/t/a-question-about-geospatial-queries-with-variable-bindings/2528/4 "2020-06-16T17:52:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![arwesterinen](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/arwesterinen/32/40_2.png) [@arwesterinen](https://community.stardog.com/u/arwesterinen)\
**Post date:** [June 16, 2020, 6:32pm UTC](https://community.stardog.com/t/a-question-about-geospatial-queries-with-variable-bindings/2528/5 "2020-06-16T18:32:26Z")

</div>

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

```auto
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.

Andrea

---

<div class="post-metadata">

**Author:** ![arwesterinen](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/arwesterinen/32/40_2.png) [@arwesterinen](https://community.stardog.com/u/arwesterinen)\
**Post date:** [June 23, 2020, 3:09am UTC](https://community.stardog.com/t/a-question-about-geospatial-queries-with-variable-bindings/2528/6 "2020-06-23T03:09:02Z")

</div>

Any reply on this?

Andrea

---

<div class="post-metadata">

**Author:** ![zachary.whitley](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/zachary.whitley/32/94_2.png) [@zachary.whitley](https://community.stardog.com/u/zachary.whitley)\
**Post date:** [June 23, 2020, 12:00pm UTC](https://community.stardog.com/t/a-question-about-geospatial-queries-with-variable-bindings/2528/7 "2020-06-23T12:00:39Z")

</div>

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

```

Can you try the above?

---

<div class="post-metadata">

**Author:** ![arwesterinen](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/arwesterinen/32/40_2.png) [@arwesterinen](https://community.stardog.com/u/arwesterinen)\
**Post date:** [June 23, 2020, 4:02pm UTC](https://community.stardog.com/t/a-question-about-geospatial-queries-with-variable-bindings/2528/8 "2020-06-23T16:02:02Z")

</div>

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)

Andrea

---

<div class="post-metadata">

**Author:** ![zachary.whitley](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/zachary.whitley/32/94_2.png) [@zachary.whitley](https://community.stardog.com/u/zachary.whitley)\
**Post date:** [June 23, 2020, 4:29pm UTC](https://community.stardog.com/t/a-question-about-geospatial-queries-with-variable-bindings/2528/9 "2020-06-23T16:29:55Z")

</div>

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

```auto
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?

---

<div class="post-metadata">

**Author:** ![zachary.whitley](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/zachary.whitley/32/94_2.png) [@zachary.whitley](https://community.stardog.com/u/zachary.whitley)\
**Post date:** [June 23, 2020, 4:46pm UTC](https://community.stardog.com/t/a-question-about-geospatial-queries-with-variable-bindings/2528/10 "2020-06-23T16:46:20Z")

</div>

```auto
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()

---

<div class="post-metadata">

**Author:** ![arwesterinen](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/arwesterinen/32/40_2.png) [@arwesterinen](https://community.stardog.com/u/arwesterinen)\
**Post date:** [June 23, 2020, 5:03pm UTC](https://community.stardog.com/t/a-question-about-geospatial-queries-with-variable-bindings/2528/11 "2020-06-23T17:03:24Z")

</div>

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.

Andrea

---

<div class="post-metadata">

**Author:** ![jess](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/jess/32/10_2.png) [@jess](https://community.stardog.com/u/jess)\
**Post date:** [June 23, 2020, 5:32pm UTC](https://community.stardog.com/t/a-question-about-geospatial-queries-with-variable-bindings/2528/12 "2020-06-23T17:32:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![arwesterinen](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/arwesterinen/32/40_2.png) [@arwesterinen](https://community.stardog.com/u/arwesterinen)\
**Post date:** [June 23, 2020, 6:38pm UTC](https://community.stardog.com/t/a-question-about-geospatial-queries-with-variable-bindings/2528/13 "2020-06-23T18:38:54Z")

</div>

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

Andrea

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex030/uploads/stardog/original/2X/e/ed66b48a616f505106a4acde3c9cee7e6da9bc67.svg) [@system](https://community.stardog.com/u/system)\
**Post date:** [July 7, 2020, 6:41pm UTC](https://community.stardog.com/t/a-question-about-geospatial-queries-with-variable-bindings/2528/14 "2020-07-07T18:41:05Z")

</div>

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