Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement @turf/clusters-dbscan module #812

Merged
merged 33 commits into from
Jul 14, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
d0d8970
Implement `@turf/clusters-distance` module
DenisCarriere Jun 21, 2017
24cf66d
Update yarn lock
DenisCarriere Jun 21, 2017
8669d30
Update debug file
DenisCarriere Jun 21, 2017
e5fb827
simplified calculation (run almost x2 faster);
stebogit Jul 7, 2017
17ca549
Convert index.js to ES5
DenisCarriere Jul 12, 2017
ed0e48a
Publish new clusters-distance approach
DenisCarriere Jul 12, 2017
88d3953
Add minPoints to tests param
DenisCarriere Jul 12, 2017
cf53d66
Update Typescript tests
DenisCarriere Jul 12, 2017
4636f3a
Merge branch 'master' into clusters-distance
DenisCarriere Jul 12, 2017
0893af2
added units parameter; added parameters validation and throw tests
stebogit Jul 13, 2017
359815a
Suggested DBSCAN implementation for `@turf/clusters-distance` (#840)
stebogit Jul 14, 2017
a14fa69
Update Typescript Defintion (3 outputs)
DenisCarriere Jul 14, 2017
0a60b90
Merge branch 'master' into clusters-distance
DenisCarriere Jul 14, 2017
144ef79
Merge branch 'clusters-distance' of /~https://github.com/Turfjs/turf in…
stebogit Jul 14, 2017
ef76c37
Add geokdbush as reference to repo
DenisCarriere Jul 14, 2017
7bee88f
Single line JSDocs param
DenisCarriere Jul 14, 2017
3eb3d66
Fix tests (results.points)
DenisCarriere Jul 14, 2017
07dea46
Make both index + index.geokdbush work
DenisCarriere Jul 14, 2017
62e4e91
Place Geokdbush to DevDependencies
DenisCarriere Jul 14, 2017
63275fc
Fix noise issue
DenisCarriere Jul 14, 2017
82485d8
Prevent input mutation & add edges
DenisCarriere Jul 14, 2017
8a45889
Major changes
DenisCarriere Jul 14, 2017
c2be7be
Create a set of clusters to colorize
DenisCarriere Jul 14, 2017
01e92de
Define edges with cross
DenisCarriere Jul 14, 2017
01bc50b
Add CentroidFromProperty to tests
DenisCarriere Jul 14, 2017
d3a3166
Updates based on @stebogit comments
DenisCarriere Jul 14, 2017
28436e7
Update Readme
DenisCarriere Jul 14, 2017
d926702
Update benchmark results & drop geokdbush
DenisCarriere Jul 14, 2017
7fe4acf
Add noisePoint.properties fallback incase no props
DenisCarriere Jul 14, 2017
67e9071
Added Array of Features handling
DenisCarriere Jul 14, 2017
e7fb4a8
Update library to clusters-dbscan
DenisCarriere Jul 14, 2017
9bdd634
Rename folder to clusters-dbscan
DenisCarriere Jul 14, 2017
af57608
Update readme to clusters-dbscan
DenisCarriere Jul 14, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add CentroidFromProperty to tests
  • Loading branch information
DenisCarriere committed Jul 14, 2017
commit 01bc50b106ab57dcc12cf7d1d8830ed07d0a27d0
46 changes: 46 additions & 0 deletions packages/turf-clusters-distance/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const path = require('path');
const load = require('load-json-file');
const write = require('write-json-file');
const chromatism = require('chromatism');
const centerOfMass = require('@turf/center-of-mass');
const {featureEach, propEach} = require('@turf/meta');
const {featureCollection, point, polygon} = require('@turf/helpers');
const clustersDistance = require('./');
Expand Down Expand Up @@ -99,5 +100,50 @@ function colorize(clustered) {
}
}
});
// Create Centroid from cluster property
const properties = {
'marker-symbol': 'star',
'marker-size': 'large'
};
const centroids = centroidFromProperty(points, 'cluster', properties);
featureEach(centroids, point => {
const color = chromatism.brightness(-15, colours[point.properties.cluster]).hex;
point.properties['marker-color'] = color;
points.push(point);
});

return featureCollection(points);
}

/**
* Basic implementation of creating centroids based on a single Property
* Can be expanded with very complex property combination
*
* @private
* @param {FeatureCollection|Feature[]} geojson GeoJSON
* @param {string} property Name of property of GeoJSON Properties to bin each feature - This property will be translated to each centroid's properties
* @param {*} properties Properties to translate down to each centroid (2nd priority)
* @returns {FeatureCollection<Point>} centroids
* @example
* var centroids = centroidFromProperty(points, 'cluster');
*/
function centroidFromProperty(geojson, property, properties) {
Copy link
Member Author

@DenisCarriere DenisCarriere Jul 14, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stebogit Added back your centroids points 😄

Would be interesting to know the benchmark results on that centroidFromProperty method (i'm sure it's really fast... just quickly scans the FeatureCollection once and then applies clusters based on those bins).

image

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mmmh... in density clustering the centroids are less useful/identifying/important than in k-means, I guess. 🤔
But I might be wrong.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I see now, this is only in test.js. Good 👍

Copy link
Member Author

@DenisCarriere DenisCarriere Jul 14, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes! :) only for test.js This where centroidByProperty module would be used... we wouldn't apply this directly in the modules, but for visual purposes.

Also this can be applied against Polygons or any Geometry Types, finding the "centroid" of stuff based on properties is quite useful.

if (Array.isArray(geojson)) geojson = featureCollection(geojson);

const centroids = [];
const bins = new Map();
featureEach(geojson, feature => {
const prop = feature.properties[property];
if (prop === undefined) return;
if (bins.has(prop)) bins.get(prop).push(feature);
else bins.set(prop, [feature]);
});
bins.forEach(features => {
// Retrieve property of first feature (only the defined property tags, nothing else)
const props = JSON.parse(JSON.stringify(properties));
props[property] = features[0].properties[property];
const centroid = centerOfMass(featureCollection(features), props);
centroids.push(centroid);
});
return featureCollection(centroids);
}
32 changes: 32 additions & 0 deletions packages/turf-clusters-distance/test/out/fiji.geojson
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,38 @@
"marker-color": "#ffff00",
"marker-size": "small"
}
},
{
"type": "Feature",
"properties": {
"marker-symbol": "star",
"marker-size": "large",
"cluster": 0,
"marker-color": "#0000b3"
},
"geometry": {
"type": "Point",
"coordinates": [
179.31884765625,
-16.85349333079957
]
}
},
{
"type": "Feature",
"properties": {
"marker-symbol": "star",
"marker-size": "large",
"cluster": 1,
"marker-color": "#b3b300"
},
"geometry": {
"type": "Point",
"coordinates": [
180.992431640625,
-16.436035791177318
]
}
}
]
}
224 changes: 224 additions & 0 deletions packages/turf-clusters-distance/test/out/many-points.geojson
Original file line number Diff line number Diff line change
Expand Up @@ -8000,6 +8000,230 @@
"marker-color": "#cb00ff",
"marker-size": "small"
}
},
{
"type": "Feature",
"properties": {
"marker-symbol": "star",
"marker-size": "large",
"cluster": 0,
"marker-color": "#0000b3"
},
"geometry": {
"type": "Point",
"coordinates": [
-82.2818192293862,
18.574500694938493
]
}
},
{
"type": "Feature",
"properties": {
"marker-symbol": "star",
"marker-size": "large",
"cluster": 1,
"marker-color": "#4600b3"
},
"geometry": {
"type": "Point",
"coordinates": [
-85.50093063239368,
21.224065996591293
]
}
},
{
"type": "Feature",
"properties": {
"marker-symbol": "star",
"marker-size": "large",
"cluster": 2,
"marker-color": "#8e00b3"
},
"geometry": {
"type": "Point",
"coordinates": [
-76.38586227451087,
21.98614593162952
]
}
},
{
"type": "Feature",
"properties": {
"marker-symbol": "star",
"marker-size": "large",
"cluster": 3,
"marker-color": "#b3008c"
},
"geometry": {
"type": "Point",
"coordinates": [
-85.29197467462896,
16.89833198921854
]
}
},
{
"type": "Feature",
"properties": {
"marker-symbol": "star",
"marker-size": "large",
"cluster": 4,
"marker-color": "#b30045"
},
"geometry": {
"type": "Point",
"coordinates": [
-71.19866708595949,
25.44237624486178
]
}
},
{
"type": "Feature",
"properties": {
"marker-symbol": "star",
"marker-size": "large",
"cluster": 5,
"marker-color": "#b30000"
},
"geometry": {
"type": "Point",
"coordinates": [
-84.40228073804343,
24.487873950148828
]
}
},
{
"type": "Feature",
"properties": {
"marker-symbol": "star",
"marker-size": "large",
"cluster": 11,
"marker-color": "#00b348"
},
"geometry": {
"type": "Point",
"coordinates": [
-86.27619027173938,
19.29109029324635
]
}
},
{
"type": "Feature",
"properties": {
"marker-symbol": "star",
"marker-size": "large",
"cluster": 6,
"marker-color": "#b34700"
},
"geometry": {
"type": "Point",
"coordinates": [
-82.06608907239291,
17.412329163401335
]
}
},
{
"type": "Feature",
"properties": {
"marker-symbol": "star",
"marker-size": "large",
"cluster": 10,
"marker-color": "#00b301"
},
"geometry": {
"type": "Point",
"coordinates": [
-82.73913749561382,
26.855457529245705
]
}
},
{
"type": "Feature",
"properties": {
"marker-symbol": "star",
"marker-size": "large",
"cluster": 7,
"marker-color": "#b38f00"
},
"geometry": {
"type": "Point",
"coordinates": [
-80.96312016186486,
27.219565274371575
]
}
},
{
"type": "Feature",
"properties": {
"marker-symbol": "star",
"marker-size": "large",
"cluster": 12,
"marker-color": "#00b390"
},
"geometry": {
"type": "Point",
"coordinates": [
-81.1051665883531,
24.395330154899057
]
}
},
{
"type": "Feature",
"properties": {
"marker-symbol": "star",
"marker-size": "large",
"cluster": 8,
"marker-color": "#8eb300"
},
"geometry": {
"type": "Point",
"coordinates": [
-76.16707065674852,
21.25441552688179
]
}
},
{
"type": "Feature",
"properties": {
"marker-symbol": "star",
"marker-size": "large",
"cluster": 9,
"marker-color": "#46b300"
},
"geometry": {
"type": "Point",
"coordinates": [
-78.7626466683383,
20.005421882638764
]
}
},
{
"type": "Feature",
"properties": {
"marker-symbol": "star",
"marker-size": "large",
"cluster": 13,
"marker-color": "#008db3"
},
"geometry": {
"type": "Point",
"coordinates": [
-74.77871030764776,
16.703901619603467
]
}
}
]
}
48 changes: 48 additions & 0 deletions packages/turf-clusters-distance/test/out/noise.geojson
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,54 @@
21.718679805703154
]
}
},
{
"type": "Feature",
"properties": {
"marker-symbol": "star",
"marker-size": "large",
"cluster": 0,
"marker-color": "#0000b3"
},
"geometry": {
"type": "Point",
"coordinates": [
-82.29918627072443,
23.09352518096456
]
}
},
{
"type": "Feature",
"properties": {
"marker-symbol": "star",
"marker-size": "large",
"cluster": 1,
"marker-color": "#b30057"
},
"geometry": {
"type": "Point",
"coordinates": [
-75.85202121882148,
20.122630322815162
]
}
},
{
"type": "Feature",
"properties": {
"marker-symbol": "star",
"marker-size": "large",
"cluster": 2,
"marker-color": "#b3b300"
},
"geometry": {
"type": "Point",
"coordinates": [
-80.44391685584579,
22.135422527231796
]
}
}
]
}
Loading