Richie 的个人资料The Sandpit - "We hack s...日志列表 工具 帮助
7月31日

Convert Decimal Degrees to Degrees Minutes Seconds

This post contains C# code to convert decimal degrees into degrees, minute and seconds.  The screenshot below demonstration the code being using to convert a latitude (in decimal degrees) to a correctly formatted DMS value.

Sample application to convert DD to DMS

Here is the code associated with the button that does the conversion.

private void Button_Click(object sender, EventArgs e) {
    if (sender == this.buttonConvertToDms) {
        if (string.IsNullOrEmpty(this.textBoxDD.Text)) { return; }
        double d;
        if (!double.TryParse(this.textBoxDD.Text, out d)) { return; }
        CoordinateType type = this.radioButtonLat.Checked ? CoordinateType.latitude : CoordinateType.longitude;
        this.textBoxDMS.Text = Engine.DDtoDMS(d, type);
    }
    else if (sender == this.buttonClose) {
        this.Close();
    }
}

Lastly, here is the formatting code.  Unlike similar sample, this code will pad minutes and seconds with zeros and append a N, S, W or E compass heading.

public enum CoordinateType { longitude, latitude };
public static string DDtoDMS(double coordinate, CoordinateType type) {
    // Set flag if number is negative
    bool neg = coordinate < 0d;

    // Work with a positive number
    coordinate = Math.Abs(coordinate);

    // Get d/m/s components
    double d = Math.Floor(coordinate);
    coordinate -= d;
    coordinate *= 60;
    double m = Math.Floor(coordinate);
    coordinate -= m;
    coordinate *= 60;
    double s = Math.Round(coordinate);

    // Create padding character
    char pad;
    char.TryParse("0", out pad);

    // Create d/m/s strings
    string dd = d.ToString();
    string mm = m.ToString().PadLeft(2, pad);
    string ss = s.ToString().PadLeft(2, pad);

    // Append d/m/s
    string dms = string.Format("{0}°{1}'{2}\"", dd, mm, ss);

    // Append compass heading
    switch (type) {
        case CoordinateType.longitude:
            dms += neg ? "W" : "E";
            break;
        case CoordinateType.latitude:
            dms += neg ? "S" : "N";
            break;
    }

    // Return formated string
    return dms;
}
Technorati Tags:

评论 (2)

请稍候...
很抱歉,您输入的评论太长。请缩短您的评论。
您没有输入任何内容,请重试。
很抱歉,我们当前无法添加您的评论。请稍后重试。
若要添加评论,需要您的家长授予您相应权限。请求权限
您的家长禁用了评论功能。
很抱歉,我们当前无法删除您的评论。请稍后重试。
您已超过了一天之内允许提供的评论数上限。请在 24 小时后重试。
因为我们的系统表明您可能在向其他用户提供垃圾评论,您的帐户已禁用了评论功能。如果您认为我们错误地禁用了您的帐户,请联系 Windows Live 支持部门
完成下面的安全检查,您提供评论的过程才能完成。
您在安全检查中键入的字符必须与图片或音频中的字符一致。
Carmichael​Richie 在此页禁用了评论功能。

Dylan also suggested the following error checking...

if (double.IsNaN(coordinate)){

    return string.Empty;

}

 

switch (latlon){

    case CoordinateType.Latitude:

        if (coordinate > 90.0D || coordinate < -90.0D){

            throw new ArgumentOutOfRangeException(

                "coordinate",

                "Latitudes fall between -90 (South Pole) and +90 (North Pole)");

        }

        break;

        case CoordinateType.Longitude:

            if (coordinate > 180.0D || coordinate < -180.00){

                throw new ArgumentOutOfRangeException(

                    "coordinate",

                    "Longitudes cannot be greater than +180 or less than -180 (International Date Line)");

            }

            break;

}

 

 

8 月 7 日
匿名 的图片
Neil 发表:
Hey Richie.

Thanks for posting the utility. I think you can alter the format statement so it also takes care of the padding also...

string dms = string.Format("{0:00}°{1:00}'{2:00}\"", d, m, s);

Neil
8 月 2 日

引用通告

引用此项的网络日志