Search in sources :

Example 1 with GeoHashQuery

use of com.firebase.geofire.core.GeoHashQuery in project geofire-java by firebase.

the class GeoHashQueryTest method pointsInGeoHash.

@Test
public void pointsInGeoHash() {
    for (int i = 0; i < 1000; i++) {
        double centerLat = Math.random() * 160 - 80;
        double centerLong = Math.random() * 360 - 180;
        double radius = Math.random() * 100000;
        double radiusDegrees = GeoUtils.distanceToLatitudeDegrees(radius);
        Set<GeoHashQuery> queries = GeoHashQuery.queriesAtLocation(new GeoLocation(centerLat, centerLong), radius);
        for (int j = 0; j < 1000; j++) {
            double pointLat = Math.max(-89.9, Math.min(89.9, centerLat + Math.random() * radiusDegrees));
            double pointLong = GeoUtils.wrapLongitude(centerLong + Math.random() * radiusDegrees);
            if (GeoUtils.distance(centerLat, centerLong, pointLat, pointLong) < radius) {
                GeoHash geoHash = new GeoHash(pointLat, pointLong);
                boolean inQuery = false;
                for (GeoHashQuery query : queries) {
                    if (query.containsGeoHash(geoHash)) {
                        inQuery = true;
                    }
                }
                Assert.assertTrue(inQuery);
            }
        }
    }
}
Also used : GeoHashQuery(com.firebase.geofire.core.GeoHashQuery) GeoHash(com.firebase.geofire.core.GeoHash) Test(org.junit.Test)

Example 2 with GeoHashQuery

use of com.firebase.geofire.core.GeoHashQuery in project geofire-java by firebase.

the class GeoHashQueryTest method joinWith.

@Test
public void joinWith() {
    Assert.assertEquals(new GeoHashQuery("abcd", "abcf"), new GeoHashQuery("abcd", "abce").joinWith(new GeoHashQuery("abce", "abcf")));
    Assert.assertEquals(new GeoHashQuery("abcd", "abcf"), new GeoHashQuery("abce", "abcf").joinWith(new GeoHashQuery("abcd", "abce")));
    Assert.assertEquals(new GeoHashQuery("abcd", "abcf"), new GeoHashQuery("abcd", "abcf").joinWith(new GeoHashQuery("abcd", "abce")));
    Assert.assertEquals(new GeoHashQuery("abcd", "abcf"), new GeoHashQuery("abcd", "abcf").joinWith(new GeoHashQuery("abce", "abcf")));
    Assert.assertEquals(new GeoHashQuery("abc", "abd"), new GeoHashQuery("abc", "abd").joinWith(new GeoHashQuery("abce", "abcf")));
    Assert.assertEquals(new GeoHashQuery("abc", "abd"), new GeoHashQuery("abce", "abcf").joinWith(new GeoHashQuery("abc", "abd")));
    Assert.assertEquals(new GeoHashQuery("abc", "abd"), new GeoHashQuery("abcd", "abce~").joinWith(new GeoHashQuery("abc", "abd")));
    Assert.assertEquals(new GeoHashQuery("abcd", "abcf"), new GeoHashQuery("abcd", "abce~").joinWith(new GeoHashQuery("abce", "abcf")));
    Assert.assertEquals(new GeoHashQuery("abcd", "abcg"), new GeoHashQuery("abcd", "abcf").joinWith(new GeoHashQuery("abce", "abcg")));
    try {
        new GeoHashQuery("abcd", "abce").joinWith(new GeoHashQuery("abcg", "abch"));
        Assert.fail("Exception was not thrown!");
    } catch (IllegalArgumentException expected) {
    }
    try {
        new GeoHashQuery("abcd", "abce").joinWith(new GeoHashQuery("dce", "dcf"));
        Assert.fail("Exception was not thrown!");
    } catch (IllegalArgumentException expected) {
    }
    try {
        new GeoHashQuery("abc", "abd").joinWith(new GeoHashQuery("dce", "dcf"));
        Assert.fail("Exception was not thrown!");
    } catch (IllegalArgumentException expected) {
    }
}
Also used : GeoHashQuery(com.firebase.geofire.core.GeoHashQuery) Test(org.junit.Test)

Example 3 with GeoHashQuery

use of com.firebase.geofire.core.GeoHashQuery in project geofire-java by firebase.

the class GeoQuery method setupQueries.

private void setupQueries() {
    Set<GeoHashQuery> oldQueries = (this.queries == null) ? new HashSet<GeoHashQuery>() : this.queries;
    Set<GeoHashQuery> newQueries = GeoHashQuery.queriesAtLocation(center, radius);
    this.queries = newQueries;
    for (GeoHashQuery query : oldQueries) {
        if (!newQueries.contains(query)) {
            firebaseQueries.get(query).removeEventListener(this.childEventLister);
            firebaseQueries.remove(query);
            outstandingQueries.remove(query);
        }
    }
    for (final GeoHashQuery query : newQueries) {
        if (!oldQueries.contains(query)) {
            outstandingQueries.add(query);
            DatabaseReference databaseReference = this.geoFire.getDatabaseReference();
            Query firebaseQuery = databaseReference.orderByChild("g").startAt(query.getStartValue()).endAt(query.getEndValue());
            firebaseQuery.addChildEventListener(this.childEventLister);
            addValueToReadyListener(firebaseQuery, query);
            firebaseQueries.put(query, firebaseQuery);
        }
    }
    for (Map.Entry<String, LocationInfo> info : this.locationInfos.entrySet()) {
        LocationInfo oldLocationInfo = info.getValue();
        if (oldLocationInfo != null) {
            updateLocationInfo(oldLocationInfo.dataSnapshot, oldLocationInfo.location);
        }
    }
    // remove locations that are not part of the geo query anymore
    Iterator<Map.Entry<String, LocationInfo>> it = this.locationInfos.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry<String, LocationInfo> entry = it.next();
        if (!this.geoHashQueriesContainGeoHash(entry.getValue().geoHash)) {
            it.remove();
        }
    }
    checkAndFireReady();
}
Also used : GeoHashQuery(com.firebase.geofire.core.GeoHashQuery) Query(com.google.firebase.database.Query) GeoHashQuery(com.firebase.geofire.core.GeoHashQuery) DatabaseReference(com.google.firebase.database.DatabaseReference) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with GeoHashQuery

use of com.firebase.geofire.core.GeoHashQuery in project geofire-java by firebase.

the class GeoHashQueryTest method queryForGeoHash.

@Test
public void queryForGeoHash() {
    Assert.assertEquals(new GeoHashQuery("60", "6h"), GeoHashQuery.queryForGeoHash(new GeoHash("64m9yn96mx"), 6));
    Assert.assertEquals(new GeoHashQuery("0", "h"), GeoHashQuery.queryForGeoHash(new GeoHash("64m9yn96mx"), 1));
    Assert.assertEquals(new GeoHashQuery("64", "65"), GeoHashQuery.queryForGeoHash(new GeoHash("64m9yn96mx"), 10));
    Assert.assertEquals(new GeoHashQuery("640", "64h"), GeoHashQuery.queryForGeoHash(new GeoHash("6409yn96mx"), 11));
    Assert.assertEquals(new GeoHashQuery("64h", "64~"), GeoHashQuery.queryForGeoHash(new GeoHash("64m9yn96mx"), 11));
    Assert.assertEquals(new GeoHashQuery("6", "6~"), GeoHashQuery.queryForGeoHash(new GeoHash("6"), 10));
    Assert.assertEquals(new GeoHashQuery("64s", "64~"), GeoHashQuery.queryForGeoHash(new GeoHash("64z178"), 12));
    Assert.assertEquals(new GeoHashQuery("64z", "64~"), GeoHashQuery.queryForGeoHash(new GeoHash("64z178"), 15));
}
Also used : GeoHashQuery(com.firebase.geofire.core.GeoHashQuery) GeoHash(com.firebase.geofire.core.GeoHash) Test(org.junit.Test)

Aggregations

GeoHashQuery (com.firebase.geofire.core.GeoHashQuery)4 Test (org.junit.Test)3 GeoHash (com.firebase.geofire.core.GeoHash)2 DatabaseReference (com.google.firebase.database.DatabaseReference)1 Query (com.google.firebase.database.Query)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1