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

fix albers edge cases and lambert center #11229

Merged
merged 4 commits into from Nov 5, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
59 changes: 30 additions & 29 deletions src/geo/projection/albers.js
@@ -1,6 +1,6 @@
// @flow
import LngLat from '../lng_lat.js';
import {clamp, wrap, degToRad, radToDeg} from '../../util/util.js';
import {clamp, degToRad, radToDeg} from '../../util/util.js';
import {MAX_MERCATOR_LATITUDE} from '../mercator_coordinate.js';
import {vec2} from 'gl-matrix';

Expand All @@ -13,47 +13,48 @@ export default {

conic: true,

// based on https://github.com/d3/d3-geo-projection, MIT-licensed

initializeConstants() {
if (this.constants && vec2.exactEquals(this.parallels, this.constants.parallels)) {
return;
}

const p1 = degToRad(this.parallels[0]);
const p2 = degToRad(this.parallels[1]);
const sinp1 = Math.sin(p1);
const cosp1 = Math.cos(p1);
const cosp12 = cosp1 * cosp1;
const r = 0.5;
const n = 0.5 * (sinp1 + Math.sin(p2));
const c = cosp12 + 2 * n * sinp1;
const b = r / n * Math.sqrt(c);

this.constants = {n, b, c, parallels: this.parallels};
const sy0 = Math.sin(degToRad(this.parallels[0]));
const n = (sy0 + Math.sin(degToRad(this.parallels[1]))) / 2;
const c = 1 + sy0 * (2 * n - sy0);
const r0 = Math.sqrt(c) / n;

this.constants = {n, c, r0, parallels: this.parallels};
},
project(lng: number, lat: number) {
this.initializeConstants();

const {n, b, c} = this.constants;
const theta = n * degToRad(lng - this.center[0]);
const a = 0.5 / n * Math.sqrt(c - 2 * n * Math.sin(degToRad(lat)));
const x = a * Math.sin(theta);
const y = b - a * Math.cos(theta);
const lng_ = degToRad(lng - this.center[0]);
const lat_ = degToRad(lat);

return {x: 1 + 0.5 * x, y: 1 - 0.5 * y};
const {n, c, r0} = this.constants;
const r = Math.sqrt(c - 2 * n * Math.sin(lat_)) / n;
const x = r * Math.sin(lng_ * n);
const y = r0 - r * Math.cos(lng_ * n);
return {x, y: -y};
},
unproject(x: number, y: number) {
this.initializeConstants();
const {n, c, r0} = this.constants;

y = -y;

const r0y = r0 - y;
let l = Math.atan2(x, Math.abs(r0y)) * Math.sign(r0y);
if (r0y * n < 0) {
l -= Math.PI * Math.sign(x) * Math.sign(r0y);
}
const lng = l / n;
const lat = Math.asin(clamp((c - (x * x + r0y * r0y) * n * n) / (2 * n), -1, 1));

const {n, b, c} = this.constants;
const x_ = (x - 1) * 2;
const y_ = (y - 1) * -2;
const y2 = -(y_ - b);
const dt = degToRad(this.center[0]) * n;
const theta = wrap(Math.atan2(x_, y2), -Math.PI - dt, Math.PI - dt);
const lng = radToDeg(theta / n) + this.center[0];
const a = x_ / Math.sin(theta);
const s = clamp((Math.pow(a / 0.5 * n, 2) - c) / (-2 * n), -1, 1);
const lat = clamp(radToDeg(Math.asin(s)), -MAX_MERCATOR_LATITUDE, MAX_MERCATOR_LATITUDE);
return new LngLat(lng, lat);
return new LngLat(
radToDeg(lng) + this.center[0],
clamp(radToDeg(lat), -MAX_MERCATOR_LATITUDE, MAX_MERCATOR_LATITUDE));
}
};
2 changes: 1 addition & 1 deletion src/geo/projection/cylindrical_equal_area.js
Expand Up @@ -4,7 +4,7 @@ import {clamp, degToRad, radToDeg} from '../../util/util.js';
import {MAX_MERCATOR_LATITUDE} from '../mercator_coordinate.js';

export default function(phi: number) {
const cosPhi = Math.cos(degToRad(phi));
const cosPhi = Math.max(0.01, Math.cos(degToRad(phi)));
// scale coordinates between 0 and 1 to avoid constraint issues
const scale = 1 / (2 * Math.max(Math.PI * cosPhi, 1 / cosPhi));

Expand Down
4 changes: 2 additions & 2 deletions src/geo/projection/lambert.js
Expand Up @@ -38,7 +38,7 @@ export default {

// based on https://github.com/d3/d3-geo, MIT-licensed
lat = degToRad(lat);
lng = degToRad(lng);
lng = degToRad(lng - this.center[0]);

const epsilon = 1e-6;
const {n, f} = this.constants;
Expand Down Expand Up @@ -73,7 +73,7 @@ export default {

if (fy * n < 0) l -= Math.PI * Math.sign(x) * signFy;

const lng = clamp(radToDeg(l / n), -180, 180);
const lng = clamp(radToDeg(l / n) + this.center[0], -180, 180);
const phi = 2 * Math.atan(Math.pow(f / r, 1 / n)) - halfPi;
const lat = clamp(radToDeg(phi), -MAX_MERCATOR_LATITUDE, MAX_MERCATOR_LATITUDE);

Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/integration/render-tests/video/projected/expected.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.