index.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var meta_1 = require("@turf/meta");
  4. var helpers_1 = require("@turf/helpers");
  5. /**
  6. * Get Cluster
  7. *
  8. * @name getCluster
  9. * @param {FeatureCollection} geojson GeoJSON Features
  10. * @param {*} filter Filter used on GeoJSON properties to get Cluster
  11. * @returns {FeatureCollection} Single Cluster filtered by GeoJSON Properties
  12. * @example
  13. * var geojson = turf.featureCollection([
  14. * turf.point([0, 0], {'marker-symbol': 'circle'}),
  15. * turf.point([2, 4], {'marker-symbol': 'star'}),
  16. * turf.point([3, 6], {'marker-symbol': 'star'}),
  17. * turf.point([5, 1], {'marker-symbol': 'square'}),
  18. * turf.point([4, 2], {'marker-symbol': 'circle'})
  19. * ]);
  20. *
  21. * // Create a cluster using K-Means (adds `cluster` to GeoJSON properties)
  22. * var clustered = turf.clustersKmeans(geojson);
  23. *
  24. * // Retrieve first cluster (0)
  25. * var cluster = turf.getCluster(clustered, {cluster: 0});
  26. * //= cluster
  27. *
  28. * // Retrieve cluster based on custom properties
  29. * turf.getCluster(clustered, {'marker-symbol': 'circle'}).length;
  30. * //= 2
  31. * turf.getCluster(clustered, {'marker-symbol': 'square'}).length;
  32. * //= 1
  33. */
  34. function getCluster(geojson, filter) {
  35. // Validation
  36. if (!geojson)
  37. throw new Error("geojson is required");
  38. if (geojson.type !== "FeatureCollection")
  39. throw new Error("geojson must be a FeatureCollection");
  40. if (filter === undefined || filter === null)
  41. throw new Error("filter is required");
  42. // Filter Features
  43. var features = [];
  44. meta_1.featureEach(geojson, function (feature) {
  45. if (applyFilter(feature.properties, filter))
  46. features.push(feature);
  47. });
  48. return helpers_1.featureCollection(features);
  49. }
  50. exports.getCluster = getCluster;
  51. /**
  52. * Callback for clusterEach
  53. *
  54. * @callback clusterEachCallback
  55. * @param {FeatureCollection} [cluster] The current cluster being processed.
  56. * @param {*} [clusterValue] Value used to create cluster being processed.
  57. * @param {number} [currentIndex] The index of the current element being processed in the array.Starts at index 0
  58. * @returns {void}
  59. */
  60. /**
  61. * clusterEach
  62. *
  63. * @name clusterEach
  64. * @param {FeatureCollection} geojson GeoJSON Features
  65. * @param {string|number} property GeoJSON property key/value used to create clusters
  66. * @param {Function} callback a method that takes (cluster, clusterValue, currentIndex)
  67. * @returns {void}
  68. * @example
  69. * var geojson = turf.featureCollection([
  70. * turf.point([0, 0]),
  71. * turf.point([2, 4]),
  72. * turf.point([3, 6]),
  73. * turf.point([5, 1]),
  74. * turf.point([4, 2])
  75. * ]);
  76. *
  77. * // Create a cluster using K-Means (adds `cluster` to GeoJSON properties)
  78. * var clustered = turf.clustersKmeans(geojson);
  79. *
  80. * // Iterate over each cluster
  81. * turf.clusterEach(clustered, 'cluster', function (cluster, clusterValue, currentIndex) {
  82. * //= cluster
  83. * //= clusterValue
  84. * //= currentIndex
  85. * })
  86. *
  87. * // Calculate the total number of clusters
  88. * var total = 0
  89. * turf.clusterEach(clustered, 'cluster', function () {
  90. * total++;
  91. * });
  92. *
  93. * // Create an Array of all the values retrieved from the 'cluster' property
  94. * var values = []
  95. * turf.clusterEach(clustered, 'cluster', function (cluster, clusterValue) {
  96. * values.push(clusterValue);
  97. * });
  98. */
  99. function clusterEach(geojson, property, callback) {
  100. // Validation
  101. if (!geojson)
  102. throw new Error("geojson is required");
  103. if (geojson.type !== "FeatureCollection")
  104. throw new Error("geojson must be a FeatureCollection");
  105. if (property === undefined || property === null)
  106. throw new Error("property is required");
  107. // Create clusters based on property values
  108. var bins = createBins(geojson, property);
  109. var values = Object.keys(bins);
  110. for (var index = 0; index < values.length; index++) {
  111. var value = values[index];
  112. var bin = bins[value];
  113. var features = [];
  114. for (var i = 0; i < bin.length; i++) {
  115. features.push(geojson.features[bin[i]]);
  116. }
  117. callback(helpers_1.featureCollection(features), value, index);
  118. }
  119. }
  120. exports.clusterEach = clusterEach;
  121. /**
  122. * Callback for clusterReduce
  123. *
  124. * The first time the callback function is called, the values provided as arguments depend
  125. * on whether the reduce method has an initialValue argument.
  126. *
  127. * If an initialValue is provided to the reduce method:
  128. * - The previousValue argument is initialValue.
  129. * - The currentValue argument is the value of the first element present in the array.
  130. *
  131. * If an initialValue is not provided:
  132. * - The previousValue argument is the value of the first element present in the array.
  133. * - The currentValue argument is the value of the second element present in the array.
  134. *
  135. * @callback clusterReduceCallback
  136. * @param {*} [previousValue] The accumulated value previously returned in the last invocation
  137. * of the callback, or initialValue, if supplied.
  138. * @param {FeatureCollection} [cluster] The current cluster being processed.
  139. * @param {*} [clusterValue] Value used to create cluster being processed.
  140. * @param {number} [currentIndex] The index of the current element being processed in the
  141. * array. Starts at index 0, if an initialValue is provided, and at index 1 otherwise.
  142. */
  143. /**
  144. * Reduce clusters in GeoJSON Features, similar to Array.reduce()
  145. *
  146. * @name clusterReduce
  147. * @param {FeatureCollection} geojson GeoJSON Features
  148. * @param {string|number} property GeoJSON property key/value used to create clusters
  149. * @param {Function} callback a method that takes (previousValue, cluster, clusterValue, currentIndex)
  150. * @param {*} [initialValue] Value to use as the first argument to the first call of the callback.
  151. * @returns {*} The value that results from the reduction.
  152. * @example
  153. * var geojson = turf.featureCollection([
  154. * turf.point([0, 0]),
  155. * turf.point([2, 4]),
  156. * turf.point([3, 6]),
  157. * turf.point([5, 1]),
  158. * turf.point([4, 2])
  159. * ]);
  160. *
  161. * // Create a cluster using K-Means (adds `cluster` to GeoJSON properties)
  162. * var clustered = turf.clustersKmeans(geojson);
  163. *
  164. * // Iterate over each cluster and perform a calculation
  165. * var initialValue = 0
  166. * turf.clusterReduce(clustered, 'cluster', function (previousValue, cluster, clusterValue, currentIndex) {
  167. * //=previousValue
  168. * //=cluster
  169. * //=clusterValue
  170. * //=currentIndex
  171. * return previousValue++;
  172. * }, initialValue);
  173. *
  174. * // Calculate the total number of clusters
  175. * var total = turf.clusterReduce(clustered, 'cluster', function (previousValue) {
  176. * return previousValue++;
  177. * }, 0);
  178. *
  179. * // Create an Array of all the values retrieved from the 'cluster' property
  180. * var values = turf.clusterReduce(clustered, 'cluster', function (previousValue, cluster, clusterValue) {
  181. * return previousValue.concat(clusterValue);
  182. * }, []);
  183. */
  184. function clusterReduce(geojson, property, callback, initialValue) {
  185. var previousValue = initialValue;
  186. clusterEach(geojson, property, function (cluster, clusterValue, currentIndex) {
  187. if (currentIndex === 0 && initialValue === undefined)
  188. previousValue = cluster;
  189. else
  190. previousValue = callback(previousValue, cluster, clusterValue, currentIndex);
  191. });
  192. return previousValue;
  193. }
  194. exports.clusterReduce = clusterReduce;
  195. /**
  196. * Create Bins
  197. *
  198. * @private
  199. * @param {FeatureCollection} geojson GeoJSON Features
  200. * @param {string|number} property Property values are used to create bins
  201. * @returns {Object} bins with Feature IDs
  202. * @example
  203. * var geojson = turf.featureCollection([
  204. * turf.point([0, 0], {cluster: 0, foo: 'null'}),
  205. * turf.point([2, 4], {cluster: 1, foo: 'bar'}),
  206. * turf.point([5, 1], {0: 'foo'}),
  207. * turf.point([3, 6], {cluster: 1}),
  208. * ]);
  209. * createBins(geojson, 'cluster');
  210. * //= { '0': [ 0 ], '1': [ 1, 3 ] }
  211. */
  212. function createBins(geojson, property) {
  213. var bins = {};
  214. meta_1.featureEach(geojson, function (feature, i) {
  215. var properties = feature.properties || {};
  216. if (Object.prototype.hasOwnProperty.call(properties, String(property))) {
  217. var value = properties[property];
  218. if (Object.prototype.hasOwnProperty.call(bins, value))
  219. bins[value].push(i);
  220. else
  221. bins[value] = [i];
  222. }
  223. });
  224. return bins;
  225. }
  226. exports.createBins = createBins;
  227. /**
  228. * Apply Filter
  229. *
  230. * @private
  231. * @param {*} properties Properties
  232. * @param {*} filter Filter
  233. * @returns {boolean} applied Filter to properties
  234. */
  235. function applyFilter(properties, filter) {
  236. if (properties === undefined)
  237. return false;
  238. var filterType = typeof filter;
  239. // String & Number
  240. if (filterType === "number" || filterType === "string")
  241. return Object.prototype.hasOwnProperty.call(properties, filter);
  242. // Array
  243. else if (Array.isArray(filter)) {
  244. for (var i = 0; i < filter.length; i++) {
  245. if (!applyFilter(properties, filter[i]))
  246. return false;
  247. }
  248. return true;
  249. // Object
  250. }
  251. else {
  252. return propertiesContainsFilter(properties, filter);
  253. }
  254. }
  255. exports.applyFilter = applyFilter;
  256. /**
  257. * Properties contains filter (does not apply deepEqual operations)
  258. *
  259. * @private
  260. * @param {*} properties Properties
  261. * @param {Object} filter Filter
  262. * @returns {boolean} does filter equal Properties
  263. * @example
  264. * propertiesContainsFilter({foo: 'bar', cluster: 0}, {cluster: 0})
  265. * //= true
  266. * propertiesContainsFilter({foo: 'bar', cluster: 0}, {cluster: 1})
  267. * //= false
  268. */
  269. function propertiesContainsFilter(properties, filter) {
  270. var keys = Object.keys(filter);
  271. for (var i = 0; i < keys.length; i++) {
  272. var key = keys[i];
  273. if (properties[key] !== filter[key])
  274. return false;
  275. }
  276. return true;
  277. }
  278. exports.propertiesContainsFilter = propertiesContainsFilter;
  279. /**
  280. * Filter Properties
  281. *
  282. * @private
  283. * @param {*} properties Properties
  284. * @param {Array<string>} keys Used to filter Properties
  285. * @returns {*} filtered Properties
  286. * @example
  287. * filterProperties({foo: 'bar', cluster: 0}, ['cluster'])
  288. * //= {cluster: 0}
  289. */
  290. function filterProperties(properties, keys) {
  291. if (!keys)
  292. return {};
  293. if (!keys.length)
  294. return {};
  295. var newProperties = {};
  296. for (var i = 0; i < keys.length; i++) {
  297. var key = keys[i];
  298. if (Object.prototype.hasOwnProperty.call(properties, key))
  299. newProperties[key] = properties[key];
  300. }
  301. return newProperties;
  302. }
  303. exports.filterProperties = filterProperties;