Skip to content
This repository was archived by the owner on Dec 29, 2022. It is now read-only.
This repository was archived by the owner on Dec 29, 2022. It is now read-only.

Should GeoHashQuery include endValue or not? #155

Description

@algrid

Hello! Thanks for nice library.

I have a question about the following methods from GeoHashQuery.java:

    public static GeoHashQuery queryForGeoHash(GeoHash geohash, int bits) {
        String hash = geohash.getGeoHashString();
        int precision = (int)Math.ceil((double)bits/Base32Utils.BITS_PER_BASE32_CHAR);
        if (hash.length() < precision) {
            return new GeoHashQuery(hash, hash+"~");
        }
        hash = hash.substring(0, precision);
        String base = hash.substring(0, hash.length() - 1);
        int lastValue = Base32Utils.base32CharToValue(hash.charAt(hash.length() - 1));
        int significantBits = bits - (base.length() * Base32Utils.BITS_PER_BASE32_CHAR);
        int unusedBits = Base32Utils.BITS_PER_BASE32_CHAR - significantBits;
        // delete unused bits
        int startValue = (lastValue >> unusedBits) << unusedBits;
        int endValue = startValue + (1 << unusedBits);
        String startHash = base + Base32Utils.valueToBase32Char(startValue);
        String endHash;
        if (endValue > 31) {
            endHash = base + "~";
        } else {
            endHash = base + Base32Utils.valueToBase32Char(endValue);
        }
        return new GeoHashQuery(startHash, endHash);
    }

    public static Set<GeoHashQuery> queriesAtLocation(GeoLocation location, double radius) {
    ...
    }

I don't fully understand this code, but I have an impression that endValue from queryForGeoHash is not supposed to be included into the query. I mean that the query should be interpreted as startValue <= geohash < endValue but in fact when the query is executed it's inclusive: startValue <= geohash <= endValue.

To fix that we could change the line:

        int endValue = startValue + (1 << unusedBits);

into:

        int endValue = startValue + (1 << unusedBits) - 1;

To illustrate this, look at my results with center in Caracas, Venezuela and radius=350km (the selected area is what's included into the query):

  1. Original version:

Screen Shot 2019-07-05 at 11 40 44 PM

  1. My fixed version:

Screen Shot 2019-07-05 at 11 37 59 PM

Note: actually I'm using a kotlin version of the code from here: https://github.com/imperiumlabs/GeoFirestore-Android/blob/34b935c534c583520a706c3388c57962122846bf/geofirestore/src/main/java/org/imperiumlabs/geofirestore/core/GeoHashQuery.kt

but the logic is exactly the same, so I think that the code in this repo is (or is closer to) the original.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions