Exemplo n.º 1
0
        private static HashBox DecodeBox(string hash)
        {
            var isLongitude = true;

            var min = new GeoCoordinate(-90, -180);
            var max = new GeoCoordinate(90, 180);

            var shifts = new List <int>()
            {
                4, 3, 2, 1, 0
            };

            hash.ToList().ForEach(c =>
            {
                shifts.ForEach(shift =>
                {
                    var coordinate = ((Base32Codes[c] >> shift) & 1) == 1 ? min : max;

                    if (isLongitude)
                    {
                        coordinate.Longitude = (max.Longitude + min.Longitude) / 2;
                    }
                    else
                    {
                        coordinate.Latitude = (max.Latitude + min.Latitude) / 2;
                    }

                    isLongitude = !isLongitude;
                });

                Log.Debug($"char: {c} min: {min.PrettyPrint()} max: {max.PrettyPrint()}");
            });

            return(new HashBox(min, max));
        }
Exemplo n.º 2
0
        public static GeoCoordinate Convert(string locator)
        {
            if (locator is null)
            {
                throw new ArgumentNullException(nameof(locator));
            }

            if (!IsValid(locator))
            {
                throw new ArgumentException(nameof(locator));
            }

            var c = new GeoCoordinate(-90, -180);

            var divisor = 1d;

            var cnt = 0;

            foreach (var pair in Split(locator.ToUpperInvariant()))
            {
                cnt++;

                var isLetter = true;

                if (cnt == 1)
                {
                    divisor *= 18;
                }
                else if (cnt % 2 == 0)
                {
                    divisor *= 10;
                    isLetter = false;
                }
                else
                {
                    divisor *= 24;
                }

                int longValue = pair[0] - (isLetter ? 'A' : '0');
                int latValue  = pair[1] - (isLetter ? 'A' : '0');

                var longPart = (360d * longValue) / divisor;
                var latPart  = (180d * latValue) / divisor;

                c.Longitude += longPart;
                c.Latitude  += latPart;

                Log.Debug($"{pair[0]} (long): {longValue}*360/{divisor} = {longValue}*{360 / divisor} = {longPart}°");
                Log.Debug($"{pair[1]} (lat): {latValue}*180/{divisor} = {latValue}*{180 / divisor} = {latPart}°");
                Log.Debug($"--> {c}");
                Log.Debug($"--> {c.PrettyPrint()}");
            }

            return(c);
        }