Geometry.js
4.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/* Copyright (c) 2006 MetaCarta, Inc., published under the BSD license.
* See http://svn.openlayers.org/trunk/openlayers/release-license.txt
* for the full text of the license. */
/**
* @class
* @requires OpenLayers/Format/WKT.js
* @requires OpenLayers/Feature/Vector.js
*/
OpenLayers.Geometry = OpenLayers.Class.create();
OpenLayers.Geometry.prototype = {
/** @type String */
id: null,
/** This is set when a Geometry is added as Component of another Geometry
*
* @type OpenLayers.Geometry */
parent: null,
/** @type OpenLayers.Bounds */
bounds: null,
/**
* @constructor
*/
initialize: function() {
this.id = OpenLayers.Util.createUniqueID(this.CLASS_NAME+ "_");
},
/**
*
*/
destroy: function() {
this.id = null;
this.bounds = null;
},
/**
* Set the bounds for this Geometry.
*
* @param {OpenLayers.Bounds} object
*/
setBounds: function(bounds) {
if (bounds) {
this.bounds = bounds.clone();
}
},
/**
* Nullify this components bounds and that of its parent as well.
*/
clearBounds: function() {
this.bounds = null;
if (this.parent) {
this.parent.clearBounds();
}
},
/**
* Extend the existing bounds to include the new bounds.
* If geometry's bounds is not yet set, then set a new Bounds.
*
* @param {OpenLayers.Bounds} newBounds
*/
extendBounds: function(newBounds){
var bounds = this.getBounds();
if (!bounds) {
this.setBounds(newBounds);
} else {
this.bounds.extend(newBounds);
}
},
/**
* Get the bounds for this Geometry. If bounds is not set, it
* is calculated again, this makes queries faster.
*
* @type OpenLayers.Bounds
*/
getBounds: function() {
if (this.bounds == null) {
this.calculateBounds();
}
return this.bounds;
},
/** Recalculate the bounds for the geometry.
*
*/
calculateBounds: function() {
//
// This should be overridden by subclasses.
//
},
/**
* Note: This is only an approximation based on the bounds of the
* geometry.
*
* @param {OpenLayers.LonLat} lonlat
* @param {float} toleranceLon Optional tolerance in Geometric Coords
* @param {float} toleranceLat Optional tolerance in Geographic Coords
*
* @returns Whether or not the geometry is at the specified location
* @type Boolean
*/
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;
},
/**
* @returns The length of the geometry
* @type float
*/
getLength: function() {
//to be overridden by geometries that actually have a length
//
return 0.0;
},
/**
* @returns The area of the geometry
* @type float
*/
getArea: function() {
//to be overridden by geometries that actually have an area
//
return 0.0;
},
/**
* @returns the Well-Known Text representation of a geometry
* @type String
*/
toString: function() {
return OpenLayers.Format.WKT.prototype.write(
new OpenLayers.Feature.Vector(this)
);
},
/** @final @type String */
CLASS_NAME: "OpenLayers.Geometry"
};