PointTrack.js 4.6 KB

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