-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
96 lines (93 loc) · 2.51 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
var inside = require('turf-inside');
/**
* Takes a set of {@link Point|points} and a set of {@link Polygon|polygons} and calculates the number of points that fall within the set of polygons.
*
* @module turf/count
* @category aggregation
* @param {FeatureCollection<Polygon>} polygons input polygons
* @param {FeatureCollection<Point>} points input points
* @param {String} countField a field to append to the attributes of the Polygon features representing Point counts
* @return {FeatureCollection<Polygon>} polygons with `countField` appended
* @example
* var polygons = {
* "type": "FeatureCollection",
* "features": [
* {
* "type": "Feature",
* "properties": {},
* "geometry": {
* "type": "Polygon",
* "coordinates": [[
* [-112.072391,46.586591],
* [-112.072391,46.61761],
* [-112.028102,46.61761],
* [-112.028102,46.586591],
* [-112.072391,46.586591]
* ]]
* }
* }, {
* "type": "Feature",
* "properties": {},
* "geometry": {
* "type": "Polygon",
* "coordinates": [[
* [-112.023983,46.570426],
* [-112.023983,46.615016],
* [-111.966133,46.615016],
* [-111.966133,46.570426],
* [-112.023983,46.570426]
* ]]
* }
* }
* ]
* };
* var points = {
* "type": "FeatureCollection",
* "features": [
* {
* "type": "Feature",
* "properties": {
* "population": 200
* },
* "geometry": {
* "type": "Point",
* "coordinates": [-112.0372, 46.608058]
* }
* }, {
* "type": "Feature",
* "properties": {
* "population": 600
* },
* "geometry": {
* "type": "Point",
* "coordinates": [-112.045955, 46.596264]
* }
* }
* ]
* };
*
* var counted = turf.count(polygons, points, 'pt_count');
*
* var resultFeatures = points.features.concat(counted.features);
* var result = {
* "type": "FeatureCollection",
* "features": resultFeatures
* };
*
* //=result
*/
module.exports = function(polyFC, ptFC, outField) {
for (var i = 0; i < polyFC.features.length; i++) {
var poly = polyFC.features[i];
if(!poly.properties) poly.properties = {};
var values = 0;
for (var j = 0; j < ptFC.features.length; j++) {
var pt = ptFC.features[j];
if (inside(pt, poly)) {
values++;
}
}
poly.properties[outField] = values;
}
return polyFC;
};