예제 #1
0
        public bool IsMatch(GeoCoordinate source, GeoCoordinate comparer, GeoHashPrecision precision)
        {
            var sourceGeoHash   = _encoder.Encode(source);
            var comparerGeoHash = _encoder.Encode(comparer);

            return(IsMatch(sourceGeoHash, comparerGeoHash, precision));
        }
예제 #2
0
        public void EncodesAsExpected(double lat, double lng, GeoHashPrecision precision)
        {
            var expectedOutput = BuckingHamPalaceGeoHash.Substring(0, (int)precision);

            var encoder = new GeoHashEncoder <string>();
            var encoded = encoder.Encode(lat, lng, precision);

            Assert.Equal(encoded, expectedOutput);
        }
예제 #3
0
        public void EncodesAsExpected(GeoHashPrecision precision, int expectedMatches)
        {
            var encoder = new GeoHashEncoder <Location>();
            var matcher = new GeoHashMatcher <Location>();

            var geoHashedCities = encoder.Encode(_aussieSuburbs);
            var actualMatches   = matcher.GetMatches(CoogeeGeoHash, geoHashedCities, precision).Count();

            Assert.Equal(actualMatches, expectedMatches);
        }
예제 #4
0
        public bool IsMatch(string source, string comparer, GeoHashPrecision precision)
        {
            if (source.Length < (int)precision)
            {
                throw new ArgumentOutOfRangeException($"{nameof(source)} should be greater than or equal to {nameof(precision)}.");
            }

            if (comparer.Length < (int)precision)
            {
                throw new ArgumentOutOfRangeException($"{nameof(comparer)} should be greater than or equal to {nameof(precision)}.");
            }

            if (string.IsNullOrEmpty(source) || string.IsNullOrEmpty(comparer))
            {
                throw new ArgumentOutOfRangeException($"{nameof(source)} and {nameof(comparer)} cannot be null.");
            }

            if (source.Length < (int)GeoHashPrecision.MinimumPrecision ||
                source.Length > (int)GeoHashPrecision.MaximumPrecision ||
                comparer.Length < (int)GeoHashPrecision.MinimumPrecision ||
                comparer.Length > (int)GeoHashPrecision.MaximumPrecision)
            {
                throw new ArgumentOutOfRangeException(
                          $"{nameof(source)} and {nameof(comparer)} must have a length between 1 and 12.");
            }


            /*
             * If the source length is greater than the precision, we'll truncate it
             * so that we can do a "StartsWith" to determine if it's a match.
             */
            if (source.Length > (int)precision)
            {
                source = source.Substring(0, (int)precision);
            }

            return(comparer.StartsWith(source, StringComparison.OrdinalIgnoreCase));
        }
 public GeoHashAggregationDescriptor <T> GeoHashPrecision(GeoHashPrecision precision)
 {
     Self.Precision = precision;
     return(this);
 }
예제 #6
0
        public IEnumerable <KeyValuePair <TKey, string> > GetMatches(string source, IEnumerable <KeyValuePair <TKey, string> > comparers, GeoHashPrecision precision)
        {
            if (comparers == null)
            {
                throw new ArgumentNullException($"{nameof(comparers)} cannot be null.");
            }

            source = source.Substring(0, (int)precision);

            return(comparers.AsParallel().Where(x => IsMatch(source, x.Value, precision)));
        }
예제 #7
0
 public GeoHashGridAggregationDescriptor <T> GeoHashPrecision(GeoHashPrecision precision) =>
 Assign(a => a.Precision = precision);
예제 #8
0
 public IEnumerable <string> Encode(IEnumerable <GeoCoordinate> geoCoords, GeoHashPrecision precision = GeoHashPrecision.MaximumPrecision)
 {
     return(geoCoords.AsParallel().Select(geoCoord => Encode(geoCoord, precision)));
 }
예제 #9
0
        public IDictionary <TKey, string> Encode(IDictionary <TKey, GeoCoordinate> geoCoords, GeoHashPrecision precision = GeoHashPrecision.MaximumPrecision)
        {
            var kvp = geoCoords.Cast <KeyValuePair <TKey, GeoCoordinate> >();

            return(Encode(kvp, precision).ToDictionary(x => x.Key, x => x.Value));
        }
예제 #10
0
 public IEnumerable <KeyValuePair <TKey, string> > Encode(IEnumerable <KeyValuePair <TKey, GeoCoordinate> > geoCoords, GeoHashPrecision precision = GeoHashPrecision.MaximumPrecision)
 {
     return(geoCoords.AsParallel().Select(geoCoord => new KeyValuePair <TKey, string>(geoCoord.Key, Encode(geoCoord.Value, precision))));
 }
예제 #11
0
 public string Encode(GeoCoordinate geoCoord, GeoHashPrecision precision = GeoHashPrecision.MaximumPrecision)
 {
     return(Encode(geoCoord.Latitude, geoCoord.Longitude, precision));
 }
예제 #12
0
        public string Encode(double latitude, double longitude, GeoHashPrecision precision = GeoHashPrecision.Level12)
        {
            if (latitude < -90 || latitude > 90)
                throw new ArgumentOutOfRangeException($"{nameof(latitude)} must be between -90 and 90.");

            if (longitude < -180 || longitude > 180)
                throw new ArgumentOutOfRangeException($"{nameof(longitude)} must be between -180 and 180.");

            double latMin = -90, latMax = 90;
            double lngMin = -180, lngMax = 180;

            var geoHash = new StringBuilder();
            var isEven = true;
            int bit = 0, ch = 0;

            while (geoHash.Length < (int)precision)
            {
                double midPoint;
                if (isEven)
                {
                    midPoint = (lngMin + lngMax) / 2;
                    if (longitude > midPoint)
                    {
                        ch |= _bits[bit];
                        lngMin = midPoint;
                    }
                    else
                    {
                        lngMax = midPoint;
                    }
                }
                else
                {
                    midPoint = (latMin + latMax) / 2;
                    if (latitude > midPoint)
                    {
                        ch |= _bits[bit];
                        latMin = midPoint;
                    }
                    else
                    {
                        latMax = midPoint;
                    }
                }

                isEven = !isEven;

                if (bit < 4)
                {
                    bit++;
                }
                else
                {
                    geoHash.Append(_base32[ch]);
                    bit = 0;
                    ch = 0;
                }
            }

            return geoHash.ToString();
        }