/// <summary> /// The function convertToGeodetic converts UPS (hemisphere, easting, /// and northing) coordinates to geodetic (latitude and longitude) coordinates /// according to the current ellipsoid parameters. If any errors occur, an /// exception is thrown with a description of the error. /// </summary> /// <param name="upsCoordinates"></param> /// <returns></returns> public GeodeticCoordinates convertToGeodetic(UPSCoordinates upsCoordinates) { /* * hemisphere : Hemisphere either 'N' or 'S' (input) * easting : Easting/X in meters (input) * northing : Northing/Y in meters (input) * latitude : Latitude in radians (output) * longitude : Longitude in radians (output) */ string errorStatus = ""; char hemisphere = upsCoordinates.getHemisphere(); double easting = upsCoordinates.getEasting(); double northing = upsCoordinates.getNorthing(); if ((hemisphere != 'N') && (hemisphere != 'S')) { errorStatus += ErrorMessages.hemisphere; } if ((easting < MIN_EAST_NORTH) || (easting > MAX_EAST_NORTH)) { errorStatus += ErrorMessages.easting; } if ((northing < MIN_EAST_NORTH) || (northing > MAX_EAST_NORTH)) { errorStatus += ErrorMessages.northing; } //this.UPS_Origin_Latitude = MAX_ORIGIN_LAT; //if (hemisphere == 'N') //{ // UPS_Origin_Latitude = MAX_ORIGIN_LAT; //} //else if (hemisphere == 'S') //{ // UPS_Origin_Latitude = -MAX_ORIGIN_LAT; //} if (errorStatus.Length > 0) { throw new ArgumentException(errorStatus); } PolarStereographic polarStereographic = new PolarStereographic(this.semiMajorAxis, this.flattening, this.UPS_Origin_Longitude, 0.994, hemisphere, this.UPS_False_Easting, this.UPS_False_Northing); GeodeticCoordinates geodeticCoordinates = polarStereographic.convertToGeodetic(new MapProjectionCoordinates(CoordinateType.Enum.polarStereographicStandardParallel, easting, northing)); double latitude = geodeticCoordinates.latitude; if ((latitude < 0) && (latitude >= (MAX_SOUTH_LAT + EPSILON))) { throw new ArgumentException(ErrorMessages.latitude); } if ((latitude >= 0) && (latitude < (MIN_NORTH_LAT - EPSILON))) { throw new ArgumentException(ErrorMessages.latitude); } return(geodeticCoordinates); }
/// <summary> /// The function convertToGeodetic converts UPS (hemisphere, easting, /// and northing) coordinates to geodetic (latitude and longitude) coordinates /// according to the current ellipsoid parameters. If any errors occur, an /// exception is thrown with a description of the error. /// </summary> /// <param name="upsCoordinates"></param> /// <returns></returns> public GeodeticCoordinates convertToGeodetic(UPSCoordinates upsCoordinates) { /* * hemisphere : Hemisphere either 'N' or 'S' (input) * easting : Easting/X in meters (input) * northing : Northing/Y in meters (input) * latitude : Latitude in radians (output) * longitude : Longitude in radians (output) */ string errorStatus = ""; char hemisphere = upsCoordinates.getHemisphere(); double easting = upsCoordinates.getEasting(); double northing = upsCoordinates.getNorthing(); if ((hemisphere != 'N') && (hemisphere != 'S')) { errorStatus += ErrorMessages.hemisphere; } if ((easting < MIN_EAST_NORTH) || (easting > MAX_EAST_NORTH)) { errorStatus += ErrorMessages.easting; } if ((northing < MIN_EAST_NORTH) || (northing > MAX_EAST_NORTH)) { errorStatus += ErrorMessages.northing; } //this.UPS_Origin_Latitude = MAX_ORIGIN_LAT; //if (hemisphere == 'N') //{ // UPS_Origin_Latitude = MAX_ORIGIN_LAT; //} //else if (hemisphere == 'S') //{ // UPS_Origin_Latitude = -MAX_ORIGIN_LAT; //} if (errorStatus.Length > 0) { throw new ArgumentException(errorStatus); } PolarStereographic polarStereographic = new PolarStereographic(this.semiMajorAxis, this.flattening, this.UPS_Origin_Longitude, 0.994, hemisphere, this.UPS_False_Easting, this.UPS_False_Northing); GeodeticCoordinates geodeticCoordinates = polarStereographic.convertToGeodetic(new MapProjectionCoordinates(CoordinateType.Enum.polarStereographicStandardParallel, easting, northing)); double latitude = geodeticCoordinates.latitude; if ((latitude < 0) && (latitude >= (MAX_SOUTH_LAT + EPSILON))) { throw new ArgumentException(ErrorMessages.latitude); } if ((latitude >= 0) && (latitude < (MIN_NORTH_LAT - EPSILON))) { throw new ArgumentException(ErrorMessages.latitude); } return geodeticCoordinates; }
public MGRSorUSNGCoordinates fromUPS(UPSCoordinates upsCoordinates, long precision) { /* * The function fromUPS converts UPS (hemisphere, easting, * and northing) coordinates to an MGRS coordinate string according to * the current ellipsoid parameters. * * hemisphere : Hemisphere either 'N' or 'S' (input) * easting : Easting/X in meters (input) * northing : Northing/Y in meters (input) * precision : Precision level of MGRS string (input) * MGRSString : MGRS coordinate string (output) */ double false_easting; /* False easting for 2nd letter */ double false_northing; /* False northing for 3rd letter */ double grid_easting; /* Easting used to derive 2nd letter of MGRS */ double grid_northing; /* Northing used to derive 3rd letter of MGRS */ long ltr2_low_value; /* 2nd letter range - low number */ int[] letters = new int[3]; /* Number location of 3 letters in alphabet */ double divisor = 0.0; int index = 0; char hemisphere = upsCoordinates.getHemisphere(); double easting = upsCoordinates.getEasting(); double northing = upsCoordinates.getNorthing(); easting = Math.Round(easting); northing = Math.Round(northing); divisor = Math.Pow(10.0, (5.0 - precision)); easting = (long)(easting / divisor) * divisor; northing = (long)(northing / divisor) * divisor; if (hemisphere == 'N') { if (easting >= TWOMIL) { letters[0] = LETTER_Z; } else { letters[0] = LETTER_Y; } index = letters[0] - 22; ltr2_low_value = UPS_Constant_Table[index].ltr2_low_value; false_easting = UPS_Constant_Table[index].false_easting; false_northing = UPS_Constant_Table[index].false_northing; } else { if (easting >= TWOMIL) { letters[0] = LETTER_B; } else { letters[0] = LETTER_A; } ltr2_low_value = UPS_Constant_Table[letters[0]].ltr2_low_value; false_easting = UPS_Constant_Table[letters[0]].false_easting; false_northing = UPS_Constant_Table[letters[0]].false_northing; } grid_northing = northing; grid_northing = grid_northing - false_northing; letters[2] = (int)(grid_northing / ONEHT); if (letters[2] > LETTER_H) { letters[2] = letters[2] + 1; } if (letters[2] > LETTER_N) { letters[2] = letters[2] + 1; } grid_easting = easting; grid_easting = grid_easting - false_easting; letters[1] = (int)(ltr2_low_value + ((long)(grid_easting / ONEHT))); if (easting < TWOMIL) { if (letters[1] > LETTER_L) { letters[1] = letters[1] + 3; } if (letters[1] > LETTER_U) { letters[1] = letters[1] + 2; } } else { if (letters[1] > LETTER_C) { letters[1] = letters[1] + 2; } if (letters[1] > LETTER_H) { letters[1] = letters[1] + 1; } if (letters[1] > LETTER_L) { letters[1] = letters[1] + 3; } } return new MGRSorUSNGCoordinates(CoordinateType.Enum.militaryGridReferenceSystem, makeMGRSString(0, letters, easting, northing, precision)); }
public UPSCoordinates convertToUPS(MGRSorUSNGCoordinates mgrsorUSNGCoordinates) { /* * The function convertToUPS converts an MGRS coordinate string * to UPS (hemisphere, easting, and northing) coordinates, according * to the current ellipsoid parameters. If any errors occur, an * exception is thrown with a description of the error. * * MGRSString : MGRS coordinate string (input) * hemisphere : Hemisphere either 'N' or 'S' (output) * easting : Easting/X in meters (output) * northing : Northing/Y in meters (output) */ long zone = 0; long[] letters = new long[3]; long precision = 0; double mgrs_easting = 0.0; double mgrs_northing = 0.0; UPSCoordinates upsCoordinates = new UPSCoordinates(); breakMGRSString(mgrsorUSNGCoordinates.MGRSString, ref zone, ref letters, ref mgrs_easting, ref mgrs_northing, ref precision); if (zone == 0) { upsCoordinates = toUPS(letters, mgrs_easting, mgrs_northing); } else { upsCoordinates = ups.convertFromGeodetic(utm.convertToGeodetic(toUTM(zone, letters, mgrs_easting, mgrs_northing, precision))); } return upsCoordinates; }
public MGRSorUSNGCoordinates convertFromUPS(ref UPSCoordinates upsCoordinates, long precision) { /* * The function convertFromUPS converts UPS (hemisphere, easting, * and northing) coordinates to an MGRS coordinate string according to * the current ellipsoid parameters. If any errors occur, an * exception is thrown with a description of the error. * * hemisphere : Hemisphere either 'N' or 'S' (input) * easting : Easting/X in meters (input) * northing : Northing/Y in meters (input) * precision : Precision level of MGRS string (input) * MGRSString : MGRS coordinate string (output) */ string errorStatus = ""; char hemisphere = upsCoordinates.getHemisphere(); double easting = upsCoordinates.getEasting(); double northing = upsCoordinates.getNorthing(); if ((hemisphere != 'N') && (hemisphere != 'S')) { errorStatus += ErrorMessages.hemisphere; } if ((easting < MIN_EAST_NORTH) || (easting > MAX_EAST_NORTH)) { errorStatus += ErrorMessages.easting; } if ((northing < MIN_EAST_NORTH) || (northing > MAX_EAST_NORTH)) { errorStatus += ErrorMessages.northing; } if ((precision < 0) || (precision > MAX_PRECISION)) { errorStatus += ErrorMessages.precision; } if (errorStatus.Length > 0) { throw new ArgumentException(errorStatus); } GeodeticCoordinates geodeticCoordinates = ups.convertToGeodetic(upsCoordinates); //If the latitude is within the valid mgrs polar range [-90, -80) or [84, 90], //convert to mgrs using the ups path, otherwise convert to mgrs using the utm path MGRSorUSNGCoordinates mgrsorUSNGCoordinates = new MGRSorUSNGCoordinates(); double latitude = geodeticCoordinates.latitude; if ((latitude < (MIN_MGRS_NON_POLAR_LAT + EPSILON)) || (latitude >= (MAX_MGRS_NON_POLAR_LAT - EPSILON))) { mgrsorUSNGCoordinates = fromUPS(upsCoordinates, precision); } else { mgrsorUSNGCoordinates = fromUTM(utm.convertFromGeodetic(geodeticCoordinates), geodeticCoordinates.longitude, latitude, precision); } return mgrsorUSNGCoordinates; }