Curve.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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/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. /**
  32. * APIMethod: getLength
  33. *
  34. * Returns:
  35. * {Float} The length of the curve
  36. */
  37. getLength: function() {
  38. var length = 0.0;
  39. if ( this.components && (this.components.length > 1)) {
  40. for(var i=1, len=this.components.length; i<len; i++) {
  41. length += this.components[i-1].distanceTo(this.components[i]);
  42. }
  43. }
  44. return length;
  45. },
  46. /**
  47. * APIMethod: getGeodesicLength
  48. * Calculate the approximate length of the geometry were it projected onto
  49. * the earth.
  50. *
  51. * projection - {<OpenLayers.Projection>} The spatial reference system
  52. * for the geometry coordinates. If not provided, Geographic/WGS84 is
  53. * assumed.
  54. *
  55. * Returns:
  56. * {Float} The appoximate geodesic length of the geometry in meters.
  57. */
  58. getGeodesicLength: function(projection) {
  59. var geom = this; // so we can work with a clone if needed
  60. if(projection) {
  61. var gg = new OpenLayers.Projection("EPSG:4326");
  62. if(!gg.equals(projection)) {
  63. geom = this.clone().transform(projection, gg);
  64. }
  65. }
  66. var length = 0.0;
  67. if(geom.components && (geom.components.length > 1)) {
  68. var p1, p2;
  69. for(var i=1, len=geom.components.length; i<len; i++) {
  70. p1 = geom.components[i-1];
  71. p2 = geom.components[i];
  72. // this returns km and requires lon/lat properties
  73. length += OpenLayers.Util.distVincenty(
  74. {lon: p1.x, lat: p1.y}, {lon: p2.x, lat: p2.y}
  75. );
  76. }
  77. }
  78. // convert to m
  79. return length * 1000;
  80. },
  81. CLASS_NAME: "OpenLayers.Geometry.Curve"
  82. });