Skip to content

Commit

Permalink
support viewports crossing the antimeridian (mapbox#31)
Browse files Browse the repository at this point in the history
* also prep for immediate release of v0.5.0
  • Loading branch information
Josh Erb authored Sep 21, 2021
1 parent 865e4b7 commit f114fdc
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 14 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## v0.5.0

* Update viewport method to take an optional `allowAntiMeridian` parameter

## v0.4.0

* Update viewport method to take an optional `allowFloat` parameter to allow float values (h/t @TeaSeaLancs) [#15](https://github.com/mapbox/geo-viewport/pull/15)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ console.log(center);

## api

### `viewport(bounds, dimensions, minzoom, maxzoom, tileSize, allowFloat)`
### `viewport(bounds, dimensions, minzoom, maxzoom, tileSize, allowFloat, allowAntiMeridian)`

Given a `WSEN` array of bounds and a `[x, y]` array of pixel dimensions, return a `{ center: [lon, lat], zoom: zoom }` viewport. Use `allowFloat` to retain float values in the output.

Expand Down
14 changes: 8 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ var smCache = {};
module.exports.viewport = viewport;
module.exports.bounds = bounds;

function fetchMerc(tileSize) {
function fetchMerc(tileSize, allowAntiMeridian) {
tileSize = tileSize || 256;
antiMeridian = allowAntiMeridian || false;

if (!smCache[tileSize]) {
smCache[tileSize] = new SphericalMercator({ size: tileSize });
var cacheKey = tileSize + String(antiMeridian);
if (!smCache[cacheKey]) {
smCache[cacheKey] = new SphericalMercator({ size: tileSize, antimeridian: antiMeridian });
}

return smCache[tileSize];
return smCache[cacheKey];
}

function getAdjusted(base, ratios, allowFloat) {
Expand All @@ -29,10 +31,10 @@ function getAdjusted(base, ratios, allowFloat) {
return allowFloat ? adjusted : Math.floor(adjusted);
}

function viewport(bounds, dimensions, minzoom, maxzoom, tileSize, allowFloat) {
function viewport(bounds, dimensions, minzoom, maxzoom, tileSize, allowFloat, allowAntiMeridian) {
minzoom = (minzoom === undefined) ? 0 : minzoom;
maxzoom = (maxzoom === undefined) ? 20 : maxzoom;
var merc = fetchMerc(tileSize);
var merc = fetchMerc(tileSize, allowAntiMeridian);
var base = maxzoom;
var bl = merc.px([bounds[0], bounds[1]], base);
var tr = merc.px([bounds[2], bounds[3]], base);
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
"bugs": {
"url": "https://github.com/mapbox/geo-viewport/issues"
},
"version": "0.4.1",
"version": "0.5.0",
"dependencies": {
"@mapbox/sphericalmercator": "~1.1.0"
"@mapbox/sphericalmercator": "^1.2.0"
},
"scripts": {
"test": "nyc tap test/*.js",
Expand Down Expand Up @@ -42,4 +42,4 @@
"index.js"
]
}
}
}
14 changes: 14 additions & 0 deletions test/viewport.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,20 @@ test('viewport in Southern hemisphere', function(t) {
t.end();
});

test('viewport across the antimeridian', function(t) {
t.ok(areViewportsApproximatelyEqual(
viewport.viewport([175, -43, 190, -43], [300, 200], undefined, undefined, 512, true, false),
{ center: [177.5000001490116, -43.00000017011762], zoom: 5.398743777929521 }
));

t.ok(areViewportsApproximatelyEqual(
viewport.viewport([175, -43, 190, -43], [300, 200], undefined, undefined, 512, true, true),
{ center: [182.50000018626451, -43.00000017011762], zoom: 3.8137812127148685 }
));

t.end();
});

test('bounds for 512px tiles', function(t) {
var bounds = viewport.bounds([-77.036556, 38.897708], 17, [1080, 350], 512);
var xMin = bounds[0];
Expand Down

0 comments on commit f114fdc

Please sign in to comment.