Beispiel #1
0
        public void Test1()
        {
            var key       = "abc";
            var base32Key = Base32Utils.Encode(key);

            Console.WriteLine($"key={key},base32Key={base32Key}");
        }
        public static GeoHashQuery queryForGeoHash(GeoHash geohash, int bits)
        {
            string hash      = geohash.getGeoHashString();
            int    precision = (int)Math.Ceiling((double)bits / Base32Utils.BITS_PER_BASE32_CHAR);

            if (hash.Length < precision)
            {
                return(new GeoHashQuery(hash, hash + "~"));
            }
            hash = hash.Substring(0, precision);
            string baseS           = hash.Substring(0, hash.Length - 1);
            int    lastValue       = Base32Utils.base32CharToValue(hash[(hash.Length - 1)]);
            int    significantBits = bits - (baseS.Length * Base32Utils.BITS_PER_BASE32_CHAR);
            int    unusedBits      = (Base32Utils.BITS_PER_BASE32_CHAR - significantBits);
            // delete unused bits
            int    startValue = (lastValue >> unusedBits) << unusedBits;
            int    endValue   = startValue + (1 << unusedBits);
            string startHash  = baseS + Base32Utils.valueToBase32Char(startValue);
            string endHash;

            if (endValue > 31)
            {
                endHash = baseS + "~";
            }
            else
            {
                endHash = baseS + Base32Utils.valueToBase32Char(endValue);
            }
            return(new GeoHashQuery(startHash, endHash));
        }
Beispiel #3
0
 public GeoHash(string hash)
 {
     if (hash.Length == 0 || !Base32Utils.IsValidBase32String(hash))
     {
         throw new ArgumentException("Not a valid geoHash: " + hash);
     }
     _geoHash = hash;
 }
Beispiel #4
0
 public GeoHash(string hash)
 {
     if (hash.Length == 0 || !Base32Utils.isValidBase32string(hash))
     {
         throw new System.Exception("Not a valid geoHash: " + hash);
     }
     this.geoHash = hash;
 }
Beispiel #5
0
        public GeoHash(double latitude, double longitude, int precision = DefaultPrecision)
        {
            if (precision < 1)
            {
                throw new ArgumentException("Precision of GeoHash must be larger than zero!");
            }
            if (precision > MaxPrecision)
            {
                throw new ArgumentException("Precision of a GeoHash must be less than " + (MaxPrecision + 1) + "!");
            }
            if (!GeoUtils.CoordinatesValid(latitude, longitude))
            {
                throw new ArgumentException(string.Format("Not valid location coordinates: [%f, %f]", latitude, longitude));
            }
            double[] longitudeRange = { -180, 180 };
            double[] latitudeRange  = { -90, 90 };

            var buffer = new char[precision];

            for (var i = 0; i < precision; i++)
            {
                var hashValue = 0;
                for (var j = 0; j < Base32Utils.BitsPerBase32Char; j++)
                {
                    var even  = (i * Base32Utils.BitsPerBase32Char + j) % 2 == 0;
                    var val   = even ? longitude : latitude;
                    var range = even ? longitudeRange : latitudeRange;
                    var mid   = (range[0] + range[1]) / 2;
                    if (val > mid)
                    {
                        hashValue = (hashValue << 1) + 1;
                        range[0]  = mid;
                    }
                    else
                    {
                        hashValue <<= 1;
                        range[1]    = mid;
                    }
                }
                buffer[i] = Base32Utils.ValueToBase32Char(hashValue);
            }
            _geoHash = new string(buffer);
        }
Beispiel #6
0
        public GeoHash(double latitude, double longitude, int precision)
        {
            if (precision < 1)
            {
                throw new System.Exception("Precision of GeoHash must be larger than zero!");
            }
            if (precision > MAX_PRECISION)
            {
                throw new System.Exception("Precision of a GeoHash must be less than " + (MAX_PRECISION + 1) + "!");
            }
            if (!GeoLocation.coordinatesValid(latitude, longitude))
            {
                throw new System.Exception(string.Format("Not valid location coordinates: [{0}, {1}]", latitude, longitude));
            }
            double[] longitudeRange = { -180, 180 };
            double[] latitudeRange  = { -90, 90 };

            char[] buffer = new char[precision];

            for (int i = 0; i < precision; i++)
            {
                int hashValue = 0;
                for (int j = 0; j < Base32Utils.BITS_PER_BASE32_CHAR; j++)
                {
                    bool     even  = (((i * Base32Utils.BITS_PER_BASE32_CHAR) + j) % 2) == 0;
                    double   val   = even ? longitude : latitude;
                    double[] range = even ? longitudeRange : latitudeRange;
                    double   mid   = (range[0] + range[1]) / 2;
                    if (val > mid)
                    {
                        hashValue = (hashValue << 1) + 1;
                        range[0]  = mid;
                    }
                    else
                    {
                        hashValue = (hashValue << 1);
                        range[1]  = mid;
                    }
                }
                buffer[i] = Base32Utils.valueToBase32Char(hashValue);
            }
            this.geoHash = new string(buffer);
        }
        public static GeoHashQuery QueryForGeoHash(GeoHash geohash, int bits)
        {
            var hash      = geohash.GetGeoHashString();
            var precision = (int)Math.Ceiling((double)bits / Base32Utils.BitsPerBase32Char);

            if (hash.Length < precision)
            {
                return(new GeoHashQuery(hash, hash + "~"));
            }
            hash = hash.Substring(0, precision);
            var hashBase        = hash.Substring(0, hash.Length - 1);
            var lastValue       = Base32Utils.Base32CharToValue(hash[hash.Length - 1]);
            var significantBits = bits - hashBase.Length * Base32Utils.BitsPerBase32Char;
            var unusedBits      = Base32Utils.BitsPerBase32Char - significantBits;
            // delete unused bits
            var startValue = (lastValue >> unusedBits) << unusedBits;
            var endValue   = startValue + (1 << unusedBits);
            var startHash  = hashBase + Base32Utils.ValueToBase32Char(startValue);
            var endHash    = endValue > 31 ? hashBase + "~" : hashBase + Base32Utils.ValueToBase32Char(endValue);

            return(new GeoHashQuery(startHash, endHash));
        }