#community-help

Guiding GeoPoint Search with Typesense's Polygon

TLDR Ignacio had trouble with geopoint search in Typesense. Jason helped them solve it by using the counter-clockwise method with polygon points. The solution was posted on GitHub for future reference.

Powered by Struct AI

2

1

1

31
28mo
Solved
Join the chat
Aug 03, 2021 (28 months ago)
Ignacio
Photo of md5-f7965ddb4eec1a7d3abd4f89eb705965
Ignacio
04:22 PM
Hey, is this the right place to ask about an issue I have using geopoint search with polygon. I see it working sometimes and somehow not working on others.
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
04:24 PM
Yes it is. Could you share the search query you’re using?
Ignacio
Photo of md5-f7965ddb4eec1a7d3abd4f89eb705965
Ignacio
04:24 PM
let me get the search params array
04:25
Ignacio
04:25 PM
"{"q":"*","query_by":"title,address,listingId","page":1,"filter_by":"location:(20.643101970825, -105.2271453804, 20.647860850082, -105.22630853119, 20.646696243606, -105.22096557083, 20.646897038462, -105.22055787506, 20.646756482091, -105.22010726395, 20.641214441604, -105.22118014755, 20.641234521826, -105.22208136978, 20.642057808619, -105.22328299942)"}"
04:26
Ignacio
04:26 PM
I see on the documentation that polygon should be The polygon's points must be defined in a *counter-clockwise (i.e. anti-clockwise) direction*.
04:26
Ignacio
04:26 PM
how can I verify the direction? :thinking_face:
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
04:28 PM
That's likely the issue... You can add one marker at a time on a map visually, for each of the points and make sure that each marker added is in a counter-clockwise direction, compared to the previous one
04:30
Jason
04:30 PM
A shortcut is also to go to maps.google.com, then entering your first lat long, then click on get directions and then add the other lat longs one at a time as additional destinations
04:31
Jason
04:31 PM
And make sure every marker that is added is in a counter-clockwise direction compared to the previous marker.
Ignacio
Photo of md5-f7965ddb4eec1a7d3abd4f89eb705965
Ignacio
04:31 PM
mhmm
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
04:32 PM
In this case, I noticed that the 2rd lat/long is in the clockwise-direction
Ignacio
Photo of md5-f7965ddb4eec1a7d3abd4f89eb705965
Ignacio
04:32 PM
on the typesense response there is a request_params
04:32
Ignacio
04:32 PM
and it returns this:
{"collection_name":"properties","per_page":10,"q":"*"}
04:32
Ignacio
04:32 PM
it does not mention the filter.
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
04:33 PM
Yup that's expected. request_params in the response contains just enough information to render most search UIs and not all the params that were sent
Ignacio
Photo of md5-f7965ddb4eec1a7d3abd4f89eb705965
Ignacio
04:33 PM
mhmm when the search works it returns the same
04:34
Ignacio
04:34 PM
let me try what you mention on the map to check the direction, so I should see the points going from right to left.

1

04:36
Ignacio
04:36 PM
this query
04:36
Ignacio
04:36 PM
"{"q":"*","query_by":"title,address,listingId","page":1,"filter_by":"location:(20.647836009729, -105.22632746033, 20.643036969737, -105.22716430954, 20.642032967319, -105.22321609787, 20.641149439709, -105.2214994841, 20.641591204155, -105.22109178833, 20.646832038996, -105.22008327774)"}"
04:36
Ignacio
04:36 PM
does work
04:39
Ignacio
04:39 PM
So the issue is the order in which I draw on the map
04:40
Ignacio
04:40 PM
if I draw clockwise I get the polygon on that order.
04:40
Ignacio
04:40 PM
if I draw not clockwise the search work
04:40
Ignacio
04:40 PM
now need to figure out how to verify order at runtime before sending the search. 😅
04:52
Ignacio
04:52 PM
$polygonSource = $sourceSearch->polygon;
                    if ($polygonSource[0]->lat < $polygonSource[1]->lat) {
                        $polygonSource = array_reverse($polygonSource);
                    }
04:53
Ignacio
04:53 PM
that is all to get the polygon on the right direction. Thanks! Cool product! Congrats!

1

Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
05:16 PM
Nice, thank you for sharing!
05:17
Jason
05:17 PM
Ignacio mind opening a github issue describing your original issue and then adding your solution as a response? I think others searching in the future would find it useful.
Ignacio
Photo of md5-f7965ddb4eec1a7d3abd4f89eb705965
Ignacio
05:18 PM
sure

1

05:34
Ignacio
05:34 PM
Thanks for looking at it.

1