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

Feature Request: sub-cells by direction. #33

Open
ShamblingCrane opened this issue Feb 14, 2018 · 1 comment
Open

Feature Request: sub-cells by direction. #33

ShamblingCrane opened this issue Feb 14, 2018 · 1 comment

Comments

@ShamblingCrane
Copy link

First of all, I'd like to say that it's really a pleasure to use this library.

I am having a use case for which I need to identify "border cells" below a certain distance, e.g. all cells with resolution of about 2.4x2.4 m^2 (geohash lenght = 9) on the border between cells "b" and "c".

What I would require in this case is, being able to extract all cells of geohash length n+1 (e.g. lenght 2 starting from cells of length 1) by direction, so to have the rightmost column of sub-cells in "b" of geohash length, say, 9, and the leftmost column of subcells of "c" of the same geohash length.

@Psijic
Copy link

Psijic commented Jun 27, 2023

I'd like to get n-level neighbours. Current GeoHash.neighbours() returns n=1 level as does gridAsString. Chatbot says:
To find the n-level neighbors of a geohash, you can use a recursive approach. Here's an example implementation:

fun getNLevelNeighbors(geohash: String, level: Int): Set<String> {
    return when (level) {
        0 -> setOf(geohash)
        else -> {
            val neighbors = GeoHash.neighbours(geohash)
            val result = HashSet<String>()
            for (i in 0 until 8) {
                result.addAll(getNeighbors(neighbors[i], level - 1))
            }
            result
        }
    }
}

In this implementation, we define a getNLevelNeighbors function that takes a geohash and an integer n as input. If n is 0, we return a set containing only the original geohash. Otherwise, we use the neighbours() method of the GeoHash class to get the immediate neighbors of the geohash. We then create an empty set to store the n-level neighbors and loop over the immediate neighbors. For each immediate neighbor, we recursively call the getNLevelNeighbors function with n - 1 as the new value of n. We add the resulting set of n-level neighbors to the result set. Finally, we return the result set containing all n-level neighbors.

Here's an example usage of the getNLevelNeighbors function:

val geohash = "u4pruydqqvj"
val n = 2
val nLevelNeighbors = getNLevelNeighbors(geohash, n)
println(nLevelNeighbors)

This will print a set of all geohashes that are within 2 levels of the u4pruydqqvj geohash. You can adjust the value of n to get neighbors at different levels.

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

No branches or pull requests

3 participants