Curve.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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/Geometry/MultiPoint.js
  7. */
  8. /**
  9. * Class: OpenLayers.Geometry.Curve
  10. * A Curve is a MultiPoint, whose points are assumed to be connected. To
  11. * this end, we provide a "getLength()" function, which iterates through
  12. * the points, summing the distances between them.
  13. *
  14. * Inherits:
  15. * - <OpenLayers.Geometry.MultiPoint>
  16. */
  17. OpenLayers.Geometry.Curve = OpenLayers.Class(OpenLayers.Geometry.MultiPoint, {
  18. /**
  19. * Property: componentTypes
  20. * {Array(String)} An array of class names representing the types of
  21. * components that the collection can include. A null
  22. * value means the component types are not restricted.
  23. */
  24. componentTypes: ["OpenLayers.Geometry.Point"],
  25. /**
  26. * Constructor: OpenLayers.Geometry.Curve
  27. *
  28. * Parameters:
  29. * point - {Array(<OpenLayers.Geometry.Point>)}
  30. */
  31. initialize: function(points) {
  32. OpenLayers.Geometry.MultiPoint.prototype.initialize.apply(this,
  33. arguments);
  34. },
  35. /**
  36. * APIMethod: getLength
  37. *
  38. * Returns:
  39. * {Float} The length of the curve
  40. */
  41. getLength: function() {
  42. var length = 0.0;
  43. if ( this.components && (this.components.length > 1)) {
  44. for(var i=1, len=this.components.length; i<len; i++) {
  45. length += this.components[i-1].distanceTo(this.components[i]);
  46. }
  47. }
  48. return length;
  49. },
  50. /**
  51. * APIMethod: getGeodesicLength
  52. * Calculate the approximate length of the geometry were it projected onto
  53. * the earth.
  54. *
  55. * projection - {<OpenLayers.Projection>} The spatial reference system
  56. * for the geometry coordinates. If not provided, Geographic/WGS84 is
  57. * assumed.
  58. *
  59. * Returns:
  60. * {Float} The appoximate geodesic length of the geometry in meters.
  61. */
  62. getGeodesicLength: function(projection) {
  63. var geom = this; // so we can work with a clone if needed
  64. if(projection) {
  65. var gg = new OpenLayers.Projection("EPSG:4326");
  66. if(!gg.equals(projection)) {
  67. geom = this.clone().transform(projection, gg);
  68. }
  69. }
  70. var length = 0.0;
  71. if(geom.components && (geom.components.length > 1)) {
  72. var p1, p2;
  73. for(var i=1, len=geom.components.length; i<len; i++) {
  74. p1 = geom.components[i-1];
  75. p2 = geom.components[i];
  76. // this returns km and requires lon/lat properties
  77. length += OpenLayers.Util.distVincenty(
  78. {lon: p1.x, lat: p1.y}, {lon: p2.x, lat: p2.y}
  79. );
  80. }
  81. }
  82. // convert to m
  83. return length * 1000;
  84. },
  85. CLASS_NAME: "OpenLayers.Geometry.Curve"
  86. });