Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions GeoFire/Implementation/GFBase32Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#import <Foundation/Foundation.h>

#define BITS_PER_BASE32_CHAR 5
#define BIT_MASK_FOR_BASE32_CHAR 0x1f

@interface GFBase32Utils : NSObject

Expand Down
1 change: 1 addition & 0 deletions GeoFire/Implementation/GFGeoHash.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

+ (GFGeoHash *)newWithLocation:(CLLocationCoordinate2D)location;
+ (GFGeoHash *)newWithLocation:(CLLocationCoordinate2D)location precision:(NSUInteger)precision;
+ (GFGeoHash *)newWithOrigin:(CLLocationCoordinate2D)origin destination:(CLLocationCoordinate2D)destination precision:(NSUInteger)precision;

- (id)initWithString:(NSString *)string;
+ (GFGeoHash *)newWithString:(NSString *)string;
Expand Down
27 changes: 27 additions & 0 deletions GeoFire/Implementation/GFGeoHash.m
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,33 @@ + (GFGeoHash *)newWithString:(NSString *)string
return [[GFGeoHash alloc] initWithString:string];
}

+ (NSUInteger)interleaveBitsOf:(NSUInteger)x withNumber:(NSUInteger)y
{
return ((x * 0x0101010101010101ULL & 0x8040201008040201ULL) * 0x0102040810204081ULL >> 49) & 0x5555 |
((y * 0x0101010101010101ULL & 0x8040201008040201ULL) * 0x0102040810204081ULL >> 48) & 0xAAAA;
}

+ (GFGeoHash *)newWithOrigin:(CLLocationCoordinate2D)origin destination:(CLLocationCoordinate2D)destination precision:(NSUInteger)precision
{
char buffer[2 * precision + 1];
buffer[2 * precision] = 0;

GFGeoHash *originHash = [GFGeoHash newWithLocation:origin precision:precision];
GFGeoHash *destinationHash = [GFGeoHash newWithLocation:destination precision:precision];
const char * const originHashBuffer = originHash.geoHashValue.UTF8String;
const char * const destinationHashBuffer = destinationHash.geoHashValue.UTF8String;

for (NSUInteger i = 0; i < precision; i++) {
const NSUInteger x = [GFBase32Utils base32CharacterToValue:originHashBuffer[i]];
const NSUInteger y = [GFBase32Utils base32CharacterToValue:destinationHashBuffer[i]];
const NSUInteger z = [GFGeoHash interleaveBitsOf:x withNumber:y];
buffer[2 * i] = [GFBase32Utils valueToBase32Character:(z & (BIT_MASK_FOR_BASE32_CHAR << BITS_PER_BASE32_CHAR)) >> BITS_PER_BASE32_CHAR];
buffer[2 * i + 1] = [GFBase32Utils valueToBase32Character:z & BIT_MASK_FOR_BASE32_CHAR];
}

return [GFGeoHash newWithString:[NSString stringWithUTF8String:buffer]];
}

- (id)initWithString:(NSString *)hashValue
{
if ([GFGeoHash isValidGeoHash:hashValue]) {
Expand Down
5 changes: 5 additions & 0 deletions GeoFire/Implementation/GFGeoHashQuery.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
+ (NSSet *)queriesForLocation:(CLLocationCoordinate2D)location radius:(double)radius;
+ (NSSet *)queriesForRegion:(MKCoordinateRegion)region;

+ (GFGeoHashQuery *)geoHashQueryWithGeoHash:(GFGeoHash *)geohash bits:(NSUInteger)bits;

+ (NSUInteger)bitsForRegion:(MKCoordinateRegion)region;
+ (double)bitsForLongitudeWithResolution:(double)resolution atLatitude:(CLLocationDegrees)latitude;

- (BOOL)containsGeoHash:(GFGeoHash *)hash;

- (BOOL)canJoinWith:(GFGeoHashQuery *)other;
Expand Down