Remove the mixin approach and give the Point class a projection.

This commit is contained in:
Marc Jansen
2012-06-20 11:18:07 +02:00
parent 073c636cb2
commit aec8f953e5
5 changed files with 119 additions and 55 deletions

View File

@@ -1,6 +1,7 @@
goog.provide('ol.geom.point');
goog.require('ol.geom.Point');
goog.require('ol.projection');
/**
* @typedef {ol.PointLike|Array.<number>|Object} point Point.
@@ -21,23 +22,29 @@ ol.geom.point = function(opt_arg){
var x = 0;
var y = 0;
var z;
var projection;
if (arguments.length == 1 && goog.isDef(opt_arg)) {
if (goog.isArray(opt_arg)) {
x = opt_arg[0];
y = opt_arg[1];
z = opt_arg[2];
projection = opt_arg[3];
} else if (goog.isObject(opt_arg)) {
x = opt_arg.x;
y = opt_arg.y;
z = opt_arg.z;
projection = opt_arg.projection;
} else {
throw new Error('ol.geom.point');
}
}
if (goog.isDef(projection)) {
projection = ol.projection(projection);
}
var p = new ol.geom.Point(x,y,z);
var p = new ol.geom.Point(x,y,z,projection);
return p;
};
goog.inherits(ol.geom.point, ol.geom.geometry);

View File

@@ -1,7 +1,6 @@
goog.provide('ol.geom.Geometry');
goog.require('ol.Bounds');
goog.require('ol.mixins.coordinate');
/**
* Creates ol.Geometry objects.

View File

@@ -1,7 +1,9 @@
goog.provide('ol.geom.Point');
goog.provide('ol.geom.Point');
goog.require('ol.geom.Geometry');
goog.require('ol.mixins.coordinate');
goog.require('ol.interfaces.coordinateAccessor');
goog.require('ol.Projection');
goog.require('ol.coord.AccessorInterface');
/**
* Creates ol.geom.Point objects.
@@ -10,12 +12,13 @@ goog.require('ol.interfaces.coordinateAccessor');
* @param {number} x X.
* @param {number} y Y.
* @param {number=} opt_z Z.
* @param {ol.Projection=} opt_projection Projection.
*
* @implements {ol.interfaces.coordinateAccessor}
* @implements {ol.coord.AccessorInterface}
*
* @constructor
*/
ol.geom.Point = function(x, y, opt_z) {
ol.geom.Point = function(x, y, opt_z, opt_projection) {
/**
* @private
* @type {number}
@@ -33,33 +36,73 @@ ol.geom.Point = function(x, y, opt_z) {
* @type {number|undefined}
*/
this.z_ = opt_z;
/**
* @private
* @type {ol.Projection}
*/
this.projection_ = goog.isDef(opt_projection) ? opt_projection : null;
};
goog.inherits(ol.geom.Point, ol.geom.Geometry);
goog.mixin(ol.geom.Point.prototype, ol.mixins.coordinate);
/**
* @return {number} X.
*/
ol.geom.Point.prototype.getX = function() {
return this.x_;
};
/**
* @return {number} Y.
*/
ol.geom.Point.prototype.getY = function() {
return this.y_;
};
/**
* @return {number|undefined} Z.
*/
ol.geom.Point.prototype.getZ = function() {
return this.z_;
};
/**
* @return {ol.Projection|undefined} Projection.
*/
ol.geom.Point.prototype.getProjection = function() {
return this.projection_;
};
/**
* @param {ol.Projection} projection Projection.
*/
ol.geom.Point.prototype.setProjection = function(projection) {
this.projection_ = projection;
};
/**
* @param {number} x X.
*/
ol.geom.Point.prototype.setX = function(x) {
this.x_ = x;
};
/**
* @param {number} y Y.
*/
ol.geom.Point.prototype.setY = function(y) {
this.y_ = y;
};
/**
* @param {number|undefined} z Z.
*/
ol.geom.Point.prototype.setZ = function(z) {
this.z_ = z;
};
///**
// * @override
// */
//ol.geom.Point.prototype.getX = ol.geom.Point.prototype.getX;
///**
// * @override
// */
//ol.geom.Point.prototype.setX = ol.geom.Point.prototype.setX;
///**
// * @override
// */
//ol.geom.Point.prototype.getY = ol.geom.Point.prototype.getY;
///**
// * @override
// */
//ol.geom.Point.prototype.setY = ol.geom.Point.prototype.setY;
///**
// * @override
// */
//ol.geom.Point.prototype.getZ = ol.geom.Point.prototype.getZ;
///**
// * @override
// */
//ol.geom.Point.prototype.setZ = ol.geom.Point.prototype.setZ;

View File

@@ -12,13 +12,6 @@ describe("ol.geom.Geometry", function() {
it("constructs instances", function() {
expect(g).toEqual(jasmine.any(ol.geom.Geometry));
console.log(g);
console.log(g.foo);
console.log(g.foo());
});
it("can set bounds", function() {

View File

@@ -1,35 +1,41 @@
describe("ol.geom.Point", function() {
var pNoArgs,
pNoZ,
p;
pWithZ,
p,
proj = "EPSG:4326";
var instances = {
"no arguments passed": ol.geom.point(),
"only two arguments [x,y] passed": ol.geom.point([21, 4]),
"all arguments passed": ol.geom.point([21, 4, 8])
"one argument [x,y] passed": ol.geom.point([21, 4]),
"one argument [x,y,z] passed": ol.geom.point([21, 4, 8]),
"two arguments passed [x,y,z] & projection": ol.geom.point([21, 4, 8, proj])
};
beforeEach(function() {
proj = ol.projection("EPSG:4326");
instances = {
"no arguments passed": ol.geom.point(),
"only two arguments [x,y] passed": ol.geom.point([21, 4]),
"all arguments passed": ol.geom.point([21, 4, 8])
"one argument [x,y] passed": ol.geom.point([21, 4]),
"one argument [x,y,z] passed": ol.geom.point([21, 4, 8]),
"two arguments passed [x,y,z] & projection": ol.geom.point([21, 4, 8, proj])
};
pNoArgs = instances['no arguments passed'];
pNoZ = instances['only two arguments [x,y] passed'];
p = instances['all arguments passed'];
pNoZ = instances['one argument [x,y] passed'];
pWithZ = instances['one argument [x,y,z] passed'];
p = instances['two arguments passed [x,y,z] & projection'];
});
afterEach(function() {
pNoArgs = pNoZ = p = null;
pNoArgs = pNoZ = pWithZ = p = null;
instances = {
"no arguments passed": pNoArgs,
"only two arguments [x,y] passed": pNoZ,
"all arguments passed": p
"no arguments passed": ol.geom.point(),
"one argument [x,y] passed": ol.geom.point([21, 4]),
"one argument [x,y,z] passed": ol.geom.point([21, 4, 8]),
"two arguments passed [x,y,z] & projection": ol.geom.point([21, 4, 8, proj])
};
});
for (instancesDesc in instances) {
if (instances.hasOwnProperty(instancesDesc)) {
var instance = instances[instancesDesc];
@@ -42,7 +48,7 @@ describe("ol.geom.Point", function() {
expect(instance).toEqual(jasmine.any(ol.geom.Geometry));
});
it("has the coordinate mixin methods (" + instancesDesc + ")", function() {
it("has the coordinate accessor methods (" + instancesDesc + ")", function() {
expect(instance.getX).not.toBeUndefined();
expect(instance.getY).not.toBeUndefined();
expect(instance.getZ).not.toBeUndefined();
@@ -50,6 +56,11 @@ describe("ol.geom.Point", function() {
expect(instance.setY).not.toBeUndefined();
expect(instance.setZ).not.toBeUndefined();
});
it("has the projection accessor methods (" + instancesDesc + ")", function() {
expect(instance.getProjection).not.toBeUndefined();
expect(instance.setProjection).not.toBeUndefined();
});
}
}
@@ -57,17 +68,28 @@ describe("ol.geom.Point", function() {
expect(pNoArgs.getX()).toBe(0);
expect(pNoArgs.getY()).toBe(0);
expect(pNoArgs.getZ()).toBeUndefined();
expect(pNoArgs.getProjection()).toBeNull();
});
it("has functional getters (only two arguments [x,y] passed)", function(){
it("has functional getters (one argument [x,y] passed)", function(){
expect(pNoZ.getX()).toBe(21);
expect(pNoZ.getY()).toBe(4);
expect(pNoZ.getZ()).toBeUndefined();
expect(pNoZ.getProjection()).toBeNull();
});
it("has functional getters (all arguments passed)", function(){
it("has functional getters (one argument [x,y,z] passed)", function(){
expect(pWithZ.getX()).toBe(21);
expect(pWithZ.getY()).toBe(4);
expect(pWithZ.getZ()).toBe(8);
expect(pWithZ.getProjection()).toBeNull();
});
it("has functional getters (two arguments passed [x,y,z] & projection)", function(){
expect(p.getX()).toBe(21);
expect(p.getY()).toBe(4);
expect(p.getZ()).toBe(8);
expect(p.getProjection()).not.toBeNull();
expect(p.getProjection()).toEqual(jasmine.any(ol.Projection));
});
});