has been done in the NaturalDocs branch back to trunk. Thanks to everyone who helped out in making this happen. (I could list people, but the list would be long, and I'm already mentally on vacation.) git-svn-id: http://svn.openlayers.org/trunk/openlayers@3545 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
193 lines
5.0 KiB
JavaScript
193 lines
5.0 KiB
JavaScript
/* Copyright (c) 2006 MetaCarta, Inc., published under a modified BSD license.
|
|
* See http://svn.openlayers.org/trunk/openlayers/repository-license.txt
|
|
* for the full text of the license. */
|
|
|
|
/**
|
|
* @requires OpenLayers/Format/WKT.js
|
|
* @requires OpenLayers/Feature/Vector.js
|
|
*
|
|
* Class: OpenLayers.Geometry
|
|
* 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.
|
|
*/
|
|
OpenLayers.Geometry = OpenLayers.Class.create();
|
|
OpenLayers.Geometry.prototype = {
|
|
|
|
/**
|
|
* Property: id
|
|
* {String} A unique identifier for this geometry.
|
|
*/
|
|
id: null,
|
|
|
|
/**
|
|
* Property: parent
|
|
* {<OpenLayers.Geometry>}This is set when a Geometry is added as component
|
|
* of another geometry
|
|
*/
|
|
parent: null,
|
|
|
|
/**
|
|
* Property: bounds
|
|
* {<OpenLayers.Bounds>} The bounds of this geometry
|
|
*/
|
|
bounds: null,
|
|
|
|
/**
|
|
* Constructor: OpenLayers.Geometry
|
|
* Creates a geometry object.
|
|
*/
|
|
initialize: function() {
|
|
this.id = OpenLayers.Util.createUniqueID(this.CLASS_NAME+ "_");
|
|
},
|
|
|
|
/**
|
|
* Method: destroy
|
|
* Destroy this geometry.
|
|
*/
|
|
destroy: function() {
|
|
this.id = null;
|
|
this.bounds = null;
|
|
},
|
|
|
|
/**
|
|
* Set the bounds for this Geometry.
|
|
*
|
|
* Parameters:
|
|
* object - {<OpenLayers.Bounds>}
|
|
*/
|
|
setBounds: function(bounds) {
|
|
if (bounds) {
|
|
this.bounds = bounds.clone();
|
|
}
|
|
},
|
|
|
|
/**
|
|
* Method: clearBounds
|
|
* Nullify this components bounds and that of its parent as well.
|
|
*/
|
|
clearBounds: function() {
|
|
this.bounds = null;
|
|
if (this.parent) {
|
|
this.parent.clearBounds();
|
|
}
|
|
},
|
|
|
|
/**
|
|
* Method: extendBounds
|
|
* Extend the existing bounds to include the new bounds.
|
|
* If geometry's bounds is not yet set, then set a new Bounds.
|
|
*
|
|
* Parameters:
|
|
* newBounds - {<OpenLayers.Bounds>}
|
|
*/
|
|
extendBounds: function(newBounds){
|
|
var bounds = this.getBounds();
|
|
if (!bounds) {
|
|
this.setBounds(newBounds);
|
|
} else {
|
|
this.bounds.extend(newBounds);
|
|
}
|
|
},
|
|
|
|
/**
|
|
* APIMethod: getBounds
|
|
* Get the bounds for this Geometry. If bounds is not set, it
|
|
* is calculated again, this makes queries faster.
|
|
*
|
|
* Return:
|
|
* {<OpenLayers.Bounds>}
|
|
*/
|
|
getBounds: function() {
|
|
if (this.bounds == null) {
|
|
this.calculateBounds();
|
|
}
|
|
return this.bounds;
|
|
},
|
|
|
|
/**
|
|
* APIMethod: calculateBounds
|
|
* Recalculate the bounds for the geometry.
|
|
*/
|
|
calculateBounds: function() {
|
|
//
|
|
// This should be overridden by subclasses.
|
|
//
|
|
},
|
|
|
|
/**
|
|
* Method: atPoint
|
|
* Note - This is only an approximation based on the bounds of the
|
|
* geometry.
|
|
*
|
|
* Parameters:
|
|
* lonlat - {<OpenLayers.LonLat>}
|
|
* toleranceLon - {float} Optional tolerance in Geometric Coords
|
|
* toleranceLat - {float} Optional tolerance in Geographic Coords
|
|
*
|
|
* Return:
|
|
* {Boolean} Whether or not the geometry is at the specified location
|
|
*/
|
|
atPoint: function(lonlat, toleranceLon, toleranceLat) {
|
|
var atPoint = false;
|
|
var bounds = this.getBounds();
|
|
if ((bounds != null) && (lonlat != null)) {
|
|
|
|
var dX = (toleranceLon != null) ? toleranceLon : 0;
|
|
var dY = (toleranceLat != null) ? toleranceLat : 0;
|
|
|
|
var toleranceBounds =
|
|
new OpenLayers.Bounds(this.bounds.left - dX,
|
|
this.bounds.bottom - dY,
|
|
this.bounds.right + dX,
|
|
this.bounds.top + dY);
|
|
|
|
atPoint = toleranceBounds.containsLonLat(lonlat);
|
|
}
|
|
return atPoint;
|
|
},
|
|
|
|
/**
|
|
* Method: getLength
|
|
* Calculate the length of this geometry. This method is defined in
|
|
* subclasses.
|
|
*
|
|
* Return:
|
|
* {Float} The length of the collection by summing its parts
|
|
*/
|
|
getLength: function() {
|
|
//to be overridden by geometries that actually have a length
|
|
//
|
|
return 0.0;
|
|
},
|
|
|
|
/**
|
|
* Method: getArea
|
|
* Calculate the area of this geometry. This method is defined in subclasses.
|
|
*
|
|
* Return:
|
|
* {Float} The area of the collection by summing its parts
|
|
*/
|
|
getArea: function() {
|
|
//to be overridden by geometries that actually have an area
|
|
//
|
|
return 0.0;
|
|
},
|
|
|
|
/**
|
|
* Method: toString
|
|
* Returns the Well-Known Text representation of a geometry
|
|
*
|
|
* Return:
|
|
* {String} Well-Known Text
|
|
*/
|
|
toString: function() {
|
|
return OpenLayers.Format.WKT.prototype.write(
|
|
new OpenLayers.Feature.Vector(this)
|
|
);
|
|
},
|
|
|
|
/** @final @type String*/
|
|
CLASS_NAME: "OpenLayers.Geometry"
|
|
};
|