index.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // index.ts
  2. import { area } from "@turf/area";
  3. import { bbox } from "@turf/bbox";
  4. import { bboxPolygon } from "@turf/bbox-polygon";
  5. import { centroid } from "@turf/centroid";
  6. import { distance } from "@turf/distance";
  7. import { nearestPoint } from "@turf/nearest-point";
  8. import { featureEach } from "@turf/meta";
  9. import {
  10. convertArea,
  11. featureCollection
  12. } from "@turf/helpers";
  13. function nearestNeighborAnalysis(dataset, options) {
  14. options = options || {};
  15. const studyArea = options.studyArea || bboxPolygon(bbox(dataset));
  16. const properties = options.properties || {};
  17. const units = options.units || "kilometers";
  18. const features = [];
  19. featureEach(dataset, (feature) => {
  20. features.push(centroid(feature));
  21. });
  22. const n = features.length;
  23. const observedMeanDistance = features.map((feature, index) => {
  24. const otherFeatures = featureCollection(
  25. features.filter((f, i) => {
  26. return i !== index;
  27. })
  28. );
  29. return distance(
  30. feature,
  31. nearestPoint(feature, otherFeatures).geometry.coordinates,
  32. { units }
  33. );
  34. }).reduce((sum, value) => {
  35. return sum + value;
  36. }, 0) / n;
  37. const populationDensity = n / convertArea(area(studyArea), "meters", units);
  38. const expectedMeanDistance = 1 / (2 * Math.sqrt(populationDensity));
  39. const variance = 0.26136 / Math.sqrt(n * populationDensity);
  40. properties.nearestNeighborAnalysis = {
  41. units,
  42. arealUnits: units + "\xB2",
  43. observedMeanDistance,
  44. expectedMeanDistance,
  45. nearestNeighborIndex: observedMeanDistance / expectedMeanDistance,
  46. numberOfPoints: n,
  47. zScore: (observedMeanDistance - expectedMeanDistance) / variance
  48. };
  49. studyArea.properties = properties;
  50. return studyArea;
  51. }
  52. var turf_nearest_neighbor_analysis_default = nearestNeighborAnalysis;
  53. export {
  54. turf_nearest_neighbor_analysis_default as default,
  55. nearestNeighborAnalysis
  56. };
  57. //# sourceMappingURL=index.js.map