I am trying to retrieve a polygon from my database and use it in a where clause to find points within the polygon but I can’t get the polygon into WKT format. I tried using the “Convert to WKT” block and put the GeoJSON object that the UI retrieves into the “geometry” input, but it keeps giving me the error “TypeError: (intermediate value)(…).asWKT is not a function” How else can I extract the polygon data from the database in WKT format?
This should help you:
https://backendless.com/docs/rest/data_spatial_retrieval_api.html#aswkt
Here’s an example of using the function as a dynamic column:
I’m sorry Mark, it’s not. I’m not having trouble searching a column for points, I need to convert a single polygon that is coming out of the database and into a variable into WKT format so I can put it into the polygonVariable position shown below
Within(locationColumn, ‘polygonVariable’) = True
I tried doing
Within(locationColumn, ‘AsWKT(polygonVariable)’ = True
and it gives me
Within(locationColumn, ‘AsWKT([object Object])’)
Here’s what I understood:
- You make a database query and get your
polygonVariable
. - You need to convert that value to WKT
- Using the WKT value you need to make another database query to search for records that satisfy the
Within
condition.
Did I get that right?
Yes, please just let me convert one polygon from GeoJSON to WKT in the UI so I can use it wherever I need to plug it in.
You can use the REST Console
interface and retrieve the value, as I showed in the screenshot above.
Alternatively, the console already displays the values for the Geo columns in either WKT or GeoJSON formats:
I need it in the UI, not the REST Console. I need the polygon for whatever user is currently using the app, not hardcoded.
This is a super common functionality, searching for restaurants within the customer’s vicinity (in my case service area around their home). I don’t know why I can’t figure out how to do it, been trying everything I can think of for the last 2 hours. There’s probably a simple solution that I’m not seeing in my options.
I do not understand where the original polygon value comes from. Does the user draw it on the map? What is the format of the data you want to convert to WKT?
In your very first post you wrote:
If it comes from the database, the database already provides a function to retrieve the value in the WKT format. However, as we went back and forth, it sounded like the value is not coming from the database and you need it in the UI. So before we get to the actual question of WKT, it would be helpful to understand where you’re getting the polygon definition from.
When a user creates their account I create a point from their home address and draw a polygon around it to define their service area and store it in the database. When they open the app it should populate a list of service providers in their service area. I need to load table objects that have location GEO Points that are within the user’s service area. serviceArea is a table column of polygons in the users table and location is a column of geo points of the service provider’s locations. If I could search a distance from the user’s home address GEO Point that would work just as well.
You can use the distanceOnSphere
function to retrieve all points located within the specified distance (radius) from the main point. See the examples for this section:
https://backendless.com/docs/rest/data_spatial_retrieval_api.html#distanceonsphere-operator
Regarding your current approach - you already store the polygon in the database. To use it with the Within
function, you can simply retrieve that polygon in the WKT format using the AsWKT
function and then use the value retrieved with that function in a where clause that uses Within
.
This isn’t working, Bad Request. I don’t think I understand what you mean by “using the AsWKT function and then use the value retrieved” This doesn’t look right, but I can’t figure out any other way to do it. Do I need to use custom code? I swear, I’m not dumb, I’m just not a coder, hence why I’m using backendless and not just writing this from scratch. How do I call AsWKT on a variable in local storage and retrieve the result to plug into the where clause.
Is the user’s geo location stored in the browser’s local storage? The logic retrieves some value from the browser local storage (see the Get Local Storage Data
block) and that value is then treated as a column name in the where clause. Just wanted to make sure I understand what you’re doing here correctly.
I’m just sticking that Geo Point in local storage instead of page data or app data so I don’t have to retrieve it over and over again since I’ll use it every time they open a page and it doesn’t change unless they update their user account. Yes, it’s a Geo Point saved in browser local storage, when I print it it looks like this
How do I convert that to this?
POINT (-95.99547299999999 36.1625688)
This will convert your point to WKT:
OMG MARK! That is the first thing I tried over 3 hours ago and the first thing I said didn’t work.
We would have saved a lot of time if you had attached that screenshot to the first post.
The value from the local storage must be deserialized into a plain object rather than a Point
object. I need to play with it to see how it can be deserialized so that the asWKT
function becomes available. As a workaround, bypassing local storage and using the value from the database should work.
Ok, sorry, I will include screenshots next time. I will try just converting it before I stick it in local storage so I don’t have to do it over and over again.
It worked!