Skip to content

Commit d7faab3

Browse files
authored
Merge pull request #1354 from OpenGeoscience/map-corners
feat: Add a map.corners() utility function
2 parents 4aaa9a6 + efe5029 commit d7faab3

File tree

4 files changed

+36
-9
lines changed

4 files changed

+36
-9
lines changed

src/map.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1440,6 +1440,27 @@ var map = function (arg) {
14401440
return m_this;
14411441
};
14421442

1443+
/**
1444+
* Get the corners of the map. Since the map can be rotated, this is
1445+
* necessarily not the same as the overall bounds, which is the orthogonal
1446+
* bounding box.
1447+
*
1448+
* @param {string|geo.transform|null} [gcs] `undefined` to use the interface
1449+
* gcs, `null` to use the map gcs, or any other transform. If setting the
1450+
* bounds, they are converted from this gcs to the map projection. The
1451+
* returned bounds are converted from the map projection to this gcs.
1452+
* @returns {geo.geoPosition[]} The corners of the map in the order
1453+
* upper-left, upper-right, lower-right, lower-left.
1454+
*/
1455+
this.corners = function (gcs) {
1456+
return [
1457+
m_this.displayToGcs({x: 0, y: 0}, gcs),
1458+
m_this.displayToGcs({x: m_width, y: 0}, gcs),
1459+
m_this.displayToGcs({x: m_width, y: m_height}, gcs),
1460+
m_this.displayToGcs({x: 0, y: m_height}, gcs)
1461+
];
1462+
};
1463+
14431464
/**
14441465
* Get the center zoom level necessary to display the given bounds.
14451466
*

src/markerFeature.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ var markerFeature = function (arg) {
228228
// Find markers inside the bounding box
229229
idx = m_rangeTree.range(min.x, min.y, max.x, max.y);
230230

231-
idx.sort((a, b) => a - b);
231+
idx = Uint32Array.from(idx).sort();
232232
// Filter by circular region
233233
idx.forEach(function (i) {
234234
var d = data[i],
@@ -353,16 +353,20 @@ var markerFeature = function (arg) {
353353
};
354354
// Find markers inside the bounding box. Only these could be in the polygon
355355
idx = m_rangeTree.range(min.x, min.y, max.x, max.y);
356-
// sort by index
357-
idx.sort((a, b) => a - b);
356+
/* sort by index. This had been
357+
* idx.sort((a, b) => a - b);
358+
* but this requires continual casting from int to str and back, so using
359+
* a Uint32Array is faster, though potentially limits the maximum number of
360+
* markers. */
361+
idx = Uint32Array.from(idx).sort();
358362
// filter markers within the polygon
359363
idx.forEach(function (i) {
360364
var d = data[i];
361365
let p = m_this.position()(d, i);
362-
let rad = radius(data[i], i),
363-
swz = scaleWithZoom(data[i], i);
364-
const so = strokeOffset(data[i], i),
365-
s = swz ? strokeWidth(data[i], i) : 0;
366+
let rad = radius(d, i),
367+
swz = scaleWithZoom(d, i);
368+
const so = strokeOffset(d, i),
369+
s = swz ? strokeWidth(d, i) : 0;
366370
let ris = radiusIncludesStroke(d, i);
367371
ris = ris === undefined ? true : ris;
368372
const rwos = ris ? rad + s * (so - 1) / 2 : rad; // radius without stroke

src/pointFeature.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ var pointFeature = function (arg) {
329329
// Find points inside the bounding box
330330
idx = m_rangeTree.range(min.x, min.y, max.x, max.y);
331331

332-
idx.sort((a, b) => a - b);
332+
idx = Uint32Array.from(idx).sort();
333333
// Filter by circular region
334334
idx.forEach(function (i) {
335335
var d = data[i],
@@ -434,7 +434,7 @@ var pointFeature = function (arg) {
434434
// Find points inside the bounding box. Only these could be in the polygon
435435
idx = m_rangeTree.range(min.x, min.y, max.x, max.y);
436436
// sort by index
437-
idx.sort((a, b) => a - b);
437+
idx = Uint32Array.from(idx).sort();
438438
// filter points within the polygon
439439
idx.forEach(function (i) {
440440
var d = data[i],

tests/cases/map.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,8 @@ describe('geo.core.map', function () {
266266
bottom: -128 * units,
267267
width: 256 * units,
268268
height: 256 * units})).toBe(true);
269+
expect(closeToEqual(m.corners()[0], {x: -180, y: 85.05}));
270+
expect(closeToEqual(m.corners(null)[0], {x: -128 * units, y: 128 * units}));
269271
m.ingcs('EPSG:3857');
270272
expect(m.ingcs()).toBe('EPSG:3857');
271273
expect(closeToEqual(m.bounds(), {

0 commit comments

Comments
 (0)