Simplify meters per unit handling
This commit is contained in:
@@ -429,6 +429,7 @@ olx.OverlayOptions.prototype.autoPanMargin;
|
||||
* extent: (ol.Extent|undefined),
|
||||
* axisOrientation: (string|undefined),
|
||||
* global: (boolean|undefined),
|
||||
* metersPerUnit: (number|undefined),
|
||||
* worldExtent: (ol.Extent|undefined),
|
||||
* getPointResolution: (function(number, ol.Coordinate):number|undefined) }}
|
||||
* @api
|
||||
@@ -476,6 +477,15 @@ olx.ProjectionOptions.prototype.axisOrientation;
|
||||
olx.ProjectionOptions.prototype.global;
|
||||
|
||||
|
||||
/**
|
||||
* The meters per unit for the SRS. If not provided, the `units` are used to get
|
||||
* the meters per unit from the {@link ol.proj.METERS_PER_UNIT} lookup table.
|
||||
* @type {number|undefined}
|
||||
* @api
|
||||
*/
|
||||
olx.ProjectionOptions.prototype.metersPerUnit;
|
||||
|
||||
|
||||
/**
|
||||
* The world extent for the SRS.
|
||||
* @type {ol.Extent|undefined}
|
||||
|
||||
@@ -3,6 +3,7 @@ goog.provide('ol.proj.EPSG4326');
|
||||
goog.require('ol.proj');
|
||||
goog.require('ol.proj.Projection');
|
||||
goog.require('ol.proj.Units');
|
||||
goog.require('ol.sphere.WGS84');
|
||||
|
||||
|
||||
|
||||
@@ -27,6 +28,7 @@ ol.proj.EPSG4326_ = function(code, opt_axisOrientation) {
|
||||
extent: ol.proj.EPSG4326.EXTENT,
|
||||
axisOrientation: opt_axisOrientation,
|
||||
global: true,
|
||||
metersPerUnit: ol.proj.EPSG4326.METERS_PER_UNIT,
|
||||
worldExtent: ol.proj.EPSG4326.EXTENT
|
||||
});
|
||||
};
|
||||
@@ -50,6 +52,13 @@ ol.proj.EPSG4326_.prototype.getPointResolution = function(resolution, point) {
|
||||
ol.proj.EPSG4326.EXTENT = [-180, -90, 180, 90];
|
||||
|
||||
|
||||
/**
|
||||
* @const
|
||||
* @type {number}
|
||||
*/
|
||||
ol.proj.EPSG4326.METERS_PER_UNIT = Math.PI * ol.sphere.WGS84.radius / 180;
|
||||
|
||||
|
||||
/**
|
||||
* Projections equal to EPSG:4326.
|
||||
*
|
||||
|
||||
@@ -143,6 +143,12 @@ ol.proj.Projection = function(options) {
|
||||
*/
|
||||
this.defaultTileGrid_ = null;
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {number|undefined}
|
||||
*/
|
||||
this.metersPerUnit_ = options.metersPerUnit;
|
||||
|
||||
var projections = ol.proj.projections_;
|
||||
var code = options.code;
|
||||
goog.asserts.assert(code !== undefined,
|
||||
@@ -155,16 +161,11 @@ ol.proj.Projection = function(options) {
|
||||
if (def.axis !== undefined && options.axisOrientation === undefined) {
|
||||
this.axisOrientation_ = def.axis;
|
||||
}
|
||||
if (options.metersPerUnit === undefined) {
|
||||
this.metersPerUnit_ = def.to_meter;
|
||||
}
|
||||
if (options.units === undefined) {
|
||||
var units = def.units;
|
||||
if (def.to_meter !== undefined) {
|
||||
if (units === undefined ||
|
||||
ol.proj.METERS_PER_UNIT[units] === undefined) {
|
||||
units = def.to_meter.toString();
|
||||
ol.proj.METERS_PER_UNIT[units] = def.to_meter;
|
||||
}
|
||||
}
|
||||
this.units_ = units;
|
||||
this.units_ = def.units;
|
||||
}
|
||||
var currentCode, currentDef, currentProj, proj4Transform;
|
||||
for (currentCode in projections) {
|
||||
@@ -227,12 +228,13 @@ ol.proj.Projection.prototype.getUnits = function() {
|
||||
|
||||
/**
|
||||
* Get the amount of meters per unit of this projection. If the projection is
|
||||
* not configured with a units identifier, the return is `undefined`.
|
||||
* not configured with `metersPerUnit` or a units identifier, the return is
|
||||
* `undefined`.
|
||||
* @return {number|undefined} Meters.
|
||||
* @api stable
|
||||
*/
|
||||
ol.proj.Projection.prototype.getMetersPerUnit = function() {
|
||||
return ol.proj.METERS_PER_UNIT[this.units_];
|
||||
return this.metersPerUnit_ || ol.proj.METERS_PER_UNIT[this.units_];
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -554,7 +554,8 @@ describe('ol.proj', function() {
|
||||
|
||||
it('returns value in meters', function() {
|
||||
var epsg4326 = ol.proj.get('EPSG:4326');
|
||||
expect(epsg4326.getMetersPerUnit()).to.eql(111194.87428468118);
|
||||
expect(epsg4326.getMetersPerUnit()).to.eql(
|
||||
ol.proj.EPSG4326.METERS_PER_UNIT);
|
||||
});
|
||||
|
||||
it('works for proj4js projections without units', function() {
|
||||
@@ -582,6 +583,7 @@ describe('ol.proj', function() {
|
||||
|
||||
|
||||
goog.require('ol.proj');
|
||||
goog.require('ol.proj.EPSG4326');
|
||||
goog.require('ol.proj.Projection');
|
||||
goog.require('ol.proj.Units');
|
||||
goog.require('ol.proj.common');
|
||||
|
||||
@@ -31,7 +31,8 @@ describe('ol.reproj', function() {
|
||||
var resolution3857 = ol.reproj.calculateSourceResolution(
|
||||
proj3857, proj4326, point4326, resolution4326);
|
||||
expect(resolution3857).not.to.be(resolution4326);
|
||||
expect(resolution3857).to.roughlyEqual(555974.3714343394, 1e-6);
|
||||
expect(resolution3857).to.roughlyEqual(
|
||||
5 * proj4326.getMetersPerUnit(), 1e-4);
|
||||
|
||||
var result = ol.reproj.calculateSourceResolution(
|
||||
proj4326, proj3857, point3857, resolution3857);
|
||||
|
||||
Reference in New Issue
Block a user