use of com.firebase.geofire.core.GeoHash 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);
}
}
}
}
use of com.firebase.geofire.core.GeoHash in project geofire-java by firebase.
the class GeoFire method setLocation.
/**
* Sets the location for a given key.
*
* @param key The key to save the location for
* @param location The location of this key
* @param completionListener A listener that is called once the location was successfully saved on the server or an
* error occurred
*/
public void setLocation(final String key, final GeoLocation location, final CompletionListener completionListener) {
if (key == null) {
throw new NullPointerException();
}
DatabaseReference keyRef = this.getDatabaseRefForKey(key);
GeoHash geoHash = new GeoHash(location);
Map<String, Object> updates = new HashMap<>();
updates.put("g", geoHash.getGeoHashString());
updates.put("l", Arrays.asList(location.latitude, location.longitude));
if (completionListener != null) {
keyRef.setValue(updates, geoHash.getGeoHashString(), new DatabaseReference.CompletionListener() {
@Override
public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
completionListener.onComplete(key, databaseError);
}
});
} else {
keyRef.setValue(updates, geoHash.getGeoHashString());
}
}
use of com.firebase.geofire.core.GeoHash in project geofire-java by firebase.
the class GeoQuery method childRemoved.
private void childRemoved(DataSnapshot dataSnapshot) {
final String key = dataSnapshot.getKey();
final LocationInfo info = this.locationInfos.get(key);
if (info != null) {
this.geoFire.getDatabaseRefForKey(key).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(final DataSnapshot dataSnapshot) {
synchronized (GeoQuery.this) {
GeoLocation location = GeoFire.getLocationValue(dataSnapshot);
GeoHash hash = (location != null) ? new GeoHash(location) : null;
if (hash == null || !GeoQuery.this.geoHashQueriesContainGeoHash(hash)) {
final LocationInfo info = locationInfos.remove(key);
if (info != null && info.inGeoQuery) {
for (final GeoQueryDataEventListener listener : GeoQuery.this.eventListeners) {
GeoQuery.this.geoFire.raiseEvent(new Runnable() {
@Override
public void run() {
listener.onDataExited(info.dataSnapshot);
}
});
}
}
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
// tough luck
}
});
}
}
use of com.firebase.geofire.core.GeoHash 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));
}
use of com.firebase.geofire.core.GeoHash in project geofire-java by firebase.
the class GeoHashTest method customPrecision.
@Test
public void customPrecision() {
Assert.assertEquals(new GeoHash("000000"), new GeoHash(-90, -180, 6));
Assert.assertEquals(new GeoHash("zzzzzzzzzzzzzzzzzzzz"), new GeoHash(90, 180, 20));
Assert.assertEquals(new GeoHash("p"), new GeoHash(-90, 180, 1));
Assert.assertEquals(new GeoHash("bpbpb"), new GeoHash(90, -180, 5));
Assert.assertEquals(new GeoHash("9q8yywe5"), new GeoHash(37.7853074, -122.4054274, 8));
Assert.assertEquals(new GeoHash("dqcjf17sy6cppp8vfn"), new GeoHash(38.98719, -77.250783, 18));
Assert.assertEquals(new GeoHash("tj4p5gerfzqu"), new GeoHash(29.3760648, 47.9818853, 12));
Assert.assertEquals(new GeoHash("u"), new GeoHash(78.216667, 15.55, 1));
Assert.assertEquals(new GeoHash("4qpzmre"), new GeoHash(-54.933333, -67.616667, 7));
Assert.assertEquals(new GeoHash("4w2kg3s54"), new GeoHash(-54, -67, 9));
}
Aggregations