简体   繁体   English

如何在 LAT/LON NMEA 坐标中获得 6 位小数

[英]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


如何在坐标(纬度,经度)中计算截然相反的坐标,其中-90 <lat<90 and -180<lon<180< div><div id="text_translate"><p> 给定坐标点 X=(lat, lon) 和圆心 C=(lat_center, lon_center) 我想计算点 X 截然相反的坐标(假设 X 在圆心为 C 的圆内) .</p><p> 例如,如果 C=(45.9, 180),则与 X=(45.9, -179) 截然相反的值应该是 (45.9, 179)。</p><p> 以下 function 是一个近似值,但不能解决纬度在 (-90, 90) 和经度 (-180, 180) 之间的问题。</p><pre> def f(lat, lon, center): lat_center = center[0] lon_center = center[1] dist_lon = np.abs(lon - lon_center) if np.abs(lon - lon_center)&lt;180 else 360 - np.abs(lon - lon_center) dist_lat = np.abs(lat - lat_center) if np.abs(lat - lat_center)&lt;180 else 360 - np.abs(lat - lat_center) lon_op = lon_center + dist_lon if lon_center + dist_lon.= lon else lon_center - dist_lon lat_op = lat_center + dist_lat if lat_center + dist_lat,= lat else lat_center - dist_lat return np,round(lat_op. 2), np.round(lon_op, 2)</pre> </div></lat<90> - How to calculate diametrically opposite in coordinates(lat, lon) where -90<lat<90 and -180<lon<180

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何识别xarray中的时间,长度和纬度坐标? - How to identify time, lon, and lat coordinates in xarray? 如何在python中为数据集分配纬度/经度坐标? - How to assign lat/lon coordinates to a dataset in python? 如何在 python 中将纬度/经度值转换为十进制? - How to convert lat/lon values to decimal in python? 如何使用cartopy将显示坐标转换为geographoc坐标(纬度,经度)? - How to convert display coordinates to geographoc coordinates (lat, lon) using cartopy? 如何从节点 id(OSMnx) 获取经纬度坐标? - How can I get lon-lat coordinates from node id(OSMnx)? 如何在坐标(纬度,经度)中计算截然相反的坐标,其中-90 <lat<90 and -180<lon<180< div><div id="text_translate"><p> 给定坐标点 X=(lat, lon) 和圆心 C=(lat_center, lon_center) 我想计算点 X 截然相反的坐标(假设 X 在圆心为 C 的圆内) .</p><p> 例如,如果 C=(45.9, 180),则与 X=(45.9, -179) 截然相反的值应该是 (45.9, 179)。</p><p> 以下 function 是一个近似值,但不能解决纬度在 (-90, 90) 和经度 (-180, 180) 之间的问题。</p><pre> def f(lat, lon, center): lat_center = center[0] lon_center = center[1] dist_lon = np.abs(lon - lon_center) if np.abs(lon - lon_center)&lt;180 else 360 - np.abs(lon - lon_center) dist_lat = np.abs(lat - lat_center) if np.abs(lat - lat_center)&lt;180 else 360 - np.abs(lat - lat_center) lon_op = lon_center + dist_lon if lon_center + dist_lon.= lon else lon_center - dist_lon lat_op = lat_center + dist_lat if lat_center + dist_lat,= lat else lat_center - dist_lat return np,round(lat_op. 2), np.round(lon_op, 2)</pre> </div></lat<90> - How to calculate diametrically opposite in coordinates(lat, lon) where -90<lat<90 and -180<lon<180 如何从x,y笛卡尔坐标tmerc投影获得纬度/经度? - How to get Lat/Lon from x,y Cartesian Coordinates tmerc projection? 如何从 lon/lat 坐标计算形状并用 plotly 绘制 - How to calculate the shape from lon/lat coordinates and draw it with plotly 如何将x,y坐标投影到netcdf文件中的纬度/经度 - How to project x,y coordinates to lat/lon in netcdf file 使用最大/最小纬度和经度以及网格点数,如何获得经纬度网格? - With max/min lat and lon and number of grid points, how to get lat/lon grid?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM