Allowing for WKT free builds.

The WKT format is used for converting geometries to and from string representations.  For builds that do not explicitly include the WKT format, the `OpenLayers.Geometry.fromWKT` returns `undefined`.
This commit is contained in:
tschaub
2011-12-10 16:36:53 -07:00
parent 038a34c4ad
commit 66de55ef9b
2 changed files with 46 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;
};