-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
correct zoom, bearing and shear for projections #10976
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
// @flow | ||
|
||
import LngLat from '../lng_lat.js'; | ||
import MercatorCoordinate from '../mercator_coordinate.js'; | ||
import {mat4} from 'gl-matrix'; | ||
import type {Projection} from './index.js'; | ||
import type Transform from '../transform.js'; | ||
|
||
export default function getProjectionAdjustments(transform: Transform) { | ||
|
||
const projection = transform.projection; | ||
|
||
const interpT = getInterpolationT(transform); | ||
|
||
const zoomAdjustment = getZoomAdjustment(projection, transform.center); | ||
const zoomAdjustmentOrigin = getZoomAdjustment(projection, LngLat.convert(projection.center)); | ||
const scaleAdjustment = Math.pow(2, zoomAdjustment * interpT + (1 - interpT) * zoomAdjustmentOrigin); | ||
|
||
const matrix = getShearAdjustment(transform.projection, transform.zoom, transform.center, interpT); | ||
|
||
mat4.scale(matrix, matrix, [scaleAdjustment, scaleAdjustment, 1]); | ||
|
||
return matrix; | ||
} | ||
|
||
function getInterpolationT(transform: Transform) { | ||
const range = transform.projection.range; | ||
if (!range) return 0; | ||
|
||
const size = Math.max(transform.width, transform.height); | ||
const rangeAdjustment = Math.log(size / 1024) / Math.LN2; | ||
const zoomA = range[0] + rangeAdjustment; | ||
const zoomB = range[1] + rangeAdjustment; | ||
const t = Math.max(0, Math.min(1, (transform.zoom - zoomA) / (zoomB - zoomA))); | ||
|
||
return t; | ||
} | ||
|
||
/* | ||
* Calculates the scale difference between Mercator and the given projection at a certain location. | ||
*/ | ||
function getZoomAdjustment(projection: Projection, loc: LngLat) { | ||
const loc2 = new LngLat(loc.lng + 360 / 40000, loc.lat); | ||
const p1 = projection.project(loc.lng, loc.lat); | ||
const p2 = projection.project(loc2.lng, loc2.lat); | ||
|
||
const m1 = MercatorCoordinate.fromLngLat(loc); | ||
const m2 = MercatorCoordinate.fromLngLat(loc2); | ||
|
||
const pdx = p2.x - p1.x; | ||
const pdy = p2.y - p1.y; | ||
const mdx = m2.x - m1.x; | ||
const mdy = m2.y - m1.y; | ||
|
||
const scale = Math.sqrt((mdx * mdx + mdy * mdy) / (pdx * pdx + pdy * pdy)); | ||
|
||
return Math.log(scale) / Math.LN2; | ||
} | ||
|
||
function getShearAdjustment(projection, zoom, loc, interpT) { | ||
|
||
// create a location a tiny amount (~1km) east of the given location | ||
const loc2 = new LngLat(loc.lng + 360 / 40000, loc.lat); | ||
|
||
const p1 = projection.project(loc.lng, loc.lat); | ||
const p2 = projection.project(loc2.lng, loc2.lat); | ||
|
||
const pdx = p2.x - p1.x; | ||
const pdy = p2.y - p1.y; | ||
|
||
// Calculate how much the map would need to be rotated to make east-west in | ||
// projected coordinates be left-right | ||
const angleAdjust = -Math.atan(pdy / pdx) / Math.PI * 180; | ||
|
||
// Find the projected coordinates of two locations, one slightly north and one slightly east. | ||
// Then calculate the transform that would make the projected coordinates of the two locations be: | ||
// - equal distances from the original location | ||
// - perpendicular to one another | ||
// | ||
// Only the position of the coordinate to the north is adjusted. | ||
// The coordinate to the east stays where it is. | ||
const mc3 = MercatorCoordinate.fromLngLat(loc); | ||
const offset = 1 / 40000; | ||
mc3.x += offset; | ||
const loc3 = mc3.toLngLat(); | ||
const p3 = projection.project(loc3.lng, loc3.lat); | ||
const pdx3 = p3.x - p1.x; | ||
const pdy3 = p3.y - p1.y; | ||
const delta3 = rotate(pdx3, pdy3, angleAdjust); | ||
|
||
const mc4 = MercatorCoordinate.fromLngLat(loc); | ||
mc4.y += offset; | ||
const loc4 = mc4.toLngLat(); | ||
const p4 = projection.project(loc4.lng, loc4.lat); | ||
const pdx4 = p4.x - p1.x; | ||
const pdy4 = p4.y - p1.y; | ||
const delta4 = rotate(pdx4, pdy4, angleAdjust); | ||
|
||
const scale = Math.abs(delta3.x) / Math.abs(delta4.y); | ||
|
||
const unrotate = mat4.identity([]); | ||
mat4.rotateZ(unrotate, unrotate, -angleAdjust / 180 * Math.PI * (1 - interpT)); | ||
|
||
// unskew | ||
const shear = mat4.identity([]); | ||
mat4.scale(shear, shear, [1, 1 - (1 - scale) * interpT, 1]); | ||
shear[4] = -delta4.x / delta4.y * interpT; | ||
|
||
// unrotate | ||
mat4.rotateZ(shear, shear, angleAdjust / 180 * Math.PI); | ||
|
||
mat4.multiply(shear, unrotate, shear); | ||
|
||
return shear; | ||
} | ||
|
||
function rotate(x, y, angle) { | ||
const cos = Math.cos(angle / 180 * Math.PI); | ||
const sin = Math.sin(angle / 180 * Math.PI); | ||
return { | ||
x: x * cos - y * sin, | ||
y: x * sin + y * cos | ||
}; | ||
} |
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
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.
the 40000 number is used in a few places. it might make sense to pull it out into a constant that explains where this figure comes from