Exemple #1
0
        /// <summary>
        /// Converts a 'Maidenhead Locator' to geographical point (latitude and longitude, in degrees).
        /// </summary>
        /// <param name="maidenheadLocator">The 'Maidenhead Locator'.</param>
        /// <param name="positionInRectangle">The position of the geographical coordinates in the locator.</param>
        /// <returns>The geographical point as LatLon.GPoint.</returns>
        /// <exception cref="ArgumentException">
        /// If the length of the locator text is null or not an even number.
        /// If the locator text contains invalid characters.
        /// </exception>
        /// <summary>
        public static LatLon.GPoint GPointFromLoc(string maidenheadLocator, PositionInRectangle positionInRectangle = PositionInRectangle.MiddleMiddle)
        {
            double lat, lon;

            LatLonFromLoc(maidenheadLocator, positionInRectangle, out lat, out lon);
            return(new LatLon.GPoint(lat, lon));
        }
Exemple #2
0
        /// <summary>
        /// Calculates the azimuth to a 'Maidenhead Locator'.
        /// </summary>
        /// <param name="maidenheadLocator">The 'Maidenhead Locator'.</param>
        /// <param name="positionInRectangle">>The position of the geographical coordinates in the locator.</param>
        /// <returns>The azimuth in degrees.</returns>
        /// <exception cref="ArgumentException">
        /// If the length of the locator text is null or not an even number.
        /// If the locator text contains invalid characters.
        /// </exception>
        public double GetAzimuth(string maidenheadLocator, PositionInRectangle positionInRectangle)
        {
            var    geographicalPoint = new GeographicalPoint(maidenheadLocator, positionInRectangle);
            double result            = GetAzimuth(geographicalPoint);

            return(result);
        }
Exemple #3
0
        /// <summary>
        /// Calculates the distance to a 'Maidenhead Locator'.
        /// </summary>
        /// <param name="maidenheadLocator">The 'Maidenhead Locator'.</param>
        /// <param name="positionInRectangle">>The position of the geographical coordinates in the locator.</param>
        /// <returns>The distance in km.</returns>
        /// <exception cref="ArgumentException">
        /// If the length of the locator text is null or not an even number.
        /// If the locator text contains invalid characters.
        /// </exception>
        public double GetDistance(string maidenheadLocator, PositionInRectangle positionInRectangle)
        {
            GeographicalPoint geographicalPoint = new GeographicalPoint(maidenheadLocator, positionInRectangle);
            double            result            = GetDistance(geographicalPoint);

            return(result);
        }
Exemple #4
0
        /// <summary>
        /// Creates a new instance, based on a 'Maidenhead Locator'.
        /// </summary>
        /// <param name="maidenheadLocator">The 'Maidenhead Locator'.</param>
        /// <param name="positionInRectangle">The position of the geographical coordinates in the locator.</param>
        /// <exception cref="ArgumentException">
        /// If the length of the locator text is null or not an even number.
        /// If the locator text contains invalid characters.
        /// </exception>
        public GeographicalPoint(string maidenheadLocator, PositionInRectangle positionInRectangle)
            : this()
        {
            double latitude;
            double longitude;

            MaidenheadLocator.LatLonFromLoc(maidenheadLocator, positionInRectangle, out latitude, out longitude);

            Latitude  = latitude;
            Longitude = longitude;
        }
Exemple #5
0
        /// <summary>
        /// Calculates the azimuth from a 'Maidenhead Locator' 1 to a 'Maidenhead Locator' 2.
        /// </summary>
        /// <param name="maidenheadLocator1">The 'Maidenhead Locator' 1.</param>
        /// <param name="positionInRectangle1">The position of the geographical coordinates in the locator 1.</param>
        /// <param name="maidenheadLocator2">The 'Maidenhead Locator' 2.</param>
        /// <param name="positionInRectangle2">>The position of the geographical coordinates in the locator 2.</param>
        /// <returns>The azimuth in degrees.</returns>
        /// <exception cref="ArgumentException">
        /// If the length of any locator text is null or not an even number.
        /// If any locator text contains invalid characters.
        /// </exception>
        public static double GetAzimuth(
            string maidenheadLocator1, PositionInRectangle positionInRectangle1,
            string maidenheadLocator2, PositionInRectangle positionInRectangle2
            )
        {
            var geographicalPoint1 = new GeographicalPoint(maidenheadLocator1, positionInRectangle1);
            var geographicalPoint2 = new GeographicalPoint(maidenheadLocator2, positionInRectangle2);

            double result = GetAzimuth(geographicalPoint1, geographicalPoint2);

            return(result);
        }
Exemple #6
0
        /// <summary>
        /// Gets a waypoint projection, based on a 'Maidenhead Locator',
        /// a azimuth and a distance, respectively.
        /// </summary>
        /// <param name="maidenheadLocator">The 'Maidenhead Locator'.</param>
        /// <param name="positionInRectangle">The position of the geographical coordinates in the locator.</param>
        /// <param name="azimuth">The azimuth in degrees [0...360).</param>
        /// <param name="distance">The distance in km.</param>
        /// <returns>The waypoint projection as a <see cref="GeographicalPoint" /> instance.</returns>
        /// <exception cref="ArgumentException">
        /// If the length of the locator text is null or not an even number.
        /// If the locator text contains invalid characters.
        /// </exception>
        /// <exception cref="ArgumentException">If the azimuth exceeds its allowed interval.</exception>
        public static GeographicalPoint GetWaypointProjection(
            string maidenheadLocator, PositionInRectangle positionInRectangle,
            double azimuth, double distance
            )
        {
            var geographicalPoint = new GeographicalPoint(maidenheadLocator, positionInRectangle);
            var result            = GetWaypointProjection(
                geographicalPoint,
                azimuth, distance
                );

            return(result);
        }
Exemple #7
0
        /// <summary>
        /// Shifts geographical coordinates within a rectangle.
        /// </summary>
        /// <param name="latitude">The geographical latitude.</param>
        /// <param name="longitude">The geographical longitude.</param>
        /// <param name="positionInRectangle">The desired <see cref="PositionInRectangle" /> value.</param>
        /// <param name="height">The rectangle height.</param>
        /// <param name="width">The rectangle width.</param>
        internal static void ShiftPositionInRectangle(
            ref double latitude, ref double longitude,
            PositionInRectangle positionInRectangle,
            double height, double width
            )
        {
            switch (positionInRectangle)
            {
            case PositionInRectangle.TopLeft:
            case PositionInRectangle.TopMiddle:
            case PositionInRectangle.TopRight:
                latitude += height;
                break;
            }

            switch (positionInRectangle)
            {
            case PositionInRectangle.MiddleLeft:
            case PositionInRectangle.MiddleMiddle:
            case PositionInRectangle.MiddleRight:
                latitude += height / 2;
                break;
            }

            switch (positionInRectangle)
            {
            case PositionInRectangle.TopRight:
            case PositionInRectangle.MiddleRight:
            case PositionInRectangle.BottomRight:
                longitude += width;
                break;
            }

            switch (positionInRectangle)
            {
            case PositionInRectangle.TopMiddle:
            case PositionInRectangle.MiddleMiddle:
            case PositionInRectangle.BottomMiddle:
                longitude += width / 2;
                break;
            }
        }
Exemple #8
0
        /// <summary>
        /// Converts a 'Maidenhead Locator' to geographical coordinates (latitude and longitude, in degrees).
        /// </summary>
        /// <param name="maidenheadLocator">The 'Maidenhead Locator'.</param>
        /// <param name="positionInRectangle">The position of the geographical coordinates in the locator.</param>
        /// <param name="latitude">The geographical latitude.</param>
        /// <param name="longitude">The geographical longitude.</param>
        /// <exception cref="ArgumentException">
        /// If the length of the locator text is null or not an even number.
        /// If the locator text contains invalid characters.
        /// </exception>
        public static void LatLonFromLoc(string maidenheadLocator, PositionInRectangle positionInRectangle, out double latitude, out double longitude)
        {
            //Check arguments
            {
                if (
                    string.IsNullOrEmpty(maidenheadLocator) ||
                    maidenheadLocator.Length % 2 != 0
                    )
                {
                    throw new ArgumentException("Length of locator text is null or not an even number.", "maidenheadLocator");
                }
            }

            //Corrections
            {
                //Upper cases
                maidenheadLocator = maidenheadLocator.ToUpper();
            }
            //Work
            {
                int precision = maidenheadLocator.Length / 2;

                latitude  = GeographicalPoint.LowerLatitudeLimit;
                longitude = GeographicalPoint.LowerLongitudeLimit;

                //Zone size for step "0"
                double height;
                double width;
                InitializeZoneSize(out height, out width);

                for (int step = 1; step <= precision; step++)
                {
                    int  zones;
                    char firstCharacter;
                    RetrieveStepValues(step, false, out zones, out firstCharacter);

                    //Zone size of current step
                    height /= zones;
                    width  /= zones;

                    //Retrieve precision specific geographical coordinates
                    double longitudeStep = 0;
                    double latitudeStep  = 0;
                    {
                        bool error    = false;
                        int  position = -1;

                        if (!error)
                        {
                            //Longitude

                            position = step * 2 - 2;
                            char locatorCharacter = maidenheadLocator[position];
                            int  zone             = (int)(locatorCharacter - firstCharacter);

                            if (zone >= 0 && zone < zones)
                            {
                                longitudeStep = zone * width;
                            }
                            else
                            {
                                error = true;
                            }
                        }

                        if (!error)
                        {
                            //Latitude

                            position = step * 2 - 1;
                            char locatorCharacter = maidenheadLocator[position];
                            int  zone             = (int)(locatorCharacter - firstCharacter);

                            if (zone >= 0 && zone < zones)
                            {
                                latitudeStep = zone * height;
                            }
                            else
                            {
                                error = true;
                            }
                        }

                        if (error)
                        {
                            throw new ArgumentException("Locator text contains an invalid character at position " + (position + 1) + " (Current precision step is " + step + ").", "maidenheadLocator");
                        }
                    }
                    longitude += longitudeStep;
                    latitude  += latitudeStep;
                }

                //Corrections according argument positionInRectangle
                GeographicalPoint.ShiftPositionInRectangle(ref latitude, ref longitude, positionInRectangle, height, width);
            }
        }
        /// <summary>
        /// Converts a 'Maidenhead Locator' to geographical coordinates (latitude and longitude, in degrees).
        /// </summary>
        /// <param name="maidenheadLocator">The 'Maidenhead Locator'.</param>
        /// <param name="positionInRectangle">The position of the geographical coordinates in the locator.</param>
        /// <param name="latitude">The geographical latitude.</param>
        /// <param name="longitude">The geographical longitude.</param>
        /// <exception cref="ArgumentException">
        /// If the length of the locator text is null or not an even number.
        /// If the locator text contains invalid characters.
        /// </exception>
        public static void GeographicalCoordinatesByMaidenheadLocator(String maidenheadLocator, PositionInRectangle positionInRectangle, out Double latitude, out Double longitude)
        {
            //Check arguments
            if ( String.IsNullOrEmpty(maidenheadLocator) || maidenheadLocator.Length % 2 != 0 )
            {
                throw new ArgumentException("Length of locator text is null or not an even number.", "maidenheadLocator");
            }

            //Corrections
            maidenheadLocator = maidenheadLocator.ToUpper();

            //Work
            {
                Int32 precisionValue = maidenheadLocator.Length / 2;

                latitude = _LowerLatitudeLimit;
                longitude = _LowerLongitudeLimit;

                //Zone size for step "0"
                Double height;
                Double width;
                InitializeZoneSize(out height, out width);

                for ( int step = 1 ; step <= precisionValue ; step++ )
                {
                    Int32 zones;
                    char firstCharacter;
                    RetrieveStepValues(step, false, out zones, out firstCharacter);

                    //Zone size of current step
                    height /= zones;
                    width /= zones;

                    //Retrieve precision specific geographical coordinates
                    Double longitudeStep = 0;
                    Double latitudeStep = 0;
                    {
                        Boolean error = false;
                        Int32 position = -1;

                        if ( !error )
                        {
                            //Longitude

                            position = step * 2 - 2;
                            char locatorCharacter = maidenheadLocator[position];
                            Int32 zone = (Int32)(locatorCharacter - firstCharacter);

                            if ( zone >= 0 && zone < zones )
                            {
                                longitudeStep = zone * width;
                            }
                            else
                            {
                                error = true;
                            }
                        }

                        if ( !error )
                        {
                            //Latitude

                            position = step * 2 - 1;
                            char locatorCharacter = maidenheadLocator[position];
                            Int32 zone = (Int32)(locatorCharacter - firstCharacter);

                            if ( zone >= 0 && zone < zones )
                            {
                                latitudeStep = zone * height;
                            }
                            else
                            {
                                error = true;
                            }
                        }

                        if ( error )
                        {
                            throw new ArgumentException("Locator text contains an invalid character at position " + (position + 1) + " (Current precision step is " + step + ").", "maidenheadLocator");
                        }
                    }
                    longitude += longitudeStep;
                    latitude += latitudeStep;
                }

                //Corrections according argument positionInRectangle
                GeoPoint.ShiftPositionInRectangle(ref latitude, ref longitude, positionInRectangle, height, width);
            }
        }