Skip to content

Commit

Permalink
fix clustering in sf
Browse files Browse the repository at this point in the history
  • Loading branch information
vdelacruzb committed Apr 16, 2024
1 parent 6f7f574 commit f871201
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 9 deletions.
30 changes: 29 additions & 1 deletion clouds/snowflake/libraries/javascript/libs/clustering.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,35 @@
import { featureCollection, feature, clustersKmeans } from '@turf/turf';

function prioritizeDistinctSort(arr) {
const uniqueValues = [];
const duplicatedValues = [];

// Split the array into unique and duplicated values
const countMap = {};
for (const item of arr) {
if (countMap[item] === undefined) {
countMap[item] = 1;
uniqueValues.push(item);
} else {
countMap[item]++;
duplicatedValues.push(item);
}
}

// Sort unique values alphabetically
uniqueValues.sort();

// Sort duplicated values alphabetically
duplicatedValues.sort();

// Concatenate unique and duplicated values
const result = [...uniqueValues, ...duplicatedValues];
return result;
}

export default {
featureCollection,
feature,
clustersKmeans
clustersKmeans,
prioritizeDistinctSort
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ test('clustering library defined', () => {
expect(clusteringLib.featureCollection).toBeDefined();
expect(clusteringLib.feature).toBeDefined();
expect(clusteringLib.clustersKmeans).toBeDefined();
expect(clusteringLib.prioritizeDistinctSort).toBeDefined();
});
3 changes: 1 addition & 2 deletions clouds/snowflake/modules/sql/clustering/ST_CLUSTERKMEANS.sql
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ AS $$
const options = {};
options.numberOfClusters = Number(NUMBEROFCLUSTERS);
options.mutate = true;
GEOJSONS = Array.from(new Set(GEOJSONS));
const featuresCollection = clusteringLib.featureCollection(GEOJSONS.map(x => clusteringLib.feature(JSON.parse(x))));
const featuresCollection = clusteringLib.featureCollection(clusteringLib.prioritizeDistinctSort(GEOJSONS).map(x => clusteringLib.feature(JSON.parse(x))));
clusteringLib.clustersKmeans(featuresCollection, options);
const cluster = [];
featuresCollection.features.forEach(function(item, index, array) {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f871201

Please sign in to comment.