First round of lat,lng -> lng,lat switcheroo

This commit is contained in:
Patrick Niklaus
2016-02-23 21:23:13 +01:00
parent 03a230a768
commit eb179af1ce
70 changed files with 893 additions and 817 deletions
+62 -69
View File
@@ -17,12 +17,16 @@ namespace util
namespace coordinate_calculation
{
double haversineDistance(const int lat1, const int lon1, const int lat2, const int lon2)
double haversineDistance(const Coordinate coordinate_1, const Coordinate coordinate_2)
{
BOOST_ASSERT(lat1 != std::numeric_limits<int>::min());
auto lon1 = static_cast<int>(coordinate_1.lon);
auto lat1 = static_cast<int>(coordinate_1.lat);
auto lon2 = static_cast<int>(coordinate_2.lon);
auto lat2 = static_cast<int>(coordinate_2.lat);
BOOST_ASSERT(lon1 != std::numeric_limits<int>::min());
BOOST_ASSERT(lat2 != std::numeric_limits<int>::min());
BOOST_ASSERT(lat1 != std::numeric_limits<int>::min());
BOOST_ASSERT(lon2 != std::numeric_limits<int>::min());
BOOST_ASSERT(lat2 != std::numeric_limits<int>::min());
const double lt1 = lat1 / COORDINATE_PRECISION;
const double ln1 = lon1 / COORDINATE_PRECISION;
const double lt2 = lat2 / COORDINATE_PRECISION;
@@ -42,22 +46,12 @@ double haversineDistance(const int lat1, const int lon1, const int lat2, const i
return EARTH_RADIUS * charv;
}
double haversineDistance(const FixedPointCoordinate coordinate_1,
const FixedPointCoordinate coordinate_2)
{
return haversineDistance(coordinate_1.lat, coordinate_1.lon, coordinate_2.lat,
coordinate_2.lon);
}
double greatCircleDistance(const FixedPointCoordinate coordinate_1,
const FixedPointCoordinate coordinate_2)
{
return greatCircleDistance(coordinate_1.lat, coordinate_1.lon, coordinate_2.lat,
coordinate_2.lon);
}
double greatCircleDistance(const int lat1, const int lon1, const int lat2, const int lon2)
double greatCircleDistance(const Coordinate coordinate_1, const Coordinate coordinate_2)
{
auto lon1 = static_cast<int>(coordinate_1.lon);
auto lat1 = static_cast<int>(coordinate_1.lat);
auto lon2 = static_cast<int>(coordinate_2.lon);
auto lat2 = static_cast<int>(coordinate_2.lat);
BOOST_ASSERT(lat1 != std::numeric_limits<int>::min());
BOOST_ASSERT(lon1 != std::numeric_limits<int>::min());
BOOST_ASSERT(lat2 != std::numeric_limits<int>::min());
@@ -73,65 +67,65 @@ double greatCircleDistance(const int lat1, const int lon1, const int lat2, const
return std::hypot(x_value, y_value) * EARTH_RADIUS;
}
double perpendicularDistance(const FixedPointCoordinate source_coordinate,
const FixedPointCoordinate target_coordinate,
const FixedPointCoordinate query_location)
double perpendicularDistance(const Coordinate source_coordinate,
const Coordinate target_coordinate,
const Coordinate query_location)
{
double ratio;
FixedPointCoordinate nearest_location;
Coordinate nearest_location;
return perpendicularDistance(source_coordinate, target_coordinate, query_location,
nearest_location, ratio);
}
double perpendicularDistance(const FixedPointCoordinate segment_source,
const FixedPointCoordinate segment_target,
const FixedPointCoordinate query_location,
FixedPointCoordinate &nearest_location,
double perpendicularDistance(const Coordinate segment_source,
const Coordinate segment_target,
const Coordinate query_location,
Coordinate &nearest_location,
double &ratio)
{
using namespace coordinate_calculation;
return perpendicularDistanceFromProjectedCoordinate(
segment_source, segment_target, query_location,
{mercator::latToY(query_location.lat / COORDINATE_PRECISION),
query_location.lon / COORDINATE_PRECISION},
{static_cast<double>(toFloating(query_location.lon)),
mercator::latToY(toFloating(query_location.lat))},
nearest_location, ratio);
}
double
perpendicularDistanceFromProjectedCoordinate(const FixedPointCoordinate source_coordinate,
const FixedPointCoordinate target_coordinate,
const FixedPointCoordinate query_location,
const std::pair<double, double> projected_coordinate)
double perpendicularDistanceFromProjectedCoordinate(
const Coordinate source_coordinate,
const Coordinate target_coordinate,
const Coordinate query_location,
const std::pair<double, double> projected_xy_coordinate)
{
double ratio;
FixedPointCoordinate nearest_location;
Coordinate nearest_location;
return perpendicularDistanceFromProjectedCoordinate(source_coordinate, target_coordinate,
query_location, projected_coordinate,
query_location, projected_xy_coordinate,
nearest_location, ratio);
}
double
perpendicularDistanceFromProjectedCoordinate(const FixedPointCoordinate segment_source,
const FixedPointCoordinate segment_target,
const FixedPointCoordinate query_location,
const std::pair<double, double> projected_coordinate,
FixedPointCoordinate &nearest_location,
double &ratio)
double perpendicularDistanceFromProjectedCoordinate(
const Coordinate segment_source,
const Coordinate segment_target,
const Coordinate query_location,
const std::pair<double, double> projected_xy_coordinate,
Coordinate &nearest_location,
double &ratio)
{
using namespace coordinate_calculation;
BOOST_ASSERT(query_location.IsValid());
// initialize values
const double x = projected_coordinate.first;
const double y = projected_coordinate.second;
const double a = mercator::latToY(segment_source.lat / COORDINATE_PRECISION);
const double b = segment_source.lon / COORDINATE_PRECISION;
const double c = mercator::latToY(segment_target.lat / COORDINATE_PRECISION);
const double d = segment_target.lon / COORDINATE_PRECISION;
const double x = projected_xy_coordinate.first;
const double y = projected_xy_coordinate.second;
const double a = mercator::latToY(toFloating(segment_source.lat));
const double b = static_cast<double>(toFloating(segment_source.lon));
const double c = mercator::latToY(toFloating(segment_target.lat));
const double d = static_cast<double>(toFloating(segment_target.lon));
double p, q /*,mX*/, new_y;
if (std::abs(a - c) > std::numeric_limits<double>::epsilon())
{
@@ -184,8 +178,8 @@ perpendicularDistanceFromProjectedCoordinate(const FixedPointCoordinate segment_
else
{
// point lies in between
nearest_location.lat = static_cast<int>(mercator::yToLat(p) * COORDINATE_PRECISION);
nearest_location.lon = static_cast<int>(q * COORDINATE_PRECISION);
nearest_location.lon = toFixed(FloatLongitude(q));
nearest_location.lat = toFixed(FloatLatitude(mercator::yToLat(p)));
}
BOOST_ASSERT(nearest_location.IsValid());
@@ -206,14 +200,13 @@ double radToDeg(const double radian)
return radian * (180.0 * (1. / pi<double>()));
}
double bearing(const FixedPointCoordinate first_coordinate,
const FixedPointCoordinate second_coordinate)
double bearing(const Coordinate first_coordinate, const Coordinate second_coordinate)
{
const double lon_diff =
second_coordinate.lon / COORDINATE_PRECISION - first_coordinate.lon / COORDINATE_PRECISION;
static_cast<double>(toFloating(second_coordinate.lon - first_coordinate.lon));
const double lon_delta = degToRad(lon_diff);
const double lat1 = degToRad(first_coordinate.lat / COORDINATE_PRECISION);
const double lat2 = degToRad(second_coordinate.lat / COORDINATE_PRECISION);
const double lat1 = degToRad(static_cast<double>(toFloating(first_coordinate.lat)));
const double lat2 = degToRad(static_cast<double>(toFloating(second_coordinate.lat)));
const double y = std::sin(lon_delta) * std::cos(lat2);
const double x =
std::cos(lat1) * std::sin(lat2) - std::sin(lat1) * std::cos(lat2) * std::cos(lon_delta);
@@ -230,19 +223,17 @@ double bearing(const FixedPointCoordinate first_coordinate,
return result;
}
double computeAngle(const FixedPointCoordinate first,
const FixedPointCoordinate second,
const FixedPointCoordinate third)
double computeAngle(const Coordinate first, const Coordinate second, const Coordinate third)
{
using namespace boost::math::constants;
using namespace coordinate_calculation;
const double v1x = (first.lon - second.lon) / COORDINATE_PRECISION;
const double v1y = mercator::latToY(first.lat / COORDINATE_PRECISION) -
mercator::latToY(second.lat / COORDINATE_PRECISION);
const double v2x = (third.lon - second.lon) / COORDINATE_PRECISION;
const double v2y = mercator::latToY(third.lat / COORDINATE_PRECISION) -
mercator::latToY(second.lat / COORDINATE_PRECISION);
const double v1x = static_cast<double>(toFloating(first.lon - second.lon));
const double v1y =
mercator::latToY(toFloating(first.lat)) - mercator::latToY(toFloating(second.lat));
const double v2x = static_cast<double>(toFloating(third.lon - second.lon));
const double v2y =
mercator::latToY(toFloating(third.lat)) - mercator::latToY(toFloating(second.lat));
double angle = (atan2_lookup(v2y, v2x) - atan2_lookup(v1y, v1x)) * 180. / pi<double>();
@@ -256,20 +247,22 @@ double computeAngle(const FixedPointCoordinate first,
namespace mercator
{
double yToLat(const double value)
FloatLatitude yToLat(const double value)
{
using namespace boost::math::constants;
return 180. * (1. / pi<long double>()) *
(2. * std::atan(std::exp(value * pi<double>() / 180.)) - half_pi<double>());
return FloatLatitude(
180. * (1. / pi<long double>()) *
(2. * std::atan(std::exp(value * pi<double>() / 180.)) - half_pi<double>()));
}
double latToY(const double latitude)
double latToY(const FloatLatitude latitude)
{
using namespace boost::math::constants;
return 180. * (1. / pi<double>()) *
std::log(std::tan((pi<double>() / 4.) + latitude * (pi<double>() / 180.) / 2.));
std::log(std::tan((pi<double>() / 4.) +
static_cast<double>(latitude) * (pi<double>() / 180.) / 2.));
}
} // ns mercato // ns mercatorr
} // ns coordinate_calculation