Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[all] Make LatLng coordinates read-only
Browse files Browse the repository at this point in the history
  • Loading branch information
jfirebaugh committed Apr 4, 2017
1 parent 1586ffa commit b19e9c0
Show file tree
Hide file tree
Showing 21 changed files with 231 additions and 228 deletions.
78 changes: 41 additions & 37 deletions include/mbgl/util/geo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand All @@ -46,33 +47,36 @@ class LatLng {
}
}

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);
};

constexpr bool operator==(const LatLng& a, const LatLng& b) {
return a.latitude == b.latitude && a.longitude == b.longitude;
}
friend constexpr bool operator==(const LatLng& a, const LatLng& b) {
return a.lat == b.lat && a.lon == b.lon;
}

constexpr bool operator!=(const LatLng& a, const LatLng& b) {
return !(a == b);
}
friend constexpr bool operator!=(const LatLng& a, const LatLng& b) {
return !(a == b);
}
};

class ProjectedMeters {
public:
Expand Down Expand Up @@ -116,26 +120,26 @@ class LatLngBounds {
// 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; }
LatLng southeast() const { return LatLng(south(), east()); }
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) {
Expand All @@ -144,22 +148,22 @@ class LatLngBounds {
}

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:
Expand Down
8 changes: 4 additions & 4 deletions include/mbgl/util/projection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ class Projection {
}

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);
Expand All @@ -49,8 +49,8 @@ class Projection {

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;
}

Expand Down
4 changes: 2 additions & 2 deletions platform/android/src/geometry/lat_lng.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
namespace mbgl {
namespace android {

jni::Object<LatLng> LatLng::New(jni::JNIEnv& env, double latitude, double longitude) {
jni::Object<LatLng> LatLng::New(jni::JNIEnv& env, const mbgl::LatLng& latLng) {
static auto constructor = LatLng::javaClass.GetConstructor<double, double>(env);
return LatLng::javaClass.New(env, constructor, latitude, longitude);
return LatLng::javaClass.New(env, constructor, latLng.latitude(), latLng.longitude());
}

mbgl::Point<double> LatLng::getGeometry(jni::JNIEnv& env, jni::Object<LatLng> latLng) {
Expand Down
2 changes: 1 addition & 1 deletion platform/android/src/geometry/lat_lng.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class LatLng : private mbgl::util::noncopyable {

static constexpr auto Name() { return "com/mapbox/mapboxsdk/geometry/LatLng"; };

static jni::Object<LatLng> New(jni::JNIEnv&, double, double);
static jni::Object<LatLng> New(jni::JNIEnv&, const mbgl::LatLng&);

static mbgl::Point<double> getGeometry(jni::JNIEnv&, jni::Object<LatLng>);

Expand Down
13 changes: 5 additions & 8 deletions platform/android/src/native_map_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -426,8 +426,7 @@ void NativeMapView::flyTo(jni::JNIEnv&, jni::jdouble angle, jni::jdouble latitud
}

jni::Object<LatLng> NativeMapView::getLatLng(JNIEnv& env) {
mbgl::LatLng latLng = map->getLatLng(insets);
return LatLng::New(env, latLng.latitude, latLng.longitude);
return LatLng::New(env, map->getLatLng(insets));
}

void NativeMapView::setLatLng(jni::JNIEnv&, jni::jdouble latitude, jni::jdouble longitude, jni::jlong duration) {
Expand Down Expand Up @@ -563,8 +562,8 @@ jni::Array<jni::jdouble> NativeMapView::getCameraValues(jni::JNIEnv& env) {
//Create buffer with values
jdouble buf[5];
mbgl::LatLng latLng = map->getLatLng(insets);
buf[0] = latLng.latitude;
buf[1] = latLng.longitude;
buf[0] = latLng.latitude();
buf[1] = latLng.longitude();
buf[2] = -map->getBearing();
buf[3] = map->getPitch();
buf[4] = map->getZoom();
Expand Down Expand Up @@ -650,13 +649,11 @@ jni::Object<PointF> NativeMapView::pixelForLatLng(JNIEnv& env, jdouble latitude,
}

jni::Object<LatLng> NativeMapView::latLngForProjectedMeters(JNIEnv& env, jdouble northing, jdouble easting) {
mbgl::LatLng latLng = map->latLngForProjectedMeters(mbgl::ProjectedMeters(northing, easting));
return LatLng::New(env, latLng.latitude, latLng.longitude);
return LatLng::New(env, map->latLngForProjectedMeters(mbgl::ProjectedMeters(northing, easting)));
}

jni::Object<LatLng> NativeMapView::latLngForPixel(JNIEnv& env, jfloat x, jfloat y) {
mbgl::LatLng latLng = map->latLngForPixel(mbgl::ScreenCoordinate(x, y));
return LatLng::New(env, latLng.latitude, latLng.longitude);
return LatLng::New(env, map->latLngForPixel(mbgl::ScreenCoordinate(x, y)));
}

jni::Array<jlong> NativeMapView::addPolylines(JNIEnv& env, jni::Array<jni::Object<Polyline>> polylines) {
Expand Down
2 changes: 1 addition & 1 deletion platform/darwin/src/MGLGeometry_Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ NS_INLINE mbgl::Point<double> MGLPointFromLocationCoordinate2D(CLLocationCoordin
}

NS_INLINE CLLocationCoordinate2D MGLLocationCoordinate2DFromLatLng(mbgl::LatLng latLng) {
return CLLocationCoordinate2DMake(latLng.latitude, latLng.longitude);
return CLLocationCoordinate2DMake(latLng.latitude(), latLng.longitude());
}

NS_INLINE MGLCoordinateBounds MGLCoordinateBoundsFromLatLngBounds(mbgl::LatLngBounds latLngBounds) {
Expand Down
2 changes: 1 addition & 1 deletion platform/glfw/glfw_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ mbgl::Point<double> GLFWView::makeRandomPoint() const {
const double x = width * double(std::rand()) / RAND_MAX;
const double y = height * double(std::rand()) / RAND_MAX;
mbgl::LatLng latLng = map->latLngForPixel({ x, y });
return { latLng.longitude, latLng.latitude };
return { latLng.longitude(), latLng.latitude() };
}

std::shared_ptr<const mbgl::SpriteImage>
Expand Down
4 changes: 2 additions & 2 deletions platform/glfw/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,8 @@ int main(int argc, char *argv[]) {

// Save settings
mbgl::LatLng latLng = map.getLatLng();
settings.latitude = latLng.latitude;
settings.longitude = latLng.longitude;
settings.latitude = latLng.latitude();
settings.longitude = latLng.longitude();
settings.zoom = map.getZoom();
settings.bearing = map.getBearing();
settings.pitch = map.getPitch();
Expand Down
2 changes: 1 addition & 1 deletion platform/macos/src/MGLMapView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -2676,7 +2676,7 @@ - (MGLCoordinateBounds)convertRect:(NSRect)rect toCoordinateBoundsFromView:(null
(bounds.south() + bounds.north()) / 2,
bounds.west() - 1,
};
} else if (bounds.northeast().longitude < 180) {
} else if (bounds.northeast().longitude() < 180) {
outsideLatLng = {
(bounds.south() + bounds.north()) / 2,
bounds.east() + 1,
Expand Down
14 changes: 7 additions & 7 deletions platform/qt/src/qmapboxgl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ void QMapboxGL::setStyleUrl(const QString &url)
*/
double QMapboxGL::latitude() const
{
return d_ptr->mapObj->getLatLng(d_ptr->margins).latitude;
return d_ptr->mapObj->getLatLng(d_ptr->margins).latitude();
}

void QMapboxGL::setLatitude(double latitude_)
Expand All @@ -532,7 +532,7 @@ void QMapboxGL::setLatitude(double latitude_)
*/
double QMapboxGL::longitude() const
{
return d_ptr->mapObj->getLatLng(d_ptr->margins).longitude;
return d_ptr->mapObj->getLatLng(d_ptr->margins).longitude();
}

void QMapboxGL::setLongitude(double longitude_)
Expand Down Expand Up @@ -614,7 +614,7 @@ double QMapboxGL::maximumZoom() const
Coordinate QMapboxGL::coordinate() const
{
const mbgl::LatLng& latLng = d_ptr->mapObj->getLatLng(d_ptr->margins);
return Coordinate(latLng.latitude, latLng.longitude);
return Coordinate(latLng.latitude(), latLng.longitude());
}

void QMapboxGL::setCoordinate(const QMapbox::Coordinate &coordinate_)
Expand Down Expand Up @@ -1130,7 +1130,7 @@ QMapbox::ProjectedMeters QMapboxGL::projectedMetersForCoordinate(const QMapbox::
QMapbox::Coordinate QMapboxGL::coordinateForProjectedMeters(const QMapbox::ProjectedMeters &projectedMeters) const
{
auto latLng = d_ptr->mapObj->latLngForProjectedMeters(mbgl::ProjectedMeters { projectedMeters.first, projectedMeters.second });
return QMapbox::Coordinate(latLng.latitude, latLng.longitude);
return QMapbox::Coordinate(latLng.latitude(), latLng.longitude());
}

/*!
Expand Down Expand Up @@ -1160,7 +1160,7 @@ QMapbox::Coordinate QMapboxGL::coordinateForPixel(const QPointF &pixel) const
const mbgl::LatLng latLng =
d_ptr->mapObj->latLngForPixel(mbgl::ScreenCoordinate { pixel.x(), pixel.y() });

return Coordinate(latLng.latitude, latLng.longitude);
return Coordinate(latLng.latitude(), latLng.longitude());
}

/*!
Expand All @@ -1172,7 +1172,7 @@ QMapbox::CoordinateZoom QMapboxGL::coordinateZoomForBounds(const QMapbox::Coordi
auto bounds = mbgl::LatLngBounds::hull(mbgl::LatLng { sw.first, sw.second }, mbgl::LatLng { ne.first, ne.second });
mbgl::CameraOptions camera = d_ptr->mapObj->cameraForLatLngBounds(bounds, d_ptr->margins);

return {{ (*camera.center).latitude, (*camera.center).longitude }, *camera.zoom };
return {{ (*camera.center).latitude(), (*camera.center).longitude() }, *camera.zoom };
}

/*!
Expand All @@ -1198,7 +1198,7 @@ QMapbox::CoordinateZoom QMapboxGL::coordinateZoomForBounds(const QMapbox::Coordi
setBearing(currentBearing);
setPitch(currentPitch);

return {{ (*camera.center).latitude, (*camera.center).longitude }, *camera.zoom };
return {{ (*camera.center).latitude(), (*camera.center).longitude() }, *camera.zoom };
}

/*!
Expand Down
2 changes: 1 addition & 1 deletion src/mbgl/annotation/symbol_annotation_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#pragma GCC diagnostic pop

// Make Boost Geometry aware of our LatLng type
BOOST_GEOMETRY_REGISTER_POINT_2D(mbgl::LatLng, double, boost::geometry::cs::cartesian, longitude, latitude)
BOOST_GEOMETRY_REGISTER_POINT_2D_CONST(mbgl::LatLng, double, boost::geometry::cs::cartesian, longitude(), latitude())
BOOST_GEOMETRY_REGISTER_BOX(mbgl::LatLngBounds, mbgl::LatLng, southwest(), northeast())

namespace mbgl {
Expand Down
4 changes: 3 additions & 1 deletion src/mbgl/map/transform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ void Transform::easeTo(const CameraOptions& camera, const AnimationOptions& anim
// If gesture in progress, we transfer the world rounds from the end
// longitude into start, so we can guarantee the "scroll effect" of rounding
// the world while assuring the end longitude remains wrapped.
if (isGestureInProgress()) startLatLng.longitude -= unwrappedLatLng.longitude - latLng.longitude;
if (isGestureInProgress()) {
startLatLng = LatLng(startLatLng.latitude(), startLatLng.longitude() - (unwrappedLatLng.longitude() - latLng.longitude()));
}
// Find the shortest path otherwise.
else startLatLng.unwrapForShortestPath(latLng);

Expand Down
4 changes: 2 additions & 2 deletions src/mbgl/map/transform_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -325,10 +325,10 @@ void TransformState::setLatLngZoom(const LatLng &latLng, double zoom) {
Cc = newWorldSize / util::M2PI;

const double m = 1 - 1e-15;
const double f = util::clamp(std::sin(util::DEG2RAD * latLng.latitude), -m, m);
const double f = util::clamp(std::sin(util::DEG2RAD * latLng.latitude()), -m, m);

ScreenCoordinate point = {
-latLng.longitude * Bc,
-latLng.longitude() * Bc,
0.5 * Cc * std::log((1 + f) / (1 - f)),
};
setScalePoint(newScale, point);
Expand Down
4 changes: 2 additions & 2 deletions src/mbgl/style/layers/custom_layer_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ void CustomLayer::Impl::render(const TransformState& state) const {

parameters.width = state.getSize().width;
parameters.height = state.getSize().height;
parameters.latitude = state.getLatLng().latitude;
parameters.longitude = state.getLatLng().longitude;
parameters.latitude = state.getLatLng().latitude();
parameters.longitude = state.getLatLng().longitude();
parameters.zoom = state.getZoom();
parameters.bearing = -state.getAngle() * util::RAD2DEG;
parameters.pitch = state.getPitch();
Expand Down
4 changes: 2 additions & 2 deletions src/mbgl/style/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ StyleParseResult Parser::parse(const std::string& json) {
const JSValue& value = document["center"];
if (value.IsArray() && value.Size() >= 2) {
// Style spec uses lon/lat order
latLng.longitude = value[0].IsNumber() ? value[0].GetDouble() : 0;
latLng.latitude = value[1].IsNumber() ? value[1].GetDouble() : 0;
latLng = LatLng(value[1].IsNumber() ? value[1].GetDouble() : 0,
value[0].IsNumber() ? value[0].GetDouble() : 0);
}
}

Expand Down
Loading

0 comments on commit b19e9c0

Please sign in to comment.