[英]How to get 6 decimal places in LAT/LON NMEA Coordinates
I'm using this module:我正在使用这个模块:
https://github.com/inmcm/micropyGPS/blob/master/micropyGPS.py https://github.com/inmcm/micropyGPS/blob/master/micropyGPS.py
Unfortunately, float in micropython are limited to 5 decimal places eg.不幸的是,micropython 中的浮点数限制为小数点后 5 位,例如。 80.12345
80.12345
How to get at least 6 eg 80.123456?如何获得至少 6 个,例如 80.123456?
I know it is accurate to 1 meter, but I need more accuracy.我知道它精确到 1 米,但我需要更高的精度。
I had a look at the class MicropyGPS
, and concluded that if you don't pass 'dms'
or 'dd'
as the location_formatting
(3rd) parameter of the constructor, you will get the latitude
and longitude
properties of the class directly in their internal representation, which is a list
of 3 items:我查看了
latitude
MicropyGPS
,得出的结论是,如果您不将“ longitude
'dms'
或'dd'
作为构造函数的location_formatting
(第 3 个)参数传递,您将直接在它们的内部表示,它是 3 个项目的list
:
int
degrees int
度float
minutes float
分钟str
hemisphere ( 'N'
, 'S'
, 'E'
or 'W'
) str
半球 ( 'N'
, 'S'
, 'E'
或'W'
) For this reason you have 2 or 3 more digits of precision than a float, in this representation.因此,在此表示中,您的精度比浮点数多 2 或 3 位。 I don't know how you need latitude and longitude to be represented for your purposes.
我不知道您如何需要纬度和经度来表示您的目的。 If you need them to be a single float each, you lost.
如果您需要它们每个都成为一个浮动,那么您就输了。 If you can treat degrees and minutes separately, or if you need strings, you won.
如果您可以分别处理度数和分钟数,或者如果您需要字符串,那么您就赢了。
I also had a look at MicroPython, which I didn't know.我还看了一下我不知道的 MicroPython。 It seems that while floats have a low precision, integers have unlimited precision.
似乎虽然浮点数的精度较低,但整数的精度却是无限的。 So what you could do, if this is ok for your purposes, is convert degrees and minutes to an int, in units of 10**-6 degrees (microdegrees).
因此,如果这对您的目的来说可以的话,您可以做的是将度数和分钟数转换为 int,以 10**-6 度(微度)为单位。 Something like (untested):
类似(未经测试):
DEGREES_FACTOR = 1000000
MINUTES_FACTOR = 1000000.0 / 60.0
degrees, minutes, hemisph = my_micropyGPS.longitude
microlongitude = degrees * DEGREES_FACTOR + round(minutes * MINUTES_FACTOR)
if hemisph == 'W':
microlongitude = -microlongitude
degrees, minutes, hemisph = my_micropyGPS.latitude
microlatitude = degrees * DEGREES_FACTOR + round(minutes * MINUTES_FACTOR)
if hemisph == 'S':
microlatitude = -microlatitude
The resulting integers would be in decimal fixed point representation , with the point 6 decimal positions from the right edge.生成的整数将采用十进制定点表示,从右边缘算起 6 个小数位。 This is handy for visualization, but you could also use different units, like minutes scaled up by a power of 2, which would eliminate any conversion error.
这对于可视化来说很方便,但您也可以使用不同的单位,例如按 2 的幂放大的分钟数,这将消除任何转换错误。
Please note also that a 5-digit precision would mean that the number in your example would better be represented in decimal as 80.123
(all digits count in floating point, not only those to the right of the point).另请注意,5 位精度意味着您的示例中的数字最好用十进制表示为
80.123
(所有数字都以浮点数计算,而不仅仅是点右侧的数字)。
Thanks for the answer.感谢你的回答。 The problem is that after receiving the result I have to calculate it further, eg the distance to another point or the direction between one and the other waypoint.
问题是在收到结果后我必须进一步计算它,例如到另一个点的距离或一个和另一个航点之间的方向。 If I understand correctly, would your examples be good only for presentation of the result and not for further calculation?
如果我理解正确,您的示例是否仅用于展示结果而不用于进一步计算?
Examples code for my caluclates:我的计算示例代码:
def dist_waypoints(self, lat1, lon1, lat2, lon2):
"""
Calculate distance betweeen two coordinates.
"""
try:
lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])
result = 6371 * (acos(sin(lat1) * sin(lat2) + cos(lat1) * cos(lat2) * cos(lon1 - lon2)))
meters = int(result * 1000.0);
return meters
except Exception as e:
self.exception_save(e)
sys.exit()
return 0
def bearing_dest(self, lat1, lon1, lat2, lon2):
"""
Calculate bearing destination betweeen two coordinates.
"""
try:
theta1 = radians(lat1)
theta2 = radians(lat2)
delta1 = radians(lat2-lat1)
delta2 = radians(lon2-lon1)
y = sin(delta2) * cos(theta2)
x = cos(theta1)*sin(theta2) - sin(theta1)*cos(theta2)*cos(delta2)
brng = atan2(y,x)
bearing = degrees(brng)
bearing = (bearing + 360) % 360
return bearing
except Exception as e:
self.exception_save(e)
return 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.