Project

General

Profile

Download (2.88 KB) Statistics
| Branch: | Tag: | Revision:
1
/* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for
2
 * full list of contributors). Published under the 2-clause BSD license.
3
 * See license.txt in the OpenLayers distribution or repository 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
    
36
    /**
37
     * APIMethod: getLength
38
     * 
39
     * Returns:
40
     * {Float} The length of the curve
41
     */
42
    getLength: function() {
43
        var length = 0.0;
44
        if ( this.components && (this.components.length > 1)) {
45
            for(var i=1, len=this.components.length; i<len; i++) {
46
                length += this.components[i-1].distanceTo(this.components[i]);
47
            }
48
        }
49
        return length;
50
    },
51

    
52
    /**
53
     * APIMethod: getGeodesicLength
54
     * Calculate the approximate length of the geometry were it projected onto
55
     *     the earth.
56
     *
57
     * projection - {<OpenLayers.Projection>} The spatial reference system
58
     *     for the geometry coordinates.  If not provided, Geographic/WGS84 is
59
     *     assumed.
60
     * 
61
     * Returns:
62
     * {Float} The appoximate geodesic length of the geometry in meters.
63
     */
64
    getGeodesicLength: function(projection) {
65
        var geom = this;  // so we can work with a clone if needed
66
        if(projection) {
67
            var gg = new OpenLayers.Projection("EPSG:4326");
68
            if(!gg.equals(projection)) {
69
                geom = this.clone().transform(projection, gg);
70
            }
71
        }
72
        var length = 0.0;
73
        if(geom.components && (geom.components.length > 1)) {
74
            var p1, p2;
75
            for(var i=1, len=geom.components.length; i<len; i++) {
76
                p1 = geom.components[i-1];
77
                p2 = geom.components[i];
78
                // this returns km and requires lon/lat properties
79
                length += OpenLayers.Util.distVincenty(
80
                    {lon: p1.x, lat: p1.y}, {lon: p2.x, lat: p2.y}
81
                );
82
            }
83
        }
84
        // convert to m
85
        return length * 1000;
86
    },
87

    
88
    CLASS_NAME: "OpenLayers.Geometry.Curve"
89
});
(2-2/9)