~mortenoh/+junk/dhis2-detailed-import-export

« back to all changes in this revision

Viewing changes to gis/dhis-gis-geostat/mfbase/openlayers/lib/OpenLayers/Geometry/Point.js

  • Committer: larshelge at gmail
  • Date: 2009-03-03 16:46:36 UTC
  • Revision ID: larshelge@gmail.com-20090303164636-2sjlrquo7ib1gf7r
Initial check-in

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (c) 2006-2008 MetaCarta, Inc., published under the Clear BSD
 
2
 * license.  See http://svn.openlayers.org/trunk/openlayers/license.txt for the
 
3
 * full text of the license. */
 
4
 
 
5
/**
 
6
 * @requires OpenLayers/Geometry.js
 
7
 */
 
8
 
 
9
/**
 
10
 * Class: OpenLayers.Geometry.Point
 
11
 * Point geometry class. 
 
12
 * 
 
13
 * Inherits from:
 
14
 *  - <OpenLayers.Geometry> 
 
15
 */
 
16
OpenLayers.Geometry.Point = OpenLayers.Class(OpenLayers.Geometry, {
 
17
 
 
18
    /** 
 
19
     * APIProperty: x 
 
20
     * {float} 
 
21
     */
 
22
    x: null,
 
23
 
 
24
    /** 
 
25
     * APIProperty: y 
 
26
     * {float} 
 
27
     */
 
28
    y: null,
 
29
 
 
30
    /**
 
31
     * Constructor: OpenLayers.Geometry.Point
 
32
     * Construct a point geometry.
 
33
     *
 
34
     * Parameters:
 
35
     * x - {float} 
 
36
     * y - {float}
 
37
     * 
 
38
     */
 
39
    initialize: function(x, y) {
 
40
        OpenLayers.Geometry.prototype.initialize.apply(this, arguments);
 
41
        
 
42
        this.x = parseFloat(x);
 
43
        this.y = parseFloat(y);
 
44
    },
 
45
 
 
46
    /**
 
47
     * APIMethod: clone
 
48
     * 
 
49
     * Returns:
 
50
     * {<OpenLayers.Geometry.Point>} An exact clone of this OpenLayers.Geometry.Point
 
51
     */
 
52
    clone: function(obj) {
 
53
        if (obj == null) {
 
54
            obj = new OpenLayers.Geometry.Point(this.x, this.y);
 
55
        }
 
56
 
 
57
        // catch any randomly tagged-on properties
 
58
        OpenLayers.Util.applyDefaults(obj, this);
 
59
 
 
60
        return obj;
 
61
    },
 
62
 
 
63
    /** 
 
64
     * Method: calculateBounds
 
65
     * Create a new Bounds based on the lon/lat
 
66
     */
 
67
    calculateBounds: function () {
 
68
        this.bounds = new OpenLayers.Bounds(this.x, this.y,
 
69
                                            this.x, this.y);
 
70
    },
 
71
 
 
72
    /**
 
73
     * APIMethod: distanceTo
 
74
     * 
 
75
     * Parameters:
 
76
     * point - {<OpenLayers.Geometry.Point>} 
 
77
     */
 
78
    distanceTo: function(point) {
 
79
        var distance = 0.0;
 
80
        if ( (this.x != null) && (this.y != null) && 
 
81
             (point != null) && (point.x != null) && (point.y != null) ) {
 
82
             
 
83
             var dx2 = Math.pow(this.x - point.x, 2);
 
84
             var dy2 = Math.pow(this.y - point.y, 2);
 
85
             distance = Math.sqrt( dx2 + dy2 );
 
86
        }
 
87
        return distance;
 
88
    },
 
89
    
 
90
    /** 
 
91
    * APIMethod: equals
 
92
    * 
 
93
    * Parameters:
 
94
    * xy - {<OpenLayers.Geometry>} 
 
95
    *
 
96
    * Returns:
 
97
    * {Boolean} Boolean value indicating whether the passed-in 
 
98
    *          {<OpenLayers.Geometry>} object has the same  components as this
 
99
    *          note that if ll passed in is null, returns false
 
100
    *
 
101
    */
 
102
    equals:function(geom) {
 
103
        var equals = false;
 
104
        if (geom != null) {
 
105
            equals = ((this.x == geom.x && this.y == geom.y) ||
 
106
                      (isNaN(this.x) && isNaN(this.y) && isNaN(geom.x) && isNaN(geom.y)));
 
107
        }
 
108
        return equals;
 
109
    },
 
110
    
 
111
    /**
 
112
     * Method: toShortString
 
113
     *
 
114
     * Returns:
 
115
     * {String} Shortened String representation of Point object. 
 
116
     *         (ex. <i>"5, 42"</i>)
 
117
     */
 
118
    toShortString: function() {
 
119
        return (this.x + ", " + this.y);
 
120
    },
 
121
    
 
122
    /**
 
123
     * APIMethod: move
 
124
     * Moves a point in place
 
125
     *
 
126
     * Parameters:
 
127
     * x - {Float} 
 
128
     * y - {Float} 
 
129
     */
 
130
    move: function(x, y) {
 
131
        this.x = this.x + x;
 
132
        this.y = this.y + y;
 
133
        this.clearBounds();
 
134
    },
 
135
 
 
136
    /**
 
137
     * APIMethod: rotate
 
138
     * Rotate a point around another.
 
139
     *
 
140
     * Parameters:
 
141
     * angle - {Float} Rotation angle in degrees (measured counterclockwise
 
142
     *                 from the positive x-axis)
 
143
     * origin - {<OpenLayers.Geometry.Point>} Center point for the rotation
 
144
     */
 
145
    rotate: function(angle, origin) {
 
146
        angle *= Math.PI / 180;
 
147
        var radius = this.distanceTo(origin);
 
148
        var theta = angle + Math.atan2(this.y - origin.y, this.x - origin.x);
 
149
        this.x = origin.x + (radius * Math.cos(theta));
 
150
        this.y = origin.y + (radius * Math.sin(theta));
 
151
        this.clearBounds();
 
152
    },
 
153
 
 
154
    /**
 
155
     * APIMethod: resize
 
156
     * Resize a point relative to some origin.  For points, this has the effect
 
157
     *     of scaling a vector (from the origin to the point).  This method is
 
158
     *     more useful on geometry collection subclasses.
 
159
     *
 
160
     * Parameters:
 
161
     * scale - {Float} Ratio of the new distance from the origin to the old
 
162
     *                 distance from the origin.  A scale of 2 doubles the
 
163
     *                 distance between the point and origin.
 
164
     * origin - {<OpenLayers.Geometry.Point>} Point of origin for resizing
 
165
     * ratio - {Float} Optional x:y ratio for resizing.  Default ratio is 1.
 
166
     */
 
167
    resize: function(scale, origin, ratio) {
 
168
        ratio = (ratio == undefined) ? 1 : ratio;
 
169
        this.x = origin.x + (scale * ratio * (this.x - origin.x));
 
170
        this.y = origin.y + (scale * (this.y - origin.y));
 
171
        this.clearBounds();
 
172
    },
 
173
    
 
174
    /**
 
175
     * APIMethod: intersects
 
176
     * Determine if the input geometry intersects this one.
 
177
     *
 
178
     * Parameters:
 
179
     * geometry - {<OpenLayers.Geometry>} Any type of geometry.
 
180
     *
 
181
     * Returns:
 
182
     * {Boolean} The input geometry intersects this one.
 
183
     */
 
184
    intersects: function(geometry) {
 
185
        var intersect = false;
 
186
        if(geometry.CLASS_NAME == "OpenLayers.Geometry.Point") {
 
187
            intersect = this.equals(geometry);
 
188
        } else {
 
189
            intersect = geometry.intersects(this);
 
190
        }
 
191
        return intersect;
 
192
    },
 
193
    
 
194
    /**
 
195
     * APIMethod: transform
 
196
     * Translate the x,y properties of the point from source to dest.
 
197
     * 
 
198
     * Parameters:
 
199
     * source - {<OpenLayers.Projection>} 
 
200
     * dest - {<OpenLayers.Projection>}
 
201
     * 
 
202
     * Returns:
 
203
     * {<OpenLayers.Geometry>} 
 
204
     */
 
205
    transform: function(source, dest) {
 
206
        if ((source && dest)) {
 
207
            OpenLayers.Projection.transform(
 
208
                this, source, dest); 
 
209
            this.bounds = null;
 
210
        }       
 
211
        return this;
 
212
    },
 
213
 
 
214
    CLASS_NAME: "OpenLayers.Geometry.Point"
 
215
});