-
Notifications
You must be signed in to change notification settings - Fork 943
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
New @turf/clusters module (getCluster, clusterEach, clusterReduce) #847
Merged
Merged
Changes from 2 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
c8e19f1
Implement getCluster method
DenisCarriere 17303a6
Implement clusterEach & clusterReduce features
DenisCarriere 8fa2b9b
Update Readme
DenisCarriere c692735
Improve `@example` documentation
DenisCarriere 0558cad
Drop Array of Features
DenisCarriere a9ca96e
Update JSDocs
DenisCarriere 0f7acf0
Leverage `@turf/helpers` & `@turf/meta` for GeoJSON compatibility
DenisCarriere 201bfe5
Update tests
DenisCarriere 9847413
Add more documentation
DenisCarriere 7d900c7
Improve example wording
DenisCarriere a12b859
Fix types test
DenisCarriere File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2017 TurfJS | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
this software and associated documentation files (the "Software"), to deal in | ||
the Software without restriction, including without limitation the rights to | ||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
the Software, and to permit persons to whom the Software is furnished to do so, | ||
subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# @turf/clusters | ||
|
||
# getCluster | ||
|
||
Get Cluster | ||
|
||
**Parameters** | ||
|
||
- `geojson` **([FeatureCollection](http://geojson.org/geojson-spec.html#feature-collection-objects) \| [Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<[Feature](http://geojson.org/geojson-spec.html#feature-objects)>)** GeoJSON Features | ||
- `filter` **Any** Filter used on GeoJSON properties to get Cluster | ||
|
||
**Examples** | ||
|
||
```javascript | ||
var geojson = turf.featureCollection([ | ||
turf.point([0, 0], {cluster: 0, foo: 'null'}), | ||
turf.point([2, 4], {cluster: 1, foo: 'bar'}), | ||
turf.point([3, 6], {cluster: 1}), | ||
turf.point([5, 1], {0: 'foo'}), | ||
turf.point([4, 2], {'bar': 'foo'}) | ||
]); | ||
// Create a cluster by Object | ||
var cluster1 = getCluster(geojson, {cluster: 1}); | ||
// Create a cluster by String | ||
var cluster2 = getCluster(geojson, 'cluster'); | ||
// Create a cluster by an Array of Strings | ||
var cluster3 = getCluster(geojson, ['cluster', 'foo']); | ||
``` | ||
|
||
Returns **[FeatureCollection](http://geojson.org/geojson-spec.html#feature-collection-objects)** Single Cluster filtered by GeoJSON Properties | ||
|
||
<!-- This file is automatically generated. Please don't edit it directly: | ||
if you find an error, edit the source file (likely index.js), and re-run | ||
./scripts/generate-readmes in the turf project. --> | ||
|
||
--- | ||
|
||
This module is part of the [Turfjs project](http://turfjs.org/), an open source | ||
module collection dedicated to geographic algorithms. It is maintained in the | ||
[Turfjs/turf](https://github.com/Turfjs/turf) repository, where you can create | ||
PRs and issues. | ||
|
||
### Installation | ||
|
||
Install this module individually: | ||
|
||
```sh | ||
$ npm install @turf/clusters | ||
``` | ||
|
||
Or install the Turf module that includes it as a function: | ||
|
||
```sh | ||
$ npm install @turf/turf | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
const Benchmark = require('benchmark'); | ||
const {featureCollection, point} = require('@turf/helpers'); | ||
const {getCluster, clusterEach, clusterReduce} = require('./'); | ||
const {propertiesContainsFilter, filterProperties, applyFilter, createBins} = require('./'); // Testing Purposes | ||
|
||
const geojson = featureCollection([ | ||
point([0, 0], {cluster: 0}), | ||
point([2, 4], {cluster: 1}), | ||
point([3, 6], {cluster: 1}), | ||
point([5, 1], {0: 'foo'}), | ||
point([4, 2], {'bar': 'foo'}), | ||
point([2, 4], {}), | ||
point([4, 3], undefined) | ||
]); | ||
|
||
/** | ||
* Benchmark Results | ||
* | ||
* testing -- createBins x 2,954,562 ops/sec ±1.29% (89 runs sampled) | ||
* testing -- propertiesContainsFilter x 10,902,619 ops/sec ±2.11% (85 runs sampled) | ||
* testing -- filterProperties x 27,860,084 ops/sec ±1.12% (88 runs sampled) | ||
* testing -- applyFilter x 18,971,594 ops/sec ±7.14% (77 runs sampled) | ||
* getCluster -- string filter x 5,873,843 ops/sec ±1.30% (87 runs sampled) | ||
* getCluster -- object filter x 883,469 ops/sec ±1.28% (93 runs sampled) | ||
* getCluster -- aray filter x 3,602,147 ops/sec ±1.30% (89 runs sampled) | ||
* clusterEach x 1,133,904 ops/sec ±0.90% (89 runs sampled) | ||
* clusterReduce x 1,057,572 ops/sec ±1.13% (89 runs sampled) | ||
*/ | ||
const suite = new Benchmark.Suite('turf-clusters'); | ||
|
||
// Testing Purposes | ||
suite | ||
.add('testing -- createBins', () => createBins(geojson.features, 'cluster')) | ||
.add('testing -- propertiesContainsFilter', () => propertiesContainsFilter({foo: 'bar', cluster: 0}, {cluster: 0})) | ||
.add('testing -- filterProperties', () => filterProperties({foo: 'bar', cluster: 0}, ['cluster'])) | ||
.add('testing -- applyFilter', () => applyFilter({foo: 'bar', cluster: 0}, ['cluster'])); | ||
|
||
suite | ||
.add('getCluster -- string filter', () => getCluster(geojson, 'cluster')) | ||
.add('getCluster -- object filter', () => getCluster(geojson, {cluster: 1})) | ||
.add('getCluster -- aray filter', () => getCluster(geojson, ['cluster'])) | ||
.add('clusterEach', () => clusterEach(geojson, 'cluster', cluster => { return cluster; })) | ||
.add('clusterReduce', () => clusterReduce(geojson, 'cluster', (previousValue, cluster) => { return cluster; })) | ||
.on('cycle', e => console.log(String(e.target))) | ||
.on('complete', () => {}) | ||
.run(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/// <reference types="geojson" /> | ||
|
||
type FeatureCollection<T extends GeoJSON.GeometryObject> = GeoJSON.FeatureCollection<T>; | ||
type Feature<T extends GeoJSON.GeometryObject> = GeoJSON.Feature<T>; | ||
type GeometryObject = GeoJSON.GeometryObject; | ||
|
||
/** | ||
* http://turfjs.org/docs/#getcluster | ||
*/ | ||
export function getCluster<T extends GeometryObject>( | ||
geojson: FeatureCollection<T> | Feature<T>[], | ||
filter: any | ||
): FeatureCollection<T>; | ||
|
||
/** | ||
* http://turfjs.org/docs/#clustereach | ||
*/ | ||
export function clusterEach<T extends GeometryObject>( | ||
geojson: FeatureCollection<T> | Feature<T>[], | ||
property: number | string, | ||
callback: (cluster: FeatureCollection<T>, clusterValue: any, currentIndex: number) => void | ||
): void; | ||
|
||
/** | ||
* http://turfjs.org/docs/#clusterreduce | ||
*/ | ||
export function clusterReduce<T extends GeometryObject>( | ||
geojson: FeatureCollection<T> | Feature<T>[], | ||
property: number | string, | ||
callback: (previousValue: any, cluster: FeatureCollection<T>, clusterValue: any, currentIndex: number) => void, | ||
initialValue: any | ||
): void; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now that I think about it, only in
k-means
the number of clusters is known before applying the module (which often is considered its drawbacks), so you can easily "query" for a specific clusterid
for example; on the other side if you applydbscan
or other algorithms you don't know a priori how many clusters will be identified.It would be useful, I guess, to have a method that returns the total number of clusters in the
FeatureCollection
(basically how many groups of points have the same property with different values); it could be this one when passednull
or'all'
as filter.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This type of questions would be better answered using
clusterEach
orclusterReduce
.Calculating how many total clusters you could use
clusterReduce
andpreviousValue++
which would give you the total, or if you want to figure all of the values which created a bin you could also useclusterReduce
.