From 75a3adccce4ec3c59f469db85fa7fdf0d7f7c6b9 Mon Sep 17 00:00:00 2001 From: Andreas Hocevar Date: Mon, 14 Feb 2022 14:09:49 +0100 Subject: [PATCH 1/3] Add useGeographic() and setUserProjection() to the API --- src/ol/proj.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/ol/proj.js b/src/ol/proj.js index fb19ed996a..9071ef9360 100644 --- a/src/ol/proj.js +++ b/src/ol/proj.js @@ -539,18 +539,17 @@ let userProjection = null; /** * Set the projection for coordinates supplied from and returned by API methods. - * Note that this method is not yet a part of the stable API. Support for user - * projections is not yet complete and should be considered experimental. + * This includes all API methods except for those interacting with tile grids. * @param {ProjectionLike} projection The user projection. + * @api */ export function setUserProjection(projection) { userProjection = get(projection); } /** - * Clear the user projection if set. Note that this method is not yet a part of - * the stable API. Support for user projections is not yet complete and should - * be considered experimental. + * Clear the user projection if set. + * @api */ export function clearUserProjection() { userProjection = null; @@ -561,15 +560,16 @@ export function clearUserProjection() { * Note that this method is not yet a part of the stable API. Support for user * projections is not yet complete and should be considered experimental. * @return {Projection|null} The user projection (or null if not set). + * @api */ export function getUserProjection() { return userProjection; } /** - * Use geographic coordinates (WGS-84 datum) in API methods. Note that this - * method is not yet a part of the stable API. Support for user projections is - * not yet complete and should be considered experimental. + * Use geographic coordinates (WGS-84 datum) in API methods. This includes all API + * methods except for those interacting with tile grids. + * @api */ export function useGeographic() { setUserProjection('EPSG:4326'); From 63fc00902faff8c017c600fbd8c1f04791add5d4 Mon Sep 17 00:00:00 2001 From: Andreas Hocevar Date: Mon, 14 Feb 2022 14:55:58 +0100 Subject: [PATCH 2/3] Warn once when suspicious coordinate are used --- src/ol/View.js | 6 +++++- src/ol/proj.js | 27 +++++++++++++++++++++++++- test/node/ol/proj.test.js | 41 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+), 2 deletions(-) diff --git a/src/ol/View.js b/src/ol/View.js index 4acc41003a..bda219cb0d 100644 --- a/src/ol/View.js +++ b/src/ol/View.js @@ -10,6 +10,7 @@ import {DEFAULT_TILE_SIZE} from './tilegrid/common.js'; import { METERS_PER_UNIT, createProjection, + disableCoordinateWarning, fromUserCoordinate, fromUserExtent, getUserProjection, @@ -241,7 +242,7 @@ const DEFAULT_MIN_ZOOM = 0; * A View has a `projection`. The projection determines the * coordinate system of the center, and its units determine the units of the * resolution (projection units per pixel). The default projection is - * Spherical Mercator (EPSG:3857). + * Web Mercator (EPSG:3857). * * ### The view states * @@ -406,6 +407,9 @@ class View extends BaseObject { if (options.extent) { options.extent = fromUserExtent(options.extent, this.projection_); } + if (options.projection) { + disableCoordinateWarning(); + } this.applyOptions_(options); } diff --git a/src/ol/proj.js b/src/ol/proj.js index 9071ef9360..c8c3876c4b 100644 --- a/src/ol/proj.js +++ b/src/ol/proj.js @@ -73,8 +73,8 @@ import { } from './proj/transforms.js'; import {applyTransform, getWidth} from './extent.js'; import {clamp, modulo} from './math.js'; +import {equals, getWorldsAway} from './coordinate.js'; import {getDistance} from './sphere.js'; -import {getWorldsAway} from './coordinate.js'; /** * A projection as {@link module:ol/proj/Projection}, SRS identifier @@ -97,6 +97,16 @@ export {METERS_PER_UNIT}; export {Projection}; +let showCoordinateWarning = true; + +/** + * @param {boolean} [opt_disable = true] Disable console info about `useGeographic()` + */ +export function disableCoordinateWarning(opt_disable) { + const hide = opt_disable === undefined ? true : opt_disable; + showCoordinateWarning = !hide; +} + /** * @param {Array} input Input coordinate array. * @param {Array} [opt_output] Output array of coordinate values. @@ -386,6 +396,7 @@ export function addCoordinateTransforms(source, destination, forward, inverse) { * @api */ export function fromLonLat(coordinate, opt_projection) { + disableCoordinateWarning(); return transform( coordinate, 'EPSG:4326', @@ -598,6 +609,20 @@ export function toUserCoordinate(coordinate, sourceProjection) { */ export function fromUserCoordinate(coordinate, destProjection) { if (!userProjection) { + if ( + showCoordinateWarning && + !equals(coordinate, [0, 0]) && + coordinate[0] >= -180 && + coordinate[0] <= 180 && + coordinate[1] >= -90 && + coordinate[1] <= 90 + ) { + showCoordinateWarning = false; + // eslint-disable-next-line no-console + console.warn( + 'Call useGeographic() ol/proj once to work with [longitude, latitude] coordinates.' + ); + } return coordinate; } return transform(coordinate, userProjection, destProjection); diff --git a/test/node/ol/proj.test.js b/test/node/ol/proj.test.js index 5002b807b7..5cf40ab139 100644 --- a/test/node/ol/proj.test.js +++ b/test/node/ol/proj.test.js @@ -1,5 +1,6 @@ import Projection from '../../../src/ol/proj/Projection.js'; import Units from '../../../src/ol/proj/Units.js'; +import View from '../../../src/ol/View.js'; import expect from '../expect.js'; import proj4 from 'proj4'; import {HALF_SIZE} from '../../../src/ol/proj/epsg3857.js'; @@ -8,6 +9,7 @@ import { addCommon, clearAllProjections, clearUserProjection, + disableCoordinateWarning, equivalent, fromLonLat, fromUserCoordinate, @@ -890,4 +892,43 @@ describe('ol/proj.js', function () { ); }); }); + + describe('Console info about `setUserProjection`', function () { + let originalConsole, callCount; + beforeEach(function () { + disableCoordinateWarning(false); + originalConsole = console; + callCount = 0; + global.console = { + ...console, + warn: () => ++callCount, + }; + }); + afterEach(function () { + global.console = originalConsole; + clearUserProjection(); + }); + it('is shown once when suspicious coordinates are used', function () { + const view = new View({ + center: [16, 48], + }); + view.setCenter([15, 47]); + expect(callCount).to.be(1); + }); + it('is not shown when fromLonLat() is used', function () { + const view = new View({ + center: fromLonLat([16, 48]), + }); + view.setCenter(fromLonLat([15, 47])); + expect(callCount).to.be(0); + }); + it('is not shown when useGeographic() is used', function () { + useGeographic(); + const view = new View({ + center: [16, 48], + }); + view.setCenter([15, 47]); + expect(callCount).to.be(0); + }); + }); }); From 4f614bc81f06c1d2e2cbf051aed4e064dcfb10e0 Mon Sep 17 00:00:00 2001 From: Andreas Hocevar Date: Tue, 15 Feb 2022 08:50:29 +0100 Subject: [PATCH 3/3] Remove experimental category from geographic examples --- examples/edit-geographic.html | 1 - examples/geographic.html | 1 - examples/immediate-geographic.html | 1 - examples/vector-wfs-geographic.html | 1 - 4 files changed, 4 deletions(-) diff --git a/examples/edit-geographic.html b/examples/edit-geographic.html index 02131724be..3f66d462ba 100644 --- a/examples/edit-geographic.html +++ b/examples/edit-geographic.html @@ -7,7 +7,6 @@ docs: > makes it so the map view uses geographic coordinates (even if the view projection is not geographic). tags: "geographic" -experimental: true ---