]> dev.renevier.net Git - syp.git/blob - openlayers/lib/OpenLayers/Layer/PointTrack.js
initial commit
[syp.git] / openlayers / lib / OpenLayers / Layer / PointTrack.js
1 /* Copyright (c) 2006-2007 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/Layer/Vector.js
7  * @requires OpenLayers/Console.js
8  */
9
10 /**
11  * Class: OpenLayers.Layer.PointTrack
12  * Vector layer to display ordered point features as a line, creating one
13  * LineString feature for each pair of two points.
14  *
15  * Inherits from:
16  *  - <OpenLayers.Layer.Vector> 
17  */
18 OpenLayers.Layer.PointTrack = OpenLayers.Class(OpenLayers.Layer.Vector, {
19   
20     /**
21      * APIProperty:
22      * dataFrom  - {<OpenLayers.Layer.PointTrack.dataFrom>} optional. If the
23      *             lines should get the data/attributes from one of the two
24      *             points, creating it, which one should it be?
25      */
26     dataFrom: null,
27     
28     /**
29      * Constructor: OpenLayers.PointTrack
30      * Constructor for a new OpenLayers.PointTrack instance.
31      *
32      * Parameters:
33      * name     - {String} name of the layer
34      * options  - {Object} Optional object with properties to tag onto the
35      *            instance.
36      */    
37     initialize: function(name, options) {
38         OpenLayers.Layer.Vector.prototype.initialize.apply(this, arguments);
39     },
40         
41     /**
42      * APIMethod: addNodes
43      * Adds point features that will be used to create lines from, using point
44      * pairs. The first point of a pair will be the source node, the second
45      * will be the target node.
46      * 
47      * Parameters:
48      * pointFeatures - {Array(<OpenLayers.Feature>)}
49      * 
50      */
51     addNodes: function(pointFeatures) {
52         if (pointFeatures.length < 2) {
53             OpenLayers.Console.error(
54                     "At least two point features have to be added to create" +
55                     "a line from");
56             return;
57         }
58         
59         var lines = new Array(pointFeatures.length-1);
60         
61         var pointFeature, startPoint, endPoint;
62         for(var i=0, len=pointFeatures.length; i<len; i++) {
63             pointFeature = pointFeatures[i];
64             endPoint = pointFeature.geometry;
65             
66             if (!endPoint) {
67               var lonlat = pointFeature.lonlat;
68               endPoint = new OpenLayers.Geometry.Point(lonlat.lon, lonlat.lat);
69             } else if(endPoint.CLASS_NAME != "OpenLayers.Geometry.Point") {
70                 OpenLayers.Console.error(
71                         "Only features with point geometries are supported.");
72                 return;
73             }
74             
75             if(i > 0) {
76                 var attributes = (this.dataFrom != null) ?
77                         (pointFeatures[i+this.dataFrom].data ||
78                                 pointFeatures[i+this.dataFrom].attributes) :
79                         null;
80                 var line = new OpenLayers.Geometry.LineString([startPoint,
81                         endPoint]);
82                         
83                 lines[i-1] = new OpenLayers.Feature.Vector(line, attributes);
84             }
85             
86             startPoint = endPoint;
87         }
88
89         this.addFeatures(lines);
90     },
91     
92     CLASS_NAME: "OpenLayers.Layer.PointTrack"
93 });
94
95 /**
96  * Constant: OpenLayers.Layer.PointTrack.dataFrom
97  * {Object} with the following keys
98  * - SOURCE_NODE: take data/attributes from the source node of the line
99  * - TARGET_NODE: take data/attributes from the target node of the line
100  */
101 OpenLayers.Layer.PointTrack.dataFrom = {'SOURCE_NODE': -1, 'TARGET_NODE': 0};
102