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