GeometryUtil.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /**
  2. * @class L.GeometryUtil
  3. * @aka GeometryUtil
  4. */
  5. L.GeometryUtil = L.extend(L.GeometryUtil || {}, {
  6. // Ported from the OpenLayers implementation. See https://github.com/openlayers/openlayers/blob/master/lib/OpenLayers/Geometry/LinearRing.js#L270
  7. // @method geodesicArea(): number
  8. geodesicArea: function (latLngs) {
  9. var pointsCount = latLngs.length,
  10. area = 0.0,
  11. d2r = Math.PI / 180,
  12. p1, p2;
  13. if (pointsCount > 2) {
  14. for (var i = 0; i < pointsCount; i++) {
  15. p1 = latLngs[i];
  16. p2 = latLngs[(i + 1) % pointsCount];
  17. area += ((p2.lng - p1.lng) * d2r) *
  18. (2 + Math.sin(p1.lat * d2r) + Math.sin(p2.lat * d2r));
  19. }
  20. area = area * 6378137.0 * 6378137.0 / 2.0;
  21. }
  22. return Math.abs(area);
  23. },
  24. // @method formattedNumber(n, precision): string
  25. // Returns n in specified number format (if defined) and precision
  26. formattedNumber: function (n, precision) {
  27. var formatted = n.toFixed(precision);
  28. var format = L.drawLocal.format && L.drawLocal.format.numeric,
  29. delimiters = format && format.delimiters,
  30. thousands = delimiters && delimiters.thousands,
  31. decimal = delimiters && delimiters.decimal;
  32. if (thousands || decimal) {
  33. var splitValue = formatted.split('.');
  34. formatted = thousands ? splitValue[0].replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1' + thousands) : splitValue[0];
  35. decimal = decimal || '.';
  36. if (splitValue.length > 1) {
  37. formatted = formatted + decimal + splitValue[1];
  38. }
  39. }
  40. return formatted;
  41. },
  42. // @method readableArea(area, isMetric): string
  43. // Returns a readable area string in yards or metric
  44. readableArea: function (area, isMetric) {
  45. var areaStr;
  46. if (isMetric) {
  47. if (area >= 10000) {
  48. areaStr = L.GeometryUtil.formattedNumber(area * 0.0001, 2) + ' ha';
  49. } else {
  50. areaStr = L.GeometryUtil.formattedNumber(area, 2) + ' m&sup2;';
  51. }
  52. } else {
  53. area /= 0.836127; // Square yards in 1 meter
  54. if (area >= 3097600) { //3097600 square yards in 1 square mile
  55. areaStr = L.GeometryUtil.formattedNumber(area / 3097600, 2) + ' mi&sup2;';
  56. } else if (area >= 4840) {//48040 square yards in 1 acre
  57. areaStr = L.GeometryUtil.formattedNumber(area / 4840, 2) + ' acres';
  58. } else {
  59. areaStr = L.GeometryUtil.formattedNumber(Math.ceil(area)) + ' yd&sup2;';
  60. }
  61. }
  62. return areaStr;
  63. },
  64. // @method readableDistance(distance, units): string
  65. // Converts a metric distance to one of [ feet, nauticalMile, metric or yards ] string
  66. //
  67. // @alternative
  68. // @method readableDistance(distance, isMetric, useFeet, isNauticalMile): string
  69. // Converts metric distance to distance string.
  70. readableDistance: function (distance, isMetric, isFeet, isNauticalMile) {
  71. var distanceStr,
  72. units;
  73. if (typeof isMetric == "string") {
  74. units = isMetric;
  75. } else {
  76. if (isFeet) {
  77. units = 'feet';
  78. } else if (isNauticalMile) {
  79. units = 'nauticalMile';
  80. } else if (isMetric) {
  81. units = 'metric';
  82. } else {
  83. units = 'yards';
  84. }
  85. }
  86. switch (units) {
  87. case 'metric':
  88. // show metres when distance is < 1km, then show km
  89. if (distance > 1000) {
  90. distanceStr = L.GeometryUtil.formattedNumber(distance / 1000, 2) + ' km';
  91. } else {
  92. distanceStr = L.GeometryUtil.formattedNumber(Math.ceil(distance)) + ' m';
  93. }
  94. break;
  95. case 'feet':
  96. distance *= 1.09361 * 3;
  97. distanceStr = L.GeometryUtil.formattedNumber(Math.ceil(distance)) + ' ft';
  98. break;
  99. case 'nauticalMile':
  100. distance *= 0.53996;
  101. distanceStr = L.GeometryUtil.formattedNumber(distance / 1000, 2) + ' nm';
  102. break;
  103. case 'yards':
  104. default:
  105. distance *= 1.09361;
  106. if (distance > 1760) {
  107. distanceStr = L.GeometryUtil.formattedNumber(distance / 1760, 2) + ' miles';
  108. } else {
  109. distanceStr = L.GeometryUtil.formattedNumber(Math.ceil(distance)) + ' yd';
  110. }
  111. break;
  112. }
  113. return distanceStr;
  114. }
  115. });