forked from Archive/PX4-Autopilot
geo lib: major rewrite of map_projection_XXX functions
This commit is contained in:
parent
7be1c400f9
commit
2284a7e985
|
@ -52,124 +52,58 @@
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Azimuthal Equidistant Projection
|
||||||
|
* formulas according to: http://mathworld.wolfram.com/AzimuthalEquidistantProjection.html
|
||||||
|
*/
|
||||||
|
|
||||||
/* values for map projection */
|
__EXPORT void map_projection_init(struct map_projection_reference_s *ref, double lat_0, double lon_0) //lat_0, lon_0 are expected to be in correct format: -> 47.1234567 and not 471234567
|
||||||
static double phi_1;
|
|
||||||
static double sin_phi_1;
|
|
||||||
static double cos_phi_1;
|
|
||||||
static double lambda_0;
|
|
||||||
static double scale;
|
|
||||||
|
|
||||||
__EXPORT void map_projection_init(double lat_0, double lon_0) //lat_0, lon_0 are expected to be in correct format: -> 47.1234567 and not 471234567
|
|
||||||
{
|
{
|
||||||
/* notation and formulas according to: http://mathworld.wolfram.com/AzimuthalEquidistantProjection.html */
|
ref->lat = lat_0 / 180.0 * M_PI;
|
||||||
phi_1 = lat_0 / 180.0 * M_PI;
|
ref->lon = lon_0 / 180.0 * M_PI;
|
||||||
lambda_0 = lon_0 / 180.0 * M_PI;
|
|
||||||
|
|
||||||
sin_phi_1 = sin(phi_1);
|
|
||||||
cos_phi_1 = cos(phi_1);
|
|
||||||
|
|
||||||
/* calculate local scale by using the relation of true distance and the distance on plane */ //TODO: this is a quick solution, there are probably easier ways to determine the scale
|
|
||||||
|
|
||||||
/* 1) calculate true distance d on sphere to a point: http://www.movable-type.co.uk/scripts/latlong.html */
|
|
||||||
|
|
||||||
double lat1 = phi_1;
|
|
||||||
double lon1 = lambda_0;
|
|
||||||
|
|
||||||
double lat2 = phi_1 + 0.5 / 180 * M_PI;
|
|
||||||
double lon2 = lambda_0 + 0.5 / 180 * M_PI;
|
|
||||||
double sin_lat_2 = sin(lat2);
|
|
||||||
double cos_lat_2 = cos(lat2);
|
|
||||||
double d = acos(sin(lat1) * sin_lat_2 + cos(lat1) * cos_lat_2 * cos(lon2 - lon1)) * CONSTANTS_RADIUS_OF_EARTH;
|
|
||||||
|
|
||||||
/* 2) calculate distance rho on plane */
|
|
||||||
double k_bar = 0;
|
|
||||||
double c = acos(sin_phi_1 * sin_lat_2 + cos_phi_1 * cos_lat_2 * cos(lon2 - lambda_0));
|
|
||||||
|
|
||||||
if (0 != c)
|
|
||||||
k_bar = c / sin(c);
|
|
||||||
|
|
||||||
double x2 = k_bar * (cos_lat_2 * sin(lon2 - lambda_0)); //Projection of point 2 on plane
|
|
||||||
double y2 = k_bar * ((cos_phi_1 * sin_lat_2 - sin_phi_1 * cos_lat_2 * cos(lon2 - lambda_0)));
|
|
||||||
double rho = sqrt(pow(x2, 2) + pow(y2, 2));
|
|
||||||
|
|
||||||
scale = d / rho;
|
|
||||||
|
|
||||||
|
ref->sin_lat = sin(ref->lat);
|
||||||
|
ref->cos_lat = cos(ref->lat);
|
||||||
}
|
}
|
||||||
|
|
||||||
__EXPORT void map_projection_project(double lat, double lon, float *x, float *y)
|
__EXPORT void map_projection_project(struct map_projection_reference_s *ref, double lat, double lon, float *x, float *y)
|
||||||
{
|
{
|
||||||
/* notation and formulas accoring to: http://mathworld.wolfram.com/AzimuthalEquidistantProjection.html */
|
double lat_rad = lat / 180.0 * M_PI;
|
||||||
double phi = lat / 180.0 * M_PI;
|
double lon_rad = lon / 180.0 * M_PI;
|
||||||
double lambda = lon / 180.0 * M_PI;
|
|
||||||
|
|
||||||
double sin_phi = sin(phi);
|
double sin_lat = sin(lat_rad);
|
||||||
double cos_phi = cos(phi);
|
double cos_lat = cos(lat_rad);
|
||||||
|
double cos_d_lon = cos(lon_rad - ref->lon);
|
||||||
|
|
||||||
double k_bar = 0;
|
double c = acos(ref->sin_lat * sin_lat + ref->cos_lat * cos_lat * cos_d_lon);
|
||||||
/* using small angle approximation (formula in comment is without aproximation) */
|
double k = (c == 0.0) ? 1.0 : (c / sin(c));
|
||||||
double c = acos(sin_phi_1 * sin_phi + cos_phi_1 * cos_phi * (1 - pow((lambda - lambda_0), 2) / 2)); //double c = acos( sin_phi_1 * sin_phi + cos_phi_1 * cos_phi * cos(lambda - lambda_0) );
|
|
||||||
|
|
||||||
if (0 != c)
|
*x = k * (ref->cos_lat * sin_lat - ref->sin_lat * cos_lat * cos_d_lon) * CONSTANTS_RADIUS_OF_EARTH;
|
||||||
k_bar = c / sin(c);
|
*y = k * (cos_lat * sin(lon_rad - ref->lon)) * CONSTANTS_RADIUS_OF_EARTH;
|
||||||
|
|
||||||
/* using small angle approximation (formula in comment is without aproximation) */
|
|
||||||
*y = k_bar * (cos_phi * (lambda - lambda_0)) * scale;//*y = k_bar * (cos_phi * sin(lambda - lambda_0)) * scale;
|
|
||||||
*x = k_bar * ((cos_phi_1 * sin_phi - sin_phi_1 * cos_phi * (1 - pow((lambda - lambda_0), 2) / 2))) * scale; // *x = k_bar * ((cos_phi_1 * sin_phi - sin_phi_1 * cos_phi * cos(lambda - lambda_0))) * scale;
|
|
||||||
|
|
||||||
// printf("%phi_1=%.10f, lambda_0 =%.10f\n", phi_1, lambda_0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
__EXPORT void map_projection_reproject(float x, float y, double *lat, double *lon)
|
__EXPORT void map_projection_reproject(struct map_projection_reference_s *ref, float x, float y, double *lat, double *lon)
|
||||||
{
|
{
|
||||||
/* notation and formulas accoring to: http://mathworld.wolfram.com/AzimuthalEquidistantProjection.html */
|
float x_rad = x / CONSTANTS_RADIUS_OF_EARTH;
|
||||||
|
float y_rad = y / CONSTANTS_RADIUS_OF_EARTH;
|
||||||
double x_descaled = x / scale;
|
double c = sqrtf(x_rad * x_rad + y_rad * y_rad);
|
||||||
double y_descaled = y / scale;
|
|
||||||
|
|
||||||
double c = sqrt(pow(x_descaled, 2) + pow(y_descaled, 2));
|
|
||||||
double sin_c = sin(c);
|
double sin_c = sin(c);
|
||||||
double cos_c = cos(c);
|
double cos_c = cos(c);
|
||||||
|
|
||||||
double lat_sphere = 0;
|
double lat_rad;
|
||||||
|
double lon_rad;
|
||||||
|
|
||||||
if (c != 0)
|
if (c != 0.0) {
|
||||||
lat_sphere = asin(cos_c * sin_phi_1 + (x_descaled * sin_c * cos_phi_1) / c);
|
lat_rad = asin(cos_c * ref->sin_lat + (x_rad * sin_c * ref->cos_lat) / c);
|
||||||
else
|
lon_rad = (ref->lon + atan2(y_rad * sin_c, c * ref->cos_lat * cos_c - x_rad * ref->sin_lat * sin_c));
|
||||||
lat_sphere = asin(cos_c * sin_phi_1);
|
|
||||||
|
|
||||||
// printf("lat_sphere = %.10f\n",lat_sphere);
|
|
||||||
|
|
||||||
double lon_sphere = 0;
|
|
||||||
|
|
||||||
if (phi_1 == M_PI / 2) {
|
|
||||||
//using small angle approximation (formula in comment is without aproximation)
|
|
||||||
lon_sphere = (lambda_0 - y_descaled / x_descaled); //lon_sphere = (lambda_0 + atan2(-y_descaled, x_descaled));
|
|
||||||
|
|
||||||
} else if (phi_1 == -M_PI / 2) {
|
|
||||||
//using small angle approximation (formula in comment is without aproximation)
|
|
||||||
lon_sphere = (lambda_0 + y_descaled / x_descaled); //lon_sphere = (lambda_0 + atan2(y_descaled, x_descaled));
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
lat_rad = ref->lat;
|
||||||
lon_sphere = (lambda_0 + atan2(y_descaled * sin_c , c * cos_phi_1 * cos_c - x_descaled * sin_phi_1 * sin_c));
|
lon_rad = ref->lon;
|
||||||
//using small angle approximation
|
|
||||||
// double denominator = (c * cos_phi_1 * cos_c - x_descaled * sin_phi_1 * sin_c);
|
|
||||||
// if(denominator != 0)
|
|
||||||
// {
|
|
||||||
// lon_sphere = (lambda_0 + (y_descaled * sin_c) / denominator);
|
|
||||||
// }
|
|
||||||
// else
|
|
||||||
// {
|
|
||||||
// ...
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// printf("lon_sphere = %.10f\n",lon_sphere);
|
*lat = lat_rad * 180.0 / M_PI;
|
||||||
|
*lon = lon_rad * 180.0 / M_PI;
|
||||||
*lat = lat_sphere * 180.0 / M_PI;
|
|
||||||
*lon = lon_sphere * 180.0 / M_PI;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -67,6 +67,14 @@ struct crosstrack_error_s {
|
||||||
float bearing; // Bearing in radians to closest point on line/arc
|
float bearing; // Bearing in radians to closest point on line/arc
|
||||||
} ;
|
} ;
|
||||||
|
|
||||||
|
/* lat/lon are in radians */
|
||||||
|
struct map_projection_reference_s {
|
||||||
|
double lat;
|
||||||
|
double lon;
|
||||||
|
double sin_lat;
|
||||||
|
double cos_lat;
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initializes the map transformation.
|
* Initializes the map transformation.
|
||||||
*
|
*
|
||||||
|
@ -74,7 +82,7 @@ struct crosstrack_error_s {
|
||||||
* @param lat in degrees (47.1234567°, not 471234567°)
|
* @param lat in degrees (47.1234567°, not 471234567°)
|
||||||
* @param lon in degrees (8.1234567°, not 81234567°)
|
* @param lon in degrees (8.1234567°, not 81234567°)
|
||||||
*/
|
*/
|
||||||
__EXPORT void map_projection_init(double lat_0, double lon_0);
|
__EXPORT void map_projection_init(struct map_projection_reference_s *ref, double lat_0, double lon_0);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Transforms a point in the geographic coordinate system to the local azimuthal equidistant plane
|
* Transforms a point in the geographic coordinate system to the local azimuthal equidistant plane
|
||||||
|
@ -83,7 +91,7 @@ __EXPORT void map_projection_init(double lat_0, double lon_0);
|
||||||
* @param lat in degrees (47.1234567°, not 471234567°)
|
* @param lat in degrees (47.1234567°, not 471234567°)
|
||||||
* @param lon in degrees (8.1234567°, not 81234567°)
|
* @param lon in degrees (8.1234567°, not 81234567°)
|
||||||
*/
|
*/
|
||||||
__EXPORT void map_projection_project(double lat, double lon, float *x, float *y);
|
__EXPORT void map_projection_project(struct map_projection_reference_s *ref, double lat, double lon, float *x, float *y);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Transforms a point in the local azimuthal equidistant plane to the geographic coordinate system
|
* Transforms a point in the local azimuthal equidistant plane to the geographic coordinate system
|
||||||
|
@ -93,7 +101,7 @@ __EXPORT void map_projection_project(double lat, double lon, float *x, float *y)
|
||||||
* @param lat in degrees (47.1234567°, not 471234567°)
|
* @param lat in degrees (47.1234567°, not 471234567°)
|
||||||
* @param lon in degrees (8.1234567°, not 81234567°)
|
* @param lon in degrees (8.1234567°, not 81234567°)
|
||||||
*/
|
*/
|
||||||
__EXPORT void map_projection_reproject(float x, float y, double *lat, double *lon);
|
__EXPORT void map_projection_reproject(struct map_projection_reference_s *ref, float x, float y, double *lat, double *lon);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the distance to the next waypoint in meters.
|
* Returns the distance to the next waypoint in meters.
|
||||||
|
|
Loading…
Reference in New Issue