private static bool TryDegreeMinuteSecond(string s, out double[] d)
        {
            d = null;
            if (Regex.Matches(s, @"[a-zA-Z]").Count != 2)
            {
                return(false);
            }                                                               //Should only contain 1 letter.

            string[] sA = SpecialSplit(s, true);
            if (sA.Count() == 6 || sA.Count() == 8)
            {
                double latD;
                double latM;
                double latS;
                double lngD;
                double lngM;
                double lngS;

                double latR = 0; //Sets 1 if South
                double lngR = 0; //Sets 1 if West

                //Contact get in order to combine directional indicator together with string
                //Should reduce 8 items to 6
                if (sA.Count() == 8)
                {
                    if (char.IsLetter(sA[0][0]))
                    {
                        sA[0] += sA[1]; sA[1] = sA[2]; sA[2] = sA[3];
                    }
                    else if (char.IsLetter(sA[1][0]))
                    {
                        sA[0] += sA[1]; sA[1] = sA[2]; sA[2] = sA[3];
                    }
                    else if (char.IsLetter(sA[3][0]))
                    {
                        sA[0] += sA[3];
                    }
                    else
                    {
                        return(false);
                    }

                    if (char.IsLetter(sA[4][0]))
                    {
                        sA[4] += sA[5]; sA[5] = sA[6]; sA[6] = sA[7];
                    }
                    else if (char.IsLetter(sA[5][0]))
                    {
                        sA[4] += sA[5]; sA[5] = sA[6]; sA[6] = sA[7];
                    }
                    else if (char.IsLetter(sA[7][0]))
                    {
                        sA[4] += sA[7];
                    }
                    else
                    {
                        return(false);
                    }

                    //Shift values for below logic
                    sA[3] = sA[4];
                    sA[4] = sA[5];
                    sA[5] = sA[6];
                }

                string latString  = string.Empty;
                string longString = string.Empty;

                //Find Directions (new struct allows for reverse formatted coordinate)
                DirectionFinder p1 = new DirectionFinder(sA[0] + " " + sA[1] + " " + sA[2]);
                DirectionFinder p2 = new DirectionFinder(sA[3] + " " + sA[4] + " " + sA[5]);

                if (p1.Success)
                {
                    if (p1.CoordinateType.Value == CoordinateType.Lat)
                    {
                        latString = p1.PartString; latR = p1.RadZero;
                    }
                    else
                    {
                        longString = p1.PartString; lngR = p1.RadZero;
                    }
                }
                if (p2.Success)
                {
                    if (p2.CoordinateType.Value == CoordinateType.Lat)
                    {
                        latString = p2.PartString; latR = p2.RadZero;
                    }
                    else
                    {
                        longString = p2.PartString; lngR = p2.RadZero;
                    }
                }

                //Either lat or long not provided in this case
                if (string.IsNullOrEmpty(latString) || string.IsNullOrEmpty(longString))
                {
                    return(false);
                }


                string[] latSplit = latString.Split(' ');
                latSplit[0] = Regex.Replace(latSplit[0], "[^0-9.]", "");
                latSplit[1] = Regex.Replace(latSplit[1], "[^0-9.]", "");
                latSplit[2] = Regex.Replace(latSplit[2], "[^0-9.]", "");

                string[] longSplit = longString.Split(' ');
                longSplit[0] = Regex.Replace(longSplit[0], "[^0-9.]", "");
                longSplit[1] = Regex.Replace(longSplit[1], "[^0-9.]", "");
                longSplit[2] = Regex.Replace(longSplit[2], "[^0-9.]", "");


                if (!double.TryParse(latSplit[0], NumberStyles.Any, CultureInfo.InvariantCulture, out latD))
                {
                    return(false);
                }
                if (!double.TryParse(latSplit[1], NumberStyles.Any, CultureInfo.InvariantCulture, out latM))
                {
                    return(false);
                }
                if (!double.TryParse(latSplit[2], NumberStyles.Any, CultureInfo.InvariantCulture, out latS))
                {
                    return(false);
                }
                if (!double.TryParse(longSplit[0], NumberStyles.Any, CultureInfo.InvariantCulture, out lngD))
                {
                    return(false);
                }
                if (!double.TryParse(longSplit[1], NumberStyles.Any, CultureInfo.InvariantCulture, out lngM))
                {
                    return(false);
                }
                if (!double.TryParse(longSplit[2], NumberStyles.Any, CultureInfo.InvariantCulture, out lngS))
                {
                    return(false);
                }

                d = new double[] { latD, latM, latS, latR, lngD, lngM, lngS, lngR };
                return(true);
            }
            return(false);
        }
Esempio n. 2
0
        private static bool TryDecimalDegree(string s, out double[] d)
        {
            d = null;

            string rs = Geodetic_Position_Spacer(s);

            if (Regex.Matches(rs, @"[a-zA-Z]").Count != 2)
            {
                return(false);
            }                                                                //Should only contain 1 letter per part.

            string[] sA = SpecialSplit(rs, true);
            if (sA.Count() == 4)
            {
                double lat;
                double lng;

                int latR = 1; //Sets negative if South
                int lngR = 1; //Sets negative if West

                //Put both directional indicator together with string

                sA[0] += sA[1];
                sA[1]  = sA[2] + sA[3];


                string latString  = string.Empty;
                string longString = string.Empty;

                //Find Directions (new struct allows for reverse formatted coordinate)
                DirectionFinder p1 = new DirectionFinder(sA[0]);
                DirectionFinder p2 = new DirectionFinder(sA[1]);

                if (p1.Success)
                {
                    if (p1.CoordinateType.Value == CoordinateType.Lat)
                    {
                        latString = p1.PartString; latR = p1.Rad;
                    }
                    else
                    {
                        longString = p1.PartString; lngR = p1.Rad;
                    }
                }
                if (p2.Success)
                {
                    if (p2.CoordinateType.Value == CoordinateType.Lat)
                    {
                        latString = p2.PartString; latR = p2.Rad;
                    }
                    else
                    {
                        longString = p2.PartString; lngR = p2.Rad;
                    }
                }

                //Either lat or long not provided in this case
                if (string.IsNullOrEmpty(latString) || string.IsNullOrEmpty(longString))
                {
                    return(false);
                }

                latString  = Regex.Replace(latString, "[^0-9.]", "");
                longString = Regex.Replace(longString, "[^0-9.]", "");

                if (!double.TryParse(latString, NumberStyles.Any, CultureInfo.InvariantCulture, out lat))
                {
                    return(false);
                }
                if (!double.TryParse(longString, NumberStyles.Any, CultureInfo.InvariantCulture, out lng))
                {
                    return(false);
                }

                lat *= latR;
                lng *= lngR;

                d = new double[] { lat, lng };

                return(true);
            }

            return(false);
        }