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
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #886 from mapbox/conversions-squash
- Loading branch information
Showing
19 changed files
with
403 additions
and
146 deletions.
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
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 |
---|---|---|
@@ -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,65 @@ | ||
#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); | ||
|
||
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.