void GeohashNeighbors(GeoHashBits hash, GeoHashNeighbors neighbors) { neighbors.East = hash; neighbors.West = hash; neighbors.North = hash; neighbors.South = hash; neighbors.SouthEast = hash; neighbors.SouthWest = hash; neighbors.NorthEast = hash; neighbors.NorthWest = hash; geohash_move_x(neighbors.East, 1); geohash_move_y(neighbors.East, 0); geohash_move_x(neighbors.West, -1); geohash_move_y(neighbors.West, 0); geohash_move_x(neighbors.South, 0); geohash_move_y(neighbors.South, -1); geohash_move_x(neighbors.North, 0); geohash_move_y(neighbors.North, 1); geohash_move_x(neighbors.NorthWest, -1); geohash_move_y(neighbors.NorthWest, 1); geohash_move_x(neighbors.NorthEast, 1); geohash_move_y(neighbors.NorthEast, 1); geohash_move_x(neighbors.SouthEast, 1); geohash_move_y(neighbors.SouthEast, -1); geohash_move_x(neighbors.SouthWest, -1); geohash_move_y(neighbors.SouthWest, -1); }
int GeohashDecode(GeoHashRange longRange, GeoHashRange latRange, GeoHashBits hash, GeoHashArea area) { if (Hashiszero(hash) || Rangeiszero(latRange) || Rangeiszero(longRange)) { return(0); } area.Hash = hash; ushort step = hash.Step; ulong hashSep = Deinterleave64(hash.Bits); /* hash = [LAT][LONG] */ double latScale = latRange.Max - latRange.Min; double longScale = longRange.Max - longRange.Min; ulong ilato = hashSep; /* get lat part of deinterleaved hash */ ulong ilono = hashSep >> 32; /* shift over to get long part of hash */ /* divide by 2**step. * Then, for 0-1 coordinate, multiply times scale and add * to the min to get the absolute coordinate. */ area.Latitude.Min = latRange.Min + (ilato * 1.0 / (1ul << step)) * latScale; area.Latitude.Max = latRange.Min + ((ilato + 1) * 1.0 / (1ul << step)) * latScale; area.Longitude.Min = longRange.Min + (ilono * 1.0 / (1ul << step)) * longScale; area.Longitude.Max = longRange.Min + ((ilono + 1) * 1.0 / (1ul << step)) * longScale; return(1); }
void geohash_move_y(GeoHashBits hash, short d) { if (d == 0) { return; } ulong x = hash.Bits & 0xaaaaaaaaaaaaaaaaUL; ulong y = hash.Bits & 0x5555555555555555UL; ulong zz = 0xaaaaaaaaaaaaaaaaUL >> (64 - hash.Step * 2); if (d > 0) { y = y + (zz + 1); } else { y = y | zz; y = y - (zz + 1); } y &= (0x5555555555555555UL >> (64 - hash.Step * 2)); hash.Bits = (x | y); }
void geohash_move_x(GeoHashBits hash, short d) { if (d == 0) { return; } ulong x = hash.Bits & 0xaaaaaaaaaaaaaaaaUL; ulong y = hash.Bits & 0x5555555555555555UL; ulong zz = 0x5555555555555555UL >> (64 - hash.Step * 2); if (d > 0) { x = x + (zz + 1); } else { x = x | zz; x = x - (zz + 1); } x &= (0xaaaaaaaaaaaaaaaaUL >> (64 - hash.Step * 2)); hash.Bits = (x | y); }
public RedisGeoHash(double[] latlon) { LatLon = latlon; var bits = new GeoHashBits(); var res = GeohashEncodeWgs84(Longitude, Latitude, RedisGeoHash.GeoStepMax, ref bits); GeoHashBits = bits; }
int GeohashDecodeToLongLatType(GeoHashBits hash, double[] xy) { GeoHashArea area = new GeoHashArea(); if (GeohashDecodeType(hash, area) == 0) { return(0); } return(GeohashDecodeAreaToLongLat(area, xy)); }
int GeohashEncode(GeoHashRange longRange, GeoHashRange latRange, double longitude, double latitude, ushort step, ref GeoHashBits hash) { /* Check basic arguments sanity. */ if (step > 32 || step == 0 || Rangepiszero(latRange) || Rangepiszero(longRange)) { return(0); } /* Return an error when trying to index outside the supported * constraints. */ if (longitude > GeoLongMax) { return(0); } if (longitude < GeoLongMin) { return(0); } if (latitude > GeoLatMax) { return(0); } if (latitude < GeoLatMin) { return(0); } hash.Bits = 0; hash.Step = step; if (latitude < latRange.Min || latitude > latRange.Max || longitude < longRange.Min || longitude > longRange.Max) { return(0); } double latOffset = (latitude - latRange.Min) / (latRange.Max - latRange.Min); double longOffset = (longitude - longRange.Min) / (longRange.Max - longRange.Min); /* convert to fixed point based on the step size */ latOffset *= (1UL << step); longOffset *= (1UL << step); hash.Bits = Interleave64((uint)latOffset, (uint)longOffset); return(1); }
private bool Hashiszero(GeoHashBits r) { return(r.Bits == 0 && r.Step == 0); }
int GeohashDecodeToLongLatWgs84(GeoHashBits hash, double[] xy) { return(GeohashDecodeToLongLatType(hash, xy)); }
int GeohashDecodeWgs84(GeoHashBits hash, GeoHashArea area) { return(GeohashDecodeType(hash, area)); }
int GeohashDecodeType(GeoHashBits hash, GeoHashArea area) { GeoHashRange[] r = new GeoHashRange[2]; GeohashGetCoordRange(ref r[0], ref r[1]); return(GeohashDecode(r[0], r[1], hash, area)); }
public int GeohashEncodeWgs84(double longitude, double latitude, ushort step, ref GeoHashBits hash) { return(GeohashEncodeType(longitude, latitude, step, ref hash)); }
int GeohashEncodeType(double longitude, double latitude, ushort step, ref GeoHashBits hash) { GeoHashRange[] r = new GeoHashRange[2]; GeohashGetCoordRange(ref r[0], ref r[1]); return(GeohashEncode(r[0], r[1], longitude, latitude, step, ref hash)); }