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

feat: Add a map.corners() utility function #1354

Merged
merged 3 commits into from
Dec 10, 2024
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
21 changes: 21 additions & 0 deletions src/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -1440,6 +1440,27 @@ var map = function (arg) {
return m_this;
};

/**
* Get the corners of the map. Since the map can be rotated, this is
* necessarily not the same as the overall bounds, which is the orthogonal
* bounding box.
*
* @param {string|geo.transform|null} [gcs] `undefined` to use the interface
* gcs, `null` to use the map gcs, or any other transform. If setting the
* bounds, they are converted from this gcs to the map projection. The
* returned bounds are converted from the map projection to this gcs.
* @returns {geo.geoPosition[]} The corners of the map in the order
* upper-left, upper-right, lower-right, lower-left.
*/
this.corners = function (gcs) {
return [
m_this.displayToGcs({x: 0, y: 0}, gcs),
m_this.displayToGcs({x: m_width, y: 0}, gcs),
m_this.displayToGcs({x: m_width, y: m_height}, gcs),
m_this.displayToGcs({x: 0, y: m_height}, gcs)
];
};

/**
* Get the center zoom level necessary to display the given bounds.
*
Expand Down
18 changes: 11 additions & 7 deletions src/markerFeature.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ var markerFeature = function (arg) {
// Find markers inside the bounding box
idx = m_rangeTree.range(min.x, min.y, max.x, max.y);

idx.sort((a, b) => a - b);
idx = Uint32Array.from(idx).sort();
// Filter by circular region
idx.forEach(function (i) {
var d = data[i],
Expand Down Expand Up @@ -353,16 +353,20 @@ var markerFeature = function (arg) {
};
// Find markers inside the bounding box. Only these could be in the polygon
idx = m_rangeTree.range(min.x, min.y, max.x, max.y);
// sort by index
idx.sort((a, b) => a - b);
/* sort by index. This had been
* idx.sort((a, b) => a - b);
* but this requires continual casting from int to str and back, so using
* a Uint32Array is faster, though potentially limits the maximum number of
* markers. */
idx = Uint32Array.from(idx).sort();
// filter markers within the polygon
idx.forEach(function (i) {
var d = data[i];
let p = m_this.position()(d, i);
let rad = radius(data[i], i),
swz = scaleWithZoom(data[i], i);
const so = strokeOffset(data[i], i),
s = swz ? strokeWidth(data[i], i) : 0;
let rad = radius(d, i),
swz = scaleWithZoom(d, i);
const so = strokeOffset(d, i),
s = swz ? strokeWidth(d, i) : 0;
let ris = radiusIncludesStroke(d, i);
ris = ris === undefined ? true : ris;
const rwos = ris ? rad + s * (so - 1) / 2 : rad; // radius without stroke
Expand Down
4 changes: 2 additions & 2 deletions src/pointFeature.js
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ var pointFeature = function (arg) {
// Find points inside the bounding box
idx = m_rangeTree.range(min.x, min.y, max.x, max.y);

idx.sort((a, b) => a - b);
idx = Uint32Array.from(idx).sort();
// Filter by circular region
idx.forEach(function (i) {
var d = data[i],
Expand Down Expand Up @@ -434,7 +434,7 @@ var pointFeature = function (arg) {
// Find points inside the bounding box. Only these could be in the polygon
idx = m_rangeTree.range(min.x, min.y, max.x, max.y);
// sort by index
idx.sort((a, b) => a - b);
idx = Uint32Array.from(idx).sort();
// filter points within the polygon
idx.forEach(function (i) {
var d = data[i],
Expand Down
2 changes: 2 additions & 0 deletions tests/cases/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,8 @@ describe('geo.core.map', function () {
bottom: -128 * units,
width: 256 * units,
height: 256 * units})).toBe(true);
expect(closeToEqual(m.corners()[0], {x: -180, y: 85.05}));
expect(closeToEqual(m.corners(null)[0], {x: -128 * units, y: 128 * units}));
m.ingcs('EPSG:3857');
expect(m.ingcs()).toBe('EPSG:3857');
expect(closeToEqual(m.bounds(), {
Expand Down
Loading