Skip to content
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

Clamp latitude value to valid Web Mercator range when projecting to y #6918

Merged
merged 1 commit into from
Aug 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/geo/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class Transform {
tileZoom: number;
lngRange: ?[number, number];
latRange: ?[number, number];
maxValidLatitude: number;
scale: number;
width: number;
height: number;
Expand All @@ -47,6 +48,7 @@ class Transform {

constructor(minZoom: ?number, maxZoom: ?number, renderWorldCopies: boolean | void) {
this.tileSize = 512; // constant
this.maxValidLatitude = 85.051129; // constant

this._renderWorldCopies = renderWorldCopies === undefined ? true : renderWorldCopies;
this._minZoom = minZoom || 0;
Expand Down Expand Up @@ -284,7 +286,7 @@ class Transform {
get point(): Point { return new Point(this.x, this.y); }

/**
* latitude to absolute x coord
* longitude to absolute x coord
* @returns {number} pixel coordinate
*/
lngX(lng: number) {
Expand All @@ -295,6 +297,7 @@ class Transform {
* @returns {number} pixel coordinate
*/
latY(lat: number) {
lat = clamp(lat, -this.maxValidLatitude, this.maxValidLatitude);
const y = 180 / Math.PI * Math.log(Math.tan(Math.PI / 4 + lat * Math.PI / 360));
return (180 - y) * this.worldSize / 360;
}
Expand Down Expand Up @@ -433,7 +436,7 @@ class Transform {
this._constrain();
} else {
this.lngRange = null;
this.latRange = [-85.05113, 85.05113];
this.latRange = [-this.maxValidLatitude, this.maxValidLatitude];
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/style-spec/reference/v8.json
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,9 @@
"length": 4,
"default": [
-180,
-85.0511,
-85.051129,
180,
85.0511
85.051129
],
"doc": "An array containing the longitude and latitude of the southwest and northeast corners of the source's bounding box in the following order: `[sw.lng, sw.lat, ne.lng, ne.lat]`. When this property is included in a source, no tiles outside of the given bounds are requested by Mapbox GL."
},
Expand Down Expand Up @@ -208,9 +208,9 @@
"length": 4,
"default": [
-180,
-85.0511,
-85.051129,
180,
85.0511
85.051129
],
"doc": "An array containing the longitude and latitude of the southwest and northeast corners of the source's bounding box in the following order: `[sw.lng, sw.lat, ne.lng, ne.lat]`. When this property is included in a source, no tiles outside of the given bounds are requested by Mapbox GL."
},
Expand Down Expand Up @@ -278,9 +278,9 @@
"length": 4,
"default": [
-180,
-85.0511,
-85.051129,
180,
85.0511
85.051129
],
"doc": "An array containing the longitude and latitude of the southwest and northeast corners of the source's bounding box in the following order: `[sw.lng, sw.lat, ne.lng, ne.lat]`. When this property is included in a source, no tiles outside of the given bounds are requested by Mapbox GL."
},
Expand Down
9 changes: 9 additions & 0 deletions test/unit/geo/transform.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ test('transform', (t) => {
const transform = new Transform();
transform.resize(500, 500);
t.equal(transform.unmodified, true);
t.equal(transform.maxValidLatitude, 85.051129);
t.equal(transform.tileSize, 512, 'tileSize');
t.equal(transform.worldSize, 512, 'worldSize');
t.equal(transform.width, 500, 'width');
Expand Down Expand Up @@ -216,6 +217,14 @@ test('transform', (t) => {
t.end();
});

t.test('clamps latitude', (t) => {
const transform = new Transform();

t.equal(transform.latY(-90), transform.latY(-transform.maxValidLatitude));
t.equal(transform.latY(90), transform.latY(transform.maxValidLatitude));
t.end();
});

t.test('clamps pitch', (t) => {
const transform = new Transform();

Expand Down
8 changes: 4 additions & 4 deletions test/unit/ui/camera.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ test('camera', (t) => {
const camera = createCamera({ zoom: 0 });
camera.rotateTo(90, { around: [5, 0], duration: 0 });
t.equal(camera.getBearing(), 90);
t.deepEqual(fixedLngLat(camera.getCenter()), fixedLngLat({ lng: 4.999999999999972, lat: 0.000014144426558004852 }));
t.deepEqual(fixedLngLat(camera.getCenter()), fixedLngLat({ lng: 4.999999999999972, lat: 0.000002552471840999715 }));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quick question — why is value here different? Latitude is close to 0 so isn't supposed to be affected by clamping here...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is caused by a slight change in the maxValidLatitude value from 85.05113 to 85.051129. Should I revert to the older value?

t.end();
});

Expand All @@ -519,7 +519,7 @@ test('camera', (t) => {
const camera = createCamera({ zoom: 0 });
camera.rotateTo(90, { offset: [100, 0], duration: 0 });
t.equal(camera.getBearing(), 90);
t.deepEqual(fixedLngLat(camera.getCenter()), fixedLngLat({ lng: 70.3125, lat: 0.000014144426558004852 }));
t.deepEqual(fixedLngLat(camera.getCenter()), fixedLngLat({ lng: 70.3125, lat: 0.000002552471840999715 }));
t.end();
});

Expand Down Expand Up @@ -684,15 +684,15 @@ test('camera', (t) => {
const camera = createCamera();
camera.easeTo({ bearing: 90, offset: [100, 0], duration: 0 });
t.equal(camera.getBearing(), 90);
t.deepEqual(fixedLngLat(camera.getCenter()), fixedLngLat({ lng: 70.3125, lat: 0.0000141444 }));
t.deepEqual(fixedLngLat(camera.getCenter()), fixedLngLat({ lng: 70.3125, lat: 0.000002552471840999715 }));
t.end();
});

t.test('rotates with specified offset relative to viewport on a rotated camera', (t) => {
const camera = createCamera({bearing: 180});
camera.easeTo({ bearing: 90, offset: [100, 0], duration: 0 });
t.equal(camera.getBearing(), 90);
t.deepEqual(fixedLngLat(camera.getCenter()), fixedLngLat({ lng: -70.3125, lat: 0.0000141444 }));
t.deepEqual(fixedLngLat(camera.getCenter()), fixedLngLat({ lng: -70.3125, lat: 0.000002552471840999715 }));
t.end();
});

Expand Down