PointTrack.js 4.4 KB

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