-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathcluster.dart
333 lines (315 loc) · 10.6 KB
/
cluster.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
import 'package:turf/helpers.dart';
import 'package:turf/src/meta/feature.dart';
/// Get Cluster
/// Takes a [FeatureCollection<Feature>] and a [dynamic] [filter] used on GeoJSON properties
/// to get Cluster.
/// Returns a [FeatureCollection] single cluster filtered by GeoJSON Properties
/// For example:
///
/// ```dart
/// var geojson = FeatureCollection<Point>(features: [
/// Feature(
/// geometry: Point(coordinates: Position.of([10, 10])),
/// properties: {'marker-symbol': 'circle'},
/// ),
/// Feature(
/// geometry: Point(coordinates: Position.of([20, 20])),
/// properties: {'marker-symbol': 'circle'},
/// ),
/// Feature(
/// geometry: Point(coordinates: Position.of([30, 30])),
/// properties: {'marker-symbol': 'square'},
/// ),
/// Feature(
/// geometry: Point(coordinates: Position.of([40, 40])),
/// properties: {'marker-symbol': 'triangle'},
/// ),
/// ]);
///
/// // Creates a cluster using K-Means (adds `cluster` to GeoJSON properties)
/// var clustered = clustersKmeans(geojson);
///
/// // Retrieves first cluster (0)
/// var cluster = getCluster(clustered, {cluster: 0});
/// //= cluster
///
/// // Retrieves cluster based on custom properties
/// getCluster(clustered, {'marker-symbol': 'circle'}).length;
/// //= 2
/// getCluster(clustered, {'marker-symbol': 'square'}).length;
/// //= 1
/// ```
FeatureCollection getCluster(FeatureCollection geojson, dynamic filter) {
// Filter Features
List<Feature> features = [];
featureEach(geojson, (feature, i) {
if (applyFilter(feature.properties, filter)) features.add(feature);
});
return FeatureCollection(features: features);
}
/// ClusterEachCallback
/// Takes a [FeatureCollection], the cluster being processed, a [clusterValue]
/// used to create cluster being processed, and the [currentIndex], the index of
/// current element being processed in the [List]. Starts at index 0
/// Returns void.
typedef ClusterEachCallback = dynamic Function(
FeatureCollection? cluster,
dynamic clusterValue,
int? currentIndex,
);
/// clusterEach
/// Takes a [FeatureCollection], a dynamic [property] key/value used to create clusters,
/// and a [ClusterEachCallback] method that takes (cluster, clusterValue, currentIndex) and
/// Returns void.
/// For example:
///
/// ```dart
/// var geojson = FeatureCollection<Point>(features: [
/// Feature(
/// geometry: Point(coordinates: Position.of([10, 10])),
/// ),
/// Feature(
/// geometry: Point(coordinates: Position.of([20, 20])),
/// ),
/// Feature(
/// geometry: Point(coordinates: Position.of([30, 30])),
/// ),
/// Feature(
/// geometry: Point(coordinates: Position.of([40, 40])),
/// ),
/// ]);
///
/// // Create a cluster using K-Means (adds `cluster` to [GeoJSONObject]'s properties)
/// var clustered = clustersKmeans(geojson);
///
/// // Iterates over each cluster
/// clusterEach(clustered, 'cluster', (cluster, clusterValue, currentIndex) {
/// //= cluster
/// //= clusterValue
/// //= currentIndex
/// })
///
/// // Calculates the total number of clusters
/// var total = 0
/// clusterEach(clustered, 'cluster', function () {
/// total++;
/// });
///
/// // Creates [List] of all the values retrieved from the 'cluster' property
/// var values = []
/// clusterEach(clustered, 'cluster', (cluster, clusterValue) {
/// values.add(clusterValue);
/// });
/// ```
void clusterEach(
FeatureCollection geojson, dynamic property, ClusterEachCallback callback) {
if (property == null) {
throw Exception("property is required");
}
// Creates clusters based on property values
var bins = createBins(geojson, property);
var values = bins.keys.toList();
for (var index = 0; index < values.length; index++) {
var value = values[index];
List<int> bin = bins[value]!;
List<Feature> features = [];
for (var i = 0; i < bin.length; i++) {
features.add(geojson.features[bin[i]]);
}
callback(FeatureCollection(features: features), value, index);
}
}
/// ClusterReduceCallback
/// The first time the callback function is called, the values provided as arguments depend
/// on whether the reduce method has an [initialValue] argument.
///
/// If an [initialValue] is provided to the reduce method:
/// - The [previousValue] argument is [initialValue].
/// - The [currentValue] argument is the value of the first element present in the [List].
///
/// If an [initialValue] is not provided:
/// - The [previousValue] argument is the value of the first element present in the [List].
/// - The [currentValue] argument is the value of the second element present in the [List].
///
/// Takes a [previousValue], the accumulated value previously returned in the last invocation
/// of the callback, or [initialValue], if supplied, a [FeatureCollection] [cluster], the current
/// cluster being processed, a [clusterValue] used to create cluster being processed and a
/// [currentIndex], the index of the current element being processed in the
/// [List].
/// Starts at index 0, if an [initialValue] is provided, and at index 1 otherwise.
typedef ClusterReduceCallback<T> = T? Function(
T? previousValue,
FeatureCollection? cluster,
dynamic clusterValue,
int? currentIndex,
);
/// Reduces clusters in Features, similar to [Iterable.reduce]
/// Takes a [FeatureCollection][geojson], a dynamic [porperty], a [GeoJSONObject]'s property key/value
/// used to create clusters, a [ClusterReduceCallback] method, and an [initialValue] to
/// use as the first argument to the first call of the callback.
/// Returns the value that results from the reduction.
/// For example:
///
/// ```dart
/// var geojson = FeatureCollection<Point>(features: [
/// Feature(
/// geometry: Point(coordinates: Position.of([10, 10])),
/// ),
/// Feature(
/// geometry: Point(coordinates: Position.of([20, 20])),
/// ),
/// Feature(
/// geometry: Point(coordinates: Position.of([30, 30])),
/// ),
/// Feature(
/// geometry: Point(coordinates: Position.of([40, 40])),
/// ),
/// ]);
///
/// // Creates a cluster using K-Means (adds `cluster` to GeoJSON properties)
/// var clustered = clustersKmeans(geojson);
///
/// // Iterates over each cluster and perform a calculation
/// var initialValue = 0
/// clusterReduce(clustered, 'cluster', (previousValue, cluster, clusterValue, currentIndex) {
/// //=previousValue
/// //=cluster
/// //=clusterValue
/// //=currentIndex
/// return previousValue++;
/// }, initialValue);
///
/// // Calculates the total number of clusters
/// var total = clusterReduce(clustered, 'cluster', function (previousValue) {
/// return previousValue++;
/// }, 0);
///
/// // Creates a [List] of all the values retrieved from the 'cluster' property.
/// var values = clusterReduce(clustered, 'cluster', (previousValue, cluster, clusterValue){
/// return previousValue.addAll(clusterValue);
/// }, []);
/// ```
T? clusterReduce<T>(
FeatureCollection geojson,
dynamic property,
ClusterReduceCallback<T> callback,
dynamic initialValue,
) {
var previousValue = initialValue;
clusterEach(geojson, property, (cluster, clusterValue, currentIndex) {
if (currentIndex == 0 && initialValue == null) {
previousValue = cluster;
} else {
previousValue =
callback(previousValue, cluster, clusterValue, currentIndex);
}
});
return previousValue;
}
/// createBins
/// Takes a [FeatureCollection] geojson, and dynamic [property] key whose
/// corresponding values of the [Feature]s will be used to create bins.
/// Returns Map<String, List<int>> bins with Feature IDs
/// For example
///
/// ```dart
/// var geojson = FeatureCollection<Point>(features: [
/// Feature(
/// geometry: Point(coordinates: Position.of([10, 10])),
/// properties:{'cluster': 0, 'foo': 'null'},
/// ),
/// Feature(
/// geometry: Point(coordinates: Position.of([20, 20])),
/// properties: {'cluster': 1, 'foo': 'bar'},
/// ),
/// Feature(
/// geometry: Point(coordinates: Position.of([30, 30])),
/// properties: {'0': 'foo'},
/// ),
/// Feature(
/// geometry: Point(coordinates: Position.of([40, 40])),
/// properties: {'cluster': 1},
/// ),
/// ]);
/// createBins(geojson, 'cluster');
/// //= { '0': [ 0 ], '1': [ 1, 3 ] }
/// ```
Map<dynamic, List<int>> createBins(
FeatureCollection geojson, dynamic property) {
Map<dynamic, List<int>> bins = {};
featureEach(geojson, (feature, i) {
var properties = feature.properties ?? {};
if (properties.containsKey(property)) {
var value = properties[property];
if (bins.containsKey(value)) {
bins[value]!.add(i);
} else {
bins[value] = [i];
}
}
});
return bins;
}
/// applyFilter
/// Takes a [Map] [properties] and a [filter],
/// Returns a [bool] indicating filter is applied to the properties.
bool applyFilter(Map? properties, dynamic filter) {
if (properties == null) return false;
if (filter is! List && filter is! Map && filter is! String) {
throw Exception("filter('s) key must be String");
}
if (filter is String) {
return properties.containsKey(filter);
}
if (filter is List) {
for (var i = 0; i < filter.length; i++) {
if (!applyFilter(properties, filter[i])) return false;
}
return true;
}
if (filter is Map) {
return propertiesContainsFilter(properties, filter);
}
return false;
}
/// Properties contains filter (does not apply deepEqual operations)
/// Takes a [Map] [properties] value, and a [Map] filter and
/// Returns [bool] if filter does equal the [properties]
/// For example
///
/// ```dart
/// propertiesContainsFilter({foo: 'bar', cluster: 0}, {cluster: 0})
/// //= true
/// propertiesContainsFilter({foo: 'bar', cluster: 0}, {cluster: 1})
/// //= false
/// ```
bool propertiesContainsFilter(Map properties, Map filter) {
var keys = filter.keys.toList();
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
if (properties[key] != filter[key]) return false;
}
return true;
}
/// filterProperties
/// Takes [Map<String, dynamic>] [properties], and [List<String>] [keys] used to
/// filter Properties.
/// Returns [Map<String, dynamic>] filtered Properties
/// For example:
///
/// ```dart
/// filterProperties({foo: 'bar', cluster: 0}, ['cluster'])
/// //= {cluster: 0}
/// ```
Map<String, dynamic> filterProperties(
Map<String, dynamic> properties, List<String>? keys) {
if (keys == null || keys.isEmpty) return {};
Map<String, dynamic> newProperties = {};
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
if (properties.containsKey(key)) {
newProperties[key] = properties[key];
}
}
return newProperties;
}