Replace proj/Units enum with typedef

This commit is contained in:
Maximilian Krög
2022-07-17 00:36:18 +02:00
parent 361f1ab837
commit 7ac61fdc70
14 changed files with 52 additions and 93 deletions

View File

@@ -2,7 +2,6 @@
* @module ol/View * @module ol/View
*/ */
import BaseObject from './Object.js'; import BaseObject from './Object.js';
import Units from './proj/Units.js';
import ViewHint from './ViewHint.js'; import ViewHint from './ViewHint.js';
import ViewProperty from './ViewProperty.js'; import ViewProperty from './ViewProperty.js';
import {DEFAULT_TILE_SIZE} from './tilegrid/common.js'; import {DEFAULT_TILE_SIZE} from './tilegrid/common.js';
@@ -2008,7 +2007,7 @@ export function createResolutionConstraint(options) {
// calculate the default min and max resolution // calculate the default min and max resolution
const size = !projExtent const size = !projExtent
? // use an extent that can fit the whole world if need be ? // use an extent that can fit the whole world if need be
(360 * METERS_PER_UNIT[Units.DEGREES]) / projection.getMetersPerUnit() (360 * METERS_PER_UNIT.degrees) / projection.getMetersPerUnit()
: Math.max(getWidth(projExtent), getHeight(projExtent)); : Math.max(getWidth(projExtent), getHeight(projExtent));
const defaultMaxResolution = const defaultMaxResolution =

View File

@@ -2,7 +2,6 @@
* @module ol/control/ScaleLine * @module ol/control/ScaleLine
*/ */
import Control from './Control.js'; import Control from './Control.js';
import ProjUnits from '../proj/Units.js';
import {CLASS_UNSELECTABLE} from '../css.js'; import {CLASS_UNSELECTABLE} from '../css.js';
import {METERS_PER_UNIT, getPointResolution} from '../proj.js'; import {METERS_PER_UNIT, getPointResolution} from '../proj.js';
import {assert} from '../asserts.js'; import {assert} from '../asserts.js';
@@ -237,8 +236,7 @@ class ScaleLine extends Control {
const center = viewState.center; const center = viewState.center;
const projection = viewState.projection; const projection = viewState.projection;
const units = this.getUnits(); const units = this.getUnits();
const pointResolutionUnits = const pointResolutionUnits = units == 'degrees' ? 'degrees' : 'm';
units == 'degrees' ? ProjUnits.DEGREES : ProjUnits.METERS;
let pointResolution = getPointResolution( let pointResolution = getPointResolution(
projection, projection,
viewState.resolution, viewState.resolution,
@@ -257,7 +255,7 @@ class ScaleLine extends Control {
let nominalCount = minWidth * pointResolution; let nominalCount = minWidth * pointResolution;
let suffix = ''; let suffix = '';
if (units == 'degrees') { if (units == 'degrees') {
const metersPerDegree = METERS_PER_UNIT[ProjUnits.DEGREES]; const metersPerDegree = METERS_PER_UNIT.degrees;
nominalCount *= metersPerDegree; nominalCount *= metersPerDegree;
if (nominalCount < metersPerDegree / 60) { if (nominalCount < metersPerDegree / 60) {
suffix = '\u2033'; // seconds suffix = '\u2033'; // seconds
@@ -498,7 +496,7 @@ class ScaleLine extends Control {
this.viewState_.projection, this.viewState_.projection,
this.viewState_.resolution, this.viewState_.resolution,
this.viewState_.center, this.viewState_.center,
ProjUnits.METERS 'm'
); );
const dpi = this.dpi_ || DEFAULT_DPI; const dpi = this.dpi_ || DEFAULT_DPI;
const inchesPerMeter = 1000 / 25.4; const inchesPerMeter = 1000 / 25.4;

View File

@@ -1,7 +1,6 @@
/** /**
* @module ol/format/Feature * @module ol/format/Feature
*/ */
import Units from '../proj/Units.js';
import {abstract} from '../util.js'; import {abstract} from '../util.js';
import { import {
equivalent as equivalentProjection, equivalent as equivalentProjection,
@@ -103,7 +102,7 @@ class FeatureFormat {
if ( if (
opt_options.extent && opt_options.extent &&
dataProjection && dataProjection &&
dataProjection.getUnits() === Units.TILE_PIXELS dataProjection.getUnits() === 'tile-pixels'
) { ) {
dataProjection = getProjection(dataProjection); dataProjection = getProjection(dataProjection);
dataProjection.setWorldExtent(opt_options.extent); dataProjection.setWorldExtent(opt_options.extent);

View File

@@ -13,7 +13,6 @@ import Point from '../geom/Point.js';
import Polygon from '../geom/Polygon.js'; import Polygon from '../geom/Polygon.js';
import Projection from '../proj/Projection.js'; import Projection from '../proj/Projection.js';
import RenderFeature from '../render/Feature.js'; import RenderFeature from '../render/Feature.js';
import Units from '../proj/Units.js';
import {assert} from '../asserts.js'; import {assert} from '../asserts.js';
import {get} from '../proj.js'; import {get} from '../proj.js';
import {inflateEnds} from '../geom/flat/orient.js'; import {inflateEnds} from '../geom/flat/orient.js';
@@ -52,7 +51,7 @@ class MVT extends FeatureFormat {
*/ */
this.dataProjection = new Projection({ this.dataProjection = new Projection({
code: '', code: '',
units: Units.TILE_PIXELS, units: 'tile-pixels',
}); });
/** /**

View File

@@ -2,7 +2,6 @@
* @module ol/geom/Geometry * @module ol/geom/Geometry
*/ */
import BaseObject from '../Object.js'; import BaseObject from '../Object.js';
import Units from '../proj/Units.js';
import {abstract} from '../util.js'; import {abstract} from '../util.js';
import { import {
compose as composeTransform, compose as composeTransform,
@@ -310,7 +309,7 @@ class Geometry extends BaseObject {
/** @type {import("../proj/Projection.js").default} */ /** @type {import("../proj/Projection.js").default} */
const sourceProj = getProjection(source); const sourceProj = getProjection(source);
const transformFn = const transformFn =
sourceProj.getUnits() == Units.TILE_PIXELS sourceProj.getUnits() == 'tile-pixels'
? function (inCoordinates, outCoordinates, stride) { ? function (inCoordinates, outCoordinates, stride) {
const pixelExtent = sourceProj.getExtent(); const pixelExtent = sourceProj.getExtent();
const projectedExtent = sourceProj.getWorldExtent(); const projectedExtent = sourceProj.getWorldExtent();

View File

@@ -54,13 +54,13 @@
* this. * this.
*/ */
import Projection from './proj/Projection.js'; import Projection from './proj/Projection.js';
import Units, {METERS_PER_UNIT} from './proj/Units.js';
import { import {
PROJECTIONS as EPSG3857_PROJECTIONS, PROJECTIONS as EPSG3857_PROJECTIONS,
fromEPSG4326, fromEPSG4326,
toEPSG4326, toEPSG4326,
} from './proj/epsg3857.js'; } from './proj/epsg3857.js';
import {PROJECTIONS as EPSG4326_PROJECTIONS} from './proj/epsg4326.js'; import {PROJECTIONS as EPSG4326_PROJECTIONS} from './proj/epsg4326.js';
import {METERS_PER_UNIT} from './proj/Units.js';
import { import {
add as addProj, add as addProj,
clear as clearProj, clear as clearProj,
@@ -192,7 +192,7 @@ export function get(projectionLike) {
* @param {ProjectionLike} projection The projection. * @param {ProjectionLike} projection The projection.
* @param {number} resolution Nominal resolution in projection units. * @param {number} resolution Nominal resolution in projection units.
* @param {import("./coordinate.js").Coordinate} point Point to find adjusted resolution at. * @param {import("./coordinate.js").Coordinate} point Point to find adjusted resolution at.
* @param {import("./proj/Units.js").default} [opt_units] Units to get the point resolution in. * @param {import("./proj/Units.js").Units} [opt_units] Units to get the point resolution in.
* Default is the projection's units. * Default is the projection's units.
* @return {number} Point resolution. * @return {number} Point resolution.
* @api * @api
@@ -212,7 +212,7 @@ export function getPointResolution(projection, resolution, point, opt_units) {
} }
} else { } else {
const units = projection.getUnits(); const units = projection.getUnits();
if ((units == Units.DEGREES && !opt_units) || opt_units == Units.DEGREES) { if ((units == 'degrees' && !opt_units) || opt_units == 'degrees') {
pointResolution = resolution; pointResolution = resolution;
} else { } else {
// Estimate point resolution by transforming the center pixel to EPSG:4326, // Estimate point resolution by transforming the center pixel to EPSG:4326,
@@ -222,7 +222,7 @@ export function getPointResolution(projection, resolution, point, opt_units) {
projection, projection,
get('EPSG:4326') get('EPSG:4326')
); );
if (toEPSG4326 === identityTransform && units !== Units.DEGREES) { if (toEPSG4326 === identityTransform && units !== 'degrees') {
// no transform is available // no transform is available
pointResolution = resolution * projection.getMetersPerUnit(); pointResolution = resolution * projection.getMetersPerUnit();
} else { } else {

View File

@@ -6,13 +6,13 @@ import {METERS_PER_UNIT} from './Units.js';
/** /**
* @typedef {Object} Options * @typedef {Object} Options
* @property {string} code The SRS identifier code, e.g. `EPSG:4326`. * @property {string} code The SRS identifier code, e.g. `EPSG:4326`.
* @property {import("./Units.js").default|string} [units] Units. Required unless a * @property {import("./Units.js").Units} [units] Units. Required unless a
* proj4 projection is defined for `code`. * proj4 projection is defined for `code`.
* @property {import("../extent.js").Extent} [extent] The validity extent for the SRS. * @property {import("../extent.js").Extent} [extent] The validity extent for the SRS.
* @property {string} [axisOrientation='enu'] The axis orientation as specified in Proj4. * @property {string} [axisOrientation='enu'] The axis orientation as specified in Proj4.
* @property {boolean} [global=false] Whether the projection is valid for the whole globe. * @property {boolean} [global=false] Whether the projection is valid for the whole globe.
* @property {number} [metersPerUnit] The meters per unit for the SRS. * @property {number} [metersPerUnit] The meters per unit for the SRS.
* If not provided, the `units` are used to get the meters per unit from the {@link module:ol/proj/Units~METERS_PER_UNIT} * If not provided, the `units` are used to get the meters per unit from the {@link METERS_PER_UNIT}
* lookup table. * lookup table.
* @property {import("../extent.js").Extent} [worldExtent] The world extent for the SRS. * @property {import("../extent.js").Extent} [worldExtent] The world extent for the SRS.
* @property {function(number, import("../coordinate.js").Coordinate):number} [getPointResolution] * @property {function(number, import("../coordinate.js").Coordinate):number} [getPointResolution]
@@ -65,9 +65,9 @@ class Projection {
* `this.extent_` and `this.worldExtent_` must be configured properly for each * `this.extent_` and `this.worldExtent_` must be configured properly for each
* tile. * tile.
* @private * @private
* @type {import("./Units.js").default} * @type {import("./Units.js").Units}
*/ */
this.units_ = /** @type {import("./Units.js").default} */ (options.units); this.units_ = /** @type {import("./Units.js").Units} */ (options.units);
/** /**
* Validity extent of the projection in projected coordinates. For projections * Validity extent of the projection in projected coordinates. For projections
@@ -153,7 +153,7 @@ class Projection {
/** /**
* Get the units of this projection. * Get the units of this projection.
* @return {import("./Units.js").default} Units. * @return {import("./Units.js").Units} Units.
* @api * @api
*/ */
getUnits() { getUnits() {

View File

@@ -3,58 +3,20 @@
*/ */
/** /**
* Projection units: `'degrees'`, `'ft'`, `'m'`, `'pixels'`, `'tile-pixels'` or * @typedef {'radians' | 'degrees' | 'ft' | 'm' | 'pixels' | 'tile-pixels' | 'us-ft'} Units
* `'us-ft'`. * Projection units.
* @enum {string}
*/ */
const Units = {
/**
* Radians
* @api
*/
RADIANS: 'radians',
/**
* Degrees
* @api
*/
DEGREES: 'degrees',
/**
* Feet
* @api
*/
FEET: 'ft',
/**
* Meters
* @api
*/
METERS: 'm',
/**
* Pixels
* @api
*/
PIXELS: 'pixels',
/**
* Tile Pixels
* @api
*/
TILE_PIXELS: 'tile-pixels',
/**
* US Feet
* @api
*/
USFEET: 'us-ft',
};
/** /**
* See http://duff.ess.washington.edu/data/raster/drg/docs/geotiff.txt * See http://duff.ess.washington.edu/data/raster/drg/docs/geotiff.txt
* @type {Object<number, Units>} * @type {Object<number, Units>}
*/ */
const unitByCode = { const unitByCode = {
'9001': Units.METERS, '9001': 'm',
'9002': Units.FEET, '9002': 'ft',
'9003': Units.USFEET, '9003': 'us-ft',
'9101': Units.RADIANS, '9101': 'radians',
'9102': Units.DEGREES, '9102': 'degrees',
}; };
/** /**
@@ -65,18 +27,26 @@ export function fromCode(code) {
return unitByCode[code]; return unitByCode[code];
} }
/**
* @typedef {Object} MetersPerUnitLookup
* @property {number} radians Radians
* @property {number} degrees Degrees
* @property {number} ft Feet
* @property {number} m Meters
* @property {number} us-ft US feet
*/
/** /**
* Meters per unit lookup table. * Meters per unit lookup table.
* @const * @const
* @type {Object<Units, number>} * @type {MetersPerUnitLookup}
* @api * @api
*/ */
export const METERS_PER_UNIT = {}; export const METERS_PER_UNIT = {
// use the radius of the Normal sphere // use the radius of the Normal sphere
METERS_PER_UNIT[Units.RADIANS] = 6370997 / (2 * Math.PI); 'radians': 6370997 / (2 * Math.PI),
METERS_PER_UNIT[Units.DEGREES] = (2 * Math.PI * 6370997) / 360; 'degrees': (2 * Math.PI * 6370997) / 360,
METERS_PER_UNIT[Units.FEET] = 0.3048; 'ft': 0.3048,
METERS_PER_UNIT[Units.METERS] = 1; 'm': 1,
METERS_PER_UNIT[Units.USFEET] = 1200 / 3937; 'us-ft': 1200 / 3937,
};
export default Units;

View File

@@ -2,7 +2,6 @@
* @module ol/proj/epsg3857 * @module ol/proj/epsg3857
*/ */
import Projection from './Projection.js'; import Projection from './Projection.js';
import Units from './Units.js';
/** /**
* Radius of WGS84 sphere * Radius of WGS84 sphere
@@ -48,7 +47,7 @@ class EPSG3857Projection extends Projection {
constructor(code) { constructor(code) {
super({ super({
code: code, code: code,
units: Units.METERS, units: 'm',
extent: EXTENT, extent: EXTENT,
global: true, global: true,
worldExtent: WORLD_EXTENT, worldExtent: WORLD_EXTENT,

View File

@@ -2,7 +2,6 @@
* @module ol/proj/epsg4326 * @module ol/proj/epsg4326
*/ */
import Projection from './Projection.js'; import Projection from './Projection.js';
import Units from './Units.js';
/** /**
* Semi-major radius of the WGS84 ellipsoid. * Semi-major radius of the WGS84 ellipsoid.
@@ -42,7 +41,7 @@ class EPSG4326Projection extends Projection {
constructor(code, opt_axisOrientation) { constructor(code, opt_axisOrientation) {
super({ super({
code: code, code: code,
units: Units.DEGREES, units: 'degrees',
extent: EXTENT, extent: EXTENT,
axisOrientation: opt_axisOrientation, axisOrientation: opt_axisOrientation,
global: true, global: true,

View File

@@ -2,7 +2,6 @@
* @module ol/proj/proj4 * @module ol/proj/proj4
*/ */
import Projection from './Projection.js'; import Projection from './Projection.js';
import Units from './Units.js';
import { import {
addCoordinateTransforms, addCoordinateTransforms,
addEquivalentProjections, addEquivalentProjections,
@@ -33,7 +32,7 @@ export function register(proj4) {
const def = proj4.defs(code); const def = proj4.defs(code);
let units = def.units; let units = def.units;
if (!units && def.projName === 'longlat') { if (!units && def.projName === 'longlat') {
units = Units.DEGREES; units = 'degrees';
} }
addProjection( addProjection(
new Projection({ new Projection({

View File

@@ -2,7 +2,6 @@
* @module ol/tilegrid * @module ol/tilegrid
*/ */
import TileGrid from './tilegrid/TileGrid.js'; import TileGrid from './tilegrid/TileGrid.js';
import Units from './proj/Units.js';
import {DEFAULT_MAX_ZOOM, DEFAULT_TILE_SIZE} from './tilegrid/common.js'; import {DEFAULT_MAX_ZOOM, DEFAULT_TILE_SIZE} from './tilegrid/common.js';
import {METERS_PER_UNIT, get as getProjection} from './proj.js'; import {METERS_PER_UNIT, get as getProjection} from './proj.js';
import { import {
@@ -176,7 +175,7 @@ export function extentFromProjection(projection) {
let extent = projection.getExtent(); let extent = projection.getExtent();
if (!extent) { if (!extent) {
const half = const half =
(180 * METERS_PER_UNIT[Units.DEGREES]) / projection.getMetersPerUnit(); (180 * METERS_PER_UNIT.degrees) / projection.getMetersPerUnit();
extent = createOrUpdate(-half, -half, half, half); extent = createOrUpdate(-half, -half, half, half);
} }
return extent; return extent;

View File

@@ -1,5 +1,4 @@
import Projection from '../../../src/ol/proj/Projection.js'; import Projection from '../../../src/ol/proj/Projection.js';
import Units from '../../../src/ol/proj/Units.js';
import View from '../../../src/ol/View.js'; import View from '../../../src/ol/View.js';
import expect from '../expect.js'; import expect from '../expect.js';
import proj4 from 'proj4'; import proj4 from 'proj4';
@@ -159,13 +158,13 @@ describe('ol/proj.js', function () {
describe('fromUserResolution()', function () { describe('fromUserResolution()', function () {
it("adjusts a resolution for the user projection's units", function () { it("adjusts a resolution for the user projection's units", function () {
useGeographic(); useGeographic();
const user = 1 / METERS_PER_UNIT['degrees']; const user = 1 / METERS_PER_UNIT.degrees;
const resolution = fromUserResolution(user, 'EPSG:3857'); const resolution = fromUserResolution(user, 'EPSG:3857');
expect(resolution).to.roughlyEqual(1, 1e-9); expect(resolution).to.roughlyEqual(1, 1e-9);
}); });
it('returns the original if no user projection is set', function () { it('returns the original if no user projection is set', function () {
const user = METERS_PER_UNIT['meters']; const user = METERS_PER_UNIT.meters;
const resolution = fromUserResolution(user, 'EPSG:3857'); const resolution = fromUserResolution(user, 'EPSG:3857');
expect(resolution).to.eql(user); expect(resolution).to.eql(user);
}); });
@@ -176,11 +175,11 @@ describe('ol/proj.js', function () {
useGeographic(); useGeographic();
const dest = 1; const dest = 1;
const resolution = toUserResolution(dest, 'EPSG:3857'); const resolution = toUserResolution(dest, 'EPSG:3857');
expect(resolution).to.eql(1 / METERS_PER_UNIT['degrees']); expect(resolution).to.eql(1 / METERS_PER_UNIT.degrees);
}); });
it('returns the original if no user projection is set', function () { it('returns the original if no user projection is set', function () {
const dest = METERS_PER_UNIT['degrees']; const dest = METERS_PER_UNIT.degrees;
const resolution = toUserResolution(dest, 'EPSG:3857'); const resolution = toUserResolution(dest, 'EPSG:3857');
expect(resolution).to.eql(dest); expect(resolution).to.eql(dest);
}); });
@@ -435,7 +434,7 @@ describe('ol/proj.js', function () {
it('returns the correct point resolution for EPSG:3857 with custom units', function () { it('returns the correct point resolution for EPSG:3857 with custom units', function () {
let pointResolution = getPointResolution( let pointResolution = getPointResolution(
'EPSG:3857', 'EPSG:3857',
METERS_PER_UNIT['degrees'], METERS_PER_UNIT.degrees,
[0, 0], [0, 0],
'degrees' 'degrees'
); );
@@ -506,7 +505,7 @@ describe('ol/proj.js', function () {
const proj = getProjection('EPSG:4258'); const proj = getProjection('EPSG:4258');
expect(proj.getCode()).to.eql('EPSG:4258'); expect(proj.getCode()).to.eql('EPSG:4258');
expect(proj.getUnits()).to.eql('degrees'); expect(proj.getUnits()).to.eql('degrees');
expect(proj.getMetersPerUnit()).to.eql(METERS_PER_UNIT[Units.DEGREES]); expect(proj.getMetersPerUnit()).to.eql(METERS_PER_UNIT.degrees);
delete proj4.defs['EPSG:4258']; delete proj4.defs['EPSG:4258'];
}); });

View File

@@ -444,7 +444,7 @@ describe('ol/tilegrid/TileGrid.js', function () {
const grid = createForProjection(projection); const grid = createForProjection(projection);
const resolutions = grid.getResolutions(); const resolutions = grid.getResolutions();
expect(resolutions[5]).to.be( expect(resolutions[5]).to.be(
(360 * METERS_PER_UNIT['degrees']) / DEFAULT_TILE_SIZE / Math.pow(2, 5) (360 * METERS_PER_UNIT.degrees) / DEFAULT_TILE_SIZE / Math.pow(2, 5)
); );
}); });