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