Named exports from ol/proj/epsg3857

This commit is contained in:
Tim Schaub
2018-02-07 12:44:02 -07:00
parent 3266ffc58f
commit 6701d19a45
7 changed files with 79 additions and 85 deletions

View File

@@ -4,7 +4,7 @@
import {getDistance} from './sphere.js'; import {getDistance} from './sphere.js';
import {applyTransform} from './extent.js'; import {applyTransform} from './extent.js';
import {modulo} from './math.js'; import {modulo} from './math.js';
import EPSG3857 from './proj/EPSG3857.js'; import {toEPSG4326, fromEPSG4326, PROJECTIONS as EPSG3857_PROJECTIONS} from './proj/epsg3857.js';
import EPSG4326 from './proj/EPSG4326.js'; import EPSG4326 from './proj/EPSG4326.js';
import Projection from './proj/Projection.js'; import Projection from './proj/Projection.js';
import Units from './proj/Units.js'; import Units from './proj/Units.js';
@@ -438,15 +438,15 @@ export function transformWithProjections(point, sourceProjection, destinationPro
export function addCommon() { export function addCommon() {
// Add transformations that don't alter coordinates to convert within set of // Add transformations that don't alter coordinates to convert within set of
// projections with equal meaning. // projections with equal meaning.
addEquivalentProjections(EPSG3857.PROJECTIONS); addEquivalentProjections(EPSG3857_PROJECTIONS);
addEquivalentProjections(EPSG4326.PROJECTIONS); addEquivalentProjections(EPSG4326.PROJECTIONS);
// Add transformations to convert EPSG:4326 like coordinates to EPSG:3857 like // Add transformations to convert EPSG:4326 like coordinates to EPSG:3857 like
// coordinates and back. // coordinates and back.
addEquivalentTransforms( addEquivalentTransforms(
EPSG4326.PROJECTIONS, EPSG4326.PROJECTIONS,
EPSG3857.PROJECTIONS, EPSG3857_PROJECTIONS,
EPSG3857.fromEPSG4326, fromEPSG4326,
EPSG3857.toEPSG4326); toEPSG4326);
} }
addCommon(); addCommon();

View File

@@ -1,11 +1,43 @@
/** /**
* @module ol/proj/EPSG3857 * @module ol/proj/epsg3857
*/ */
import {inherits} from '../index.js'; import {inherits} from '../index.js';
import {cosh} from '../math.js'; import {cosh} from '../math.js';
import Projection from '../proj/Projection.js'; import Projection from '../proj/Projection.js';
import Units from '../proj/Units.js'; import Units from '../proj/Units.js';
const _ol_proj_EPSG3857_ = {};
/**
* Radius of WGS84 sphere
*
* @const
* @type {number}
*/
export const RADIUS = 6378137;
/**
* @const
* @type {number}
*/
export const HALF_SIZE = Math.PI * RADIUS;
/**
* @const
* @type {ol.Extent}
*/
export const EXTENT = [
-HALF_SIZE, -HALF_SIZE,
HALF_SIZE, HALF_SIZE
];
/**
* @const
* @type {ol.Extent}
*/
export const WORLD_EXTENT = [-180, -85, 180, 85];
/** /**
@@ -15,54 +47,20 @@ const _ol_proj_EPSG3857_ = {};
* @constructor * @constructor
* @extends {ol.proj.Projection} * @extends {ol.proj.Projection}
* @param {string} code Code. * @param {string} code Code.
* @private
*/ */
_ol_proj_EPSG3857_.Projection_ = function(code) { function EPSG3857Projection(code) {
Projection.call(this, { Projection.call(this, {
code: code, code: code,
units: Units.METERS, units: Units.METERS,
extent: _ol_proj_EPSG3857_.EXTENT, extent: EXTENT,
global: true, global: true,
worldExtent: _ol_proj_EPSG3857_.WORLD_EXTENT, worldExtent: WORLD_EXTENT,
getPointResolution: function(resolution, point) { getPointResolution: function(resolution, point) {
return resolution / cosh(point[1] / _ol_proj_EPSG3857_.RADIUS); return resolution / cosh(point[1] / RADIUS);
} }
}); });
}; }
inherits(_ol_proj_EPSG3857_.Projection_, Projection); inherits(EPSG3857Projection, Projection);
/**
* Radius of WGS84 sphere
*
* @const
* @type {number}
*/
_ol_proj_EPSG3857_.RADIUS = 6378137;
/**
* @const
* @type {number}
*/
_ol_proj_EPSG3857_.HALF_SIZE = Math.PI * _ol_proj_EPSG3857_.RADIUS;
/**
* @const
* @type {ol.Extent}
*/
_ol_proj_EPSG3857_.EXTENT = [
-_ol_proj_EPSG3857_.HALF_SIZE, -_ol_proj_EPSG3857_.HALF_SIZE,
_ol_proj_EPSG3857_.HALF_SIZE, _ol_proj_EPSG3857_.HALF_SIZE
];
/**
* @const
* @type {ol.Extent}
*/
_ol_proj_EPSG3857_.WORLD_EXTENT = [-180, -85, 180, 85];
/** /**
@@ -71,14 +69,14 @@ _ol_proj_EPSG3857_.WORLD_EXTENT = [-180, -85, 180, 85];
* @const * @const
* @type {Array.<ol.proj.Projection>} * @type {Array.<ol.proj.Projection>}
*/ */
_ol_proj_EPSG3857_.PROJECTIONS = [ export const PROJECTIONS = [
new _ol_proj_EPSG3857_.Projection_('EPSG:3857'), new EPSG3857Projection('EPSG:3857'),
new _ol_proj_EPSG3857_.Projection_('EPSG:102100'), new EPSG3857Projection('EPSG:102100'),
new _ol_proj_EPSG3857_.Projection_('EPSG:102113'), new EPSG3857Projection('EPSG:102113'),
new _ol_proj_EPSG3857_.Projection_('EPSG:900913'), new EPSG3857Projection('EPSG:900913'),
new _ol_proj_EPSG3857_.Projection_('urn:ogc:def:crs:EPSG:6.18:3:3857'), new EPSG3857Projection('urn:ogc:def:crs:EPSG:6.18:3:3857'),
new _ol_proj_EPSG3857_.Projection_('urn:ogc:def:crs:EPSG::3857'), new EPSG3857Projection('urn:ogc:def:crs:EPSG::3857'),
new _ol_proj_EPSG3857_.Projection_('http://www.opengis.net/gml/srs/epsg.xml#3857') new EPSG3857Projection('http://www.opengis.net/gml/srs/epsg.xml#3857')
]; ];
@@ -90,7 +88,7 @@ _ol_proj_EPSG3857_.PROJECTIONS = [
* @param {number=} opt_dimension Dimension (default is `2`). * @param {number=} opt_dimension Dimension (default is `2`).
* @return {Array.<number>} Output array of coordinate values. * @return {Array.<number>} Output array of coordinate values.
*/ */
_ol_proj_EPSG3857_.fromEPSG4326 = function(input, opt_output, opt_dimension) { export function fromEPSG4326(input, opt_output, opt_dimension) {
const length = input.length; const length = input.length;
const dimension = opt_dimension > 1 ? opt_dimension : 2; const dimension = opt_dimension > 1 ? opt_dimension : 2;
let output = opt_output; let output = opt_output;
@@ -102,10 +100,10 @@ _ol_proj_EPSG3857_.fromEPSG4326 = function(input, opt_output, opt_dimension) {
output = new Array(length); output = new Array(length);
} }
} }
const halfSize = _ol_proj_EPSG3857_.HALF_SIZE; const halfSize = HALF_SIZE;
for (let i = 0; i < length; i += dimension) { for (let i = 0; i < length; i += dimension) {
output[i] = halfSize * input[i] / 180; output[i] = halfSize * input[i] / 180;
let y = _ol_proj_EPSG3857_.RADIUS * let y = RADIUS *
Math.log(Math.tan(Math.PI * (input[i + 1] + 90) / 360)); Math.log(Math.tan(Math.PI * (input[i + 1] + 90) / 360));
if (y > halfSize) { if (y > halfSize) {
y = halfSize; y = halfSize;
@@ -115,7 +113,7 @@ _ol_proj_EPSG3857_.fromEPSG4326 = function(input, opt_output, opt_dimension) {
output[i + 1] = y; output[i + 1] = y;
} }
return output; return output;
}; }
/** /**
@@ -126,7 +124,7 @@ _ol_proj_EPSG3857_.fromEPSG4326 = function(input, opt_output, opt_dimension) {
* @param {number=} opt_dimension Dimension (default is `2`). * @param {number=} opt_dimension Dimension (default is `2`).
* @return {Array.<number>} Output array of coordinate values. * @return {Array.<number>} Output array of coordinate values.
*/ */
_ol_proj_EPSG3857_.toEPSG4326 = function(input, opt_output, opt_dimension) { export function toEPSG4326(input, opt_output, opt_dimension) {
const length = input.length; const length = input.length;
const dimension = opt_dimension > 1 ? opt_dimension : 2; const dimension = opt_dimension > 1 ? opt_dimension : 2;
let output = opt_output; let output = opt_output;
@@ -139,10 +137,9 @@ _ol_proj_EPSG3857_.toEPSG4326 = function(input, opt_output, opt_dimension) {
} }
} }
for (let i = 0; i < length; i += dimension) { for (let i = 0; i < length; i += dimension) {
output[i] = 180 * input[i] / _ol_proj_EPSG3857_.HALF_SIZE; output[i] = 180 * input[i] / HALF_SIZE;
output[i + 1] = 360 * Math.atan( output[i + 1] = 360 * Math.atan(
Math.exp(input[i + 1] / _ol_proj_EPSG3857_.RADIUS)) / Math.PI - 90; Math.exp(input[i + 1] / RADIUS)) / Math.PI - 90;
} }
return output; return output;
}; }
export default _ol_proj_EPSG3857_;

View File

@@ -1,6 +1,6 @@
import {listen} from '../../../../src/ol/events.js'; import {listen} from '../../../../src/ol/events.js';
import {get as getProjection} from '../../../../src/ol/proj.js'; import {get as getProjection} from '../../../../src/ol/proj.js';
import _ol_proj_EPSG3857_ from '../../../../src/ol/proj/EPSG3857.js'; import {HALF_SIZE} from '../../../../src/ol/proj/epsg3857.js';
import ReprojImage from '../../../../src/ol/reproj/Image.js'; import ReprojImage from '../../../../src/ol/reproj/Image.js';
import Static from '../../../../src/ol/source/ImageStatic.js'; import Static from '../../../../src/ol/source/ImageStatic.js';
import _ol_tilegrid_ from '../../../../src/ol/tilegrid.js'; import _ol_tilegrid_ from '../../../../src/ol/tilegrid.js';
@@ -45,7 +45,7 @@ describe('ol.rendering.reproj.Image', function() {
it('works for identity reprojection', function(done) { it('works for identity reprojection', function(done) {
testSingleImage(source, 'EPSG:3857', testSingleImage(source, 'EPSG:3857',
_ol_tilegrid_.createXYZ().getTileCoordExtent([5, 5, -13]), _ol_tilegrid_.createXYZ().getTileCoordExtent([5, 5, -13]),
2 * _ol_proj_EPSG3857_.HALF_SIZE / (256 * (1 << 5)), 1, 2 * HALF_SIZE / (256 * (1 << 5)), 1,
'rendering/ol/data/tiles/osm/5/5/12.png', done); 'rendering/ol/data/tiles/osm/5/5/12.png', done);
}); });

View File

@@ -12,7 +12,7 @@ import {
getTransformFromProjections getTransformFromProjections
} from '../../../src/ol/proj.js'; } from '../../../src/ol/proj.js';
import {register} from '../../../src/ol/proj/proj4.js'; import {register} from '../../../src/ol/proj/proj4.js';
import _ol_proj_EPSG3857_ from '../../../src/ol/proj/EPSG3857.js'; import {HALF_SIZE} from '../../../src/ol/proj/epsg3857.js';
import _ol_proj_EPSG4326_ from '../../../src/ol/proj/EPSG4326.js'; import _ol_proj_EPSG4326_ from '../../../src/ol/proj/EPSG4326.js';
import Projection from '../../../src/ol/proj/Projection.js'; import Projection from '../../../src/ol/proj/Projection.js';
@@ -32,10 +32,10 @@ describe('ol.proj', function() {
from: [-12356463.478053365, 5700582.732404122], from: [-12356463.478053365, 5700582.732404122],
to: [-111, 45.5] to: [-111, 45.5]
}, { }, {
from: [2 * _ol_proj_EPSG3857_.HALF_SIZE - 12356463.478053365, 5700582.732404122], from: [2 * HALF_SIZE - 12356463.478053365, 5700582.732404122],
to: [-111, 45.5] to: [-111, 45.5]
}, { }, {
from: [-4 * _ol_proj_EPSG3857_.HALF_SIZE - 12356463.478053365, 5700582.732404122], from: [-4 * HALF_SIZE - 12356463.478053365, 5700582.732404122],
to: [-111, 45.5] to: [-111, 45.5]
}]; }];

View File

@@ -1,7 +1,7 @@
import {getPointResolution, transform, get as getProjection, clearAllProjections, addCommon} from '../../../../src/ol/proj.js'; import {getPointResolution, transform, get as getProjection, clearAllProjections, addCommon} from '../../../../src/ol/proj.js';
import _ol_proj_EPSG3857_ from '../../../../src/ol/proj/EPSG3857.js'; import {fromEPSG4326, HALF_SIZE} from '../../../../src/ol/proj/epsg3857.js';
describe('ol.proj.EPSG3857', function() { describe('ol/proj/epsg3857', function() {
afterEach(function() { afterEach(function() {
clearAllProjections(); clearAllProjections();
@@ -11,9 +11,6 @@ describe('ol.proj.EPSG3857', function() {
describe('fromEPSG4326()', function() { describe('fromEPSG4326()', function() {
it('transforms from geographic to Web Mercator', function() { it('transforms from geographic to Web Mercator', function() {
const forward = _ol_proj_EPSG3857_.fromEPSG4326;
const edge = _ol_proj_EPSG3857_.HALF_SIZE;
const tolerance = 1e-5; const tolerance = 1e-5;
const cases = [{ const cases = [{
@@ -21,10 +18,10 @@ describe('ol.proj.EPSG3857', function() {
m: [0, 0] m: [0, 0]
}, { }, {
g: [-180, -90], g: [-180, -90],
m: [-edge, -edge] m: [-HALF_SIZE, -HALF_SIZE]
}, { }, {
g: [180, 90], g: [180, 90],
m: [edge, edge] m: [HALF_SIZE, HALF_SIZE]
}, { }, {
g: [-111.0429, 45.6770], g: [-111.0429, 45.6770],
m: [-12361239.084208, 5728738.469095] m: [-12361239.084208, 5728738.469095]
@@ -32,7 +29,7 @@ describe('ol.proj.EPSG3857', function() {
for (let i = 0, ii = cases.length; i < ii; ++i) { for (let i = 0, ii = cases.length; i < ii; ++i) {
const point = cases[i].g; const point = cases[i].g;
const transformed = forward(point); const transformed = fromEPSG4326(point);
expect(transformed[0]).to.roughlyEqual(cases[i].m[0], tolerance); expect(transformed[0]).to.roughlyEqual(cases[i].m[0], tolerance);
expect(transformed[1]).to.roughlyEqual(cases[i].m[1], tolerance); expect(transformed[1]).to.roughlyEqual(cases[i].m[1], tolerance);
} }

View File

@@ -4,7 +4,7 @@ import {createFromTemplate} from '../../../../src/ol/tileurlfunction.js';
import {listen} from '../../../../src/ol/events.js'; import {listen} from '../../../../src/ol/events.js';
import {addCommon, clearAllProjections, get as getProjection} from '../../../../src/ol/proj.js'; import {addCommon, clearAllProjections, get as getProjection} from '../../../../src/ol/proj.js';
import {register} from '../../../../src/ol/proj/proj4.js'; import {register} from '../../../../src/ol/proj/proj4.js';
import _ol_proj_EPSG3857_ from '../../../../src/ol/proj/EPSG3857.js'; import {WORLD_EXTENT} from '../../../../src/ol/proj/epsg3857.js';
import Projection from '../../../../src/ol/proj/Projection.js'; import Projection from '../../../../src/ol/proj/Projection.js';
import ReprojTile from '../../../../src/ol/reproj/Tile.js'; import ReprojTile from '../../../../src/ol/reproj/Tile.js';
import TileImage from '../../../../src/ol/source/TileImage.js'; import TileImage from '../../../../src/ol/source/TileImage.js';
@@ -161,7 +161,7 @@ describe('ol.source.TileImage', function() {
const source = createSource(); const source = createSource();
source.setTileGridForProjection(proj, source.setTileGridForProjection(proj,
_ol_tilegrid_.createXYZ({ _ol_tilegrid_.createXYZ({
extent: _ol_proj_EPSG3857_.WORLD_EXTENT, extent: WORLD_EXTENT,
tileSize: [2, 2] tileSize: [2, 2]
})); }));
const tile = source.getTile(0, 0, -1, 1, proj); const tile = source.getTile(0, 0, -1, 1, proj);

View File

@@ -2,7 +2,7 @@ import {DEFAULT_MAX_ZOOM, DEFAULT_TILE_SIZE} from '../../../../src/ol/tilegrid/c
import TileRange from '../../../../src/ol/TileRange.js'; import TileRange from '../../../../src/ol/TileRange.js';
import * as _ol_extent_ from '../../../../src/ol/extent.js'; import * as _ol_extent_ from '../../../../src/ol/extent.js';
import {get as getProjection, METERS_PER_UNIT} from '../../../../src/ol/proj.js'; import {get as getProjection, METERS_PER_UNIT} from '../../../../src/ol/proj.js';
import _ol_proj_EPSG3857_ from '../../../../src/ol/proj/EPSG3857.js'; import {HALF_SIZE} from '../../../../src/ol/proj/epsg3857.js';
import Projection from '../../../../src/ol/proj/Projection.js'; import Projection from '../../../../src/ol/proj/Projection.js';
import _ol_tilegrid_ from '../../../../src/ol/tilegrid.js'; import _ol_tilegrid_ from '../../../../src/ol/tilegrid.js';
import TileGrid from '../../../../src/ol/tilegrid/TileGrid.js'; import TileGrid from '../../../../src/ol/tilegrid/TileGrid.js';
@@ -348,7 +348,7 @@ describe('ol.tilegrid.TileGrid', function() {
const projection = getProjection('EPSG:3857'); const projection = getProjection('EPSG:3857');
const grid = _ol_tilegrid_.createForProjection(projection); const grid = _ol_tilegrid_.createForProjection(projection);
const origin = grid.getOrigin(); const origin = grid.getOrigin();
const half = _ol_proj_EPSG3857_.HALF_SIZE; const half = HALF_SIZE;
expect(origin).to.eql([-half, half]); expect(origin).to.eql([-half, half]);
}); });
@@ -357,7 +357,7 @@ describe('ol.tilegrid.TileGrid', function() {
const grid = _ol_tilegrid_.createForProjection( const grid = _ol_tilegrid_.createForProjection(
projection, undefined, undefined, 'bottom-left'); projection, undefined, undefined, 'bottom-left');
const origin = grid.getOrigin(); const origin = grid.getOrigin();
const half = _ol_proj_EPSG3857_.HALF_SIZE; const half = HALF_SIZE;
expect(origin).to.eql([-half, -half]); expect(origin).to.eql([-half, -half]);
}); });
@@ -366,7 +366,7 @@ describe('ol.tilegrid.TileGrid', function() {
const grid = _ol_tilegrid_.createForProjection( const grid = _ol_tilegrid_.createForProjection(
projection, undefined, undefined, 'bottom-right'); projection, undefined, undefined, 'bottom-right');
const origin = grid.getOrigin(); const origin = grid.getOrigin();
const half = _ol_proj_EPSG3857_.HALF_SIZE; const half = HALF_SIZE;
expect(origin).to.eql([half, -half]); expect(origin).to.eql([half, -half]);
}); });
@@ -375,7 +375,7 @@ describe('ol.tilegrid.TileGrid', function() {
const grid = _ol_tilegrid_.createForProjection( const grid = _ol_tilegrid_.createForProjection(
projection, undefined, undefined, 'top-left'); projection, undefined, undefined, 'top-left');
const origin = grid.getOrigin(); const origin = grid.getOrigin();
const half = _ol_proj_EPSG3857_.HALF_SIZE; const half = HALF_SIZE;
expect(origin).to.eql([-half, half]); expect(origin).to.eql([-half, half]);
}); });
@@ -384,7 +384,7 @@ describe('ol.tilegrid.TileGrid', function() {
const grid = _ol_tilegrid_.createForProjection( const grid = _ol_tilegrid_.createForProjection(
projection, undefined, undefined, 'top-right'); projection, undefined, undefined, 'top-right');
const origin = grid.getOrigin(); const origin = grid.getOrigin();
const half = _ol_proj_EPSG3857_.HALF_SIZE; const half = HALF_SIZE;
expect(origin).to.eql([half, half]); expect(origin).to.eql([half, half]);
}); });