代码之家  ›  专栏  ›  技术社区  ›  Nick Randell

将纬度和经度转换为双精度值的最简单方法是什么

  •  9
  • Nick Randell  · 技术社区  · 17 年前

    我有一个包含纬度和经度值的CSV文件,例如:

    “东经25°36'55.57”“北纬45°39'12.52”

    有人能用一段快速简单的C代码将其转换为双值吗?

    谢谢

    3 回复  |  直到 17 年前
        1
  •  11
  •   Mike Weller    17 年前

    如果您是指执行此操作的C#代码:

    结果=25+(36/60)+(55.57/3600)

    首先,需要使用正则表达式或其他机制解析表达式,并将其拆分为各个部分。然后:

    String hour = "25";
    String minute = "36";
    String second = "55.57";
    Double result = (hour) + (minute) / 60 + (second) / 3600;
    

    对于计算,西/东后缀在西半球替换为负号。令人困惑的是,人们有时也会看到对东方的否定。首选的约定——东方是正的——与北极向上的右手笛卡尔坐标系一致。然后,可以将特定经度与特定纬度(在北半球通常为正值)结合起来,给出地球表面上的精确位置。 http://en.wikipedia.org/wiki/Longitude

        2
  •  8
  •   Mike Weller    16 年前

    谢谢你的快速回答。根据amdfan的回答,我将这段代码放在一起,用C#完成这项工作。

    /// <summary>The regular expression parser used to parse the lat/long</summary>
    private static Regex Parser = new Regex("^(?<deg>[-+0-9]+)[^0-9]+(?<min>[0-9]+)[^0-9]+(?<sec>[0-9.,]+)[^0-9.,ENSW]+(?<pos>[ENSW]*)$");
    
    /// <summary>Parses the lat lon value.</summary>
    /// <param name="value">The value.</param>
    /// <remarks>It must have at least 3 parts 'degrees' 'minutes' 'seconds'. If it 
    /// has E/W and N/S this is used to change the sign.</remarks>
    /// <returns></returns>
    public static double ParseLatLonValue(string value)
    {
        // If it starts and finishes with a quote, strip them off
        if (value.StartsWith("\"") && value.EndsWith("\""))
        {
            value = value.Substring(1, value.Length - 2).Replace("\"\"", "\"");
        }
    
        // Now parse using the regex parser
        Match match = Parser.Match(value);
        if (!match.Success)
        {
            throw new ArgumentException(string.Format(CultureInfo.CurrentUICulture, "Lat/long value of '{0}' is not recognised", value));
        }
    
        // Convert - adjust the sign if necessary
        double deg = double.Parse(match.Groups["deg"].Value);
        double min = double.Parse(match.Groups["min"].Value);
        double sec = double.Parse(match.Groups["sec"].Value);
        double result = deg + (min / 60) + (sec / 3600);
        if (match.Groups["pos"].Success)
        {
            char ch = match.Groups["pos"].Value[0];
            result = ((ch == 'S') || (ch == 'W')) ? -result : result;
        }
        return result;
    }
    
        3
  •  0
  •   Dan Blair    17 年前

    然后每度60分钟,每分钟60秒。 然后你必须自己保留E和N。

    不过,一般来说,情况并非如此。