@turf/nearest-neighbor-analysis 7.0.0-alpha.1 → 7.0.0-alpha.111

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -83,26 +83,21 @@ Returns **[Feature][8]<[Polygon][9]>** A polygon of the study area or an approxi
83
83
 
84
84
  [10]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String
85
85
 
86
- <!-- This file is automatically generated. Please don't edit it directly:
87
- if you find an error, edit the source file (likely index.js), and re-run
88
- ./scripts/generate-readmes in the turf project. -->
86
+ <!-- This file is automatically generated. Please don't edit it directly. If you find an error, edit the source file of the module in question (likely index.js or index.ts), and re-run "yarn docs" from the root of the turf project. -->
89
87
 
90
88
  ---
91
89
 
92
- This module is part of the [Turfjs project](http://turfjs.org/), an open source
93
- module collection dedicated to geographic algorithms. It is maintained in the
94
- [Turfjs/turf](https://github.com/Turfjs/turf) repository, where you can create
95
- PRs and issues.
90
+ This module is part of the [Turfjs project](https://turfjs.org/), an open source module collection dedicated to geographic algorithms. It is maintained in the [Turfjs/turf](https://github.com/Turfjs/turf) repository, where you can create PRs and issues.
96
91
 
97
92
  ### Installation
98
93
 
99
- Install this module individually:
94
+ Install this single module individually:
100
95
 
101
96
  ```sh
102
97
  $ npm install @turf/nearest-neighbor-analysis
103
98
  ```
104
99
 
105
- Or install the Turf module that includes it as a function:
100
+ Or install the all-encompassing @turf/turf module that includes all modules as functions:
106
101
 
107
102
  ```sh
108
103
  $ npm install @turf/turf
@@ -0,0 +1,61 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});var __defProp = Object.defineProperty;
2
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
+
4
+ // index.ts
5
+ var _area = require('@turf/area');
6
+ var _bbox = require('@turf/bbox');
7
+ var _bboxpolygon = require('@turf/bbox-polygon');
8
+ var _centroid = require('@turf/centroid');
9
+ var _distance = require('@turf/distance');
10
+ var _nearestpoint = require('@turf/nearest-point');
11
+ var _meta = require('@turf/meta');
12
+
13
+
14
+
15
+ var _helpers = require('@turf/helpers');
16
+ function nearestNeighborAnalysis(dataset, options) {
17
+ options = options || {};
18
+ const studyArea = options.studyArea || _bboxpolygon.bboxPolygon.call(void 0, _bbox.bbox.call(void 0, dataset));
19
+ const properties = options.properties || {};
20
+ const units = options.units || "kilometers";
21
+ const features = [];
22
+ _meta.featureEach.call(void 0, dataset, (feature) => {
23
+ features.push(_centroid.centroid.call(void 0, feature));
24
+ });
25
+ const n = features.length;
26
+ const observedMeanDistance = features.map((feature, index) => {
27
+ const otherFeatures = _helpers.featureCollection.call(void 0,
28
+ features.filter((f, i) => {
29
+ return i !== index;
30
+ })
31
+ );
32
+ return _distance.distance.call(void 0,
33
+ feature,
34
+ _nearestpoint.nearestPoint.call(void 0, feature, otherFeatures).geometry.coordinates,
35
+ { units }
36
+ );
37
+ }).reduce((sum, value) => {
38
+ return sum + value;
39
+ }, 0) / n;
40
+ const populationDensity = n / _helpers.convertArea.call(void 0, _area.area.call(void 0, studyArea), "meters", units);
41
+ const expectedMeanDistance = 1 / (2 * Math.sqrt(populationDensity));
42
+ const variance = 0.26136 / Math.sqrt(n * populationDensity);
43
+ properties.nearestNeighborAnalysis = {
44
+ units,
45
+ arealUnits: units + "\xB2",
46
+ observedMeanDistance,
47
+ expectedMeanDistance,
48
+ nearestNeighborIndex: observedMeanDistance / expectedMeanDistance,
49
+ numberOfPoints: n,
50
+ zScore: (observedMeanDistance - expectedMeanDistance) / variance
51
+ };
52
+ studyArea.properties = properties;
53
+ return studyArea;
54
+ }
55
+ __name(nearestNeighborAnalysis, "nearestNeighborAnalysis");
56
+ var turf_nearest_neighbor_analysis_default = nearestNeighborAnalysis;
57
+
58
+
59
+
60
+ exports.default = turf_nearest_neighbor_analysis_default; exports.nearestNeighborAnalysis = nearestNeighborAnalysis;
61
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../index.ts"],"names":[],"mappings":";;;;AAOA,SAAS,YAAY;AACrB,SAAS,YAAY;AACrB,SAAS,mBAAmB;AAC5B,SAAS,gBAAgB;AACzB,SAAS,gBAAgB;AACzB,SAAS,oBAAoB;AAC7B,SAAS,mBAAmB;AAC5B;AAAA,EACE;AAAA,EACA;AAAA,OAGK;AAqEP,SAAS,wBACP,SACA,SAK0B;AAE1B,YAAU,WAAW,CAAC;AACtB,QAAM,YAAY,QAAQ,aAAa,YAAY,KAAK,OAAO,CAAC;AAChE,QAAM,aAAa,QAAQ,cAAc,CAAC;AAC1C,QAAM,QAAQ,QAAQ,SAAS;AAE/B,QAAM,WAAkC,CAAC;AACzC,cAAY,SAAS,CAAC,YAAY;AAChC,aAAS,KAAK,SAAS,OAAO,CAAC;AAAA,EACjC,CAAC;AACD,QAAM,IAAI,SAAS;AACnB,QAAM,uBACJ,SACG,IAAI,CAAC,SAAS,UAAU;AACvB,UAAM,gBAAgB;AAAA,MACpB,SAAS,OAAO,CAAC,GAAG,MAAM;AACxB,eAAO,MAAM;AAAA,MACf,CAAC;AAAA,IACH;AAGA,WAAO;AAAA,MACL;AAAA,MACA,aAAa,SAAS,aAAa,EAAE,SAAU;AAAA,MAC/C,EAAE,MAAM;AAAA,IACV;AAAA,EACF,CAAC,EACA,OAAO,CAAC,KAAK,UAAU;AACtB,WAAO,MAAM;AAAA,EACf,GAAG,CAAC,IAAI;AAEZ,QAAM,oBAAoB,IAAI,YAAY,KAAK,SAAS,GAAG,UAAU,KAAK;AAC1E,QAAM,uBAAuB,KAAK,IAAI,KAAK,KAAK,iBAAiB;AACjE,QAAM,WAAW,UAAU,KAAK,KAAK,IAAI,iBAAiB;AAC1D,aAAW,0BAA0B;AAAA,IACnC;AAAA,IACA,YAAY,QAAQ;AAAA,IACpB;AAAA,IACA;AAAA,IACA,sBAAsB,uBAAuB;AAAA,IAC7C,gBAAgB;AAAA,IAChB,SAAS,uBAAuB,wBAAwB;AAAA,EAC1D;AACA,YAAU,aAAa;AAEvB,SAAO;AACT;AAtDS;AA6DT,IAAO,yCAAQ","sourcesContent":["import {\n FeatureCollection,\n Feature,\n Point,\n Polygon,\n GeoJsonProperties,\n} from \"geojson\";\nimport { area } from \"@turf/area\";\nimport { bbox } from \"@turf/bbox\";\nimport { bboxPolygon } from \"@turf/bbox-polygon\";\nimport { centroid } from \"@turf/centroid\";\nimport { distance } from \"@turf/distance\";\nimport { nearestPoint } from \"@turf/nearest-point\";\nimport { featureEach } from \"@turf/meta\";\nimport {\n convertArea,\n featureCollection,\n Units,\n AreaUnits,\n} from \"@turf/helpers\";\n\ninterface NearestNeighborStatistics {\n units: Units & AreaUnits;\n arealUnits: string;\n observedMeanDistance: number;\n expectedMeanDistance: number;\n numberOfPoints: number;\n zScore: number;\n}\n\ninterface NearestNeighborStudyArea extends Feature<Polygon> {\n properties: {\n nearestNeighborAnalysis: NearestNeighborStatistics;\n [key: string]: any;\n };\n}\n\n/**\n * Nearest Neighbor Analysis calculates an index based the average distances\n * between points in the dataset, thereby providing inference as to whether the\n * data is clustered, dispersed, or randomly distributed within the study area.\n *\n * It returns a {@link Feature<Polygon>} of the study area, with the results of\n * the analysis attached as part of of the `nearestNeighborAnalysis` property\n * of the study area's `properties`. The attached\n * [_z_-score](https://en.wikipedia.org/wiki/Standard_score) indicates how many\n * standard deviations above or below the expected mean distance the data's\n * observed mean distance is. The more negative, the more clustered. The more\n * positive, the more evenly dispersed. A _z_-score between -2 and 2 indicates\n * a seemingly random distribution. That is, within _p_ of less than 0.05, the\n * distribution appears statistically significantly neither clustered nor\n * dispersed.\n *\n * **Remarks**\n *\n * - Though the analysis will work on any {@link FeatureCollection} type, it\n * works best with {@link Point} collections.\n *\n * - This analysis is _very_ sensitive to the study area provided.\n * If no {@link Feature<Polygon>} is passed as the study area, the function draws a box\n * around the data, which may distort the findings. This analysis works best\n * with a bounded area of interest within with the data is either clustered,\n * dispersed, or randomly distributed. For example, a city's subway stops may\n * look extremely clustered if the study area is an entire state. On the other\n * hand, they may look rather evenly dispersed if the study area is limited to\n * the city's downtown.\n *\n * **Bibliography**\n *\n * Philip J. Clark and Francis C. Evans, “Distance to Nearest Neighbor as a\n * Measure of Spatial Relationships in Populations,” _Ecology_ 35, no. 4\n * (1954): 445–453, doi:[10.2307/1931034](http://doi.org/10.2307/1931034).\n *\n * @name nearestNeighborAnalysis\n * @param {FeatureCollection<any>} dataset FeatureCollection (pref. of points) to study\n * @param {Object} [options={}] Optional parameters\n * @param {Feature<Polygon>} [options.studyArea] polygon representing the study area\n * @param {string} [options.units='kilometers'] unit of measurement for distances and, squared, area.\n * @param {Object} [options.properties={}] properties\n * @returns {Feature<Polygon>} A polygon of the study area or an approximation of one.\n * @example\n * var bbox = [-65, 40, -63, 42];\n * var dataset = turf.randomPoint(100, { bbox: bbox });\n * var nearestNeighborStudyArea = turf.nearestNeighborAnalysis(dataset);\n *\n * //addToMap\n * var addToMap = [dataset, nearestNeighborStudyArea];\n */\nfunction nearestNeighborAnalysis(\n dataset: FeatureCollection<any>,\n options?: {\n studyArea?: Feature<Polygon>;\n units?: Units & AreaUnits;\n properties?: GeoJsonProperties;\n }\n): NearestNeighborStudyArea {\n // Optional params\n options = options || {};\n const studyArea = options.studyArea || bboxPolygon(bbox(dataset));\n const properties = options.properties || {};\n const units = options.units || \"kilometers\";\n\n const features: Array<Feature<Point>> = [];\n featureEach(dataset, (feature) => {\n features.push(centroid(feature));\n });\n const n = features.length;\n const observedMeanDistance =\n features\n .map((feature, index) => {\n const otherFeatures = featureCollection<Point>(\n features.filter((f, i) => {\n return i !== index;\n })\n );\n // Have to add the ! to make typescript validation pass\n // see https://stackoverflow.com/a/40350534/1979085\n return distance(\n feature,\n nearestPoint(feature, otherFeatures).geometry!.coordinates,\n { units }\n );\n })\n .reduce((sum, value) => {\n return sum + value;\n }, 0) / n;\n\n const populationDensity = n / convertArea(area(studyArea), \"meters\", units);\n const expectedMeanDistance = 1 / (2 * Math.sqrt(populationDensity));\n const variance = 0.26136 / Math.sqrt(n * populationDensity);\n properties.nearestNeighborAnalysis = {\n units: units,\n arealUnits: units + \"²\",\n observedMeanDistance: observedMeanDistance,\n expectedMeanDistance: expectedMeanDistance,\n nearestNeighborIndex: observedMeanDistance / expectedMeanDistance,\n numberOfPoints: n,\n zScore: (observedMeanDistance - expectedMeanDistance) / variance,\n };\n studyArea.properties = properties;\n\n return studyArea as NearestNeighborStudyArea;\n}\n\nexport {\n nearestNeighborAnalysis,\n NearestNeighborStatistics,\n NearestNeighborStudyArea,\n};\nexport default nearestNeighborAnalysis;\n"]}
@@ -1,6 +1,7 @@
1
- import { FeatureCollection, Feature, Polygon, GeoJsonProperties } from "geojson";
2
- import { Units, AreaUnits } from "@turf/helpers";
3
- export interface NearestNeighborStatistics {
1
+ import { Feature, Polygon, FeatureCollection, GeoJsonProperties } from 'geojson';
2
+ import { Units, AreaUnits } from '@turf/helpers';
3
+
4
+ interface NearestNeighborStatistics {
4
5
  units: Units & AreaUnits;
5
6
  arealUnits: string;
6
7
  observedMeanDistance: number;
@@ -8,7 +9,7 @@ export interface NearestNeighborStatistics {
8
9
  numberOfPoints: number;
9
10
  zScore: number;
10
11
  }
11
- export interface NearestNeighborStudyArea extends Feature<Polygon> {
12
+ interface NearestNeighborStudyArea extends Feature<Polygon> {
12
13
  properties: {
13
14
  nearestNeighborAnalysis: NearestNeighborStatistics;
14
15
  [key: string]: any;
@@ -70,4 +71,5 @@ declare function nearestNeighborAnalysis(dataset: FeatureCollection<any>, option
70
71
  units?: Units & AreaUnits;
71
72
  properties?: GeoJsonProperties;
72
73
  }): NearestNeighborStudyArea;
73
- export default nearestNeighborAnalysis;
74
+
75
+ export { type NearestNeighborStatistics, type NearestNeighborStudyArea, nearestNeighborAnalysis as default, nearestNeighborAnalysis };
@@ -1,11 +1,20 @@
1
- import area from "@turf/area";
2
- import bbox from "@turf/bbox";
3
- import bboxPolygon from "@turf/bbox-polygon";
4
- import centroid from "@turf/centroid";
5
- import distance from "@turf/distance";
6
- import nearestPoint from "@turf/nearest-point";
7
- import { featureEach } from "@turf/meta";
8
- import { convertArea, featureCollection, } from "@turf/helpers";
1
+ import { Feature, Polygon, FeatureCollection, GeoJsonProperties } from 'geojson';
2
+ import { Units, AreaUnits } from '@turf/helpers';
3
+
4
+ interface NearestNeighborStatistics {
5
+ units: Units & AreaUnits;
6
+ arealUnits: string;
7
+ observedMeanDistance: number;
8
+ expectedMeanDistance: number;
9
+ numberOfPoints: number;
10
+ zScore: number;
11
+ }
12
+ interface NearestNeighborStudyArea extends Feature<Polygon> {
13
+ properties: {
14
+ nearestNeighborAnalysis: NearestNeighborStatistics;
15
+ [key: string]: any;
16
+ };
17
+ }
9
18
  /**
10
19
  * Nearest Neighbor Analysis calculates an index based the average distances
11
20
  * between points in the dataset, thereby providing inference as to whether the
@@ -57,42 +66,10 @@ import { convertArea, featureCollection, } from "@turf/helpers";
57
66
  * //addToMap
58
67
  * var addToMap = [dataset, nearestNeighborStudyArea];
59
68
  */
60
- function nearestNeighborAnalysis(dataset, options) {
61
- // Optional params
62
- options = options || {};
63
- const studyArea = options.studyArea || bboxPolygon(bbox(dataset));
64
- const properties = options.properties || {};
65
- const units = options.units || "kilometers";
66
- const features = [];
67
- featureEach(dataset, (feature) => {
68
- features.push(centroid(feature));
69
- });
70
- const n = features.length;
71
- const observedMeanDistance = features
72
- .map((feature, index) => {
73
- const otherFeatures = featureCollection(features.filter((f, i) => {
74
- return i !== index;
75
- }));
76
- // Have to add the ! to make typescript validation pass
77
- // see https://stackoverflow.com/a/40350534/1979085
78
- return distance(feature, nearestPoint(feature, otherFeatures).geometry.coordinates, { units });
79
- })
80
- .reduce((sum, value) => {
81
- return sum + value;
82
- }, 0) / n;
83
- const populationDensity = n / convertArea(area(studyArea), "meters", units);
84
- const expectedMeanDistance = 1 / (2 * Math.sqrt(populationDensity));
85
- const variance = 0.26136 / Math.sqrt(n * populationDensity);
86
- properties.nearestNeighborAnalysis = {
87
- units: units,
88
- arealUnits: units + "²",
89
- observedMeanDistance: observedMeanDistance,
90
- expectedMeanDistance: expectedMeanDistance,
91
- nearestNeighborIndex: observedMeanDistance / expectedMeanDistance,
92
- numberOfPoints: n,
93
- zScore: (observedMeanDistance - expectedMeanDistance) / variance,
94
- };
95
- studyArea.properties = properties;
96
- return studyArea;
97
- }
98
- export default nearestNeighborAnalysis;
69
+ declare function nearestNeighborAnalysis(dataset: FeatureCollection<any>, options?: {
70
+ studyArea?: Feature<Polygon>;
71
+ units?: Units & AreaUnits;
72
+ properties?: GeoJsonProperties;
73
+ }): NearestNeighborStudyArea;
74
+
75
+ export { type NearestNeighborStatistics, type NearestNeighborStudyArea, nearestNeighborAnalysis as default, nearestNeighborAnalysis };
@@ -0,0 +1,61 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
+
4
+ // index.ts
5
+ import { area } from "@turf/area";
6
+ import { bbox } from "@turf/bbox";
7
+ import { bboxPolygon } from "@turf/bbox-polygon";
8
+ import { centroid } from "@turf/centroid";
9
+ import { distance } from "@turf/distance";
10
+ import { nearestPoint } from "@turf/nearest-point";
11
+ import { featureEach } from "@turf/meta";
12
+ import {
13
+ convertArea,
14
+ featureCollection
15
+ } from "@turf/helpers";
16
+ function nearestNeighborAnalysis(dataset, options) {
17
+ options = options || {};
18
+ const studyArea = options.studyArea || bboxPolygon(bbox(dataset));
19
+ const properties = options.properties || {};
20
+ const units = options.units || "kilometers";
21
+ const features = [];
22
+ featureEach(dataset, (feature) => {
23
+ features.push(centroid(feature));
24
+ });
25
+ const n = features.length;
26
+ const observedMeanDistance = features.map((feature, index) => {
27
+ const otherFeatures = featureCollection(
28
+ features.filter((f, i) => {
29
+ return i !== index;
30
+ })
31
+ );
32
+ return distance(
33
+ feature,
34
+ nearestPoint(feature, otherFeatures).geometry.coordinates,
35
+ { units }
36
+ );
37
+ }).reduce((sum, value) => {
38
+ return sum + value;
39
+ }, 0) / n;
40
+ const populationDensity = n / convertArea(area(studyArea), "meters", units);
41
+ const expectedMeanDistance = 1 / (2 * Math.sqrt(populationDensity));
42
+ const variance = 0.26136 / Math.sqrt(n * populationDensity);
43
+ properties.nearestNeighborAnalysis = {
44
+ units,
45
+ arealUnits: units + "\xB2",
46
+ observedMeanDistance,
47
+ expectedMeanDistance,
48
+ nearestNeighborIndex: observedMeanDistance / expectedMeanDistance,
49
+ numberOfPoints: n,
50
+ zScore: (observedMeanDistance - expectedMeanDistance) / variance
51
+ };
52
+ studyArea.properties = properties;
53
+ return studyArea;
54
+ }
55
+ __name(nearestNeighborAnalysis, "nearestNeighborAnalysis");
56
+ var turf_nearest_neighbor_analysis_default = nearestNeighborAnalysis;
57
+ export {
58
+ turf_nearest_neighbor_analysis_default as default,
59
+ nearestNeighborAnalysis
60
+ };
61
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../index.ts"],"sourcesContent":["import {\n FeatureCollection,\n Feature,\n Point,\n Polygon,\n GeoJsonProperties,\n} from \"geojson\";\nimport { area } from \"@turf/area\";\nimport { bbox } from \"@turf/bbox\";\nimport { bboxPolygon } from \"@turf/bbox-polygon\";\nimport { centroid } from \"@turf/centroid\";\nimport { distance } from \"@turf/distance\";\nimport { nearestPoint } from \"@turf/nearest-point\";\nimport { featureEach } from \"@turf/meta\";\nimport {\n convertArea,\n featureCollection,\n Units,\n AreaUnits,\n} from \"@turf/helpers\";\n\ninterface NearestNeighborStatistics {\n units: Units & AreaUnits;\n arealUnits: string;\n observedMeanDistance: number;\n expectedMeanDistance: number;\n numberOfPoints: number;\n zScore: number;\n}\n\ninterface NearestNeighborStudyArea extends Feature<Polygon> {\n properties: {\n nearestNeighborAnalysis: NearestNeighborStatistics;\n [key: string]: any;\n };\n}\n\n/**\n * Nearest Neighbor Analysis calculates an index based the average distances\n * between points in the dataset, thereby providing inference as to whether the\n * data is clustered, dispersed, or randomly distributed within the study area.\n *\n * It returns a {@link Feature<Polygon>} of the study area, with the results of\n * the analysis attached as part of of the `nearestNeighborAnalysis` property\n * of the study area's `properties`. The attached\n * [_z_-score](https://en.wikipedia.org/wiki/Standard_score) indicates how many\n * standard deviations above or below the expected mean distance the data's\n * observed mean distance is. The more negative, the more clustered. The more\n * positive, the more evenly dispersed. A _z_-score between -2 and 2 indicates\n * a seemingly random distribution. That is, within _p_ of less than 0.05, the\n * distribution appears statistically significantly neither clustered nor\n * dispersed.\n *\n * **Remarks**\n *\n * - Though the analysis will work on any {@link FeatureCollection} type, it\n * works best with {@link Point} collections.\n *\n * - This analysis is _very_ sensitive to the study area provided.\n * If no {@link Feature<Polygon>} is passed as the study area, the function draws a box\n * around the data, which may distort the findings. This analysis works best\n * with a bounded area of interest within with the data is either clustered,\n * dispersed, or randomly distributed. For example, a city's subway stops may\n * look extremely clustered if the study area is an entire state. On the other\n * hand, they may look rather evenly dispersed if the study area is limited to\n * the city's downtown.\n *\n * **Bibliography**\n *\n * Philip J. Clark and Francis C. Evans, “Distance to Nearest Neighbor as a\n * Measure of Spatial Relationships in Populations,” _Ecology_ 35, no. 4\n * (1954): 445–453, doi:[10.2307/1931034](http://doi.org/10.2307/1931034).\n *\n * @name nearestNeighborAnalysis\n * @param {FeatureCollection<any>} dataset FeatureCollection (pref. of points) to study\n * @param {Object} [options={}] Optional parameters\n * @param {Feature<Polygon>} [options.studyArea] polygon representing the study area\n * @param {string} [options.units='kilometers'] unit of measurement for distances and, squared, area.\n * @param {Object} [options.properties={}] properties\n * @returns {Feature<Polygon>} A polygon of the study area or an approximation of one.\n * @example\n * var bbox = [-65, 40, -63, 42];\n * var dataset = turf.randomPoint(100, { bbox: bbox });\n * var nearestNeighborStudyArea = turf.nearestNeighborAnalysis(dataset);\n *\n * //addToMap\n * var addToMap = [dataset, nearestNeighborStudyArea];\n */\nfunction nearestNeighborAnalysis(\n dataset: FeatureCollection<any>,\n options?: {\n studyArea?: Feature<Polygon>;\n units?: Units & AreaUnits;\n properties?: GeoJsonProperties;\n }\n): NearestNeighborStudyArea {\n // Optional params\n options = options || {};\n const studyArea = options.studyArea || bboxPolygon(bbox(dataset));\n const properties = options.properties || {};\n const units = options.units || \"kilometers\";\n\n const features: Array<Feature<Point>> = [];\n featureEach(dataset, (feature) => {\n features.push(centroid(feature));\n });\n const n = features.length;\n const observedMeanDistance =\n features\n .map((feature, index) => {\n const otherFeatures = featureCollection<Point>(\n features.filter((f, i) => {\n return i !== index;\n })\n );\n // Have to add the ! to make typescript validation pass\n // see https://stackoverflow.com/a/40350534/1979085\n return distance(\n feature,\n nearestPoint(feature, otherFeatures).geometry!.coordinates,\n { units }\n );\n })\n .reduce((sum, value) => {\n return sum + value;\n }, 0) / n;\n\n const populationDensity = n / convertArea(area(studyArea), \"meters\", units);\n const expectedMeanDistance = 1 / (2 * Math.sqrt(populationDensity));\n const variance = 0.26136 / Math.sqrt(n * populationDensity);\n properties.nearestNeighborAnalysis = {\n units: units,\n arealUnits: units + \"²\",\n observedMeanDistance: observedMeanDistance,\n expectedMeanDistance: expectedMeanDistance,\n nearestNeighborIndex: observedMeanDistance / expectedMeanDistance,\n numberOfPoints: n,\n zScore: (observedMeanDistance - expectedMeanDistance) / variance,\n };\n studyArea.properties = properties;\n\n return studyArea as NearestNeighborStudyArea;\n}\n\nexport {\n nearestNeighborAnalysis,\n NearestNeighborStatistics,\n NearestNeighborStudyArea,\n};\nexport default nearestNeighborAnalysis;\n"],"mappings":";;;;AAOA,SAAS,YAAY;AACrB,SAAS,YAAY;AACrB,SAAS,mBAAmB;AAC5B,SAAS,gBAAgB;AACzB,SAAS,gBAAgB;AACzB,SAAS,oBAAoB;AAC7B,SAAS,mBAAmB;AAC5B;AAAA,EACE;AAAA,EACA;AAAA,OAGK;AAqEP,SAAS,wBACP,SACA,SAK0B;AAE1B,YAAU,WAAW,CAAC;AACtB,QAAM,YAAY,QAAQ,aAAa,YAAY,KAAK,OAAO,CAAC;AAChE,QAAM,aAAa,QAAQ,cAAc,CAAC;AAC1C,QAAM,QAAQ,QAAQ,SAAS;AAE/B,QAAM,WAAkC,CAAC;AACzC,cAAY,SAAS,CAAC,YAAY;AAChC,aAAS,KAAK,SAAS,OAAO,CAAC;AAAA,EACjC,CAAC;AACD,QAAM,IAAI,SAAS;AACnB,QAAM,uBACJ,SACG,IAAI,CAAC,SAAS,UAAU;AACvB,UAAM,gBAAgB;AAAA,MACpB,SAAS,OAAO,CAAC,GAAG,MAAM;AACxB,eAAO,MAAM;AAAA,MACf,CAAC;AAAA,IACH;AAGA,WAAO;AAAA,MACL;AAAA,MACA,aAAa,SAAS,aAAa,EAAE,SAAU;AAAA,MAC/C,EAAE,MAAM;AAAA,IACV;AAAA,EACF,CAAC,EACA,OAAO,CAAC,KAAK,UAAU;AACtB,WAAO,MAAM;AAAA,EACf,GAAG,CAAC,IAAI;AAEZ,QAAM,oBAAoB,IAAI,YAAY,KAAK,SAAS,GAAG,UAAU,KAAK;AAC1E,QAAM,uBAAuB,KAAK,IAAI,KAAK,KAAK,iBAAiB;AACjE,QAAM,WAAW,UAAU,KAAK,KAAK,IAAI,iBAAiB;AAC1D,aAAW,0BAA0B;AAAA,IACnC;AAAA,IACA,YAAY,QAAQ;AAAA,IACpB;AAAA,IACA;AAAA,IACA,sBAAsB,uBAAuB;AAAA,IAC7C,gBAAgB;AAAA,IAChB,SAAS,uBAAuB,wBAAwB;AAAA,EAC1D;AACA,YAAU,aAAa;AAEvB,SAAO;AACT;AAtDS;AA6DT,IAAO,yCAAQ;","names":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@turf/nearest-neighbor-analysis",
3
- "version": "7.0.0-alpha.1",
3
+ "version": "7.0.0-alpha.111+08576cb50",
4
4
  "description": "turf nearest-neighbor-analysis module",
5
5
  "author": "Turf Authors",
6
6
  "contributors": [
@@ -23,52 +23,57 @@
23
23
  "turf",
24
24
  "nearest-neighbor"
25
25
  ],
26
- "main": "dist/js/index.js",
27
- "module": "dist/es/index.js",
26
+ "type": "commonjs",
27
+ "main": "dist/cjs/index.cjs",
28
+ "module": "dist/esm/index.mjs",
29
+ "types": "dist/cjs/index.d.ts",
28
30
  "exports": {
29
31
  "./package.json": "./package.json",
30
32
  ".": {
31
- "types": "./dist/js/index.d.ts",
32
- "import": "./dist/es/index.js",
33
- "require": "./dist/js/index.js"
33
+ "import": {
34
+ "types": "./dist/esm/index.d.mts",
35
+ "default": "./dist/esm/index.mjs"
36
+ },
37
+ "require": {
38
+ "types": "./dist/cjs/index.d.ts",
39
+ "default": "./dist/cjs/index.cjs"
40
+ }
34
41
  }
35
42
  },
36
- "types": "dist/js/index.d.ts",
37
43
  "sideEffects": false,
38
44
  "files": [
39
45
  "dist"
40
46
  ],
41
47
  "scripts": {
42
- "bench": "tsx bench.js",
43
- "build": "npm-run-all build:*",
44
- "build:es": "tsc --outDir dist/es --module esnext --declaration false && echo '{\"type\":\"module\"}' > dist/es/package.json",
45
- "build:js": "tsc",
46
- "docs": "tsx ../../scripts/generate-readmes",
47
- "test": "npm-run-all test:*",
48
- "test:tape": "tsx test.js"
48
+ "bench": "tsx bench.ts",
49
+ "build": "tsup --config ../../tsup.config.ts",
50
+ "docs": "tsx ../../scripts/generate-readmes.ts",
51
+ "test": "npm-run-all --npm-path npm test:*",
52
+ "test:tape": "tsx test.ts"
49
53
  },
50
54
  "devDependencies": {
51
- "@turf/truncate": "^7.0.0-alpha.1",
52
- "@types/tape": "*",
53
- "benchmark": "*",
54
- "load-json-file": "*",
55
- "npm-run-all": "*",
56
- "tape": "*",
57
- "tslint": "*",
58
- "tsx": "*",
59
- "typescript": "*",
60
- "write-json-file": "*"
55
+ "@turf/truncate": "^7.0.0-alpha.111+08576cb50",
56
+ "@types/benchmark": "^2.1.5",
57
+ "@types/tape": "^4.2.32",
58
+ "benchmark": "^2.1.4",
59
+ "load-json-file": "^7.0.1",
60
+ "npm-run-all": "^4.1.5",
61
+ "tape": "^5.7.2",
62
+ "tsup": "^8.0.1",
63
+ "tsx": "^4.6.2",
64
+ "typescript": "^5.2.2",
65
+ "write-json-file": "^5.0.0"
61
66
  },
62
67
  "dependencies": {
63
- "@turf/area": "^7.0.0-alpha.1",
64
- "@turf/bbox": "^7.0.0-alpha.1",
65
- "@turf/bbox-polygon": "^7.0.0-alpha.1",
66
- "@turf/centroid": "^7.0.0-alpha.1",
67
- "@turf/distance": "^7.0.0-alpha.1",
68
- "@turf/helpers": "^7.0.0-alpha.1",
69
- "@turf/meta": "^7.0.0-alpha.1",
70
- "@turf/nearest-point": "^7.0.0-alpha.1",
71
- "tslib": "^2.3.0"
68
+ "@turf/area": "^7.0.0-alpha.111+08576cb50",
69
+ "@turf/bbox": "^7.0.0-alpha.111+08576cb50",
70
+ "@turf/bbox-polygon": "^7.0.0-alpha.111+08576cb50",
71
+ "@turf/centroid": "^7.0.0-alpha.111+08576cb50",
72
+ "@turf/distance": "^7.0.0-alpha.111+08576cb50",
73
+ "@turf/helpers": "^7.0.0-alpha.111+08576cb50",
74
+ "@turf/meta": "^7.0.0-alpha.111+08576cb50",
75
+ "@turf/nearest-point": "^7.0.0-alpha.111+08576cb50",
76
+ "tslib": "^2.6.2"
72
77
  },
73
- "gitHead": "cf7a0c507b017ca066acffd0ce23bda5b393fb5a"
78
+ "gitHead": "08576cb50376e0199aea02dbd887e3af83672246"
74
79
  }
@@ -1 +0,0 @@
1
- {"type":"module"}
package/dist/js/index.js DELETED
@@ -1,101 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const tslib_1 = require("tslib");
4
- const area_1 = tslib_1.__importDefault(require("@turf/area"));
5
- const bbox_1 = tslib_1.__importDefault(require("@turf/bbox"));
6
- const bbox_polygon_1 = tslib_1.__importDefault(require("@turf/bbox-polygon"));
7
- const centroid_1 = tslib_1.__importDefault(require("@turf/centroid"));
8
- const distance_1 = tslib_1.__importDefault(require("@turf/distance"));
9
- const nearest_point_1 = tslib_1.__importDefault(require("@turf/nearest-point"));
10
- const meta_1 = require("@turf/meta");
11
- const helpers_1 = require("@turf/helpers");
12
- /**
13
- * Nearest Neighbor Analysis calculates an index based the average distances
14
- * between points in the dataset, thereby providing inference as to whether the
15
- * data is clustered, dispersed, or randomly distributed within the study area.
16
- *
17
- * It returns a {@link Feature<Polygon>} of the study area, with the results of
18
- * the analysis attached as part of of the `nearestNeighborAnalysis` property
19
- * of the study area's `properties`. The attached
20
- * [_z_-score](https://en.wikipedia.org/wiki/Standard_score) indicates how many
21
- * standard deviations above or below the expected mean distance the data's
22
- * observed mean distance is. The more negative, the more clustered. The more
23
- * positive, the more evenly dispersed. A _z_-score between -2 and 2 indicates
24
- * a seemingly random distribution. That is, within _p_ of less than 0.05, the
25
- * distribution appears statistically significantly neither clustered nor
26
- * dispersed.
27
- *
28
- * **Remarks**
29
- *
30
- * - Though the analysis will work on any {@link FeatureCollection} type, it
31
- * works best with {@link Point} collections.
32
- *
33
- * - This analysis is _very_ sensitive to the study area provided.
34
- * If no {@link Feature<Polygon>} is passed as the study area, the function draws a box
35
- * around the data, which may distort the findings. This analysis works best
36
- * with a bounded area of interest within with the data is either clustered,
37
- * dispersed, or randomly distributed. For example, a city's subway stops may
38
- * look extremely clustered if the study area is an entire state. On the other
39
- * hand, they may look rather evenly dispersed if the study area is limited to
40
- * the city's downtown.
41
- *
42
- * **Bibliography**
43
- *
44
- * Philip J. Clark and Francis C. Evans, “Distance to Nearest Neighbor as a
45
- * Measure of Spatial Relationships in Populations,” _Ecology_ 35, no. 4
46
- * (1954): 445–453, doi:[10.2307/1931034](http://doi.org/10.2307/1931034).
47
- *
48
- * @name nearestNeighborAnalysis
49
- * @param {FeatureCollection<any>} dataset FeatureCollection (pref. of points) to study
50
- * @param {Object} [options={}] Optional parameters
51
- * @param {Feature<Polygon>} [options.studyArea] polygon representing the study area
52
- * @param {string} [options.units='kilometers'] unit of measurement for distances and, squared, area.
53
- * @param {Object} [options.properties={}] properties
54
- * @returns {Feature<Polygon>} A polygon of the study area or an approximation of one.
55
- * @example
56
- * var bbox = [-65, 40, -63, 42];
57
- * var dataset = turf.randomPoint(100, { bbox: bbox });
58
- * var nearestNeighborStudyArea = turf.nearestNeighborAnalysis(dataset);
59
- *
60
- * //addToMap
61
- * var addToMap = [dataset, nearestNeighborStudyArea];
62
- */
63
- function nearestNeighborAnalysis(dataset, options) {
64
- // Optional params
65
- options = options || {};
66
- const studyArea = options.studyArea || bbox_polygon_1.default(bbox_1.default(dataset));
67
- const properties = options.properties || {};
68
- const units = options.units || "kilometers";
69
- const features = [];
70
- meta_1.featureEach(dataset, (feature) => {
71
- features.push(centroid_1.default(feature));
72
- });
73
- const n = features.length;
74
- const observedMeanDistance = features
75
- .map((feature, index) => {
76
- const otherFeatures = helpers_1.featureCollection(features.filter((f, i) => {
77
- return i !== index;
78
- }));
79
- // Have to add the ! to make typescript validation pass
80
- // see https://stackoverflow.com/a/40350534/1979085
81
- return distance_1.default(feature, nearest_point_1.default(feature, otherFeatures).geometry.coordinates, { units });
82
- })
83
- .reduce((sum, value) => {
84
- return sum + value;
85
- }, 0) / n;
86
- const populationDensity = n / helpers_1.convertArea(area_1.default(studyArea), "meters", units);
87
- const expectedMeanDistance = 1 / (2 * Math.sqrt(populationDensity));
88
- const variance = 0.26136 / Math.sqrt(n * populationDensity);
89
- properties.nearestNeighborAnalysis = {
90
- units: units,
91
- arealUnits: units + "²",
92
- observedMeanDistance: observedMeanDistance,
93
- expectedMeanDistance: expectedMeanDistance,
94
- nearestNeighborIndex: observedMeanDistance / expectedMeanDistance,
95
- numberOfPoints: n,
96
- zScore: (observedMeanDistance - expectedMeanDistance) / variance,
97
- };
98
- studyArea.properties = properties;
99
- return studyArea;
100
- }
101
- exports.default = nearestNeighborAnalysis;