index.cjs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. "use strict";Object.defineProperty(exports, "__esModule", {value: true});// index.ts
  2. var _meta = require('@turf/meta');
  3. var _invariant = require('@turf/invariant');
  4. var _helpers = require('@turf/helpers');
  5. var _centermean = require('@turf/center-mean');
  6. var _pointswithinpolygon = require('@turf/points-within-polygon');
  7. var _ellipse = require('@turf/ellipse');
  8. function standardDeviationalEllipse(points, options) {
  9. var _a;
  10. options = options || {};
  11. if (!_helpers.isObject.call(void 0, options)) throw new Error("options is invalid");
  12. const steps = options.steps || 64;
  13. const weightTerm = options.weight;
  14. const properties = options.properties || {};
  15. if (!_helpers.isNumber.call(void 0, steps)) throw new Error("steps must be a number");
  16. if (!_helpers.isObject.call(void 0, properties)) throw new Error("properties must be a number");
  17. const numberOfFeatures = _meta.coordAll.call(void 0, points).length;
  18. const meanCenter = _centermean.centerMean.call(void 0, points, { weight: weightTerm });
  19. let xDeviationSquaredSum = 0;
  20. let yDeviationSquaredSum = 0;
  21. let xyDeviationSum = 0;
  22. _meta.featureEach.call(void 0, points, function(point) {
  23. var _a2;
  24. const weight = weightTerm ? ((_a2 = point.properties) == null ? void 0 : _a2[weightTerm]) || 1 : 1;
  25. const deviation = getDeviations(_invariant.getCoords.call(void 0, point), _invariant.getCoords.call(void 0, meanCenter));
  26. xDeviationSquaredSum += Math.pow(deviation.x, 2) * weight;
  27. yDeviationSquaredSum += Math.pow(deviation.y, 2) * weight;
  28. xyDeviationSum += deviation.x * deviation.y * weight;
  29. });
  30. const bigA = xDeviationSquaredSum - yDeviationSquaredSum;
  31. const bigB = Math.sqrt(Math.pow(bigA, 2) + 4 * Math.pow(xyDeviationSum, 2));
  32. const bigC = 2 * xyDeviationSum;
  33. const theta = Math.atan((bigA + bigB) / bigC);
  34. const thetaDeg = theta * 180 / Math.PI;
  35. let sigmaXsum = 0;
  36. let sigmaYsum = 0;
  37. let weightsum = 0;
  38. _meta.featureEach.call(void 0, points, function(point) {
  39. var _a2;
  40. const weight = weightTerm ? ((_a2 = point.properties) == null ? void 0 : _a2[weightTerm]) || 1 : 1;
  41. const deviation = getDeviations(_invariant.getCoords.call(void 0, point), _invariant.getCoords.call(void 0, meanCenter));
  42. sigmaXsum += Math.pow(
  43. deviation.x * Math.cos(theta) - deviation.y * Math.sin(theta),
  44. 2
  45. ) * weight;
  46. sigmaYsum += Math.pow(
  47. deviation.x * Math.sin(theta) + deviation.y * Math.cos(theta),
  48. 2
  49. ) * weight;
  50. weightsum += weight;
  51. });
  52. const sigmaX = Math.sqrt(2 * sigmaXsum / weightsum);
  53. const sigmaY = Math.sqrt(2 * sigmaYsum / weightsum);
  54. const theEllipse = _ellipse.ellipse.call(void 0, meanCenter, sigmaX, sigmaY, {
  55. units: "degrees",
  56. angle: thetaDeg,
  57. steps,
  58. properties
  59. });
  60. const pointsWithinEllipse = _pointswithinpolygon.pointsWithinPolygon.call(void 0,
  61. points,
  62. _helpers.featureCollection.call(void 0, [theEllipse])
  63. );
  64. const standardDeviationalEllipseProperties = {
  65. meanCenterCoordinates: _invariant.getCoords.call(void 0, meanCenter),
  66. semiMajorAxis: sigmaX,
  67. semiMinorAxis: sigmaY,
  68. numberOfFeatures,
  69. angle: thetaDeg,
  70. percentageWithinEllipse: 100 * _meta.coordAll.call(void 0, pointsWithinEllipse).length / numberOfFeatures
  71. };
  72. theEllipse.properties = (_a = theEllipse.properties) != null ? _a : {};
  73. theEllipse.properties.standardDeviationalEllipse = standardDeviationalEllipseProperties;
  74. return theEllipse;
  75. }
  76. function getDeviations(coordinates, center) {
  77. return {
  78. x: coordinates[0] - center[0],
  79. y: coordinates[1] - center[1]
  80. };
  81. }
  82. var turf_standard_deviational_ellipse_default = standardDeviationalEllipse;
  83. exports.default = turf_standard_deviational_ellipse_default; exports.standardDeviationalEllipse = standardDeviationalEllipse;
  84. //# sourceMappingURL=index.cjs.map