我真的希望有人可以帮助我!我正在尝试让这个脚本按我的意愿运行,但我似乎无法掌握它。
包含要处理数据的文件是从GPS输入的,如下所示:
Line 20081002-1119.nmea
$GPGGA,094406.00,5849.40174,N,01738.15828,E,2,08,00.9,00003.26,M,0024.93,M,005,0734*62
$GPGGA,094407.00,5849.40177,N,01738.15827,E,2,08,00.9,00003.22,M,0024.93,M,005,0734*6B
$GPGGA,094408.00,5849.40174,N,01738.15826,E,2,08,00.9,00003.00,M,0024.93,M,006,0734*65
$GPGGA,094409.00,5849.40171,N,01738.15831,E,2,08,00.9,00003.24,M,0024.93,M,005,0734*62
$GPGGA,094410.00,5849.40176,N,01738.15833,E,2,08,00.9,00003.29,M,0024.93,M,006,0734*61
$GPGGA,094411.00,5849.40172,N,01738.15831,E,2,08,00.9,00003.31,M,0024.93,M,004,0734*6D
$GPGGA,094412.00,5849.40172,N,01738.15830,E,2,08,00.9,00003.15,M,0024.93,M,005,0734*68
$GPGGA,094413.00,5849.40175,N,01738.15834,E,2,08,00.9,00003.18,M,0024.93,M,005,0734*67
$GPGGA,094414.00,5849.40173,N,01738.15835,E,2,08,00.9,00003.16,M,0024.93,M,006,0734*6A
EOL
我的输出文件看起来应该是这样的(现在只需要显示我想要的距离):
Line 20081002-1119.nmea
58.853952 17.643113 102.15
58.853946 17.643243 101.63
58.853939 17.643372 105.93
58.853933 17.643503 104.01
58.853927 17.643633 104.25
...
EOL
列是:经度,纬度,到上面的点的距离。
如何对两个点(我的情况下为100米)之间的给定间隔进行缩减采样?
到目前为止我编写的剧本:`
indata=open('C:/nav.nmea', 'r')
outdata=open('C:/nav_out.txt', 'w')
from math import *
coords_list=[]
coords=[]
def distance(coords_list):
for (longi2,lati2) in coords_list:
for (longi1,lati1) in coords_list:
a = sin(lati1) * sin(lati2)+cos(longi1-longi2)*cos(lati1) * cos(lati2)
c= 2* asin(sqrt(a))
s= (6367* c)/100000 # For results in meters
if s<100:
# Here I want to discard current line if not s<100 and jump to the next line
else:
"Return the valid lines"
return s
for line in indata:
if line.startswith('$GPGGA'):
data=line.split(",")
# Import only coordinates from input file
LON=float(data[2])/100
LAT=float(data[4])/100
# Convert coordinates from DDMM.MMMM to DD.DDDDDD
lon=((LON-int(LON))/60)*100+int(LON)
lat=((LAT-int(LAT))/60)*100+int(LAT)
coords_list.append((lon,lat))
outdata.writelines("%0.6f\t" %lon)
outdata.writelines("%0.6f\t" %lat)
outdata.writelines("%s \n" %distance(coords_list))
elif line.startswith('EOL'):
outdata.writelines("EOL")
elif line.startswith('Line'):
LineID=line
outdata.writelines('\n%s' %LineID)
indata.close()
outdata.close()
`
答案 0 :(得分:3)
对于曲线中的点减少,您可能希望使用Ramer–Douglas–Peucker algorithm。这样可以保持曲线的整体形状,但会删除细节,删除的数量可以通过参数控制。
请注意,链接的Wikipedia页面上的代码是伪代码,而不是python。
我找到了python implementation of the DP line-simplication algorthim,我没有对其进行测试,也无法保证其正确性。
答案 1 :(得分:1)
以下是对数据进行缩减采样的一种非常简单的方法:coord1
用于存储先前的坐标。每次循环时,都会计算coord1
与新坐标coord2
之间的距离。当距离<100时,跳过循环的其余部分。
import math
import itertools
def distance(coord1,coord2):
longi1,lati1=coord1
longi2,lati2=coord2
a = (math.sin(lati1)*math.sin(lati2)
+math.cos(longi1-longi2)*math.cos(lati1)*math.cos(lati2))
c = 2*math.asin(math.sqrt(a))
s = (6367*c)/100000 # For results in meters
return s
with open('nav.nmea', 'r') as indata:
with open('nav_out.txt', 'w') as outdata:
coords=[]
coord1=None
for line in indata:
if line.startswith('$GPGGA'):
data=line.split(",")
# Import only coordinates from input file
LON=float(data[2])/100
LAT=float(data[4])/100
# Convert coordinates from DDMM.MMMM to DD.DDDDDD
lon=((LON-int(LON))/60)*100+int(LON)
lat=((LAT-int(LAT))/60)*100+int(LAT)
coords.append((lon,lat))
if coord1 is None:
# The first time through the loop `coord1` is None
outdata.write('%0.6f\t%0.6f\t%s \n'%(lon,lat,0))
coord1=(lon,lat)
else:
coord2=(lon,lat)
dist=distance(coord1,coord2)
if dist<100:
# go back to the beginning of the loop
continue
outdata.write('%0.6f\t%0.6f\t%s \n'%(lon,lat,dist))
coord1=coord2
elif line.startswith('EOL'):
outdata.writelines("EOL")
elif line.startswith('Line'):
LineID=line
outdata.writelines('\n%s' %LineID)