index.d.cts 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. import { GeometryObject, GeoJsonProperties, FeatureCollection } from 'geojson';
  2. /**
  3. * Get Cluster
  4. *
  5. * @function
  6. * @param {FeatureCollection} geojson GeoJSON Features
  7. * @param {*} filter Filter used on GeoJSON properties to get Cluster
  8. * @returns {FeatureCollection} Single Cluster filtered by GeoJSON Properties
  9. * @example
  10. * var geojson = turf.featureCollection([
  11. * turf.point([0, 0], {'marker-symbol': 'circle'}),
  12. * turf.point([2, 4], {'marker-symbol': 'star'}),
  13. * turf.point([3, 6], {'marker-symbol': 'star'}),
  14. * turf.point([5, 1], {'marker-symbol': 'square'}),
  15. * turf.point([4, 2], {'marker-symbol': 'circle'})
  16. * ]);
  17. *
  18. * // Create a cluster using K-Means (adds `cluster` to GeoJSON properties)
  19. * var clustered = turf.clustersKmeans(geojson);
  20. *
  21. * // Retrieve first cluster (0)
  22. * var cluster = turf.getCluster(clustered, {cluster: 0});
  23. * //= cluster
  24. *
  25. * // Retrieve cluster based on custom properties
  26. * turf.getCluster(clustered, {'marker-symbol': 'circle'}).length;
  27. * //= 2
  28. * turf.getCluster(clustered, {'marker-symbol': 'square'}).length;
  29. * //= 1
  30. */
  31. declare function getCluster<G extends GeometryObject, P extends GeoJsonProperties = GeoJsonProperties>(geojson: FeatureCollection<G, P>, filter: any): FeatureCollection<G, P>;
  32. /**
  33. * Callback for clusterEach
  34. *
  35. * @callback clusterEachCallback
  36. * @param {FeatureCollection} [cluster] The current cluster being processed.
  37. * @param {any} [clusterValue] Value used to create cluster being processed.
  38. * @param {number} [currentIndex] The index of the current element being processed in the array.Starts at index 0
  39. * @returns {void}
  40. */
  41. /**
  42. * clusterEach
  43. *
  44. * @function
  45. * @param {FeatureCollection} geojson GeoJSON Features
  46. * @param {string|number} property GeoJSON property key/value used to create clusters
  47. * @param {clusterEachCallback} callback a method that takes (cluster, clusterValue, currentIndex)
  48. * @returns {void}
  49. * @example
  50. * var geojson = turf.featureCollection([
  51. * turf.point([0, 0]),
  52. * turf.point([2, 4]),
  53. * turf.point([3, 6]),
  54. * turf.point([5, 1]),
  55. * turf.point([4, 2])
  56. * ]);
  57. *
  58. * // Create a cluster using K-Means (adds `cluster` to GeoJSON properties)
  59. * var clustered = turf.clustersKmeans(geojson);
  60. *
  61. * // Iterate over each cluster
  62. * turf.clusterEach(clustered, 'cluster', function (cluster, clusterValue, currentIndex) {
  63. * //= cluster
  64. * //= clusterValue
  65. * //= currentIndex
  66. * })
  67. *
  68. * // Calculate the total number of clusters
  69. * var total = 0
  70. * turf.clusterEach(clustered, 'cluster', function () {
  71. * total++;
  72. * });
  73. *
  74. * // Create an Array of all the values retrieved from the 'cluster' property
  75. * var values = []
  76. * turf.clusterEach(clustered, 'cluster', function (cluster, clusterValue) {
  77. * values.push(clusterValue);
  78. * });
  79. */
  80. declare function clusterEach<G extends GeometryObject, P extends GeoJsonProperties = GeoJsonProperties>(geojson: FeatureCollection<G, P>, property: number | string, callback: (cluster: FeatureCollection<G, P>, clusterValue?: any, currentIndex?: number) => void): void;
  81. /**
  82. * Callback for clusterReduce
  83. *
  84. * The first time the callback function is called, the values provided as arguments depend
  85. * on whether the reduce method has an initialValue argument.
  86. *
  87. * If an initialValue is provided to the reduce method:
  88. * - The previousValue argument is initialValue.
  89. * - The currentValue argument is the value of the first element present in the array.
  90. *
  91. * If an initialValue is not provided:
  92. * - The previousValue argument is the value of the first element present in the array.
  93. * - The currentValue argument is the value of the second element present in the array.
  94. *
  95. * @callback clusterReduceCallback
  96. * @param {*} [previousValue] The accumulated value previously returned in the last invocation
  97. * of the callback, or initialValue, if supplied.
  98. * @param {FeatureCollection} [cluster] The current cluster being processed.
  99. * @param {*} [clusterValue] Value used to create cluster being processed.
  100. * @param {number} [currentIndex] The index of the current element being processed in the
  101. * array. Starts at index 0, if an initialValue is provided, and at index 1 otherwise.
  102. * @returns {void}
  103. */
  104. /**
  105. * Reduce clusters in GeoJSON Features, similar to Array.reduce()
  106. *
  107. * @function
  108. * @param {FeatureCollection} geojson GeoJSON Features
  109. * @param {string|number} property GeoJSON property key/value used to create clusters
  110. * @param {clusterReduceCallback} callback a method that takes (previousValue, cluster, clusterValue, currentIndex)
  111. * @param {any} [initialValue] Value to use as the first argument to the first call of the callback.
  112. * @returns {any} The value that results from the reduction.
  113. * @example
  114. * var geojson = turf.featureCollection([
  115. * turf.point([0, 0]),
  116. * turf.point([2, 4]),
  117. * turf.point([3, 6]),
  118. * turf.point([5, 1]),
  119. * turf.point([4, 2])
  120. * ]);
  121. *
  122. * // Create a cluster using K-Means (adds `cluster` to GeoJSON properties)
  123. * var clustered = turf.clustersKmeans(geojson);
  124. *
  125. * // Iterate over each cluster and perform a calculation
  126. * var initialValue = 0
  127. * turf.clusterReduce(clustered, 'cluster', function (previousValue, cluster, clusterValue, currentIndex) {
  128. * //=previousValue
  129. * //=cluster
  130. * //=clusterValue
  131. * //=currentIndex
  132. * return previousValue++;
  133. * }, initialValue);
  134. *
  135. * // Calculate the total number of clusters
  136. * var total = turf.clusterReduce(clustered, 'cluster', function (previousValue) {
  137. * return previousValue++;
  138. * }, 0);
  139. *
  140. * // Create an Array of all the values retrieved from the 'cluster' property
  141. * var values = turf.clusterReduce(clustered, 'cluster', function (previousValue, cluster, clusterValue) {
  142. * return previousValue.concat(clusterValue);
  143. * }, []);
  144. */
  145. declare function clusterReduce<G extends GeometryObject, P extends GeoJsonProperties = GeoJsonProperties>(geojson: FeatureCollection<G, P>, property: number | string, callback: (previousValue: any | undefined, cluster: FeatureCollection<G, P>, clusterValue?: any, currentIndex?: number) => void, initialValue?: any): void;
  146. /**
  147. * Create Bins
  148. *
  149. * @private
  150. * @param {FeatureCollection} geojson GeoJSON Features
  151. * @param {string|number} property Property values are used to create bins
  152. * @returns {Object} bins with Feature IDs
  153. * @example
  154. * var geojson = turf.featureCollection([
  155. * turf.point([0, 0], {cluster: 0, foo: 'null'}),
  156. * turf.point([2, 4], {cluster: 1, foo: 'bar'}),
  157. * turf.point([5, 1], {0: 'foo'}),
  158. * turf.point([3, 6], {cluster: 1}),
  159. * ]);
  160. * createBins(geojson, 'cluster');
  161. * //= { '0': [ 0 ], '1': [ 1, 3 ] }
  162. */
  163. declare function createBins(geojson: FeatureCollection<any>, property: string | number): Record<string, number[]>;
  164. /**
  165. * Apply Filter
  166. *
  167. * @private
  168. * @param {*} properties Properties
  169. * @param {*} filter Filter
  170. * @returns {boolean} applied Filter to properties
  171. */
  172. declare function applyFilter(properties: any, filter: any): boolean;
  173. /**
  174. * Properties contains filter (does not apply deepEqual operations)
  175. *
  176. * @private
  177. * @param {*} properties Properties
  178. * @param {Object} filter Filter
  179. * @returns {boolean} does filter equal Properties
  180. * @example
  181. * propertiesContainsFilter({foo: 'bar', cluster: 0}, {cluster: 0})
  182. * //= true
  183. * propertiesContainsFilter({foo: 'bar', cluster: 0}, {cluster: 1})
  184. * //= false
  185. */
  186. declare function propertiesContainsFilter(properties: any, filter: any): boolean;
  187. /**
  188. * Filter Properties
  189. *
  190. * @private
  191. * @param {*} properties Properties
  192. * @param {Array<string>} keys Used to filter Properties
  193. * @returns {*} filtered Properties
  194. * @example
  195. * filterProperties({foo: 'bar', cluster: 0}, ['cluster'])
  196. * //= {cluster: 0}
  197. */
  198. declare function filterProperties(properties: Record<string, any>, keys: string[]): any;
  199. export { applyFilter, clusterEach, clusterReduce, createBins, filterProperties, getCluster, propertiesContainsFilter };