public override void ToGrid(double lon, double lat, out string gridValue) { char[] letters = new char[3]; double zoneLat = lat < -80 ? -80 : lat > 72 ? 72 : lat; letters[0] = _zoneLetters[Convert.ToInt32(Math.Floor((zoneLat + 80) / 8))]; double n = lon >= 180 ? lon - 180 : lon + 180; int zone = Convert.ToInt32(Math.Floor(n / 6)) + 1; Hemisphere hemisphere = lat >= 0 ? Hemisphere.North : Hemisphere.South; UTM utm = new UTM(zone, hemisphere); double x; double y; utm.ToProjected(lon, lat, out x, out y); double divisor = Math.Pow(10, 5 - _precision); x = Math.Round(x / divisor) * divisor; y = Math.Round(y / divisor) * divisor; int setNumber = (zone - 1) % 6; letters[1] = _columnLetters[(setNumber % 3) * 8 + Convert.ToInt32(Math.Floor(x / 100000)) - 1]; int rowOffset = setNumber % 2 == 1 ? 5 : 0; y %= 2000000; letters[2] = _rowLetters[(Convert.ToInt32(Math.Floor(y / 100000)) + rowOffset) % _rowLetters.Length]; x = (x % 100000) / divisor; y = (y % 100000) / divisor; string f = new String('0', _precision); gridValue = String.Format("{0}{1} {2} {3}", zone, new String(letters), x.ToString(f), y.ToString(f)); }
public override string ToGrid(Coordinate g) { char[] letters = new char[3]; double zoneLat = g.Y < -80 ? -80 : g.Y > 72 ? 72 : g.Y; letters[0] = _zoneLetters[Convert.ToInt32(Math.Floor((zoneLat + 80) / 8))]; double n = g.X >= 180 ? g.X - 180 : g.X + 180; int zone = Convert.ToInt32(Math.Floor(n / 6)) + 1; Hemisphere hemisphere = g.Y >= 0 ? Hemisphere.North : Hemisphere.South; UTM utm = new UTM(zone, hemisphere); Coordinate p = utm.ToProjected(g); double x = p.X; double y = p.Y; double divisor = Math.Pow(10, 5 - _precision); x = Math.Round(x / divisor) * divisor; y = Math.Round(y / divisor) * divisor; int setNumber = (zone - 1) % 6; letters[1] = _columnLetters[(setNumber % 3) * 8 + Convert.ToInt32(Math.Floor(x / 100000)) - 1]; int rowOffset = setNumber % 2 == 1 ? 5 : 0; y %= 2000000; letters[2] = _rowLetters[(Convert.ToInt32(Math.Floor(y / 100000)) + rowOffset) % _rowLetters.Length]; x = (x % 100000) / divisor; y = (y % 100000) / divisor; string f = new String('0', _precision); return(String.Format("{0}{1} {2} {3}", zone, new String(letters), x.ToString(f), y.ToString(f))); }