private void OnConvertLatLon(object sender, EventArgs e) { try { int zone; bool northp; double x, y; string str; double lat = Double.Parse(m_latitudeTextBox.Text); double lon = Double.Parse(m_longitudeTextBox.Text); UTMUPS.Forward(lat, lon, out zone, out northp, out x, out y, (int)UTMUPS.ZoneSpec.STANDARD, true); m_utmPoleCheckBox.Checked = northp; m_utmXTextBox.Text = x.ToString(); m_utmYTextBox.Text = y.ToString(); m_utmZoneTextBox.Text = zone.ToString(); MGRS.Forward(zone, northp, x, y, 8, out str); m_mgrsTextBox.Text = str; OSGB.Forward(lat, lon, out x, out y); m_osgbXTextBox.Text = x.ToString(); m_osgbYTextBox.Text = y.ToString(); OSGB.GridReference(x, y, 8, out str); m_osgbTextBox.Text = str; } catch (Exception xcpt) { MessageBox.Show(xcpt.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
static void Main(string[] args) { try { // See also example-GeoCoords.cpp { // Sample forward calculation double lat = 33.3, lon = 44.4; // Baghdad int zone; bool northp; double x, y; UTMUPS.Forward(lat, lon, out zone, out northp, out x, out y, -1, true); string zonestr = UTMUPS.EncodeZone(zone, northp, true); Console.WriteLine(String.Format("{0} {1} {2}", zonestr, x, y)); } { // Sample reverse calculation string zonestr = "38N"; int zone; bool northp; UTMUPS.DecodeZone(zonestr, out zone, out northp); double x = 444e3, y = 3688e3; double lat, lon; UTMUPS.Reverse(zone, northp, x, y, out lat, out lon, true); Console.WriteLine(String.Format("{0} {1}", lat, lon)); } } catch (GeographicErr e) { Console.WriteLine(String.Format("Caught exception: {0}", e.Message)); } }
public void TestForward(double lat, double lon, string expeted) { var(zone, northp, x, y) = UTMUPS.Forward(lat, lon); var mgrs = MGRS.Forward(zone, northp, x, y, 5); Assert.AreEqual(expeted, mgrs); }
static void Main(string[] args) { try { // See also example-GeoCoords.cpp { // Sample forward calculation double lat = 33.3, lon = 44.4; // Baghdad int zone; bool northp; double x, y; UTMUPS.Forward(lat, lon, out zone, out northp, out x, out y, -1, true); string mgrs; MGRS.Forward(zone, northp, x, y, lat, 5, out mgrs); Console.WriteLine(mgrs); } { // Sample reverse calculation string mgrs = "38SMB4488"; int zone, prec; bool northp; double x, y; MGRS.Reverse(mgrs, out zone, out northp, out x, out y, out prec, true); double lat, lon; UTMUPS.Reverse(zone, northp, x, y, out lat, out lon, true); Console.WriteLine(String.Format("Latitude: {0} Longitude: {1}", lat, lon)); } } catch (GeographicErr e) { Console.WriteLine(String.Format("Caught exception: {0}", e.Message)); } }
private void OnValidate(object sender, EventArgs e) { try { string str; int prec, zone, zout; bool northp; double x, y, x1, y1, gamma, k, lat, lon; OSGB.Forward(52.0, -2.0, out x, out y, out gamma, out k); OSGB.Forward(52.0, -2.0, out x1, out y1); if (x != x1 || y != y1) { throw new Exception("Error in OSGB.Forward"); } OSGB.Reverse(x, y, out lat, out lon, out gamma, out k); OSGB.Reverse(x, y, out x1, out y1); if (lat != x1 || lon != y1) { throw new Exception("Error in OSGB.Reverse"); } OSGB.GridReference(x, y, 8, out str); OSGB.GridReference(str, out x1, out y1, out prec, true); UTMUPS.StandardZone(32.0, -80.0, (int)UTMUPS.ZoneSpec.STANDARD); UTMUPS.UTMShift(); UTMUPS.StandardZone(32.0, -86.0, (int)UTMUPS.ZoneSpec.STANDARD); UTMUPS.Forward(32.0, -86.0, out zone, out northp, out x, out y, out gamma, out k, (int)UTMUPS.ZoneSpec.STANDARD, true); UTMUPS.Forward(32.0, -86.0, out zone, out northp, out x1, out y1, (int)UTMUPS.ZoneSpec.STANDARD, true); if (x != x1 || y != y1) { throw new Exception("Error in UTMUPS.Forward"); } UTMUPS.Reverse(zone, northp, x, y, out lat, out lon, out gamma, out k, true); UTMUPS.Reverse(zone, northp, x, y, out x1, out y1, true); if (lat != x1 || lon != y1) { throw new Exception("Error in UTMUPS.Reverse"); } UTMUPS.Transfer(zone, northp, x, y, zone + 1, true, out x1, out y1, out zout); str = UTMUPS.EncodeZone(zone, northp, true); prec = UTMUPS.EncodeEPSG(zone, northp); UTMUPS.DecodeZone(str, out zone, out northp); UTMUPS.DecodeEPSG(prec, out zone, out northp); MGRS.Forward(zone, northp, x, y, 8, out str); MGRS.Forward(zone, northp, x, y, 32.0, 8, out str); MGRS.Reverse(str, out zone, out northp, out x, out y, out prec, true); MessageBox.Show("No errors detected", "OK", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception xcpt) { MessageBox.Show(xcpt.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
/// <summary> /// Perform some checks on the <see cref="UTMUPS"/> coordinates on this ellipsoid. /// Throw an error if any of the assumptions made in the <see cref="MGRS"/> class is not true. /// This check needs to be carried out if the ellipsoid parameters (or the UTM/UPS scales) are ever changed. /// </summary> public static void Check() { double t = tile_; var(_, lon) = UTMUPS.Reverse(31, true, 1 * t, 0 * t); if (!(lon < 0)) { throw new GeographicException("MGRS::Check: equator coverage failure"); } var(lat, _) = UTMUPS.Reverse(31, true, 1 * t, 95 * t); if (!(lat > 84)) { throw new GeographicException("MGRS::Check: UTM doesn't reach latitude = 84"); } (lat, _) = UTMUPS.Reverse(31, false, 1 * t, 10 * t); if (!(lat < -80)) { throw new GeographicException("MGRS::Check: UTM doesn't reach latitude = -80"); } var(_, _, x, _) = UTMUPS.Forward(56, 3, 32); if (!(x > 1 * t)) { throw new GeographicException("MGRS::Check: Norway exception creates a gap"); } (_, _, x, _) = UTMUPS.Forward(72, 21, 35); if (!(x > 1 * t)) { throw new GeographicException("MGRS::Check: Svalbard exception creates a gap"); } (lat, _) = UTMUPS.Reverse(0, true, 20 * t, 13 * t); if (!(lat < 84)) { throw new GeographicException("MGRS::Check: North UPS doesn't reach latitude = 84"); } (lat, _) = UTMUPS.Reverse(0, false, 20 * t, 8 * t); if (!(lat > -80)) { throw new GeographicException("MGRS::Check: South UPS doesn't reach latitude = -80"); } var bandchecks = tab.Length / 3; for (int i = 0; i < bandchecks; ++i) { (lat, _) = UTMUPS.Reverse(38, true, tab[3 * i + 1] * t, tab[3 * i + 2] * t); if (!(LatitudeBand(lat) == tab[3 * i + 0])) { throw new GeographicException( $"MGRS::Check: Band error, b = {tab[3 * i + 0]}, x = {tab[3 * i + 1]}00km, y = {tab[3 * i + 2]}00km"); } } }