Warn once when suspicious coordinate are used

This commit is contained in:
Andreas Hocevar
2022-02-14 14:55:58 +01:00
parent 75a3adccce
commit 63fc00902f
3 changed files with 72 additions and 2 deletions

View File

@@ -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);
}

View File

@@ -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<number>} input Input coordinate array.
* @param {Array<number>} [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);