简体   繁体   中英

How to calculate distance from lat long coordinate values in C#

I converted lat long to distance using sqrt((x2-x1)^2+(y2-y1)^2). If I have only lat and long I convert it easily, but my input file has other values too. So, I need to skip those values and take only lat and long to convert it to distance. Can you help me to modify the code to obtain the desired result?

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();
    }
}

My input file looks like this(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  

only lat long values to be converted to distance.

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

I guess, that the actual file's format is

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

so you can implement a Finite State Machine (something like this):

  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()); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM