如何计算C#中lat长坐标值的距离

时间:2016-09-26 08:08:17

标签: c# facebook-c#-sdk

我使用sqrt((x2-x1)^ 2 +(y2-y1)^ 2)将lat转换为长距离。如果我只有lat和long我很容易转换它,但我的输入文件也有其他值。因此,我需要跳过这些值并仅使用lat和long将其转换为距离。你能帮我修改代码以获得所需的结果吗?

double x1;
double x2;
double y1;
double y2;
double dist;
double seg;
string Inputpath = @"C:\New folder\utm.txt";
string appendText=string.Empty;

string readText = File.ReadAllText(Inputpath);
string[] stringsplitter = { "\r\n" };
string[] pointsArray = readText.Split(stringsplitter, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < pointsArray.Length - 1; i++)
{
    if ((pointsArray[i] == "1") || (pointsArray[i] == "4") || (pointsArray[i] == "3 0") || (pointsArray[i] == "2 0"))
    {
    }
    else
    {
        if (string.IsNullOrEmpty(pointsArray[i + 1]))
            return;
        string[] currentptDetails = pointsArray[i].Split(' ');
        x1 = Convert.ToDouble(currentptDetails[0]);
        x2 = Convert.ToDouble(currentptDetails[1]);
        string[] nxtptDetails = pointsArray[i + 1].Split(' ');
        y1 = Convert.ToDouble(nxtptDetails[0]);
        y2 = Convert.ToDouble(nxtptDetails[1]);
        dist = Math.Sqrt((Math.Pow((y1 - x1), 2)) + (Math.Pow((y2 - x2), 2)));
        seg = dist / Convert.ToDouble(textBox47.Text);
        appendText = appendText+seg.ToString()+" "+"1."+Environment.NewLine;/*+ "x1: " + x1.ToString() + Environment.NewLine + "x2: " + x2.ToString() + Environment.NewLine + "y1: " + y1.ToString() + Environment.NewLine + "y2: " + y2.ToString() + Environment.NewLine;*/
    }
}

using (FileStream fs1 = new FileStream(@"C:\New Folder\ctl.txt", FileMode.Append, FileAccess.Write))
{
    using (StreamWriter sw1 = new StreamWriter(fs1))
    {
        sw1.Write(appendText);
        MessageBox.Show("Segment is obtained!");
        sw1.Close();
    }
}

using (FileStream fs1 = new FileStream(@"C:\New Folder\ctl.txt", FileMode.Append, FileAccess.Write))
{
    using (StreamWriter sw1 = new StreamWriter(fs1))
    {
        sw1.Write(0);
        sw1.Close();
    }
}

我的输入文件如下所示(utm.text):

1  
4  
3 0  
102359655.484135 133380444.670738  
102636303.281131 133799218.776379  
103074890.121061 134357355.374865  
2 0  
103081335.979444 134297999.743935  
104081842.971063 130759524.079145  
2 0  
104071186.42401 130776902.916433  
103361487.055725 129967846.904082  
2 0  
103358463.55571 129997705.704386  
102402071.782712 133279959.556572  

仅将长值转换为距离。

2 个答案:

答案 0 :(得分:0)

你可以使用这个:^(\-?\d+(\.\d+)?),\s*(\-?\d+(\.\d+)?)$正则表达式来检查它是否是纬度/经度坐标。

答案 1 :(得分:0)

我猜,实际文件的格式是

1    <- version or whatever 
4    <- number of groups (4)
3 0  <- 3 items in the group
102359655.484135 133380444.670738  
102636303.281131 133799218.776379  
103074890.121061 134357355.374865  
2 0  <- 2 items in the group 
103081335.979444 134297999.743935  
104081842.971063 130759524.079145  
2 0  <- 2 items in the group 
104071186.42401 130776902.916433  
103361487.055725 129967846.904082  
2 0  <- 2 items in the group 
103358463.55571 129997705.704386  
102402071.782712 133279959.556572

所以你可以实现一个有限状态机(类似这样):

  private static IEnumerable<String> ConvertMe(String path) {
    int kind = 0;
    int groupCount = 0;

    foreach (string line in File.ReadLines(path)) {
      if (kind == 0) { // file header: 1st line
        kind = 1;
        yield return line;  

        continue; 
      }       
      else if (kind == 1) { // file header: 2nd line
        kind = 2;
        yield return line;

        continue; 
      }   

      string items[] = line.Split(' ');

      if (kind = 2) { // group header
        kind = 3;
        yield return line; 

        groupCount = int.Parse(items[0]); 

        continue;
      }

      // Longitude + Latitude
      double lat = double.Parse(items[0]); 
      double lon = double.Parse(items[1]);
      double distance = ...; //TODO: your formula here

      yield return distance.ToString(CultureInfo.InvariantCulture);

      groupCount -= 1;

      if (groupCount <= 0) 
        kind = 2;
    } 
  }

...

  String path = @"C:\MyData.txt";

  // Materialization (.ToList) if we write in the same file
  File.WriteAllLines(path, ConvertMe(path).ToList());