kungfoo / geohash-java

Implementation of GeoHashes in java. We try to be/stay compliant to the spec, as far as possible.
Other
985 stars 309 forks source link

How can I set the precision so it's a multiple of 5? #53

Closed marvinhepp closed 3 years ago

marvinhepp commented 3 years ago

Hey, I'm so sorry for asking this stupid question but I'm very new to Java and use Android studio for the first time. I spent the last few hours writing the following query for Firebase but I always get the error "Cannot convert a geohash to base32 if the precision is not a multiple of 5." I have already looked at all the questions about this on here but I can't figure out what to do about it. Like how can I change the precision to a value that is a multiple of 5? I'm really stuck :(

` private void search(){

    CollectionReference locCollectionRef = fStore.collection("users");

    WGS84Point firstPos = new WGS84Point(firstLatLng.getLatitude(), firstLatLng.getLongitude());
    WGS84Point secondPos = new WGS84Point(secLatLng.getLatitude(), secLatLng.getLongitude());

    GeoHashBoundingBoxQuery search = new GeoHashBoundingBoxQuery(new BoundingBox(firstPos, secondPos));

    List<GeoHash> hashes = search.getSearchHashes();

    for (GeoHash hash : hashes){
        String start = hash.toBase32();
        String end = start + "~";

        Query locQuery = locCollectionRef
                .orderBy("Hash")
                .startAt(start)
                .endAt(end);
        locQuery.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                if(task.isSuccessful()){
                    for(QueryDocumentSnapshot doc : task.getResult()){
                        String usr = doc.get("name").toString();
                        printUsr(usr);
                    }
                }

            }
        });
    }
}

` PS: if it's not clear, I have a database with lots of documents and each of the documents has a field with a hash code. now I want to get every document that is inside of a rectangle.

Edit: I got it working doing this. I think the code is very ugly and bad but I'll post it anyway. (improvements are appreciated)

` private void search(){

    CollectionReference locCollectionRef = fStore.collection("users");

    WGS84Point firstPos = new WGS84Point(firstLatLng.getLatitude(), firstLatLng.getLongitude());
    WGS84Point secondPos = new WGS84Point(secLatLng.getLatitude(), secLatLng.getLongitude());

    GeoHashBoundingBoxQuery search = new GeoHashBoundingBoxQuery(new BoundingBox(firstPos, secondPos));

    List<GeoHash> hashes = search.getSearchHashes();

    List<String> starts = new ArrayList<>();

    for (GeoHash hash : hashes){
        int bits = (int)(hash.significantBits() / 5) * 5;
        String start = GeoHash.fromLongValue(hash.longValue(), bits).toBase32();
        if(starts.contains(start)){

        }
        else {
            starts.add(start);
        }
    };

    for (String start : starts){

        String end = start + "~";
        printUsr(start);

        Query locQuery = locCollectionRef
                .orderBy("Hash")
                .startAt(start)
                .endAt(end);
        locQuery.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                if(task.isSuccessful()){
                    for(QueryDocumentSnapshot doc : task.getResult()){
                        String usr = doc.get("name").toString();
                        printUsr(usr);
                    }
                }

            }
        });
    }
}

`