Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

h3js index find all children using index values #122

Closed
am2222 opened this issue May 25, 2021 · 5 comments
Closed

h3js index find all children using index values #122

am2222 opened this issue May 25, 2021 · 5 comments

Comments

@am2222
Copy link

am2222 commented May 25, 2021

Hello,
Suppose we are storing h3 indexes in a database column. To perform a parent/child query I was thinking of using h3 indexes. Lets say we have this index 832834fffffffff. This cell's children are[ '8428341ffffffff', '8428343ffffffff', '8428345ffffffff', '8428347ffffffff', '8428349ffffffff', '842834bffffffff', '842834dffffffff']. What is the logic to tell if an h3index id is a child of another id. Is there any way that we can find a range of indexies that are child of the specific id.
I already found uber/h3#320 and https://stackoverflow.com/questions/53911322/is-the-h3index-ordered but could not find my answer.
Thanks

@dfellis
Copy link
Collaborator

dfellis commented May 25, 2021

So the following snippet ought to work:

function isChildOf(possibleParent, possibleChild) {
  return possibleParent === h3.h3ToParent(possibleChild, h3.h3GetResolution(possibleParent));
}

Since the parent-child relationship is a simple tree structure in H3, there can be only one parent for any H3 index at any given resolution coarser (smaller) than its own resolution.

@dfellis
Copy link
Collaborator

dfellis commented May 25, 2021

Oh, and the second piece "find a range of indexes that are all children of a specified id" is just h3.h3ToChildren(index, childResolution)

@am2222
Copy link
Author

am2222 commented May 25, 2021

@dfellis thanks for the reply. But since we don't have access to the h3 library in the database is there any equations or logic to calculate the output of h3.h3ToChildren(index, childResolution) in database?

@dfellis
Copy link
Collaborator

dfellis commented May 25, 2021

So, yes, the underlying C implementation of h3ToParent and h3ToChildren is relatively straitghtforward (parent is much easier than children, though).

h3ToParent is some sanity checking up front, and then some bit masking to replace the resolution with the new parent resolution and write 1s to all of the bits for the resolutions that are larger than that.

h3ToChildren has to allocate memory for the maximum number of children possible at that child level, then it loops through the possible children with special logic to detect and handle "missing children" if one of the indexes is actually a pentagon, and writes them into the allocated memory. This one would be hard to replicate in a database, but not impossible.

@am2222
Copy link
Author

am2222 commented May 25, 2021

@dfellis Thanks, I am looking into the functions. it seems they are the exact functions I am looking for

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants