Exemplo n.º 1
0
        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;
        }
Exemplo n.º 2
0
        public GeodeticCoordinates convertToGeodetic(MGRSorUSNGCoordinates mgrsorUSNGCoordinates)
        {
            /*
            * The function convertToGeodetic converts an MGRS coordinate string
            * 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.
            *
            *    MGRS       : MGRS coordinate string           (input)
            *    latitude   : Latitude in radians              (output)
            *    longitude  : Longitude in radians             (output)
            *
            */

            long zone = new long();
            long[] letters = new long[MGRS_LETTERS];
            double mgrs_easting = new double();
            double mgrs_northing = new double();
            long precision = new long();

            GeodeticCoordinates geodeticCoordinates;

            breakMGRSString(mgrsorUSNGCoordinates.MGRSString, ref zone, ref letters, ref mgrs_easting, ref mgrs_northing, ref precision);

            if (zone > 0)
            {
                geodeticCoordinates = utm.convertToGeodetic(toUTM(zone, letters, mgrs_easting, mgrs_northing, precision));
            }
            else
            {
                geodeticCoordinates = ups.convertToGeodetic(toUPS(letters, mgrs_easting, mgrs_northing));
            }

            return geodeticCoordinates;
        }
Exemplo n.º 3
0
        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;
        }
Exemplo n.º 4
0
        /// <summary>
        /// The function convertFromGeodetic converts Geodetic (latitude and
        /// longitude) 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.
        /// </summary>
        /// <param name="geodeticCoordinates">Geodetic Coordinates</param>
        /// <param name="precision">Precision level of MGRS string (0 to 5)</param>
        /// <returns>MGRS Coordinates</returns>
        public MGRSorUSNGCoordinates convertFromGeodetic(GeodeticCoordinates geodeticCoordinates, long precision)
        {
            MGRSorUSNGCoordinates mgrsorUSNGCoordinates = new MGRSorUSNGCoordinates();
            string errorStatus = "";

            double latitude = geodeticCoordinates.latitude;
            double longitude = geodeticCoordinates.longitude;

            if ((latitude < -PI_OVER_2) || (latitude > PI_OVER_2))
            { /* latitude out of range */
                errorStatus = ErrorMessages.latitude;
            }
            if ((longitude < -Math.PI) || (longitude > (2 * Math.PI)))
            { /* longitude out of range */
                errorStatus = ErrorMessages.longitude;
            }
            if ((precision < 0) || (precision > MAX_PRECISION))
            {
                errorStatus = ErrorMessages.precision;
            }

            if (errorStatus.Length > 0)
            {
                throw new ArgumentException(errorStatus);
            }

            // If the latitude is within the valid mgrs non polar range [-80, 84),
            // convert to mgrs using the utm path, otherwise convert to mgrs using the ups path
            if ((latitude >= MIN_MGRS_NON_POLAR_LAT) && (latitude < MAX_MGRS_NON_POLAR_LAT))
            {
                mgrsorUSNGCoordinates = fromUTM(utm.convertFromGeodetic(geodeticCoordinates), longitude, latitude, precision);
            }
            else
            {
                mgrsorUSNGCoordinates = fromUPS(ups.convertFromGeodetic(geodeticCoordinates), precision);
            }

            return mgrsorUSNGCoordinates;
        }
 public void setGrid(string Grid)
 {
     this.Grid = Grid;
     this.ValidateCoordinates();
     this.mgrsCoordinates = new MGRSorUSNGCoordinates(CoordinateType.Enum.militaryGridReferenceSystem, this.Grid);
 }
 public MilitaryGridReferenceSystem(MGRSorUSNGCoordinates mgrsCoordinates)
 {
     this.mgrsCoordinates = mgrsCoordinates;
     this.Grid = this.mgrsCoordinates.MGRSString;
     this.ValidateCoordinates();
 }