Ejemplo n.º 1
0
        /// <summary>
        /// Reset the location from a string. See <see cref="GeoCoords(string, bool, bool)"/>.
        /// </summary>
        /// <param name="s">1-element, 2-element, or 3-element string representation of the position.</param>
        /// <param name="centerp">governs the interpretation of <see cref="MGRS"/> coordinates.</param>
        /// <param name="longfirst">governs the interpretation of geographic coordinates.</param>
        public void Reset(string s, bool centerp = true, bool longfirst = false)
        {
            var sa = s.Split(" \t\n\v\f\r,".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

            if (sa.Length == 1)
            {
                int prec;
                (_zone, _northp, _easting, _northing, prec) = MGRS.Reverse(sa[0].AsSpan(), centerp);
                (_lat, _long) = UTMUPS.Reverse(_zone, _northp, _easting, _northing, out _gamma, out _k);
            }
            else if (sa.Length == 2)
            {
                (_lat, _long) = DMS.Decode(sa[0], sa[1], longfirst);
                _long         = AngNormalize(_long);
                (_zone, _northp, _easting, _northing) = UTMUPS.Forward(_lat, _long, out _gamma, out _k);
            }
            else if (sa.Length == 3)
            {
                int zoneind, coordind;
                if (sa[0].Length > 0 && char.IsLetter(sa[0][sa[0].Length - 1]))
                {
                    zoneind  = 0;
                    coordind = 1;
                }
                else if (sa[2].Length > 0 && char.IsLetter(sa[2][sa[2].Length - 1]))
                {
                    zoneind  = 2;
                    coordind = 0;
                }
                else
                {
                    throw new GeographicException("Neither " + sa[0] + " nor " + sa[2]
                                                  + " of the form UTM/UPS Zone + Hemisphere"
                                                  + " (ex: 38n, 09s, n)");
                }

                (_zone, _northp) = UTMUPS.DecodeZone(sa[zoneind].AsSpan());
                for (int i = 0; i < 2; ++i)
                {
                    if (i != 0)
                    {
                        _northing = sa[coordind + i].ParseDouble();
                    }
                    else
                    {
                        _easting = sa[coordind + i].ParseDouble();
                    }
                }

                (_lat, _long) = UTMUPS.Reverse(_zone, _northp, _easting, _northing, out _gamma, out _k);
                FixHemisphere();
            }
            else
            {
                throw new GeographicException("Coordinate requires 1, 2, or 3 elements");
            }
            CopyToAlt();
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Reset the location in terms of geographic coordinates. See <see cref="GeoCoords.GeoCoords(double, double, int)"/>.
 /// </summary>
 /// <param name="latitude">latitude in degrees.</param>
 /// <param name="longitude">longitude in degrees.</param>
 /// <param name="zone">
 /// if specified, force the UTM/UPS representation to use a specified zone using the rules given in <see cref="ZoneSpec"/>.
 /// </param>
 public void Reset(double latitude, double longitude, int zone = (int)ZoneSpec.Standard)
 {
     (_zone, _northp, _easting, _northing) = UTMUPS.Forward(latitude, longitude, out _gamma, out _k, zone);
     _lat  = latitude;
     _long = longitude;
     if (_long >= 180)
     {
         _long -= 360;
     }
     else if (_long < -180)
     {
         _long += 360;
     }
     CopyToAlt();
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Specify alternate zone number.
        /// </summary>
        /// <param name="zone">zone number for the alternate representation.</param>
        /// <remarks>
        /// See <see cref="ZoneSpec"/> for more information on the interpretation of zone.
        /// Note that <paramref name="zone"/> == <see cref="ZoneSpec.Standard"/> (the default) use the standard UPS or UTM zone,
        /// <see cref="ZoneSpec.Match"/> does nothing retaining the existing alternate representation.
        /// Before this is called the alternate zone is the input zone.
        /// </remarks>
        public void SetAltZone(int zone = (int)ZoneSpec.Standard)
        {
            if (zone == (int)ZoneSpec.Match)
            {
                return;
            }

            zone = UTMUPS.StandardZone(_lat, _long, zone);
            if (zone == _zone)
            {
                CopyToAlt();
            }
            else
            {
                (_alt_zone, _, _alt_easting, _alt_northing) = UTMUPS.Forward(_lat, _long, out _alt_gamma, out _alt_k, zone);
            }
        }
Ejemplo n.º 4
0
        static string UTMUPSString(int zone, bool northp,
                                   double easting, double northing,
                                   int prec, bool abbrev)
        {
            var os = new StringBuilder();

            prec = Max(-5, Min(9 + 0 /*Math::extra_digits()*/, prec));
            // Need extra real because, since C++11, pow(float, int) returns double
            var scale = prec < 0 ? Pow(10, -prec) : 1;

            os.Append(UTMUPS.EncodeZone(zone, northp, abbrev));// << fixed << setfill('0');

            if (IsFinite(easting))
            {
                os.Append(' ');
                os.Append((easting / scale).ToFixedString(Max(0, prec)));
                if (prec < 0 && Abs(easting / scale) > 0.5)
                {
                    os.Append("0".PadLeft(-prec, '0'));
                }
            }
            else
            {
                os.Append(" nan");
            }

            if (IsFinite(northing))
            {
                os.Append(' ');
                os.Append((northing / scale).ToFixedString(Max(0, prec)));
                if (prec < 0 && Abs(northing / scale) > 0.5)
                {
                    os.Append("0".PadLeft(-prec, '0'));
                }
            }
            else
            {
                os.Append(" nan");
            }

            return(os.ToString());
        }
Ejemplo n.º 5
0
 /// <summary>
 /// UTM/UPS string for the alternate zone, with hemisphere override.
 /// </summary>
 /// <param name="northp">hemisphere override</param>
 /// <param name="prec">precision (relative to about 1m)</param>
 /// <param name="abbrev">if <see langword="true"/> (the default) use abbreviated (<c>n</c>/<c>s</c>) notation for hemisphere;
 /// otherwise spell out the hemisphere (<c>north</c>/<c>south</c>).</param>
 /// <returns>UTM/UPS string representation: zone designator, easting, and northing.</returns>
 public string ToAltUTMUPSString(bool northp, int prec = 0, bool abbrev = true)
 {
     var(e, n, _) = UTMUPS.Transfer(_alt_zone, _northp, _alt_easting, _alt_northing, _alt_zone, northp);
     return(UTMUPSString(_alt_zone, northp, e, n, prec, abbrev));
 }