]> dev.renevier.net Git - syp.git/blob - openlayers/lib/OpenLayers/Geometry/Curve.js
fixes notices
[syp.git] / openlayers / lib / OpenLayers / Geometry / Curve.js
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/MultiPoint.js
7  */
8
9 /**
10  * Class: OpenLayers.Geometry.Curve
11  * A Curve is a MultiPoint, whose points are assumed to be connected. To 
12  * this end, we provide a "getLength()" function, which iterates through 
13  * the points, summing the distances between them. 
14  * 
15  * Inherits: 
16  *  - <OpenLayers.Geometry.MultiPoint>
17  */
18 OpenLayers.Geometry.Curve = OpenLayers.Class(OpenLayers.Geometry.MultiPoint, {
19
20     /**
21      * Property: componentTypes
22      * {Array(String)} An array of class names representing the types of 
23      *                 components that the collection can include.  A null 
24      *                 value means the component types are not restricted.
25      */
26     componentTypes: ["OpenLayers.Geometry.Point"],
27
28     /**
29      * Constructor: OpenLayers.Geometry.Curve
30      * 
31      * Parameters:
32      * point - {Array(<OpenLayers.Geometry.Point>)}
33      */
34     initialize: function(points) {
35         OpenLayers.Geometry.MultiPoint.prototype.initialize.apply(this, 
36                                                                   arguments);
37     },
38     
39     /**
40      * APIMethod: getLength
41      * 
42      * Returns:
43      * {Float} The length of the curve
44      */
45     getLength: function() {
46         var length = 0.0;
47         if ( this.components && (this.components.length > 1)) {
48             for(var i=1, len=this.components.length; i<len; i++) {
49                 length += this.components[i-1].distanceTo(this.components[i]);
50             }
51         }
52         return length;
53     },
54
55     /**
56      * APIMethod: getGeodesicLength
57      * Calculate the approximate length of the geometry were it projected onto
58      *     the earth.
59      *
60      * projection - {<OpenLayers.Projection>} The spatial reference system
61      *     for the geometry coordinates.  If not provided, Geographic/WGS84 is
62      *     assumed.
63      * 
64      * Returns:
65      * {Float} The appoximate geodesic length of the geometry in meters.
66      */
67     getGeodesicLength: function(projection) {
68         var geom = this;  // so we can work with a clone if needed
69         if(projection) {
70             var gg = new OpenLayers.Projection("EPSG:4326");
71             if(!gg.equals(projection)) {
72                 geom = this.clone().transform(projection, gg);
73             }
74         }
75         var length = 0.0;
76         if(geom.components && (geom.components.length > 1)) {
77             var p1, p2;
78             for(var i=1, len=geom.components.length; i<len; i++) {
79                 p1 = geom.components[i-1];
80                 p2 = geom.components[i];
81                 // this returns km and requires lon/lat properties
82                 length += OpenLayers.Util.distVincenty(
83                     {lon: p1.x, lat: p1.y}, {lon: p2.x, lat: p2.y}
84                 );
85             }
86         }
87         // convert to m
88         return length * 1000;
89     },
90
91     CLASS_NAME: "OpenLayers.Geometry.Curve"
92 });