This repository has been archived by the owner on Aug 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
fixes #476: pixel / projected meter / lat/long conversions #853
Closed
Closed
Changes from 33 commits
Commits
Show all changes
39 commits
Select commit
Hold shift + click to select a range
7e7a896
fixes #97: add LatLng struct & refactor function calls
incanus 6dc3c3f
refs #476: projected meters/pixel/coordinate conversions
incanus da5b5d4
bring various calculations in line style-wise
incanus ab5d361
better struct definitions
incanus 846a469
more stylistic consistency tweaks
incanus 291f43b
Merge branch 'conversions' of github.com:mapbox/mapbox-gl-native into…
incanus 07871a3
more precision on max lat
incanus 7762616
wrap longitude
incanus 3c1c80f
correct lat/lon <-> viewport pixel values
incanus 42a5bb9
bump Cocoa
incanus 6ec8db7
properly consider rotation angle in pixel <-> projected meters <-> co…
incanus 3701978
viewport conversions don't make sense in the context of rotations
incanus 6dd3b86
Merge remote-tracking branch 'origin' into conversions
incanus 77ec1dd
bump Cocoa
incanus 3592d93
bump Cocoa
incanus d9ebdaf
Merge branch 'master' into conversions
incanus 2a06f2d
post-megamerge API updates
incanus 05e6718
iOS API tweaks & ports
incanus 9473aa5
refactor Transform/TransformState/Painter for conversion routines
incanus d7eca6b
Merge remote-tracking branch 'origin/master' into conversions
incanus 0ab5299
Merge branch 'master' into conversions
incanus a2a924f
move transform state method to static projection class
incanus 2e11768
renames & reorgs
incanus aaa3710
cleanup
incanus ad0890f
LatLng/ProjectedMeters constructors
incanus 72c3ef4
fixes & cleanups
incanus 1455670
use constructors
incanus 2f834f1
fix animation bugs (finalState -> currentState)
incanus e424f09
fix submodule
incanus 6dc802d
Merge branch 'master' into conversions
incanus a46750c
headless test fix
incanus 5f7f033
render latlng fix
incanus 5555216
Android latlng fix
incanus 9538a35
fixes for date line & world wrap
incanus bc3ef1b
Merge remote-tracking branch 'origin/master' into conversions
incanus 429f7cf
get rid of redundant getLatLngZoom() combo function
incanus f13ea06
x, y by ref -> vec2<double>
incanus c2a9adb
take transform back out of painter
incanus 4fa1776
Merge remote-tracking branch 'origin/master' into conversions
incanus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,8 @@ | |
#define MBGL_MAP_MAP | ||
|
||
#include <mbgl/map/transform.hpp> | ||
#include <mbgl/util/geo.hpp> | ||
#include <mbgl/util/projection.hpp> | ||
#include <mbgl/util/noncopyable.hpp> | ||
#include <mbgl/util/uv.hpp> | ||
#include <mbgl/util/ptr.hpp> | ||
|
@@ -94,8 +96,8 @@ class Map : private util::noncopyable { | |
|
||
// Position | ||
void moveBy(double dx, double dy, std::chrono::steady_clock::duration duration = std::chrono::steady_clock::duration::zero()); | ||
void setLonLat(double lon, double lat, std::chrono::steady_clock::duration duration = std::chrono::steady_clock::duration::zero()); | ||
void getLonLat(double &lon, double &lat) const; | ||
void setLatLng(LatLng latLng, std::chrono::steady_clock::duration duration = std::chrono::steady_clock::duration::zero()); | ||
const LatLng getLatLng() const; | ||
void startPanning(); | ||
void stopPanning(); | ||
void resetPosition(); | ||
|
@@ -106,8 +108,8 @@ class Map : private util::noncopyable { | |
double getScale() const; | ||
void setZoom(double zoom, std::chrono::steady_clock::duration duration = std::chrono::steady_clock::duration::zero()); | ||
double getZoom() const; | ||
void setLonLatZoom(double lon, double lat, double zoom, std::chrono::steady_clock::duration duration = std::chrono::steady_clock::duration::zero()); | ||
void getLonLatZoom(double &lon, double &lat, double &zoom) const; | ||
void setLatLngZoom(LatLng latLng, double zoom, std::chrono::steady_clock::duration duration = std::chrono::steady_clock::duration::zero()); | ||
void getLatLngZoom(LatLng &latLng, double &zoom) const; | ||
void resetZoom(); | ||
void startScaling(); | ||
void stopScaling(); | ||
|
@@ -127,6 +129,15 @@ class Map : private util::noncopyable { | |
void setAccessToken(const std::string &token); | ||
const std::string &getAccessToken() const; | ||
|
||
// Projection | ||
inline void getWorldBoundsMeters(ProjectedMeters &sw, ProjectedMeters &ne) const { Projection::getWorldBoundsMeters(sw, ne); } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of passing out args via reference, there should be a |
||
inline void getWorldBoundsLatLng(LatLng &sw, LatLng &ne) const { Projection::getWorldBoundsLatLng(sw, ne); } | ||
inline double getMetersPerPixelAtLatitude(const double lat, const double zoom) const { return Projection::getMetersPerPixelAtLatitude(lat, zoom); } | ||
inline const ProjectedMeters projectedMetersForLatLng(const LatLng latLng) const { return Projection::projectedMetersForLatLng(latLng); } | ||
inline const LatLng latLngForProjectedMeters(const ProjectedMeters projectedMeters) const { return Projection::latLngForProjectedMeters(projectedMeters); } | ||
inline void pixelForLatLng(const LatLng latLng, double &x, double &y) const { transform.pixelForLatLng(latLng, x, y); } | ||
inline const LatLng latLngForPixel(const double x, const double y) const { return transform.latLngForPixel(x, y); } | ||
|
||
// Debug | ||
void setDebug(bool value); | ||
void toggleDebug(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
#define MBGL_MAP_TRANSFORM | ||
|
||
#include <mbgl/map/transform_state.hpp> | ||
#include <mbgl/util/geo.hpp> | ||
#include <mbgl/util/noncopyable.hpp> | ||
|
||
#include <cstdint> | ||
|
@@ -26,10 +27,10 @@ class Transform : private util::noncopyable { | |
|
||
// Position | ||
void moveBy(double dx, double dy, std::chrono::steady_clock::duration duration = std::chrono::steady_clock::duration::zero()); | ||
void setLonLat(double lon, double lat, std::chrono::steady_clock::duration duration = std::chrono::steady_clock::duration::zero()); | ||
void setLonLatZoom(double lon, double lat, double zoom, std::chrono::steady_clock::duration duration = std::chrono::steady_clock::duration::zero()); | ||
void getLonLat(double& lon, double& lat) const; | ||
void getLonLatZoom(double& lon, double& lat, double& zoom) const; | ||
void setLatLng(LatLng latLng, std::chrono::steady_clock::duration duration = std::chrono::steady_clock::duration::zero()); | ||
void setLatLngZoom(LatLng latLng, double zoom, std::chrono::steady_clock::duration duration = std::chrono::steady_clock::duration::zero()); | ||
const LatLng getLatLng() const; | ||
void getLatLngZoom(LatLng &latLng, double& zoom) const; | ||
void startPanning(); | ||
void stopPanning(); | ||
|
||
|
@@ -52,6 +53,10 @@ class Transform : private util::noncopyable { | |
void startRotating(); | ||
void stopRotating(); | ||
|
||
// Projection | ||
void pixelForLatLng(const LatLng latLng, double &x, double &y) const; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should return There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
const LatLng latLngForPixel(const double x, const double y) const; | ||
|
||
// Transitions | ||
bool needsTransition() const; | ||
void updateTransitions(std::chrono::steady_clock::time_point now); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#ifndef MBGL_UTIL_GEO | ||
#define MBGL_UTIL_GEO | ||
|
||
namespace mbgl { | ||
|
||
struct LatLng { | ||
double latitude = 0; | ||
double longitude = 0; | ||
|
||
inline LatLng(double lat = 0, double lon = 0) | ||
: latitude(lat), longitude(lon) {} | ||
}; | ||
|
||
struct ProjectedMeters { | ||
double northing = 0; | ||
double easting = 0; | ||
|
||
inline ProjectedMeters(double n = 0, double e = 0) | ||
: northing(n), easting(e) {} | ||
}; | ||
|
||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#ifndef MBGL_UTIL_PROJECTION | ||
#define MBGL_UTIL_PROJECTION | ||
|
||
#include <mbgl/util/constants.hpp> | ||
#include <mbgl/util/geo.hpp> | ||
|
||
#include <cmath> | ||
|
||
namespace mbgl { | ||
|
||
class Projection { | ||
|
||
public: | ||
static inline void getWorldBoundsMeters(ProjectedMeters &sw, ProjectedMeters &ne) { | ||
const double d = util::EARTH_RADIUS_M * M_PI; | ||
|
||
sw.easting = -d; | ||
sw.northing = -d; | ||
|
||
ne.easting = d; | ||
ne.northing = d; | ||
} | ||
|
||
static inline void getWorldBoundsLatLng(LatLng &sw, LatLng &ne) { | ||
ProjectedMeters projectedMetersSW = ProjectedMeters(); | ||
ProjectedMeters projectedMetersNE = ProjectedMeters(); | ||
|
||
getWorldBoundsMeters(projectedMetersSW, projectedMetersNE); | ||
|
||
sw = latLngForProjectedMeters(projectedMetersSW); | ||
ne = latLngForProjectedMeters(projectedMetersNE); | ||
} | ||
|
||
static inline double getMetersPerPixelAtLatitude(const double lat, const double zoom) { | ||
const double mapPixelWidthAtZoom = std::pow(2.0, zoom) * util::tileSize; | ||
const double constrainedLatitude = std::fmin(std::fmax(lat, -util::LATITUDE_MAX), util::LATITUDE_MAX); | ||
|
||
return std::cos(constrainedLatitude * util::DEG2RAD) * util::M2PI * util::EARTH_RADIUS_M / mapPixelWidthAtZoom; | ||
} | ||
|
||
static inline const ProjectedMeters projectedMetersForLatLng(const LatLng latLng) { | ||
const double constrainedLatitude = std::fmin(std::fmax(latLng.latitude, -util::LATITUDE_MAX), util::LATITUDE_MAX); | ||
|
||
const double m = 1 - 1e-15; | ||
const double f = std::fmin(std::fmax(std::sin(util::DEG2RAD * constrainedLatitude), -m), m); | ||
|
||
const double easting = util::EARTH_RADIUS_M * latLng.longitude * util::DEG2RAD; | ||
const double northing = 0.5 * util::EARTH_RADIUS_M * std::log((1 + f) / (1 - f)); | ||
|
||
return ProjectedMeters(northing, easting); | ||
} | ||
|
||
static inline const LatLng latLngForProjectedMeters(const ProjectedMeters projectedMeters) { | ||
double latitude = (2 * std::atan(std::exp(projectedMeters.northing / util::EARTH_RADIUS_M)) - (M_PI / 2)) * util::RAD2DEG; | ||
double longitude = projectedMeters.easting * util::RAD2DEG / util::EARTH_RADIUS_M; | ||
|
||
latitude = std::fmin(std::fmax(latitude, -util::LATITUDE_MAX), util::LATITUDE_MAX); | ||
|
||
while (longitude > 180) longitude -= 180; | ||
while (longitude < -180) longitude += 180; | ||
|
||
return LatLng(latitude, longitude); | ||
} | ||
}; | ||
|
||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we remove this method and use separate calls to
getLatLng()
andgetZoom()
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I think so. This was just ported for historical reasons. Good reason to scrap it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
429f7cf