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/Layer/Vector.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: dataFrom
|
22
|
* {<OpenLayers.Layer.PointTrack.TARGET_NODE>} or
|
23
|
* {<OpenLayers.Layer.PointTrack.SOURCE_NODE>} optional. If the lines
|
24
|
* should get the data/attributes from one of the two points it is
|
25
|
* composed of, which one should it be?
|
26
|
*/
|
27
|
dataFrom: null,
|
28
|
|
29
|
/**
|
30
|
* APIProperty: styleFrom
|
31
|
* {<OpenLayers.Layer.PointTrack.TARGET_NODE>} or
|
32
|
* {<OpenLayers.Layer.PointTrack.SOURCE_NODE>} optional. If the lines
|
33
|
* should get the style from one of the two points it is composed of,
|
34
|
* which one should it be?
|
35
|
*/
|
36
|
styleFrom: null,
|
37
|
|
38
|
/**
|
39
|
* Constructor: OpenLayers.PointTrack
|
40
|
* Constructor for a new OpenLayers.PointTrack instance.
|
41
|
*
|
42
|
* Parameters:
|
43
|
* name - {String} name of the layer
|
44
|
* options - {Object} Optional object with properties to tag onto the
|
45
|
* instance.
|
46
|
*/
|
47
|
|
48
|
/**
|
49
|
* APIMethod: addNodes
|
50
|
* Adds point features that will be used to create lines from, using point
|
51
|
* pairs. The first point of a pair will be the source node, the second
|
52
|
* will be the target node.
|
53
|
*
|
54
|
* Parameters:
|
55
|
* pointFeatures - {Array(<OpenLayers.Feature>)}
|
56
|
* options - {Object}
|
57
|
*
|
58
|
* Supported options:
|
59
|
* silent - {Boolean} true to suppress (before)feature(s)added events
|
60
|
*/
|
61
|
addNodes: function(pointFeatures, options) {
|
62
|
if (pointFeatures.length < 2) {
|
63
|
throw new Error("At least two point features have to be added to " +
|
64
|
"create a line from");
|
65
|
}
|
66
|
|
67
|
var lines = new Array(pointFeatures.length-1);
|
68
|
|
69
|
var pointFeature, startPoint, endPoint;
|
70
|
for(var i=0, len=pointFeatures.length; i<len; i++) {
|
71
|
pointFeature = pointFeatures[i];
|
72
|
endPoint = pointFeature.geometry;
|
73
|
|
74
|
if (!endPoint) {
|
75
|
var lonlat = pointFeature.lonlat;
|
76
|
endPoint = new OpenLayers.Geometry.Point(lonlat.lon, lonlat.lat);
|
77
|
} else if(endPoint.CLASS_NAME != "OpenLayers.Geometry.Point") {
|
78
|
throw new TypeError("Only features with point geometries are supported.");
|
79
|
}
|
80
|
|
81
|
if(i > 0) {
|
82
|
var attributes = (this.dataFrom != null) ?
|
83
|
(pointFeatures[i+this.dataFrom].data ||
|
84
|
pointFeatures[i+this.dataFrom].attributes) :
|
85
|
null;
|
86
|
var style = (this.styleFrom != null) ?
|
87
|
(pointFeatures[i+this.styleFrom].style) :
|
88
|
null;
|
89
|
var line = new OpenLayers.Geometry.LineString([startPoint,
|
90
|
endPoint]);
|
91
|
|
92
|
lines[i-1] = new OpenLayers.Feature.Vector(line, attributes,
|
93
|
style);
|
94
|
}
|
95
|
|
96
|
startPoint = endPoint;
|
97
|
}
|
98
|
|
99
|
this.addFeatures(lines, options);
|
100
|
},
|
101
|
|
102
|
CLASS_NAME: "OpenLayers.Layer.PointTrack"
|
103
|
});
|
104
|
|
105
|
/**
|
106
|
* Constant: OpenLayers.Layer.PointTrack.SOURCE_NODE
|
107
|
* {Number} value for <OpenLayers.Layer.PointTrack.dataFrom> and
|
108
|
* <OpenLayers.Layer.PointTrack.styleFrom>
|
109
|
*/
|
110
|
OpenLayers.Layer.PointTrack.SOURCE_NODE = -1;
|
111
|
|
112
|
/**
|
113
|
* Constant: OpenLayers.Layer.PointTrack.TARGET_NODE
|
114
|
* {Number} value for <OpenLayers.Layer.PointTrack.dataFrom> and
|
115
|
* <OpenLayers.Layer.PointTrack.styleFrom>
|
116
|
*/
|
117
|
OpenLayers.Layer.PointTrack.TARGET_NODE = 0;
|
118
|
|
119
|
/**
|
120
|
* Constant: OpenLayers.Layer.PointTrack.dataFrom
|
121
|
* {Object} with the following keys - *deprecated*
|
122
|
* - SOURCE_NODE: take data/attributes from the source node of the line
|
123
|
* - TARGET_NODE: take data/attributes from the target node of the line
|
124
|
*/
|
125
|
OpenLayers.Layer.PointTrack.dataFrom = {'SOURCE_NODE': -1, 'TARGET_NODE': 0};
|