@turf/nearest-neighbor-analysis 7.0.0-alpha.2 → 7.1.0-alpha.7
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 +4 -9
- package/dist/cjs/index.cjs +57 -0
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/{es/index.js → cjs/index.d.cts} +24 -47
- package/dist/{js → esm}/index.d.ts +7 -5
- package/dist/esm/index.js +57 -0
- package/dist/esm/index.js.map +1 -0
- package/package.json +39 -34
- package/dist/es/package.json +0 -1
- package/dist/js/index.js +0 -101
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](
|
|
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
|
|
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,57 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true});// index.ts
|
|
2
|
+
var _area = require('@turf/area');
|
|
3
|
+
var _bbox = require('@turf/bbox');
|
|
4
|
+
var _bboxpolygon = require('@turf/bbox-polygon');
|
|
5
|
+
var _centroid = require('@turf/centroid');
|
|
6
|
+
var _distance = require('@turf/distance');
|
|
7
|
+
var _nearestpoint = require('@turf/nearest-point');
|
|
8
|
+
var _meta = require('@turf/meta');
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
var _helpers = require('@turf/helpers');
|
|
13
|
+
function nearestNeighborAnalysis(dataset, options) {
|
|
14
|
+
options = options || {};
|
|
15
|
+
const studyArea = options.studyArea || _bboxpolygon.bboxPolygon.call(void 0, _bbox.bbox.call(void 0, dataset));
|
|
16
|
+
const properties = options.properties || {};
|
|
17
|
+
const units = options.units || "kilometers";
|
|
18
|
+
const features = [];
|
|
19
|
+
_meta.featureEach.call(void 0, dataset, (feature) => {
|
|
20
|
+
features.push(_centroid.centroid.call(void 0, feature));
|
|
21
|
+
});
|
|
22
|
+
const n = features.length;
|
|
23
|
+
const observedMeanDistance = features.map((feature, index) => {
|
|
24
|
+
const otherFeatures = _helpers.featureCollection.call(void 0,
|
|
25
|
+
features.filter((f, i) => {
|
|
26
|
+
return i !== index;
|
|
27
|
+
})
|
|
28
|
+
);
|
|
29
|
+
return _distance.distance.call(void 0,
|
|
30
|
+
feature,
|
|
31
|
+
_nearestpoint.nearestPoint.call(void 0, feature, otherFeatures).geometry.coordinates,
|
|
32
|
+
{ units }
|
|
33
|
+
);
|
|
34
|
+
}).reduce((sum, value) => {
|
|
35
|
+
return sum + value;
|
|
36
|
+
}, 0) / n;
|
|
37
|
+
const populationDensity = n / _helpers.convertArea.call(void 0, _area.area.call(void 0, 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
|
+
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
exports.default = turf_nearest_neighbor_analysis_default; exports.nearestNeighborAnalysis = nearestNeighborAnalysis;
|
|
57
|
+
//# 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;AAOA,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,11 +1,20 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
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
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
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 };
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { Units, AreaUnits } from
|
|
3
|
-
|
|
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
|
-
|
|
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
|
-
|
|
74
|
+
|
|
75
|
+
export { type NearestNeighborStatistics, type NearestNeighborStudyArea, nearestNeighborAnalysis as default, nearestNeighborAnalysis };
|
|
@@ -0,0 +1,57 @@
|
|
|
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
|
|
@@ -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;AAOA,IAAO,yCAAQ;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@turf/nearest-neighbor-analysis",
|
|
3
|
-
"version": "7.
|
|
3
|
+
"version": "7.1.0-alpha.7+0ce6ecca0",
|
|
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
|
-
"
|
|
27
|
-
"
|
|
26
|
+
"type": "module",
|
|
27
|
+
"main": "dist/cjs/index.cjs",
|
|
28
|
+
"module": "dist/esm/index.js",
|
|
29
|
+
"types": "dist/esm/index.d.ts",
|
|
28
30
|
"exports": {
|
|
29
31
|
"./package.json": "./package.json",
|
|
30
32
|
".": {
|
|
31
|
-
"
|
|
32
|
-
|
|
33
|
-
|
|
33
|
+
"import": {
|
|
34
|
+
"types": "./dist/esm/index.d.ts",
|
|
35
|
+
"default": "./dist/esm/index.js"
|
|
36
|
+
},
|
|
37
|
+
"require": {
|
|
38
|
+
"types": "./dist/cjs/index.d.cts",
|
|
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.
|
|
43
|
-
"build": "
|
|
44
|
-
"
|
|
45
|
-
"
|
|
46
|
-
"
|
|
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.
|
|
52
|
-
"@types/
|
|
53
|
-
"
|
|
54
|
-
"
|
|
55
|
-
"
|
|
56
|
-
"
|
|
57
|
-
"
|
|
58
|
-
"
|
|
59
|
-
"
|
|
60
|
-
"
|
|
55
|
+
"@turf/truncate": "^7.1.0-alpha.7+0ce6ecca0",
|
|
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.
|
|
64
|
-
"@turf/bbox": "^7.
|
|
65
|
-
"@turf/bbox-polygon": "^7.
|
|
66
|
-
"@turf/centroid": "^7.
|
|
67
|
-
"@turf/distance": "^7.
|
|
68
|
-
"@turf/helpers": "^7.
|
|
69
|
-
"@turf/meta": "^7.
|
|
70
|
-
"@turf/nearest-point": "^7.
|
|
71
|
-
"tslib": "^2.
|
|
68
|
+
"@turf/area": "^7.1.0-alpha.7+0ce6ecca0",
|
|
69
|
+
"@turf/bbox": "^7.1.0-alpha.7+0ce6ecca0",
|
|
70
|
+
"@turf/bbox-polygon": "^7.1.0-alpha.7+0ce6ecca0",
|
|
71
|
+
"@turf/centroid": "^7.1.0-alpha.7+0ce6ecca0",
|
|
72
|
+
"@turf/distance": "^7.1.0-alpha.7+0ce6ecca0",
|
|
73
|
+
"@turf/helpers": "^7.1.0-alpha.7+0ce6ecca0",
|
|
74
|
+
"@turf/meta": "^7.1.0-alpha.7+0ce6ecca0",
|
|
75
|
+
"@turf/nearest-point": "^7.1.0-alpha.7+0ce6ecca0",
|
|
76
|
+
"tslib": "^2.6.2"
|
|
72
77
|
},
|
|
73
|
-
"gitHead": "
|
|
78
|
+
"gitHead": "0ce6ecca05829690270fec6d6bed2003495fe0ea"
|
|
74
79
|
}
|
package/dist/es/package.json
DELETED
|
@@ -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;
|