[all] Make LatLng coordinates read-only
This commit is contained in:
@@ -21,14 +21,15 @@ using ScreenLineString = mapbox::geometry::line_string<double>;
|
||||
using ScreenBox = mapbox::geometry::box<double>;
|
||||
|
||||
class LatLng {
|
||||
public:
|
||||
double latitude;
|
||||
double longitude;
|
||||
private:
|
||||
double lat;
|
||||
double lon;
|
||||
|
||||
public:
|
||||
enum WrapMode : bool { Unwrapped, Wrapped };
|
||||
|
||||
LatLng(double lat = 0, double lon = 0, WrapMode mode = Unwrapped)
|
||||
: latitude(lat), longitude(lon) {
|
||||
LatLng(double lat_ = 0, double lon_ = 0, WrapMode mode = Unwrapped)
|
||||
: lat(lat_), lon(lon_) {
|
||||
if (std::isnan(lat)) {
|
||||
throw std::domain_error("latitude must not be NaN");
|
||||
}
|
||||
@@ -46,34 +47,37 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
LatLng wrapped() const { return { latitude, longitude, Wrapped }; }
|
||||
double latitude() const { return lat; }
|
||||
double longitude() const { return lon; }
|
||||
|
||||
LatLng wrapped() const { return { lat, lon, Wrapped }; }
|
||||
|
||||
void wrap() {
|
||||
longitude = util::wrap(longitude, -util::LONGITUDE_MAX, util::LONGITUDE_MAX);
|
||||
lon = util::wrap(lon, -util::LONGITUDE_MAX, util::LONGITUDE_MAX);
|
||||
}
|
||||
|
||||
// If the distance from start to end longitudes is between half and full
|
||||
// world, unwrap the start longitude to ensure the shortest path is taken.
|
||||
void unwrapForShortestPath(const LatLng& end) {
|
||||
const double delta = std::abs(end.longitude - longitude);
|
||||
const double delta = std::abs(end.lon - lon);
|
||||
if (delta < util::LONGITUDE_MAX || delta > util::DEGREES_MAX) return;
|
||||
if (longitude > 0 && end.longitude < 0) longitude -= util::DEGREES_MAX;
|
||||
else if (longitude < 0 && end.longitude > 0) longitude += util::DEGREES_MAX;
|
||||
if (lon > 0 && end.lon < 0) lon -= util::DEGREES_MAX;
|
||||
else if (lon < 0 && end.lon > 0) lon += util::DEGREES_MAX;
|
||||
}
|
||||
|
||||
// Constructs a LatLng object with the top left position of the specified tile.
|
||||
LatLng(const CanonicalTileID& id);
|
||||
LatLng(const UnwrappedTileID& id);
|
||||
|
||||
friend constexpr bool operator==(const LatLng& a, const LatLng& b) {
|
||||
return a.lat == b.lat && a.lon == b.lon;
|
||||
}
|
||||
|
||||
friend constexpr bool operator!=(const LatLng& a, const LatLng& b) {
|
||||
return !(a == b);
|
||||
}
|
||||
};
|
||||
|
||||
constexpr bool operator==(const LatLng& a, const LatLng& b) {
|
||||
return a.latitude == b.latitude && a.longitude == b.longitude;
|
||||
}
|
||||
|
||||
constexpr bool operator!=(const LatLng& a, const LatLng& b) {
|
||||
return !(a == b);
|
||||
}
|
||||
|
||||
class ProjectedMeters {
|
||||
public:
|
||||
double northing;
|
||||
@@ -116,10 +120,10 @@ public:
|
||||
// Constructs a LatLngBounds object with the tile's exact boundaries.
|
||||
LatLngBounds(const CanonicalTileID&);
|
||||
|
||||
double south() const { return sw.latitude; }
|
||||
double west() const { return sw.longitude; }
|
||||
double north() const { return ne.latitude; }
|
||||
double east() const { return ne.longitude; }
|
||||
double south() const { return sw.latitude(); }
|
||||
double west() const { return sw.longitude(); }
|
||||
double north() const { return ne.latitude(); }
|
||||
double east() const { return ne.longitude(); }
|
||||
|
||||
LatLng southwest() const { return sw; }
|
||||
LatLng northeast() const { return ne; }
|
||||
@@ -127,15 +131,15 @@ public:
|
||||
LatLng northwest() const { return LatLng(north(), west()); }
|
||||
|
||||
LatLng center() const {
|
||||
return LatLng((sw.latitude + ne.latitude) / 2,
|
||||
(sw.longitude + ne.longitude) / 2);
|
||||
return LatLng((sw.latitude() + ne.latitude()) / 2,
|
||||
(sw.longitude() + ne.longitude()) / 2);
|
||||
}
|
||||
|
||||
void extend(const LatLng& point) {
|
||||
if (point.latitude < sw.latitude) sw.latitude = point.latitude;
|
||||
if (point.latitude > ne.latitude) ne.latitude = point.latitude;
|
||||
if (point.longitude < sw.longitude) sw.longitude = point.longitude;
|
||||
if (point.longitude > ne.longitude) ne.longitude = point.longitude;
|
||||
sw = LatLng(std::min(point.latitude(), sw.latitude()),
|
||||
std::min(point.longitude(), sw.longitude()));
|
||||
ne = LatLng(std::max(point.latitude(), ne.latitude()),
|
||||
std::max(point.longitude(), ne.longitude()));
|
||||
}
|
||||
|
||||
void extend(const LatLngBounds& bounds) {
|
||||
@@ -144,22 +148,22 @@ public:
|
||||
}
|
||||
|
||||
bool isEmpty() const {
|
||||
return sw.latitude > ne.latitude ||
|
||||
sw.longitude > ne.longitude;
|
||||
return sw.latitude() > ne.latitude() ||
|
||||
sw.longitude() > ne.longitude();
|
||||
}
|
||||
|
||||
bool contains(const LatLng& point) const {
|
||||
return (point.latitude >= sw.latitude &&
|
||||
point.latitude <= ne.latitude &&
|
||||
point.longitude >= sw.longitude &&
|
||||
point.longitude <= ne.longitude);
|
||||
return (point.latitude() >= sw.latitude() &&
|
||||
point.latitude() <= ne.latitude() &&
|
||||
point.longitude() >= sw.longitude() &&
|
||||
point.longitude() <= ne.longitude());
|
||||
}
|
||||
|
||||
bool intersects(const LatLngBounds area) const {
|
||||
return (area.ne.latitude > sw.latitude &&
|
||||
area.sw.latitude < ne.latitude &&
|
||||
area.ne.longitude > sw.longitude &&
|
||||
area.sw.longitude < ne.longitude);
|
||||
return (area.ne.latitude() > sw.latitude() &&
|
||||
area.sw.latitude() < ne.latitude() &&
|
||||
area.ne.longitude() > sw.longitude() &&
|
||||
area.sw.longitude() < ne.longitude());
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
@@ -25,8 +25,8 @@ public:
|
||||
}
|
||||
|
||||
static ProjectedMeters projectedMetersForLatLng(const LatLng& latLng) {
|
||||
const double constrainedLatitude = util::clamp(latLng.latitude, -util::LATITUDE_MAX, util::LATITUDE_MAX);
|
||||
const double constrainedLongitude = util::clamp(latLng.longitude, -util::LONGITUDE_MAX, util::LONGITUDE_MAX);
|
||||
const double constrainedLatitude = util::clamp(latLng.latitude(), -util::LATITUDE_MAX, util::LATITUDE_MAX);
|
||||
const double constrainedLongitude = util::clamp(latLng.longitude(), -util::LONGITUDE_MAX, util::LONGITUDE_MAX);
|
||||
|
||||
const double m = 1 - 1e-15;
|
||||
const double f = util::clamp(std::sin(util::DEG2RAD * constrainedLatitude), -m, m);
|
||||
@@ -49,8 +49,8 @@ public:
|
||||
|
||||
static Point<double> project(const LatLng& latLng, double scale) {
|
||||
return Point<double> {
|
||||
util::LONGITUDE_MAX + latLng.longitude,
|
||||
util::LONGITUDE_MAX - util::RAD2DEG * std::log(std::tan(M_PI / 4 + latLng.latitude * M_PI / util::DEGREES_MAX))
|
||||
util::LONGITUDE_MAX + latLng.longitude(),
|
||||
util::LONGITUDE_MAX - util::RAD2DEG * std::log(std::tan(M_PI / 4 + latLng.latitude() * M_PI / util::DEGREES_MAX))
|
||||
} * worldSize(scale) / util::DEGREES_MAX;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user