{"version":3,"sources":["../../index.ts"],"sourcesContent":["import {\n Feature,\n FeatureCollection,\n GeoJsonProperties,\n GeometryObject,\n} from \"geojson\";\nimport { featureEach } from \"@turf/meta\";\nimport { featureCollection } from \"@turf/helpers\";\n\n/**\n * Get Cluster\n *\n * @function\n * @param {FeatureCollection} geojson GeoJSON Features\n * @param {*} filter Filter used on GeoJSON properties to get Cluster\n * @returns {FeatureCollection} Single Cluster filtered by GeoJSON Properties\n * @example\n * var geojson = turf.featureCollection([\n * turf.point([0, 0], {'marker-symbol': 'circle'}),\n * turf.point([2, 4], {'marker-symbol': 'star'}),\n * turf.point([3, 6], {'marker-symbol': 'star'}),\n * turf.point([5, 1], {'marker-symbol': 'square'}),\n * turf.point([4, 2], {'marker-symbol': 'circle'})\n * ]);\n *\n * // Create a cluster using K-Means (adds `cluster` to GeoJSON properties)\n * var clustered = turf.clustersKmeans(geojson);\n *\n * // Retrieve first cluster (0)\n * var cluster = turf.getCluster(clustered, {cluster: 0});\n * //= cluster\n *\n * // Retrieve cluster based on custom properties\n * turf.getCluster(clustered, {'marker-symbol': 'circle'}).length;\n * //= 2\n * turf.getCluster(clustered, {'marker-symbol': 'square'}).length;\n * //= 1\n */\nfunction getCluster<\n G extends GeometryObject,\n P extends GeoJsonProperties = GeoJsonProperties,\n>(geojson: FeatureCollection, filter: any): FeatureCollection {\n // Validation\n if (!geojson) throw new Error(\"geojson is required\");\n if (geojson.type !== \"FeatureCollection\")\n throw new Error(\"geojson must be a FeatureCollection\");\n if (filter === undefined || filter === null)\n throw new Error(\"filter is required\");\n\n // Filter Features\n var features: Feature[] = [];\n featureEach(geojson, function (feature) {\n if (applyFilter(feature.properties, filter)) features.push(feature);\n });\n return featureCollection(features);\n}\n\n/**\n * Callback for clusterEach\n *\n * @callback clusterEachCallback\n * @param {FeatureCollection} [cluster] The current cluster being processed.\n * @param {any} [clusterValue] Value used to create cluster being processed.\n * @param {number} [currentIndex] The index of the current element being processed in the array.Starts at index 0\n * @returns {void}\n */\n\n/**\n * clusterEach\n *\n * @function\n * @param {FeatureCollection} geojson GeoJSON Features\n * @param {string|number} property GeoJSON property key/value used to create clusters\n * @param {clusterEachCallback} callback a method that takes (cluster, clusterValue, currentIndex)\n * @returns {void}\n * @example\n * var geojson = turf.featureCollection([\n * turf.point([0, 0]),\n * turf.point([2, 4]),\n * turf.point([3, 6]),\n * turf.point([5, 1]),\n * turf.point([4, 2])\n * ]);\n *\n * // Create a cluster using K-Means (adds `cluster` to GeoJSON properties)\n * var clustered = turf.clustersKmeans(geojson);\n *\n * // Iterate over each cluster\n * turf.clusterEach(clustered, 'cluster', function (cluster, clusterValue, currentIndex) {\n * //= cluster\n * //= clusterValue\n * //= currentIndex\n * })\n *\n * // Calculate the total number of clusters\n * var total = 0\n * turf.clusterEach(clustered, 'cluster', function () {\n * total++;\n * });\n *\n * // Create an Array of all the values retrieved from the 'cluster' property\n * var values = []\n * turf.clusterEach(clustered, 'cluster', function (cluster, clusterValue) {\n * values.push(clusterValue);\n * });\n */\nfunction clusterEach<\n G extends GeometryObject,\n P extends GeoJsonProperties = GeoJsonProperties,\n>(\n geojson: FeatureCollection,\n property: number | string,\n callback: (\n cluster: FeatureCollection,\n clusterValue?: any,\n currentIndex?: number\n ) => void\n): void {\n // Validation\n if (!geojson) throw new Error(\"geojson is required\");\n if (geojson.type !== \"FeatureCollection\")\n throw new Error(\"geojson must be a FeatureCollection\");\n if (property === undefined || property === null)\n throw new Error(\"property is required\");\n\n // Create clusters based on property values\n var bins = createBins(geojson, property);\n var values = Object.keys(bins);\n for (var index = 0; index < values.length; index++) {\n var value = values[index];\n var bin = bins[value];\n var features = [];\n for (var i = 0; i < bin.length; i++) {\n features.push(geojson.features[bin[i]]);\n }\n callback(featureCollection(features), value, index);\n }\n}\n\n/**\n * Callback for clusterReduce\n *\n * The first time the callback function is called, the values provided as arguments depend\n * on whether the reduce method has an initialValue argument.\n *\n * If an initialValue is provided to the reduce method:\n * - The previousValue argument is initialValue.\n * - The currentValue argument is the value of the first element present in the array.\n *\n * If an initialValue is not provided:\n * - The previousValue argument is the value of the first element present in the array.\n * - The currentValue argument is the value of the second element present in the array.\n *\n * @callback clusterReduceCallback\n * @param {*} [previousValue] The accumulated value previously returned in the last invocation\n * of the callback, or initialValue, if supplied.\n * @param {FeatureCollection} [cluster] The current cluster being processed.\n * @param {*} [clusterValue] Value used to create cluster being processed.\n * @param {number} [currentIndex] The index of the current element being processed in the\n * array. Starts at index 0, if an initialValue is provided, and at index 1 otherwise.\n * @returns {void}\n */\n\n/**\n * Reduce clusters in GeoJSON Features, similar to Array.reduce()\n *\n * @function\n * @param {FeatureCollection} geojson GeoJSON Features\n * @param {string|number} property GeoJSON property key/value used to create clusters\n * @param {clusterReduceCallback} callback a method that takes (previousValue, cluster, clusterValue, currentIndex)\n * @param {any} [initialValue] Value to use as the first argument to the first call of the callback.\n * @returns {any} The value that results from the reduction.\n * @example\n * var geojson = turf.featureCollection([\n * turf.point([0, 0]),\n * turf.point([2, 4]),\n * turf.point([3, 6]),\n * turf.point([5, 1]),\n * turf.point([4, 2])\n * ]);\n *\n * // Create a cluster using K-Means (adds `cluster` to GeoJSON properties)\n * var clustered = turf.clustersKmeans(geojson);\n *\n * // Iterate over each cluster and perform a calculation\n * var initialValue = 0\n * turf.clusterReduce(clustered, 'cluster', function (previousValue, cluster, clusterValue, currentIndex) {\n * //=previousValue\n * //=cluster\n * //=clusterValue\n * //=currentIndex\n * return previousValue++;\n * }, initialValue);\n *\n * // Calculate the total number of clusters\n * var total = turf.clusterReduce(clustered, 'cluster', function (previousValue) {\n * return previousValue++;\n * }, 0);\n *\n * // Create an Array of all the values retrieved from the 'cluster' property\n * var values = turf.clusterReduce(clustered, 'cluster', function (previousValue, cluster, clusterValue) {\n * return previousValue.concat(clusterValue);\n * }, []);\n */\nfunction clusterReduce<\n G extends GeometryObject,\n P extends GeoJsonProperties = GeoJsonProperties,\n>(\n geojson: FeatureCollection,\n property: number | string,\n callback: (\n previousValue: any | undefined,\n cluster: FeatureCollection,\n clusterValue?: any,\n currentIndex?: number\n ) => void,\n initialValue?: any\n): void {\n var previousValue = initialValue;\n clusterEach(\n geojson,\n property,\n function (cluster, clusterValue, currentIndex) {\n if (currentIndex === 0 && initialValue === undefined)\n previousValue = cluster;\n else\n previousValue = callback(\n previousValue,\n cluster,\n clusterValue,\n currentIndex\n );\n }\n );\n return previousValue;\n}\n\n/**\n * Create Bins\n *\n * @private\n * @param {FeatureCollection} geojson GeoJSON Features\n * @param {string|number} property Property values are used to create bins\n * @returns {Object} bins with Feature IDs\n * @example\n * var geojson = turf.featureCollection([\n * turf.point([0, 0], {cluster: 0, foo: 'null'}),\n * turf.point([2, 4], {cluster: 1, foo: 'bar'}),\n * turf.point([5, 1], {0: 'foo'}),\n * turf.point([3, 6], {cluster: 1}),\n * ]);\n * createBins(geojson, 'cluster');\n * //= { '0': [ 0 ], '1': [ 1, 3 ] }\n */\nfunction createBins(\n geojson: FeatureCollection,\n property: string | number\n) {\n var bins: Record = {};\n\n featureEach(geojson, function (feature, i) {\n var properties = feature.properties || {};\n if (Object.prototype.hasOwnProperty.call(properties, String(property))) {\n var value = properties[property];\n if (Object.prototype.hasOwnProperty.call(bins, value))\n bins[value].push(i);\n else bins[value] = [i];\n }\n });\n return bins;\n}\n\n/**\n * Apply Filter\n *\n * @private\n * @param {*} properties Properties\n * @param {*} filter Filter\n * @returns {boolean} applied Filter to properties\n */\nfunction applyFilter(properties: any, filter: any) {\n if (properties === undefined) return false;\n var filterType = typeof filter;\n\n // String & Number\n if (filterType === \"number\" || filterType === \"string\")\n return Object.prototype.hasOwnProperty.call(properties, filter);\n // Array\n else if (Array.isArray(filter)) {\n for (var i = 0; i < filter.length; i++) {\n if (!applyFilter(properties, filter[i])) return false;\n }\n return true;\n // Object\n } else {\n return propertiesContainsFilter(properties, filter);\n }\n}\n\n/**\n * Properties contains filter (does not apply deepEqual operations)\n *\n * @private\n * @param {*} properties Properties\n * @param {Object} filter Filter\n * @returns {boolean} does filter equal Properties\n * @example\n * propertiesContainsFilter({foo: 'bar', cluster: 0}, {cluster: 0})\n * //= true\n * propertiesContainsFilter({foo: 'bar', cluster: 0}, {cluster: 1})\n * //= false\n */\nfunction propertiesContainsFilter(properties: any, filter: any): boolean {\n var keys = Object.keys(filter);\n for (var i = 0; i < keys.length; i++) {\n var key = keys[i];\n if (properties[key] !== filter[key]) return false;\n }\n return true;\n}\n\n/**\n * Filter Properties\n *\n * @private\n * @param {*} properties Properties\n * @param {Array} keys Used to filter Properties\n * @returns {*} filtered Properties\n * @example\n * filterProperties({foo: 'bar', cluster: 0}, ['cluster'])\n * //= {cluster: 0}\n */\nfunction filterProperties(\n properties: Record,\n keys: string[]\n): any {\n if (!keys) return {};\n if (!keys.length) return {};\n\n var newProperties: Record = {};\n for (var i = 0; i < keys.length; i++) {\n var key = keys[i];\n if (Object.prototype.hasOwnProperty.call(properties, key))\n newProperties[key] = properties[key];\n }\n return newProperties;\n}\n\nexport {\n getCluster,\n clusterEach,\n clusterReduce,\n createBins,\n applyFilter,\n propertiesContainsFilter,\n filterProperties,\n};\n// No default export!\n"],"mappings":";AAMA,SAAS,mBAAmB;AAC5B,SAAS,yBAAyB;AA+BlC,SAAS,WAGP,SAAkC,QAAsC;AAExE,MAAI,CAAC,QAAS,OAAM,IAAI,MAAM,qBAAqB;AACnD,MAAI,QAAQ,SAAS;AACnB,UAAM,IAAI,MAAM,qCAAqC;AACvD,MAAI,WAAW,UAAa,WAAW;AACrC,UAAM,IAAI,MAAM,oBAAoB;AAGtC,MAAI,WAA4B,CAAC;AACjC,cAAkB,SAAS,SAAU,SAAS;AAC5C,QAAI,YAAY,QAAQ,YAAY,MAAM,EAAG,UAAS,KAAK,OAAO;AAAA,EACpE,CAAC;AACD,SAAO,kBAAkB,QAAQ;AACnC;AAmDA,SAAS,YAIP,SACA,UACA,UAKM;AAEN,MAAI,CAAC,QAAS,OAAM,IAAI,MAAM,qBAAqB;AACnD,MAAI,QAAQ,SAAS;AACnB,UAAM,I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