#571: Don't subclass Geometry.Point from LonLat, and all neccesary associated

changes. Reviewed by tschaub (thx)


git-svn-id: http://svn.openlayers.org/trunk/openlayers@2943 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
crschmidt
2007-03-31 18:30:56 +00:00
parent 45da80f8ad
commit 4aec64aa67
10 changed files with 67 additions and 110 deletions

View File

@@ -492,11 +492,14 @@ OpenLayers.Bounds.prototype = {
var bounds = null;
if (object) {
switch(object.CLASS_NAME) {
case "OpenLayers.Geometry.Point":
case "OpenLayers.LonLat":
bounds = new OpenLayers.Bounds(object.lon, object.lat,
object.lon, object.lat);
break;
case "OpenLayers.Geometry.Point":
bounds = new OpenLayers.Bounds(object.x, object.y,
object.x, object.y);
break;
case "OpenLayers.Bounds":
bounds = object;

View File

@@ -5,20 +5,11 @@
/**
* @class
*
* The Point class is a subclass of Geometry and also a subclass of the
* non-vector OpenLayers.LonLat class. The basic functionality that this adds
* is the ability to switch between lon/lat and x/y at will, as well some
* convenience functions to create a Bounds from a point and measure the
* distance between two points.
*
* getX() and setX() should be used to access the x or lon variables.
*
* @requires OpenLayers/BaseTypes.js
* @requires OpenLayers/Geometry.js
*/
OpenLayers.Geometry.Point = OpenLayers.Class.create();
OpenLayers.Geometry.Point.prototype =
OpenLayers.Class.inherit(OpenLayers.Geometry, OpenLayers.LonLat, {
OpenLayers.Class.inherit(OpenLayers.Geometry, {
/** @type float */
x: null,
@@ -34,10 +25,9 @@ OpenLayers.Geometry.Point.prototype =
*/
initialize: function(x, y) {
OpenLayers.Geometry.prototype.initialize.apply(this, arguments);
OpenLayers.LonLat.prototype.initialize.apply(this, arguments);
this.x = this.lon;
this.y = this.lat;
this.x = parseFloat(x);
this.y = parseFloat(y);
},
/**
@@ -55,46 +45,12 @@ OpenLayers.Geometry.Point.prototype =
return obj;
},
/**
* Sets the x coordinate
*
* @param {float} x
*/
setX: function(x) {
this.lon = x;
this.x = x;
},
/**
* Sets the y coordinate
*
* @param {float} y
*/
setY: function(y) {
this.lat = y;
this.y = y;
},
/**
* @type float
*/
getX: function() {
return this.lon;
},
/**
* @type float
*/
getY: function() {
return this.lat;
},
/** Create a new Bounds based on the lon/lat
*
*/
calculateBounds: function () {
this.bounds = new OpenLayers.Bounds(this.lon, this.lat,
this.lon, this.lat);
this.bounds = new OpenLayers.Bounds(this.x, this.y,
this.x, this.y);
},
/**
@@ -112,6 +68,23 @@ OpenLayers.Geometry.Point.prototype =
return distance;
},
/**
* @param {OpenLayers.Geometry} xy
* @returns Boolean value indicating whether the passed-in
* OpenLayers.Geometryobject has the same components as this
* note that if ll passed in is null, returns false
*
* @type bool
*/
equals:function(geom) {
var equals = false;
if (geom != null) {
equals = ((this.x == geom.x && this.y == geom.y) ||
(isNaN(this.x) && isNaN(this.y) && isNaN(geom.x) && isNaN(geom.y)));
}
return equals;
},
/**
* @returns the coordinates as a string
* @type String
@@ -120,14 +93,23 @@ OpenLayers.Geometry.Point.prototype =
return this.toShortString();
},
/**
* @return Shortened String representation of Point object.
* (ex. <i>"5, 42"</i>)
* @type String
*/
toShortString: function() {
return (this.x + ", " + this.y);
},
/**
* Moves a point in place
* @param {Float} x
* @param {Float} y
*/
move: function(x, y) {
this.setX(this.x + x);
this.setY(this.y + y);
this.x = this.x + x;
this.y = this.y + y;
},
/** @final @type String */

View File

@@ -102,8 +102,8 @@ OpenLayers.Handler.Path.prototype =
*/
modifyGeometry: function() {
var index = this.line.components.length - 1;
this.line.components[index].setX(this.point.x);
this.line.components[index].setY(this.point.y);
this.line.components[index].x = this.point.x;
this.line.components[index].y = this.point.y;
},
/**
@@ -141,8 +141,8 @@ OpenLayers.Handler.Path.prototype =
this.mouseDown = true;
this.lastDown = evt.xy;
var lonlat = this.control.map.getLonLatFromPixel(evt.xy);
this.point.setX(lonlat.lon);
this.point.setY(lonlat.lat);
this.point.x = lonlat.lon;
this.point.y = lonlat.lat;
if((this.lastUp == null) || !this.lastUp.equals(evt.xy)) {
this.addPoint();
}
@@ -161,8 +161,8 @@ OpenLayers.Handler.Path.prototype =
mousemove: function (evt) {
if(this.drawing) {
var lonlat = this.map.getLonLatFromPixel(evt.xy);
this.point.setX(lonlat.lon);
this.point.setY(lonlat.lat);
this.point.x = lonlat.lon;
this.point.y = lonlat.lat;
if(this.mouseDown && this.freehandMode(evt)) {
this.addPoint();
} else {

View File

@@ -188,8 +188,8 @@ OpenLayers.Handler.Point.prototype =
this.lastDown = evt.xy;
this.drawing = true;
var lonlat = this.map.getLonLatFromPixel(evt.xy);
this.point.setX(lonlat.lon);
this.point.setY(lonlat.lat);
this.point.x = lonlat.lon;
this.point.y = lonlat.lat;
this.drawGeometry();
return false;
},
@@ -204,8 +204,8 @@ OpenLayers.Handler.Point.prototype =
mousemove: function (evt) {
if(this.drawing) {
var lonlat = this.map.getLonLatFromPixel(evt.xy);
this.point.setX(lonlat.lon);
this.point.setY(lonlat.lat);
this.point.x = lonlat.lon;
this.point.y = lonlat.lat;
this.drawGeometry();
}
return true;

View File

@@ -65,8 +65,8 @@ OpenLayers.Handler.Polygon.prototype =
*/
modifyGeometry: function() {
var index = this.line.components.length - 2;
this.line.components[index].setX(this.point.x);
this.line.components[index].setY(this.point.y);
this.line.components[index].x = this.point.x;
this.line.components[index].y = this.point.y;
},
/**

View File

@@ -333,8 +333,8 @@ OpenLayers.Renderer.VML.prototype =
var path = "m";
for (var i = 0; i < geometry.components.length; i++) {
var x = (geometry.components[i].getX()/resolution);
var y = (geometry.components[i].getY()/resolution);
var x = (geometry.components[i].x/resolution);
var y = (geometry.components[i].y/resolution);
path += " " + x.toFixed() + "," + y.toFixed() + " l ";
}
if (closeLine) {
@@ -360,8 +360,8 @@ OpenLayers.Renderer.VML.prototype =
path += "m";
for (var i = 0; i < linearRing.components.length; i++) {
var x = linearRing.components[i].getX() / resolution;
var y = linearRing.components[i].getY() / resolution;
var x = linearRing.components[i].x / resolution;
var y = linearRing.components[i].y / resolution;
path += " " + x.toFixed() + "," + y.toFixed();
if (i==0) {
path += " l";
@@ -399,8 +399,8 @@ OpenLayers.Renderer.VML.prototype =
var path = "";
for (var i = 0; i < geometry.components.length; i++) {
var x = geometry.components[i].getX() / resolution;
var y = geometry.components[i].getY() / resolution;
var x = geometry.components[i].x / resolution;
var y = geometry.components[i].y / resolution;
if ((i%3)==0 && (i/3)==0) {
path += "m"
@@ -426,8 +426,8 @@ OpenLayers.Renderer.VML.prototype =
var path = "";
for (var i = 0; i < geometry.components.length; i++) {
var x = geometry.components[i].getX() / resolution;
var y = geometry.components[i].getY() / resolution;
var x = geometry.components[i].x / resolution;
var y = geometry.components[i].y / resolution;
if ((i%3)==0 && (i/3)==0) {
path += "m";
} else if ((i%3)==1) {

View File

@@ -361,8 +361,8 @@
bounds.extend(object);
t.ok( ((bounds.left == object.lon) &&
(bounds.bottom == object.lat) &&
t.ok( ((bounds.left == object.x) &&
(bounds.bottom == object.y) &&
(bounds.right == originalBounds.right) &&
(bounds.top == originalBounds.top)), "obj Point to extends correclty modifies left and bottom");
@@ -375,8 +375,8 @@
t.ok( ((bounds.left == originalBounds.left) &&
(bounds.bottom == originalBounds.bottom) &&
(bounds.right == object.lon) &&
(bounds.top == object.lat)), "obj Point to extends correclty modifies right and top");
(bounds.right == object.x) &&
(bounds.top == object.y)), "obj Point to extends correclty modifies right and top");
}

View File

@@ -96,8 +96,8 @@
t.eq( curve.components.length, 4, "new point added to array" );
t.eq( bounds.bottom, -30, "bottom bound is -30 after 2nd addComponent" );
t.eq( bounds.left, -20, "left bound is 20 after 2nd addComponent" );
t.eq( curve.components[1].lon, -20, "new point.lon is -20 (index worked)" );
t.eq( curve.components[1].lat, -30, "new point.lat is -30 (index worked)" );
t.eq( curve.components[1].x, -20, "new point.lon is -20 (index worked)" );
t.eq( curve.components[1].y, -30, "new point.lat is -30 (index worked)" );
}
function test_05_Curve_removeComponent (t) {

View File

@@ -21,7 +21,7 @@
}
function test_02_MultiPoint_move(t) {
t.plan(4);
t.plan(2);
var multipoint = new OpenLayers.Geometry.MultiPoint([point]);
var x = point.x;
@@ -32,8 +32,6 @@
multipoint.move(dx, dy);
t.eq(multipoint.components[0].x, x + dx, "move() correctly modifies x");
t.eq(multipoint.components[0].y, y + dy, "move() correctly modifies y");
t.eq(multipoint.components[0].lon, x + dx, "move() correctly modifies lon");
t.eq(multipoint.components[0].lat, y + dy, "move() correctly modifies lat");
}
function test_MultiPoint_equals(t) {

View File

@@ -20,32 +20,8 @@
t.eq( point.CLASS_NAME, "OpenLayers.Geometry.Point", "point.CLASS_NAME is set correctly");
t.eq( point.x, x, "point.x is set correctly");
t.eq( point.y, y, "point.y is set correctly");
t.eq( point.lon, x, "point.lon is set correctly");
t.eq( point.lat, y, "point.lat is set correctly");
}
function test_02_Point_accessors(t) {
t.plan( 6 )
//valid
var x = 10;
var y = 20;
point = new OpenLayers.Geometry.Point(x, y);
t.eq( point.getX(), x, "point.x is get() correctly");
t.eq( point.getY(), y, "point.y is get() correctly");
var x1 = 55;
var y1 = 73;
point.setX(x1);
point.setY(y1);
t.eq( point.x, x1, "point.x is set() correctly");
t.eq( point.y, y1, "point.y is set() correctly");
t.eq( point.lon, x1, "point.lon is set() correctly");
t.eq( point.lat, y1, "point.lat is set() correctly");
t.eq( point.lon, null, "point.lon is not set");
t.eq( point.lat, null, "point.lat is not set");
}
function test_03_Point_calculateBounds (t) {
@@ -89,7 +65,7 @@
}
function test_06_Point_move(t) {
t.plan(4);
t.plan(2);
var x = 10;
var y = 20;
@@ -100,8 +76,6 @@
point.move(dx, dy);
t.eq(point.x, x + dx, "move() correctly modifies x");
t.eq(point.y, y + dy, "move() correctly modifies y");
t.eq(point.lon, x + dx, "move() correctly modifies lon");
t.eq(point.lat, y + dy, "move() correctly modifies lat");
}
function test_Point_equals(t) {