-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add MercatorCoordinate
type
#7488
Merged
Merged
Changes from 5 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
33d6089
add `MercatorCoordinate` type, fix #7485
ansis 2672d01
fixup
ansis 050b1e5
use MercatorCoordinate for projection in Transform
ansis 8856fbf
replace Coordinate with MercatorCoordinate
ansis 83cda37
clarify differences with epsg3857
ansis ec9ef36
use es default values
ansis cd3e462
fix tests
ansis 0d4c2ca
seprate projection from type for internal use
ansis f6cd838
fixup
ansis b381e56
fixup
ansis 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 was deleted.
Oops, something went wrong.
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,95 @@ | ||
// @flow | ||
|
||
import LngLat from '../geo/lng_lat'; | ||
import type {LngLatLike} from '../geo/lng_lat'; | ||
|
||
/* | ||
* The circumference of the world in meters at the given latitude. | ||
*/ | ||
function circumferenceAtLatitude(latitude: number) { | ||
const circumference = 2 * Math.PI * 6378137; | ||
return circumference * Math.cos(latitude * Math.PI / 180); | ||
} | ||
|
||
/** | ||
* A `MercatorCoordinate` object represents a projected three dimensional position. | ||
* | ||
* `MercatorCoordinate` uses the web mercator projection ([EPSG:3857](https://epsg.io/3857)) with slightly different units: | ||
* - the size of 1 unit is the width of the projected world instead of the "mercator meter" | ||
* - the origin of the coordinate space is at the north-west corner instead of the middle | ||
* | ||
* For example, `MercatorCoordinate(0, 0, 0)` is the north-west corner of the mercator world and | ||
* `MercatorCoordinate(1, 1, 0)` is the south-east corner. If you are familiar with | ||
* [vector tiles](https://github.com/mapbox/vector-tile-spec) it may be helpful to think | ||
* of the coordinate space as the `0/0/0` tile with an extent of `1`. | ||
* | ||
* The `z` dimension of `MercatorCoordinate` is conformal. A cube in the mercator coordinate space would be rendered as a cube. | ||
* | ||
* @param {number} x The x component of the position. | ||
* @param {number} y The y component of the position. | ||
* @param {number} z The z component of the position. | ||
* @example | ||
* var nullIsland = new mapboxgl.MercatorCoordinate(0.5, 0.5, 0); | ||
* | ||
* @see [Add a custom style layer](https://www.mapbox.com/mapbox-gl-js/example/custom-style-layer/) | ||
*/ | ||
class MercatorCoordinate { | ||
x: number; | ||
y: number; | ||
z: number; | ||
|
||
constructor(x: number, y: number, z?: number) { | ||
this.x = +x; | ||
this.y = +y; | ||
this.z = z === undefined ? 0 : +z; | ||
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. Nit: we can use ES default parameters for things like this (below too for altitude). |
||
} | ||
|
||
/** | ||
* Project a `LngLat` to a `MercatorCoordinate`. | ||
* | ||
* @param {LngLatLike} lngLatLike The location to project. | ||
* @param {number} altitude The altitude in meters of the position. | ||
* @returns {MercatorCoordinate} The projected mercator coordinate. | ||
* @example | ||
* var coord = mapboxgl.MercatorCoordinate.fromLngLat({ lng: 0, lat: 0}, 0); | ||
* coord; // MercatorCoordinate(0.5, 0.5, 0) | ||
*/ | ||
static fromLngLat(lngLatLike: LngLatLike, altitude?: number) { | ||
const lngLat = LngLat.convert(lngLatLike); | ||
|
||
const x = (180 + lngLat.lng) / 360; | ||
const y = (180 - (180 / Math.PI * Math.log(Math.tan(Math.PI / 4 + lngLat.lat * Math.PI / 360)))) / 360; | ||
const z = altitude === undefined ? 0 : (altitude / circumferenceAtLatitude(lngLat.lat)); | ||
return new MercatorCoordinate(x, y, z); | ||
} | ||
|
||
/** | ||
* Returns the `LatLng` for the coordinate. | ||
* | ||
* @returns {LngLat} The `LngLat` object. | ||
* @example | ||
* var coord = new mapboxglMercatorCoordinate(0.5, 0.5, 0); | ||
* var latLng = coord.toLatLng(); // LngLat(0, 0) | ||
*/ | ||
toLngLat() { | ||
const lng = this.x * 360 - 180; | ||
const y2 = 180 - this.y * 360; | ||
const lat = 360 / Math.PI * Math.atan(Math.exp(y2 * Math.PI / 180)) - 90; | ||
return new LngLat(lng, lat); | ||
} | ||
|
||
/** | ||
* Returns the altitude in meters of the coordinate. | ||
* | ||
* @returns {number} The altitude in meters. | ||
* @example | ||
* var coord = new mapboxgl.MercatorCoordinate(0, 0, 0.02); | ||
* coord.toAltitude(); // 6914.281956295339 | ||
*/ | ||
toAltitude() { | ||
const lat = this.toLngLat().lat; | ||
return this.z * circumferenceAtLatitude(lat); | ||
} | ||
} | ||
|
||
export default MercatorCoordinate; |
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.
Does there need to be a caveat about precision here? I.e. When using a mercator co-ordinate to place something on a custom layer, it may not match the exact location at higher zooms.
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.
Those issues are more on the rendering side of things than the
MercatorCoordinate
side. We should probably add caveats and work arounds in the docs for custom layers. #7268 kind of covers this