[英]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). 我使用sqrt((x2-x1)^ 2 +(y2-y1)^ 2)将lat转换为长距离。 If I have only lat and long I convert it easily, but my input file has other values too.
如果我只有lat和long我很容易转换它,但我的输入文件也有其他值。 So, I need to skip those values and take only lat and long to convert it to distance.
因此,我需要跳过这些值并仅使用lat和long将其转换为距离。 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): 我的输入文件看起来像这样(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());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.