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

Display-gcs conversion functions can now handle arrays of points. #766

Merged
merged 1 commit into from
Jan 24, 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
2 changes: 1 addition & 1 deletion src/camera.js
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@
point = this.worldToDisplay4(
[point.x, point.y, z, w]
);
return {x: point[0], y: point[1], z: point[2]};
return {x: point[0], y: point[1]};
};

/**
Expand Down
44 changes: 32 additions & 12 deletions src/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -715,12 +715,16 @@ var map = function (arg) {
/**
* Convert from gcs coordinates to map world coordinates.
*
* @param {geo.geoPosition} c The input coordinate to convert.
* @param {geo.geoPosition|geo.geoPosition[]} c The input coordinate to
* convert.
* @param {string|geo.transform|null} [gcs] Input gcs. `undefined` to use
* the interface gcs, `null` to use the map gcs, or any other transform.
* @returns {geo.worldPosition} World space coordinates.
* @returns {geo.worldPosition|geo.worldPosition[]} World space coordinates.
*/
this.gcsToWorld = function (c, gcs) {
if (Array.isArray(c)) {
return c.map(function (pt) { return m_this.gcsToWorld(pt, gcs); });
}
gcs = (gcs === null ? m_gcs : (gcs === undefined ? m_ingcs : gcs));
if (gcs !== m_gcs) {
c = transform.transformCoordinates(gcs, m_gcs, c);
Expand All @@ -739,12 +743,16 @@ var map = function (arg) {
/**
* Convert from map world coordinates to gcs coordinates.
*
* @param {geo.worldPosition} c The input coordinate to convert.
* @param {geo.worldPosition|geo.worldPosition[]} c The input coordinate to
* convert.
* @param {string|geo.transform|null} [gcs] output gcs. `undefined` to use
* the interface gcs, `null` to use the map gcs, or any other transform.
* @returns {geo.geoPosition} GCS space coordinates.
* @returns {geo.geoPosition|geo.geoPosition[]} GCS space coordinates.
*/
this.worldToGcs = function (c, gcs) {
if (Array.isArray(c)) {
return c.map(function (pt) { return m_this.worldToGcs(pt, gcs); });
}
if (m_origin.x || m_origin.y || m_origin.z) {
c = transform.affineInverse(
{origin: m_origin},
Expand All @@ -764,10 +772,12 @@ var map = function (arg) {
* Convert from gcs coordinates to display coordinates. This is identical to
* calling `gcsToWorld` and then `worldToDisplay`.
*
* @param {geo.geoPosition} c The input coordinate to convert.
* @param {geo.geoPosition|geo.geoPosition[]} c The input coordinate to
* convert.
* @param {string|geo.transform|null} [gcs] Input gcs. `undefined` to use
* the interface gcs, `null` to use the map gcs, or any other transform.
* @returns {geo.screenPosition} Display space coordinates.
* @returns {geo.screenPosition|geo.screenPosition[]} Display space
* coordinates.
*/
this.gcsToDisplay = function (c, gcs) {
c = m_this.gcsToWorld(c, gcs);
Expand All @@ -778,21 +788,27 @@ var map = function (arg) {
* Convert from world coordinates to display coordinates using the attached
* camera.
*
* @param {geo.worldPosition} c The input coordinate to convert.
* @returns {geo.screenPosition} Display space coordinates.
* @param {geo.worldPosition|geo.worldPosition[]} c The input coordinate to
* convert.
* @returns {geo.screenPosition|geo.screenPosition[]} Display space
* coordinates.
*/
this.worldToDisplay = function (c) {
if (Array.isArray(c)) {
return c.map(function (pt) { return m_camera.worldToDisplay(pt); });
}
return m_camera.worldToDisplay(c);
};

/**
* Convert from display to gcs coordinates. This is identical to calling
* `displayToWorld` and then `worldToGcs`.
*
* @param {geo.screenPosition} c The input display coordinate to convert.
* @param {geo.screenPosition|geo.screenPosition[]} c The input display
* coordinate to convert.
* @param {string|geo.transform|null} [gcs] Output gcs. `undefined` to use
* the interface gcs, `null` to use the map gcs, or any other transform.
* @returns {geo.geoPosition} GCS space coordinates.
* @returns {geo.geoPosition|geoPosition[]} GCS space coordinates.
*/
this.displayToGcs = function (c, gcs) {
c = m_this.displayToWorld(c); // done via camera
Expand All @@ -803,10 +819,14 @@ var map = function (arg) {
* Convert from display coordinates to world coordinates using the attached
* camera.
*
* @param {geo.screenPosition} c The input coordinate to convert.
* @returns {geo.worldPosition} World space coordinates.
* @param {geo.screenPosition|geo.screenPosition[]} c The input coordinate to
* convert.
* @returns {geo.worldPosition|geo.worldPosition[]} World space coordinates.
*/
this.displayToWorld = function (c) {
if (Array.isArray(c)) {
return c.map(function (pt) { return m_camera.displayToWorld(pt); });
}
return m_camera.displayToWorld(c);
};

Expand Down
31 changes: 30 additions & 1 deletion tests/cases/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,36 @@ describe('geo.core.map', function () {
expect(m.layers().length).toBe(0);
expect(m2.layers().length).toBe(1);
});

it('displayToGcs', function () {
var m = createMap(), result;
expect(closeToEqual(m.displayToGcs({x: 10, y: 20}), {x: -27.25, y: 13.92, z: 0})).toBe(true);
expect(closeToEqual(m.displayToGcs({x: 200, y: 100}), {x: -10.55, y: 7.01, z: 0})).toBe(true);
expect(closeToEqual(m.displayToGcs({x: 10, y: 20}, 'EPSG:3857'), {x: -3033021, y: 1565430, z: 0}, 0)).toBe(true);
expect(closeToEqual(m.displayToGcs({x: 200, y: 100}, 'EPSG:3857'), {x: -1174073, y: 782715, z: 0}, 0)).toBe(true);
result = m.displayToGcs([{x: 10, y: 20}, {x: 200, y: 100}]);
expect(closeToEqual(result[0], {x: -27.25, y: 13.92, z: 0})).toBe(true);
expect(closeToEqual(result[1], {x: -10.55, y: 7.01, z: 0})).toBe(true);
result = m.displayToGcs([{x: 10, y: 20}, {x: 200, y: 100}], 'EPSG:3857');
expect(closeToEqual(result[0], {x: -3033021, y: 1565430, z: 0}, 0)).toBe(true);
expect(closeToEqual(result[1], {x: -1174073, y: 782715, z: 0}, 0)).toBe(true);
m.bounds({left: -80, top: 50, right: -40, bottom: 40});
expect(closeToEqual(m.displayToGcs({x: 10, y: 20}), {x: -79.38, y: 51.83, z: 0})).toBe(true);
});
it('gcsToDisplay', function () {
var m = createMap(), result;
expect(closeToEqual(m.gcsToDisplay({x: -27.25, y: 13.92}), {x: 10, y: 20}, 1)).toBe(true);
expect(closeToEqual(m.gcsToDisplay({x: -10.55, y: 7.01}), {x: 200, y: 100}, 1)).toBe(true);
expect(closeToEqual(m.gcsToDisplay({x: -3033021, y: 1565430}, 'EPSG:3857'), {x: 10, y: 20})).toBe(true);
expect(closeToEqual(m.gcsToDisplay({x: -1174073, y: 782715}, 'EPSG:3857'), {x: 200, y: 100})).toBe(true);
result = m.gcsToDisplay([{x: -27.25, y: 13.92}, {x: -10.55, y: 7.01}]);
expect(closeToEqual(result[0], {x: 10, y: 20}, 1)).toBe(true);
expect(closeToEqual(result[1], {x: 200, y: 100}, 1)).toBe(true);
result = m.gcsToDisplay([{x: -3033021, y: 1565430}, {x: -1174073, y: 782715}], 'EPSG:3857');
expect(closeToEqual(result[0], {x: 10, y: 20})).toBe(true);
expect(closeToEqual(result[1], {x: 200, y: 100})).toBe(true);
m.bounds({left: -80, top: 50, right: -40, bottom: 40});
expect(closeToEqual(m.gcsToDisplay({x: -79.375, y: 51.83}), {x: 10, y: 20}, 1)).toBe(true);
});
});
describe('screenshot', function () {
var m, layer1, layer2, l1, l2;
Expand Down