Merge pull request #102 from tschaub/nowkt

Allow for builds without WKT format
This commit is contained in:
Tim Schaub
2011-12-14 09:51:06 -08:00
4 changed files with 70 additions and 22 deletions

View File

@@ -5,8 +5,6 @@
/**
* @requires OpenLayers/BaseTypes/Class.js
* @requires OpenLayers/Format/WKT.js
* @requires OpenLayers/Feature/Vector.js
*/
/**
@@ -14,6 +12,9 @@
* A Geometry is a description of a geographic object. Create an instance of
* this class with the <OpenLayers.Geometry> constructor. This is a base class,
* typical geometry types are described by subclasses of this class.
*
* Note that if you use the <OpenLayers.Geometry.fromWKT> method, you must
* explicitly include the OpenLayers.Format.WKT in your build.
*/
OpenLayers.Geometry = OpenLayers.Class({
@@ -240,15 +241,23 @@ OpenLayers.Geometry = OpenLayers.Class({
/**
* Method: toString
* Returns the Well-Known Text representation of a geometry
* Returns a text representation of the geometry. If the WKT format is
* included in a build, this will be the Well-Known Text
* representation.
*
* Returns:
* {String} Well-Known Text
* {String} String representation of this geometry.
*/
toString: function() {
return OpenLayers.Format.WKT.prototype.write(
new OpenLayers.Feature.Vector(this)
);
var string;
if (OpenLayers.Format && OpenLayers.Format.WKT) {
string = OpenLayers.Format.WKT.prototype.write(
new OpenLayers.Feature.Vector(this)
);
} else {
string = Object.prototype.toString.call(this);
}
return string;
},
CLASS_NAME: "OpenLayers.Geometry"
@@ -256,7 +265,9 @@ OpenLayers.Geometry = OpenLayers.Class({
/**
* Function: OpenLayers.Geometry.fromWKT
* Generate a geometry given a Well-Known Text string.
* Generate a geometry given a Well-Known Text string. For this method to
* work, you must include the OpenLayers.Format.WKT in your build
* explicitly.
*
* Parameters:
* wkt - {String} A string representing the geometry in Well-Known Text.
@@ -265,22 +276,24 @@ OpenLayers.Geometry = OpenLayers.Class({
* {<OpenLayers.Geometry>} A geometry of the appropriate class.
*/
OpenLayers.Geometry.fromWKT = function(wkt) {
var format = arguments.callee.format;
if(!format) {
format = new OpenLayers.Format.WKT();
arguments.callee.format = format;
}
var geom;
var result = format.read(wkt);
if(result instanceof OpenLayers.Feature.Vector) {
geom = result.geometry;
} else if(OpenLayers.Util.isArray(result)) {
var len = result.length;
var components = new Array(len);
for(var i=0; i<len; ++i) {
components[i] = result[i].geometry;
if (OpenLayers.Format && OpenLayers.Format.WKT) {
var format = OpenLayers.Geometry.fromWKT.format;
if (!format) {
format = new OpenLayers.Format.WKT();
OpenLayers.Geometry.fromWKT.format = format;
}
var result = format.read(wkt);
if (result instanceof OpenLayers.Feature.Vector) {
geom = result.geometry;
} else if (OpenLayers.Util.isArray(result)) {
var len = result.length;
var components = new Array(len);
for (var i=0; i<len; ++i) {
components[i] = result[i].geometry;
}
geom = new OpenLayers.Geometry.Collection(components);
}
geom = new OpenLayers.Geometry.Collection(components);
}
return geom;
};

View File

@@ -43,3 +43,10 @@ The `roundedCorner` option of `Control.LayerSwitcher` is deprecated, and it now
See more detail in the Rico deprecation [pull request](https://github.com/openlayers/openlayers/pull/99).
In future releases we intend to move the Rico and `AnchoredBubble` code into deprecated.js. You really should consider stop using Rico-based functionalities in your applications.
## Changes in Geometry
The base `OpenLayers.Geometry` class no longer depends on `OpenLayers.Format.WKT` or `OpenLayers.Feature.Vector`. If you want to make use of the `OpenLayers.Geometry.fromWKT` method, you must explicitly include the OpenLayers/Format/WKT.js file in your build.
Without the WKT format included (by default), the `OpenLayers.Geometry::toString` method now returns "[object Object]." Previously, it returned the Well-Known Text representation of the geometry. To maintain the previous behavior, include the OpenLayers/Format/WKT.js file in your build.

View File

@@ -334,6 +334,19 @@
t.geom_eq(wkt(cases[i].wkt), cases[i].geom, "case " + i);
}
}
function test_fromWKT_undefined(t) {
t.plan(1);
var WKT = OpenLayers.Format.WKT;
OpenLayers.Format.WKT = null;
delete OpenLayers.Geometry.fromWKT.format;
var geom = OpenLayers.Geometry.fromWKT("POINT(1 1)");
t.eq(geom, undefined, "undefined when OpenLayers.Format.WKT is not available");
OpenLayers.Format.WKT = WKT;
}
</script>

View File

@@ -101,6 +101,21 @@
"toString() returns WKT" );
}
function test_Point_toString_no_wkt(t) {
t.plan(1);
var WKT = OpenLayers.Format.WKT;
OpenLayers.Format.WKT = null;
var x = 10;
var y = 20;
point = new OpenLayers.Geometry.Point(x, y);
t.eq(point.toString(), "[object Object]", "default string representation");
OpenLayers.Format.WKT = WKT;
}
function test_Point_move(t) {
t.plan(3);