Give points a getCentroid/centroid method.
This commit is contained in:
+16
-7
@@ -1,4 +1,4 @@
|
|||||||
goog.provide('ol.geom.point');
|
goog.provide('ol.geom.point');
|
||||||
|
|
||||||
goog.require('ol.geom.Point');
|
goog.require('ol.geom.Point');
|
||||||
goog.require('ol.projection');
|
goog.require('ol.projection');
|
||||||
@@ -14,23 +14,23 @@ ol.PointLike;
|
|||||||
* @return {ol.geom.Point} Point.
|
* @return {ol.geom.Point} Point.
|
||||||
*/
|
*/
|
||||||
ol.geom.point = function(opt_arg){
|
ol.geom.point = function(opt_arg){
|
||||||
|
|
||||||
if (opt_arg instanceof ol.geom.Point) {
|
if (opt_arg instanceof ol.geom.Point) {
|
||||||
return opt_arg;
|
return opt_arg;
|
||||||
}
|
}
|
||||||
|
|
||||||
var x = 0;
|
var x = 0;
|
||||||
var y = 0;
|
var y = 0;
|
||||||
var z;
|
var z;
|
||||||
var projection;
|
var projection;
|
||||||
|
|
||||||
if (arguments.length == 1 && goog.isDef(opt_arg)) {
|
if (arguments.length == 1 && goog.isDef(opt_arg)) {
|
||||||
if (goog.isArray(opt_arg)) {
|
if (goog.isArray(opt_arg)) {
|
||||||
x = opt_arg[0];
|
x = opt_arg[0];
|
||||||
y = opt_arg[1];
|
y = opt_arg[1];
|
||||||
z = opt_arg[2];
|
z = opt_arg[2];
|
||||||
projection = opt_arg[3];
|
projection = opt_arg[3];
|
||||||
|
|
||||||
} else if (goog.isObject(opt_arg)) {
|
} else if (goog.isObject(opt_arg)) {
|
||||||
x = opt_arg['x'];
|
x = opt_arg['x'];
|
||||||
y = opt_arg['y'];
|
y = opt_arg['y'];
|
||||||
@@ -43,7 +43,7 @@ ol.geom.point = function(opt_arg){
|
|||||||
if (goog.isDef(projection)) {
|
if (goog.isDef(projection)) {
|
||||||
projection = ol.projection(projection);
|
projection = ol.projection(projection);
|
||||||
}
|
}
|
||||||
|
|
||||||
var p = new ol.geom.Point(x,y,z,projection);
|
var p = new ol.geom.Point(x,y,z,projection);
|
||||||
return p;
|
return p;
|
||||||
};
|
};
|
||||||
@@ -110,4 +110,13 @@ ol.geom.Point.prototype.projection = function(opt_arg){
|
|||||||
else {
|
else {
|
||||||
return this.getProjection();
|
return this.getProjection();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the centroid of this point; which is a clone of the point itself.
|
||||||
|
*
|
||||||
|
* @return {ol.geom.Point} The centroid.
|
||||||
|
*/
|
||||||
|
ol.geom.Point.prototype.centroid = function() {
|
||||||
|
return this.getCentroid();
|
||||||
|
};
|
||||||
|
|||||||
+27
-10
@@ -7,17 +7,17 @@ goog.require('ol.coord.AccessorInterface');
|
|||||||
goog.require('ol.base');
|
goog.require('ol.base');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates ol.geom.Point objects.
|
* Creates ol.geom.Point objects.
|
||||||
*
|
*
|
||||||
* @export
|
* @export
|
||||||
* @extends {ol.geom.Geometry}
|
* @extends {ol.geom.Geometry}
|
||||||
* @param {number} x X.
|
* @param {number} x X.
|
||||||
* @param {number} y Y.
|
* @param {number} y Y.
|
||||||
* @param {number=} opt_z Z.
|
* @param {number=} opt_z Z.
|
||||||
* @param {ol.Projection=} opt_projection Projection.
|
* @param {ol.Projection=} opt_projection Projection.
|
||||||
*
|
*
|
||||||
* @implements {ol.coord.AccessorInterface}
|
* @implements {ol.coord.AccessorInterface}
|
||||||
*
|
*
|
||||||
* @constructor
|
* @constructor
|
||||||
*/
|
*/
|
||||||
ol.geom.Point = function(x, y, opt_z, opt_projection) {
|
ol.geom.Point = function(x, y, opt_z, opt_projection) {
|
||||||
@@ -26,19 +26,19 @@ ol.geom.Point = function(x, y, opt_z, opt_projection) {
|
|||||||
* @type {number}
|
* @type {number}
|
||||||
*/
|
*/
|
||||||
this.x_ = x;
|
this.x_ = x;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @private
|
* @private
|
||||||
* @type {number}
|
* @type {number}
|
||||||
*/
|
*/
|
||||||
this.y_ = y;
|
this.y_ = y;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @private
|
* @private
|
||||||
* @type {number|undefined}
|
* @type {number|undefined}
|
||||||
*/
|
*/
|
||||||
this.z_ = opt_z;
|
this.z_ = opt_z;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @private
|
* @private
|
||||||
* @type {ol.Projection}
|
* @type {ol.Projection}
|
||||||
@@ -109,12 +109,12 @@ ol.geom.Point.prototype.setZ = function(z) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Transform this point to another coordinate reference system. This
|
* Transform this point to another coordinate reference system. This
|
||||||
* requires that this point has a projection set already (if not, an error
|
* requires that this point has a projection set already (if not, an error
|
||||||
* will be thrown). Returns a new point object and does not modify this
|
* will be thrown). Returns a new point object and does not modify this
|
||||||
* point.
|
* point.
|
||||||
*
|
*
|
||||||
* @param {string|!ol.Projection} proj The destination projection. Can be
|
* @param {string|!ol.Projection} proj The destination projection. Can be
|
||||||
* supplied as a projection instance of a string identifier.
|
* supplied as a projection instance of a string identifier.
|
||||||
* @returns {!ol.geom.Point} A new location.
|
* @returns {!ol.geom.Point} A new location.
|
||||||
*/
|
*/
|
||||||
@@ -140,7 +140,24 @@ ol.geom.Point.prototype._transform = function(proj) {
|
|||||||
ol.error(msg);
|
ol.error(msg);
|
||||||
}
|
}
|
||||||
ol.Projection.transform(point, sourceProj, proj);
|
ol.Projection.transform(point, sourceProj, proj);
|
||||||
|
|
||||||
return new ol.geom.Point(point['x'], point['y'], this.z_, proj);
|
return new ol.geom.Point(point['x'], point['y'], this.z_, proj);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the centroid of the point.
|
||||||
|
*
|
||||||
|
* @returns {ol.geom.Point} The centroid of the point.
|
||||||
|
*/
|
||||||
|
ol.geom.Point.prototype.getCentroid = function() {
|
||||||
|
return new ol.geom.Point(this.x_, this.y_, this.z_, this.projection_);
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the area of the geometry whcih is always 0.
|
||||||
|
*
|
||||||
|
* @returns {number} The area of the point (always 0).
|
||||||
|
*/
|
||||||
|
ol.geom.Point.prototype.getArea = function() {
|
||||||
|
return 0;
|
||||||
|
};
|
||||||
|
|||||||
@@ -166,4 +166,47 @@ describe("ol.geom.point", function() {
|
|||||||
expect(p_obj.projection()).toEqual(jasmine.any(ol.Projection));
|
expect(p_obj.projection()).toEqual(jasmine.any(ol.Projection));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("the centroid method is functional", function(){
|
||||||
|
it("returns an instance of ol.geom.Point", function(){
|
||||||
|
expect(pNoZ_arr.centroid()).toBeA(ol.geom.Point);
|
||||||
|
expect(pWithZ_arr.centroid()).toBeA(ol.geom.Point);
|
||||||
|
expect(p_arr.centroid()).toBeA(ol.geom.Point);
|
||||||
|
|
||||||
|
expect(pNoZ_obj.centroid()).toBeA(ol.geom.Point);
|
||||||
|
expect(pWithZ_obj.centroid()).toBeA(ol.geom.Point);
|
||||||
|
expect(p_obj.centroid()).toBeA(ol.geom.Point);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("does return a clone and not the point itself", function(){
|
||||||
|
expect(pNoZ_arr.centroid()).not.toBe(pNoZ_arr);
|
||||||
|
expect(pWithZ_arr.centroid()).not.toBe(pWithZ_arr);
|
||||||
|
expect(p_arr.centroid()).not.toBe(p_arr);
|
||||||
|
|
||||||
|
expect(pNoZ_obj.centroid()).not.toBe(pNoZ_obj);
|
||||||
|
expect(pWithZ_obj.centroid()).not.toBe(pWithZ_obj);
|
||||||
|
expect(p_obj.centroid()).not.toBe(p_obj);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("has the expected coordinates", function(){
|
||||||
|
var c1 = pNoZ_arr.centroid(),
|
||||||
|
c2 = pWithZ_arr.centroid(),
|
||||||
|
c3 = p_arr.centroid(),
|
||||||
|
c4 = pNoZ_obj.centroid(),
|
||||||
|
c5 = pWithZ_obj.centroid(),
|
||||||
|
c6 = p_obj.centroid();
|
||||||
|
|
||||||
|
expect(c1.x() + ',' + c1.y()).toBe('21,4');
|
||||||
|
expect(c2.x() + ',' + c2.y()).toBe('21,4');
|
||||||
|
expect(c3.x() + ',' + c3.y()).toBe('21,4');
|
||||||
|
expect(c4.x() + ',' + c4.y()).toBe('21,4');
|
||||||
|
expect(c5.x() + ',' + c5.y()).toBe('21,4');
|
||||||
|
expect(c6.x() + ',' + c6.y()).toBe('21,4');
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns point(0,0) when the point was constructed without args", function(){
|
||||||
|
var c = pNoArgs.centroid();
|
||||||
|
expect(c.x() + ',' + c.y()).toBe('0,0');
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -123,4 +123,27 @@ describe("ol.geom.Point", function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("the getCentroid method is functional", function(){
|
||||||
|
it("returns an instance of ol.geom.Point", function(){
|
||||||
|
expect(p2Args.getCentroid()).toBeA(ol.geom.Point);
|
||||||
|
expect(p3Args.getCentroid()).toBeA(ol.geom.Point);
|
||||||
|
expect(p4Args.getCentroid()).toBeA(ol.geom.Point);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("does return a clone and not the point itself", function(){
|
||||||
|
expect(p2Args.getCentroid()).not.toBe(p2Args);
|
||||||
|
expect(p3Args.getCentroid()).not.toBe(p3Args);
|
||||||
|
expect(p4Args.getCentroid()).not.toBe(p4Args);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("has the expected coordinates", function(){
|
||||||
|
var c2 = p2Args.getCentroid(),
|
||||||
|
c3 = p3Args.getCentroid(),
|
||||||
|
c4 = p4Args.getCentroid();
|
||||||
|
|
||||||
|
expect(c2.getX() + ',' + c2.getY()).toBe('21,4');
|
||||||
|
expect(c3.getX() + ',' + c3.getY()).toBe('21,4');
|
||||||
|
expect(c4.getX() + ',' + c4.getY()).toBe('21,4');
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user