예제 #1
0
        /// <summary>
        /// The constructor receives the ellipsoid parameters and sets
        /// the corresponding state variables. If any errors occur, an
        /// exception is thrown with a description of the error.
        /// </summary>
        /// <param name="ellipsoidSemiMajorAxis">Semi-major axis of ellipsoid in meters</param>
        /// <param name="ellipsoidFlattening">Flattening of ellipsoid</param>
        /// <param name="ellipsoidCode">2-letter code for ellipsoid</param>
        public MGRS(double ellipsoidSemiMajorAxis, double ellipsoidFlattening, string ellipsoidCode)
        {
            double inv_f = 1 / ellipsoidFlattening;
            string errorStatus = "";

            if (ellipsoidSemiMajorAxis <= 0.0)
            { /* Semi-major axis must be greater than zero */
                errorStatus += ErrorMessages.semiMajorAxis;
            }
            if ((inv_f < 250) || (inv_f > 350))
            { /* Inverse flattening must be between 250 and 350 */
                errorStatus += ErrorMessages.ellipsoidFlattening;
            }

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

            this.semiMajorAxis = ellipsoidSemiMajorAxis;
            this.flattening = ellipsoidFlattening;
            this.ellipsoidCode = ellipsoidCode;

            ups = new UPS(this.semiMajorAxis, this.flattening);
            utm = new UTM(this.semiMajorAxis, this.flattening, 0);
        }
예제 #2
0
        public MGRSorUSNGCoordinates fromUTM(UTMCoordinates utmCoordinates, double longitude, double latitude, long precision)
        {
            /*
             * The function fromUTM calculates an MGRS coordinate string
             * based on the zone, latitude, easting and northing.
             *
             *    zone       : Zone number             (input)
             *    hemisphere : Hemisphere              (input)
             *    longitude  : Longitude in radians    (input)
             *    latitude   : Latitude in radians     (input)
             *    easting    : Easting                 (input)
             *    northing   : Northing                (input)
             *    precision  : Precision               (input)
             *    MGRSString : MGRS coordinate string  (output)
             */

            double pattern_offset = 0.0;      /* Pattern offset for 3rd letter               */
            double grid_northing = 0.0;       /* Northing used to derive 3rd letter of MGRS  */
            long ltr2_low_value = 0;        /* 2nd letter range - low number               */
            long ltr2_high_value = 0;       /* 2nd letter range - high number              */
            int[] letters = new int[3];  /* Number location of 3 letters in alphabet    */
            int zoneOverride = 0;
            int natural_zone = 0;

            long zone = utmCoordinates.zone;
            char hemisphere = utmCoordinates.hemisphere;
            double easting = utmCoordinates.easting;
            double northing = utmCoordinates.northing;

            getLatitudeLetter(latitude, ref letters[0]);

            easting = Math.Round(easting);

            //Check if the point is within it's natural zone
            //If it is not, put it there
            if (longitude < Math.PI)
            {
                natural_zone = (int)(31 + ((longitude) / _6));
            }
            else
            {
                natural_zone = (int)(((longitude) / _6) - 29);
            }

            if (natural_zone > 60)
            {
                natural_zone = 1;
            }
            if (zone != natural_zone)
            { // reconvert to override zone
                UTM utmOverride = new UTM(semiMajorAxis, flattening, natural_zone);
                UTMCoordinates utmCoordinatesOverride = utmOverride.convertFromGeodetic(new GeodeticCoordinates(CoordinateType.Enum.geodetic, longitude, latitude));

                zone = utmCoordinatesOverride.zone;
                hemisphere = utmCoordinatesOverride.hemisphere;
                easting = utmCoordinatesOverride.easting;
                northing = utmCoordinatesOverride.northing;
            }

            easting = Math.Round(easting);

            /* UTM special cases */
            if (letters[0] == LETTER_V) // V latitude band
            {
                if ((zone == 31) && (easting >= _500000))
                {
                    zoneOverride = 32;  // extension of zone 32V
                }
            }
            else if (letters[0] == LETTER_X)
            {
                if ((zone == 32) && (easting < _500000)) // extension of zone 31X
                {
                    zoneOverride = 31;
                }
                else if (((zone == 32) && (easting >= _500000)) || // western extension of zone 33X
                         ((zone == 34) && (easting < _500000))) // eastern extension of zone 33X
                {
                    zoneOverride = 33;
                }
                else if (((zone == 34) && (easting >= _500000)) || // western extension of zone 35X
                         ((zone == 36) && (easting < _500000))) // eastern extension of zone 35X
                {
                    zoneOverride = 35;
                }
                else if ((zone == 36) && (easting >= _500000)) // western extension of zone 37X
                {
                    zoneOverride = 37;
                }
            }

            if (zoneOverride != 0)
            { // reconvert to override zone
                UTM utmOverride = new UTM(semiMajorAxis, flattening, zoneOverride);
                UTMCoordinates utmCoordinatesOverride = utmOverride.convertFromGeodetic(new GeodeticCoordinates(CoordinateType.Enum.geodetic, longitude, latitude));

                zone = utmCoordinatesOverride.zone;
                hemisphere = utmCoordinatesOverride.hemisphere;
                easting = utmCoordinatesOverride.easting;
                northing = utmCoordinatesOverride.northing;
            }

            easting = Math.Round(easting);
            northing = Math.Round(northing);
            double divisor = Math.Pow(10.0, (5.0 - precision));
            easting = (long)(easting / divisor) * divisor;
            northing = (long)(northing / divisor) * divisor;

            if (latitude <= 0.0 && northing == 1.0e7)
            {
                latitude = 0.0;
                northing = 0.0;
            }

            getGridValues(zone, ref ltr2_low_value, ref ltr2_high_value, ref pattern_offset);

            grid_northing = northing;

            while (grid_northing >= TWOMIL)
            {
                grid_northing = grid_northing - TWOMIL;
            }
            grid_northing = grid_northing + pattern_offset;
            if (grid_northing >= TWOMIL)
            {
                grid_northing = grid_northing - TWOMIL;
            }

            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;
            }

            letters[1] = (int)(ltr2_low_value + ((long)(easting / ONEHT) - 1));
            if ((ltr2_low_value == LETTER_J) && (letters[1] > LETTER_N))
            {
                letters[1] = letters[1] + 1;
            }

            return new MGRSorUSNGCoordinates(CoordinateType.Enum.militaryGridReferenceSystem, makeMGRSString(zone, letters, easting, northing, precision));
        }
예제 #3
0
 public MGRS()
 {
     //this.CoordSys.getEllipsoidParameters(ref this.semiMajorAxis, ref this.flattening);
     this.ups = new UPS(this.semiMajorAxis, this.flattening);
     this.utm = new UTM(this.semiMajorAxis, this.flattening, 0);
 }