Anyone know how geopolygon is meant to work? My c...
# community-help
e
Anyone know how geopolygon is meant to work? My collection looks like this:
Copy code
[
    'name' => 'suburbs',
    'fields' => [
        ['name' => 'council.id', 'type' => 'int32'],
        ['name' => 'council.name', 'type' => 'string'],
        ['name' => 'city.id', 'type' => 'int32'],
        ['name' => 'city.name', 'type' => 'string'],
        ['name' => 'suburb.id', 'type' => 'int32'],
        ['name' => 'suburb.name', 'type' => 'string'],
        ['name' => 'polyline', 'type' => 'string'],
        ['name' => 'area', 'type' => 'geopolygon'],
    ],
    'enable_nested_fields' => true
]
I'm adding a document that looks like this:
Copy code
$doc = [
    'id' => (string) $id,
    'council.id' => $councilID,
    'council.name' => $councilName,
    'city.id' => $cityID,
    'city.name' => $cityName,
    'suburb.id' => $suburbID,
    'suburb.name' => $suburbName,
    'polyline' => $polyline,
    'area' => $geo
];
My points are Google polylines, decoded and chunked, that look like this:
Copy code
[
    [-43.87303, 171.7223],
    [-43.88554, 171.72586],
    [-43.88888, 171.72705],
    [-43.89761, 171.73431],
    [-43.89957, 171.73657],
    [-43.90029, 171.73747],
    [-43.90204, 171.75693],
    [-43.90173, 171.7583],
    [-43.89736, 171.76499],
    [-43.87886, 171.79079],
    [-43.8779, 171.79082],
    [-43.86122, 171.77881],
    [-43.86052, 171.77765],
    [-43.85319, 171.74691],
    [-43.85222, 171.72776],
    [-43.86604, 171.72113],
    [-43.87303, 171.7223]
]
The documents all appear to emplace correctly: the total count of the collection matches the number I expect. But when I view the collection in the bfritscher dashboard, instantsearch doesn't load any results. The stats counter updates with hits, but the hits array in the results are empty. I'm unable to query
*
via SDK or CURL, and export returns
""
Removing the
area
field, the collection behaves in the way I expect. I haven't used the field before, so unsure if this is a bug or I'm just using it incorrectly. Using Typesense 30.1 and v5.2.0 of PHP SDK.
Just to add, I have a few hundred points that should be within that polygon. I'm interested in converting the polyline into a shape so I can query what's inside that polygon. Using Leaflet, in case that's an important detail 👍
Just another thought... is there a limit on how many points can be in the polygon? My polylines have a max string length of 100;
More debugging... i don't think it's a question of going over some limit. I've tried to create one with four lat/lon points, where the first point and last point are identical. Mentally looking at the points, they are drawn in counter-clockwise order. The hits array is still empty when I query it. Here's the CURL request I can see being made in the browser:
Copy code
curl 'https://localhost/_typesense/multi_search?x-typesense-api-key=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' \
  --compressed \
  -X POST \
  -H 'User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:148.0) Gecko/20100101 Firefox/148.0' \
  -H 'Accept: application/json, text/plain, */*' \
  -H 'Accept-Language: en-US,en;q=0.9' \
  -H 'Accept-Encoding: gzip, deflate, br, zstd' \
  -H 'Content-Type: text/plain' \
  -H 'Origin: http://localhost:28108' \
  -H 'Connection: keep-alive' \
  -H 'Referer: http://localhost:28108/' \
  -H 'Sec-Fetch-Dest: empty' \
  -H 'Sec-Fetch-Mode: cors' \
  -H 'Sec-Fetch-Site: same-site' \
  -H 'Pragma: no-cache' \
  -H 'Cache-Control: no-cache' \
  --data-raw '{"searches":[{"max_candidates":4,"query_by":"council.name,city.name,suburb.name,polyline","highlight_full_fields":"council.name,city.name,suburb.name,polyline","collection":"suburbs","q":"at","page":1,"per_page":12}]}'
m
TS polygons are stored as flattened coordinates, not points:
Copy code
'area' => [0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0]
And then it’s possible to search for documents whose polygon area contains a specific point:
Copy code
'q' => '*',
'filter_by' => 'area:(0.5, 0.5)',
We use this to geolocate a predefined region from a visitor’s geo coordinates, for example: AUK -> NI -> NZ. Hope it helps :)
👍 1
e
Thank you! I'll see if I can get that working
So if I look at my example above, the geopolygon would be:
[-43.87303, 171.7223, -43.88554, 171.72586, -43.88888, 171.72705, ..., -43.87303, 171.7223]
. Basically the decoded polyline, without chunking it into lat/lon?
m