From fbe63d8dc1ac00bdeacc80f2c25396ba3a7169e5 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Mon, 26 Jul 2021 19:58:35 +1000 Subject: [PATCH] map: closed form for coord_from_area() thanks to Iain Guilliard! --- MAVProxy/modules/mavproxy_map/mp_tile.py | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/MAVProxy/modules/mavproxy_map/mp_tile.py b/MAVProxy/modules/mavproxy_map/mp_tile.py index 0a4095dd3d..d4b5d57770 100755 --- a/MAVProxy/modules/mavproxy_map/mp_tile.py +++ b/MAVProxy/modules/mavproxy_map/mp_tile.py @@ -29,7 +29,7 @@ import cv2 import numpy as np -from math import log, tan, radians, degrees, sin, cos, exp, pi, asin +from math import log, tan, radians, degrees, sin, cos, exp, pi, asin, atan if sys.version_info.major < 3: from urllib2 import Request as url_request @@ -452,24 +452,15 @@ def coord_from_area(self, x, y, lat, lon, width, ground_width): y is pixel coord down from top left ''' - # ground_width is width at (0,0) - scale1 = mp_util.constrain(cos(radians(lat)), 1.0e-15, 1) - pixel_width = ground_width / float(width) - dy = y * pixel_width - (lat2,lon2) = mp_util.gps_offset(lat, lon, 0, -dy) - - # iterative form for the latitude change. We should replace this with a closed form - # solution pixel_width_equator = (ground_width / float(width)) / cos(radians(lat)) + latr = radians(lat) - for yi in range(max(0,int(y+0.5))): - pw = pixel_width_equator * cos(latr) - dlatr = pw / mp_util.radius_of_earth - latr -= dlatr - lat2 = degrees(latr) + y0 = abs(1.0/cos(latr) + tan(latr)) + lat2 = 2 * atan(y0 * exp(-(y * pixel_width_equator) / mp_util.radius_of_earth)) - pi/2.0 + lat2 = degrees(lat2) dx = pixel_width_equator * cos(radians(lat2)) * x