【问题标题】:Latitude and Longitude keep changing every time I convert from Degrees Minutes Seconds to Decimal Degrees in c#每次我在 c# 中从度分秒转换为十进制度时,纬度和经度都会不断变化
【发布时间】:2010-12-07 16:27:13
【问题描述】:

如果我输入的位置:

纬度 = 28 度 45 分 12 秒 经度 = 81 度 39 分 32.4 秒

使用以下代码将其转换为十进制度数格式以存储在数据库中:

Coordinates coordinates = new Coordinates();
coordinates.LatitudeDirection = this.radLatNorth.Checked ? Coordinates.Direction.North : Coordinates.Direction.South;
coordinates.LatitudeDegree = this.ConvertDouble(this.txtLatDegree.Text);
coordinates.LatitudeMinute = this.ConvertDouble(this.txtLatMinute.Text);
coordinates.LatitudeSecond = this.ConvertDouble(this.txtLatSecond.Text);
coordinates.LongitudeDirection = radLongEast.Checked ? Coordinates.Direction.East :     Coordinates.Direction.West; 
coordinates.LongitudeDegree = this.ConvertDouble(this.txtLongDegree.Text);
coordinates.LongitudeMinute = this.ConvertDouble(this.txtLongMinute.Text);
coordinates.LongitudeSecond = this.ConvertDouble(this.txtLongSecond.Text);
//gets the calulated fields of Lat and Long
coordinates.ConvertDegreesMinutesSeconds();

在上面的代码中,ConvertDouble 定义为:

  private double ConvertDouble(string value)
  {
        double newValue = 0;
        double.TryParse(value, out newValue);

        return newValue;
  }

ConvertDegreesMinutesSeconds 定义为:

public void ConvertDegreesMinutesSeconds()
{
    this.Latitude = this.LatitudeDegree + (this.LatitudeMinute / 60) + (this.LatitudeSecond / 3600);
        this.Longitude = this.LongitudeDegree + (this.LongitudeMinute / 60) + (this.LongitudeSecond / 3600);

        //adds the negative sign
        if (LatitudeDirection == Direction.South)
        {
            this.Latitude = 0 - this.Latitude;

        }
        else if (LongitudeDirection == Direction.West)
        {
            this.Longitude = 0 - this.Longitude;
        }
    }

如果我没有对纬度或经度进行任何更改,然后单击基本上再次执行上述计算的应用更改,它会在数据库中生成不同的纬度和经度。每次我去编辑它并且不进行更改时都会发生这种情况(我只需单击 Apply Changes,它会再次进行计算并得到不同的结果)。

在上述场景中,新的经纬度为:

纬度 = 28 度 45 分 12 秒 经度 = 81 度 40 分 32.4 秒

如果我再做一次,它会变成:

纬度 = 28 度 45 分 12 秒 经度 = 81 度 41 分 32.4 秒

另一部分是,当我进入编辑时,它采用纬度和经度的十进制度格式并将其转换为度分秒格式并将它们放入各自的文本框中。代码是:

public void SetFields()
{
    Coordinates coordinateLocation = new Coordinates();
    coordinateLocation.Latitude = this.Latitude;
    coordinateLocation.Longitude = this.Longitude;
    coordinateLocation.ConvertDecimal();

    this.radLatNorth.Checked =
        coordinateLocation.LatitudeDirection == Coordinates.Direction.North;
    this.radLatSouth.Checked = !this.radLatNorth.Checked;
    this.txtLatDegree.Text = coordinateLocation.LatitudeDegree.ToString().Replace("-", string.Empty);
    this.txtLatMinute.Text = Math.Round(coordinateLocation.LatitudeMinute, 0).ToString().Replace("-", string.Empty);
    this.txtLatSecond.Text = Math.Round(coordinateLocation.LatitudeSecond, 2).ToString().Replace("-", string.Empty);

    this.radLongEast.Checked =
        coordinateLocation.LongitudeDirection == Coordinates.Direction.East;
    this.radLongWest.Checked = !this.radLongEast.Checked;
    this.txtLongDegree.Text = coordinateLocation.LongitudeDegree.ToString().Replace("-", string.Empty); ;
    this.txtLongMinute.Text = Math.Round(coordinateLocation.LongitudeMinute, 0).ToString().Replace("-", string.Empty);
    this.txtLongSecond.Text = Math.Round(coordinateLocation.LongitudeSecond, 2).ToString().Replace("-", string.Empty);
}

从上面的例子可以看到Minute一直在增加1,这说明为什么它在数据库中生成了不同的十进制度经纬度,所以我猜问题更多的是在上面的区域,但我不确定它在哪里或为什么这样做?

  public void ConvertDecimal()
    {
        this.LatitudeDirection = this.Latitude > 0 ? Direction.North : Direction.South;
        this.LatitudeDegree = (int)Math.Truncate(this.Latitude);
        if (LatitudeDirection == Direction.South)
        {
            this.LatitudeDegree = 0 - this.LatitudeDegree;
        }            
        this.LatitudeMinute = (this.Latitude - Math.Truncate(this.Latitude)) * 60;
        this.LatitudeSecond = (this.LatitudeMinute - Math.Truncate(this.LatitudeMinute)) * 60;

        this.LongitudeDirection = this.Longitude > 0 ? Direction.East : Direction.West;
        this.LongitudeDegree = (int)Math.Truncate(this.Longitude);            
        if (LongitudeDirection == Direction.West)
        {
            this.LongitudeDegree = 0 - this.LongitudeDegree;
        }            
        this.LongitudeMinute = (this.Longitude - Math.Truncate(this.Longitude)) * 60;
        this.LongitudeSecond = (this.LongitudeMinute - Math.Truncate(this.LongitudeMinute)) * 60;
    }

【问题讨论】:

  • 你能发布 CoordinateLocation.ConvertDecimal 代码吗?
  • ConvertDecimal 代码已发布。

标签: c# asp.net math


【解决方案1】:

您需要截断您的 Minutes(也可能是 Seconds,我不确定)而不是四舍五入。请注意,下面使用的 Math.Round 只是为了将 Minutes/Seconds 组件设置为所需的有效数字位数。

double Minutes(double decimalDegrees)
{
  //Get hours
  double h = Math.Truncate(lat);
  //Get minutes + seconds
  double x = (Math.Abs(h - Math.Round(lat, 12)) * 60.0);
  //Everything after the decimal is seconds
  double min = Math.Truncate(x);
  return min;
}

我将此转换代码基于此 codeplex 项目:http://geoframework.codeplex.com/ 查看 Latitude 类。

【讨论】:

  • 谢谢,到目前为止,这似乎已经解决了。我不确定这是否可能,但有人会输入类似 30.42 分钟的内容。如果他们这样做了,那么 truncate 将不起作用。
  • @Xaisoft 在我看来,分钟总是一个整数值。如果有人要输入 30.42 分钟,那不就等于 30 分钟和 0.42 * 60 秒吗?
  • 你是对的,请原谅我,我是新手,而且我不是输入的人,所以谁知道人们会如何输入。感谢您迄今为止的帮助。
  • 顺便说一句,我只是将 SetFields 方法中的 Math.Round 更改为 Math.Truncate 几分钟,这似乎有效。我离开 Math.Round 几秒钟,因为我不想实际截断它,因为它们可以输入例如 30.42 或 30.12 之类的值
【解决方案2】:

在显示它们而不是截断它们或使用 Math.Floor 时,你正在四舍五入你的分钟和秒。由于您有超过 30 秒的时间,因此您的分钟有一个超过 0.5 的小数值,因此它会向上取整。

【讨论】:

    猜你喜欢
    • 2017-12-30
    • 1970-01-01
    • 1970-01-01
    • 2010-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多