Beispiel #1
0
        //-----------------------------------------------------------------------------------------------------------------------------------------------------------
        /// <summary>
        ///     Prints out the given information to the console using the thematic colouring
        /// </summary>
        /// <param name="info">The information to be logged.</param>
        /// <param name="useNewLine">
        ///     Whether or not to log the information on a new line.
        ///     Currently, this will always be true, apart from for processing information, that uses backspaces on the same line to overwrite the messages.
        /// </param>
        /// <param name="lht">The thematic classification to use to make the messages visually appealing.</param>
        /// <param name="severity">
        ///     The criticality of the information to be logged.  Normally in a range from 0 to 10, with 10 being the most critical.
        ///     2-4 are very low level, non critical errors.  5-7 are normal application errors (e.g. SQL errors).  8-10 are critical.
        ///     0 is normally reserved for information messages, and 1 for warnings.
        ///     As the logs have become more complicated, custom three digit severity codes are also now used for known issues.
        /// </param>
        public static void DoConsoleOutput(string info, bool useNewLine, LoggerHighlightType lht, int severity)
        {
            //-----1----- Set the thematic colouring to use for this message, based on the specified type of highlight
            if (LoggerHighlightType.Error == lht)
            {
                Console.BackgroundColor = ConsoleColor.Red;
                Console.ForegroundColor = ConsoleColor.Black;
            }
            else if (LoggerHighlightType.Warning == lht)
            {
                Console.BackgroundColor = ConsoleColor.Yellow;
                Console.ForegroundColor = ConsoleColor.Black;
            }
            else if (LoggerHighlightType.Processing == lht)
            {
                Console.BackgroundColor = ConsoleColor.Black;
                Console.ForegroundColor = ConsoleColor.DarkGray;
            }
            else if (LoggerHighlightType.Info == lht)
            {
                Console.BackgroundColor = ConsoleColor.Black;
                Console.ForegroundColor = ConsoleColor.Gray;
            }
            else
            {
                // the defaults ...
                Console.BackgroundColor = ConsoleColor.Black;
                Console.ForegroundColor = ConsoleColor.White;
            }

            //-----2----- And lets write the information, incorporating the date and time and message severity if required.
            if (info != null)
            {
                if (info != "" && LoggerHighlightType.Processing != lht && logDateAndTime == true)
                {
                    info = DateTimeInformation.PrettyDateTimeFormat(DateTime.Now, 0) + ":" + severity + ": " + info;
                }
                if (useNewLine)
                {
                    Console.WriteLine(info);
                }
                else
                {
                    Console.Write(info);
                }
            }

            //-----3----- Reset the colours to the defaults ...
            Console.BackgroundColor = ConsoleColor.Black;
            Console.ForegroundColor = ConsoleColor.White;
        }
        //----------------------------------------------------------------------------------------------------------------------------------------------------------
        /// <summary>
        ///    Longitude = Eastings
        ///    Latitude = Northings
        /// </summary>
        public bool ConvertDegreesMinutesSecondsToDecimalDegrees(string dmsLongitude, string dmsLatitude, out Point decimalDegrees,
                                                                 out string warning, out LoggerHighlightType lht)
        {
            bool success = false;

            decimalDegrees = new Point();
            warning        = "";
            lht            = LoggerHighlightType.Normal;

            LoggerHighlightType lht2 = LoggerHighlightType.Normal;
            string warningX          = "";
            string warningY          = "";

            // Parse the longitude ...
            success = ParseCoordinate(dmsLongitude, out decimalDegrees.x, out warningX, out lht);

            // Parse the Latitude ...
            success = success & ParseCoordinate(dmsLatitude, out decimalDegrees.y, out warningY, out lht2);

            if (warningX != "")
            {
                warning = warningX;
            }
            else if (warningY != "")
            {
                warning = warningY;
                lht     = lht2;
            }

            // final check - that it is in our bounding box ...
            if (success)
            {
                if (decimalDegrees.x < minLong || decimalDegrees.x > maxLong)
                {
                    success = false;
                    warning = "The Longitude is not correct for the area of interest - it should be between "
                              + minLong + " and " + maxLong + " but is actually " + decimalDegrees.x + ".  Sort it out!";
                    lht = LoggerHighlightType.Error;
                }
                if (decimalDegrees.y < minLat || decimalDegrees.y > maxLat)
                {
                    success = false;
                    warning = "The Latitude is not correct for the area of interest - it should be between "
                              + minLat + " and " + maxLat + " but is actually " + decimalDegrees.y + ".  Sort it out!";
                    lht = LoggerHighlightType.Error;
                }
            }

            return(success);
        }
Beispiel #3
0
        //-----------------------------------------------------------------------------------------------------------------------------------------------------------
        /// <summary>
        ///     Logs the given information
        /// </summary>
        /// <param name="info">The information to be logged.</param>
        /// <param name="lht">The type of formatting to use for the message in the console window.</param>
        /// <param name="severity">
        ///     The criticality of the information to be logged.  Normally in a range from 0 to 10, with 10 being the most critical.
        ///     As the logs have become more complicated, custom three digit severity codes are also now used for known issues.
        /// </param>
        public static void Log(string info, LoggerHighlightType lht, int severity)
        {
            if (LoggerHighlightType.Error == lht)
            {
                info = "ERROR - " + severity + " - " + (logDateAndTime ? DateTimeInformation.PrettyDateTimeFormat(DateTime.Now, 0) : "") + info;
            }
            else if (LoggerHighlightType.Warning == lht)
            {
                info = "WARNING - " + severity + " - " + (logDateAndTime ? DateTimeInformation.PrettyDateTimeFormat(DateTime.Now, 0) : "") + info;
            }
            else if (LoggerHighlightType.Processing == lht)
            {
                // Nothing to add for the processing messages
            }
            else if (LoggerHighlightType.Info == lht)
            {
                info = "INFO - " + severity + " - " + (logDateAndTime ? DateTimeInformation.PrettyDateTimeFormat(DateTime.Now, 0) : "") + info;
            }
            else
            {
                // NADA
            }

            // Store the info in the log list, if required
            if (storeLogList == true)
            {
                // Add the information in a threadsafe manner
                lock (logList) {
                    logList.Add(info);
                }
            }

            // write to the console if its a console application
            if (writeToConsole == true)
            {
                DoConsoleOutput(info, true, lht, severity);
            }
        }
        //----------------------------------------------------------------------------------------------------------------------------------------------------------
        public bool ParseCoordinate(string coordinateString, out double decimalDegreeCoordinate, out string warning, out LoggerHighlightType lht)
        {
            bool success = false;

            decimalDegreeCoordinate = 0;
            warning = "";
            lht     = LoggerHighlightType.Normal;

            try {
                if (coordinateString != null && coordinateString != "")
                {
                    coordinateString = coordinateString.Trim();

                    //______01______ remove leading N and E
                    if (Regex.IsMatch(coordinateString, "^[A-Za-z]"))
                    {
                        coordinateString = coordinateString.Substring(1);
                        coordinateString = coordinateString.Trim();
                    }

                    //______02______ remove trailing N and E (Added 24th May 2011) - Two cases 45.5555E and 45.454545-E
                    if (Regex.IsMatch(coordinateString, "[0-9]+ ?[-]? ?[A-Za-z]$"))
                    {
                        coordinateString = coordinateString.Substring(0, coordinateString.Length - 1);
                        coordinateString = coordinateString.Trim();

                        if (Regex.IsMatch(coordinateString, "[0-9]+ ?[-]$"))
                        {
                            coordinateString = coordinateString.Substring(0, coordinateString.Length - 1);
                            coordinateString = coordinateString.Trim();
                        }
                    }


                    // These are all not quite good enough to parse ...
                    // 30.27622* - ok
                    // 070*59523 - ok
                    // 41 52.762' - ok
                    //24.4785 0 - ok
                    // `29. 13470 - ambiguoys
                    // 68 07` 25" - ok
                    // 27 14' 50" - ok
                    // 28⁰03857 - ambiguous
                    // 28⁰03.817 - ambiguous

                    // Use these switches for debugging ...
                    //if (coordinateString.Equals("30.27622*")) {
                    //    string temp = "";
                    //}

                    //if (coordinateString.Equals("24.4785 0")) {
                    //    string temp = "";
                    //}

                    //if (coordinateString.Equals("41 52.762")) {
                    //    string temp = "";
                    //}

                    //if (coordinateString.Equals("070*59523")) {
                    //    string temp = "";
                    //}


                    // 3-May-2011 - Added the * at the end ...
                    //28.44545*
                    if (Regex.IsMatch(coordinateString, "^[0-9]+ ?[.] ?[0-9]+[*]?$"))                      //______1______ Start with the GOOD options!

                    // remove any spaces in funky places ...
                    {
                        string tempCoordinateString = coordinateString.Replace(" ", "");
                        tempCoordinateString = tempCoordinateString.Replace("*", "");

                        success = double.TryParse(tempCoordinateString, out decimalDegreeCoordinate);
                        if (success == false)
                        {
                            warning = "Could not parse the given coordinate to a decimal degree - the given value was " + coordinateString + ".";
                            lht     = LoggerHighlightType.Error;
                        }
                        else if (coordinateString.Length - coordinateString.IndexOf(".") < 5)
                        {
                            warning = "Not enough decimal places - only " + (coordinateString.Length - coordinateString.IndexOf(".")) + " were found.";
                            lht     = LoggerHighlightType.Warning;
                        }
                    }
                    else if (Regex.IsMatch(coordinateString, "^[0-9]+$"))                          //______2______ No decimal place ....

                    // hmmm - make an assumption that the first two digits are the real number and the rest is the decimal ....
                    // Flag as a warning
                    {
                        if (coordinateString.Length > 2)
                        {
                            string firstBit  = coordinateString.Substring(0, 2);
                            string secondBit = coordinateString.Substring(2);

                            if (secondBit.Length < 5)
                            {
                                warning = "Not enough decimal places found after splitting - only " + secondBit.Length + " were found.";
                                lht     = LoggerHighlightType.Warning;
                            }

                            success = double.TryParse(firstBit + "." + secondBit, out decimalDegreeCoordinate);
                            if (success == false)
                            {
                                warning = "Could not parse the given coordinate to a decimal degree - the given value was " + coordinateString + ".";
                                lht     = LoggerHighlightType.Error;
                            }
                        }
                        else
                        {
                            warning = "The given coordinate was too short! - the given value was " + coordinateString + ".";
                            lht     = LoggerHighlightType.Error;
                        }
                    }
                    else if (Regex.IsMatch(coordinateString, "^[0-9]+[** .]+[0-9]+$"))                          //______3______ No decimal place ....
                    // 30*16309   º
                    //070*95506
                    //30*16309
                    {
                        string tempCoordinateString = coordinateString.Replace(" ", ".");
                        tempCoordinateString = tempCoordinateString.Replace("*", ".");
                        tempCoordinateString = tempCoordinateString.Replace("..", ".");
                        tempCoordinateString = tempCoordinateString.Replace("..", ".");

                        success = double.TryParse(tempCoordinateString, out decimalDegreeCoordinate);
                        if (success == false)
                        {
                            warning = "Could not parse the given coordinate to a decimal degree - the given value was " + coordinateString + ".";
                            lht     = LoggerHighlightType.Error;
                        }
                    }
                    else if (Regex.IsMatch(coordinateString, "^[0-9]+[.]+[0-9 ]+$"))                          //______4______ decimal place and random spaces ....
                    // 30.1630 9
                    {
                        string tempCoordinateString = coordinateString.Replace(" ", "");
                        tempCoordinateString = tempCoordinateString.Replace("..", ".");

                        success = double.TryParse(tempCoordinateString, out decimalDegreeCoordinate);
                        if (success == false)
                        {
                            warning = "Could not parse the given coordinate to a decimal degree - the given value was " + coordinateString + ".";
                            lht     = LoggerHighlightType.Error;
                        }
                    }
                    else if (Regex.IsMatch(coordinateString, "^[0-9]+[ ]?[⁰º]+[ ]?[0-9]+[.][0-9]+$"))                          //______5______ Decimal Minutes ....
                    //28⁰03.244
                    // º
                    {
                        string   tempCoordString = coordinateString.Replace(" ", "");
                        string[] bits            = tempCoordString.Split(new string[] { "º", "⁰" }, StringSplitOptions.RemoveEmptyEntries);

                        if (bits != null && bits.Length >= 2)
                        {
                            double degrees, minutes, seconds = 0;
                            success = double.TryParse(bits[0], out degrees);
                            success = success & double.TryParse(bits[1], out minutes);

                            // seconds will always be zero in this instance ...
                            decimalDegreeCoordinate = ConvertCoordinate(degrees, minutes, seconds);

                            success = (decimalDegreeCoordinate > 0);
                        }
                        else
                        {
                            warning = "Couldn't parse the given coordinate into Degrees, and Decimal Minutes - the given value was " + coordinateString + ".";
                            lht     = LoggerHighlightType.Error;
                        }
                    }
                    else if (Regex.IsMatch(coordinateString, "^[0-9]+[ ]?[˚]+[ ]?[0-9]+[ ]?[’][ ]?[0-9]+[ ]?[”][ ]?$"))                          //______6______ Decimal Minutes Seconds ....

                    // 70˚ 43’ 758”
                    // These are actually Decimal minutes ...  Seconds should only have up to 60!!!!!

                    {
                        string   tempCoordString = coordinateString.Replace(" ", "");
                        string[] bits            = tempCoordString.Split(new string[] { "˚", "’", "”" }, StringSplitOptions.RemoveEmptyEntries);

                        if (bits != null && bits.Length >= 3)
                        {
                            double degrees, minutes, seconds = 0;
                            success = double.TryParse(bits[0], out degrees);
                            // make the minutes look pretty ...
                            string tempMinutes = bits[1].Trim() + "." + bits[2].Trim();
                            success = success & double.TryParse(tempMinutes, out minutes);

                            // seconds will always be zero in this instance ...
                            decimalDegreeCoordinate = ConvertCoordinate(degrees, minutes, seconds);

                            success = (decimalDegreeCoordinate > 0);
                        }
                        else
                        {
                            warning = "Couldn't parse the given coordinate into Degrees, and Decimal Minutes - the given value was " + coordinateString + ".";
                            lht     = LoggerHighlightType.Error;
                        }
                    }
                    else if (Regex.IsMatch(coordinateString, "^[0-9]+[ .]+[0-9]+[,. ]+[0-9]+$"))                          //______7______ Decimal Minutes ....
                    //28 03.244
                    // 28.23.455
                    // 27 45,454
                    {
                        string[] bits = coordinateString.Split(new string[] { " ", ".", "," }, StringSplitOptions.RemoveEmptyEntries);

                        if (bits != null && bits.Length >= 2)
                        {
                            double degrees, minutes, seconds = 0;
                            success = double.TryParse(bits[0], out degrees);

                            string tempMinutes = bits[1].Trim() + "." + bits[2].Trim();
                            success = success & double.TryParse(tempMinutes, out minutes);

                            // seconds will always be zero in this instance ...
                            decimalDegreeCoordinate = ConvertCoordinate(degrees, minutes, seconds);

                            success = (decimalDegreeCoordinate > 0);
                        }
                        else
                        {
                            warning = "Couldn't parse the given coordinate into Degrees, and Decimal Minutes - the given value was " + coordinateString + ".";
                            lht     = LoggerHighlightType.Error;
                        }
                    }
                    else                                                                                                      //______8______ Some sort of Decimals, Minutes and Seconds ....

                    // Ok - got to here - so we need to split it out as an Decimal Minutes Seconds activity
                    //Logger.Log( "******************Splitting Decimals, Minutes and Seconds ..." );
                    // Try parsing on spaces and all the other natty characters ...
                    // Strim non numeric characters ...
                    //30,2',13"

                    {
                        string[] bits = coordinateString.Split(new string[] { " ", "°", "*", ".", "'", "\"", "-", ",", "`" }, StringSplitOptions.RemoveEmptyEntries);

                        if (bits != null)
                        {
                            if (bits.Length >= 3)
                            {
                                if (bits.Length > 3)
                                {
                                    warning = "Couldn't parse the given coordinate into Decimals, Minutes and Seconds - too many numeric parts - the given value was " + coordinateString + ".";
                                    lht     = LoggerHighlightType.Warning;
                                    //Logger.Log( "Too Many Bits MAN - found "+bits.Length+" ..." );
                                }

                                // So how do we differentiate between decimal minutes and degrees, minutes and seconds?

                                string tempDegrees = "";
                                string tempMinutes = "";
                                string tempSeconds = "";

                                foreach (string bit in bits)
                                {
                                    if (bit != null && bit != "" && Regex.IsMatch(bit, "^[0-9]+$"))
                                    {
                                        if (tempDegrees == "")
                                        {
                                            tempDegrees = bit;
                                        }
                                        else if (tempMinutes == "")
                                        {
                                            tempMinutes = bit;
                                        }
                                        else if (tempSeconds == "")
                                        {
                                            tempSeconds = bit;
                                        }
                                        else
                                        {
                                            // oh f**k - there are four numeric parts to this string!!!
                                            // if decimal seconds, then append appropriately ....
                                            if (bits.Length == 4)
                                            {
                                                //Logger.Log("Appending Bit "+bit+" to the seconds "+tempSeconds+" ...");
                                                tempSeconds = tempSeconds + bit;
                                            }
                                            else
                                            {
                                                warning = "Couldn't parse the given coordinate into Decimals, Minutes and Seconds - too many numeric parts - the given value was " + coordinateString + ".";
                                                lht     = LoggerHighlightType.Error;
                                            }
                                        }
                                    }
                                }

                                if (tempDegrees != "" && tempMinutes != "" && tempSeconds != "")
                                {
                                    double degrees, minutes, seconds = 0;
                                    success = double.TryParse(tempDegrees, out degrees);
                                    success = success & double.TryParse(tempMinutes, out minutes);
                                    success = success & double.TryParse(tempSeconds, out seconds);

                                    if (seconds > 60)
                                    {
                                        if (seconds >= 100)
                                        {
                                            if (seconds >= 1000)
                                            {
                                                seconds = seconds / 10000 * 60;
                                            }
                                            else
                                            {
                                                seconds = seconds / 1000 * 60;
                                            }
                                        }
                                    }

                                    decimalDegreeCoordinate = ConvertCoordinate(degrees, minutes, seconds);
                                }
                                else
                                {
                                    // fuckity f**k - there are not enough decimal parts!!
                                    warning = "Couldn't parse the given coordinate into Decimals, Minutes and Seconds - not enough numeric parts - the given value was " + coordinateString + ".";
                                    lht     = LoggerHighlightType.Error;
                                }
                            }
                            else
                            {
                                // Too complicated for now !!!
                                warning = "Couldn't parse the given coordinate into Decimals, Minutes and Seconds - the given value was " + coordinateString + ".";
                                lht     = LoggerHighlightType.Error;
                            }
                        }
                        else
                        {
                            // Too complicated for now !!!
                            warning = "Couldn't parse the given coordinated into Decimals, Minutes and Seconds - the given value was " + coordinateString + ".";
                            lht     = LoggerHighlightType.Error;
                        }
                    }
                }
                else
                {
                    warning = "No Coordinate Information Provided!!!";
                    lht     = LoggerHighlightType.Error;
                }
            } catch (Exception ex) {
                Logger.Log("Error extracting the coordinate - the given value was " + coordinateString + " . " + ex.ToString());
                lht = LoggerHighlightType.Error;
            }

            return(success);
        }