I’m trying to implement a function in TypeScript where you pass in north-east, south-west lat/lng’s and you get back all documents between the two points (i.e. a bounding box query).
I can do this in Postgres easily using the PostGIS extension, but I want to try to implement the same using Fauna to compare.
Using this as a reference (which is a great post btw), I see that it looks possible using a somewhat involved index binding in FQLv4. However, I want to use FQLv10 and do not understand how to create index bindings in v10.
Firstly, how do you create an index binding in v10 (in general)?
How do you create a value or a term in an index from a value derived from the values in a document?
Secondly, how can I create an index similar to the one in the linked forum post?
This index (from its binding) needs to allow me to query if a given h3 hash is a parent hash (or the hash itself) of an h3 hash in a document.
(say the h3 hash is at document[‘location’][‘h3’])
Thirdly, how do I write the UDF that when called (in FQLv10):
allWithParentH3(<the h3 hash>)
Would return all documents that have <the h3 hash>
as one of the parent hashes.
To go from north-east, south-west lat/lng’s to an array of h3 hashes, I’m doing the following on the client side (using h3-js):
const cells = polygonToCells(
[
[northEast.lat, northEast.lng],
[northEast.lat, southWest.lng],
[southWest.lat, southWest.lng],
[southWest.lat, northEast.lng],
],
resFromZoomLvl,
);
//Minimise the number of h3 hashes
const cellsCompact = compactCells(cells);
(resFromZoomLvl
uses some heuristic that converts the given zoom level to an appropriate h3 resolution, ideally, I wouldn’t need to pass this in, but that’s an h3 problem, nothing to do with Fauna)
Then iterate through each hash in cellsCompact
calling the allWithParentH3
UDF. I’m not sure if there’s a better way. Maybe a UDF that accepts an array of h3 hashes?
The h3 hash for the lat/lng of a document is calculated by:
latLngToCell(lat, lng, 15)