| 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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872 |
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
183×
183×
183×
183×
183×
183×
183×
183×
183×
183×
183×
183×
183×
183×
183×
183×
5×
5×
178×
34×
183×
59×
183×
39×
183×
64×
1×
1×
70×
69×
1×
70×
70×
2×
67×
67×
67×
44×
44×
26×
23×
67×
1×
542×
542×
1×
545×
545×
545×
17×
15×
2×
528×
527×
544×
1×
145×
145×
1×
184×
184×
184×
184×
184×
475×
475×
475×
475×
184×
475×
475×
475×
475×
475×
459×
459×
459×
16×
184×
46×
184×
475×
1×
64×
64×
64×
322×
307×
307×
307×
64×
299×
7×
7×
7×
64×
322×
15×
15×
15×
15×
15×
64×
299×
292×
292×
292×
292×
292×
64×
1×
168×
164×
298×
298×
164×
1×
1×
1×
4×
4×
3×
3×
168×
165×
168×
168×
168×
168×
5×
168×
168×
168×
168×
168×
1×
1×
1×
73×
72×
1×
1×
1×
1×
45×
1×
37×
37×
8×
29×
29×
29×
5×
37×
37×
1×
1×
25×
25×
1×
1×
1×
22×
22×
1×
224×
224×
224×
224×
6×
1×
1×
1×
218×
218×
1×
1×
1×
217×
21×
224×
224×
224×
3×
3×
1×
1×
2×
2×
2×
2×
221×
1×
1×
1×
220×
224×
224×
1×
4×
1×
70×
70×
70×
70×
70×
70×
1×
70×
69×
69×
1×
312×
312×
12×
300×
11×
312×
312×
1×
325×
325×
325×
325×
325×
325×
3×
322×
325×
1×
3×
3×
4×
3×
3×
3×
3×
1×
1259×
1259×
1×
| // FIXME bulk feature upload - suppress events
// FIXME make change-detection more refined (notably, geometry hint)
goog.provide('ol.source.Vector');
goog.provide('ol.source.VectorEvent');
goog.provide('ol.source.VectorEventType');
goog.require('goog.array');
goog.require('goog.asserts');
goog.require('goog.events');
goog.require('goog.events.Event');
goog.require('goog.events.EventType');
goog.require('goog.object');
goog.require('ol');
goog.require('ol.Collection');
goog.require('ol.CollectionEventType');
goog.require('ol.Extent');
goog.require('ol.Feature');
goog.require('ol.FeatureLoader');
goog.require('ol.LoadingStrategy');
goog.require('ol.ObjectEventType');
goog.require('ol.extent');
goog.require('ol.featureloader');
goog.require('ol.loadingstrategy');
goog.require('ol.proj');
goog.require('ol.source.Source');
goog.require('ol.source.State');
goog.require('ol.structs.RBush');
/**
* @enum {string}
*/
ol.source.VectorEventType = {
/**
* Triggered when a feature is added to the source.
* @event ol.source.VectorEvent#addfeature
* @api stable
*/
ADDFEATURE: 'addfeature',
/**
* Triggered when a feature is updated.
* @event ol.source.VectorEvent#changefeature
* @api
*/
CHANGEFEATURE: 'changefeature',
/**
* Triggered when the clear method is called on the source.
* @event ol.source.VectorEvent#clear
* @api
*/
CLEAR: 'clear',
/**
* Triggered when a feature is removed from the source.
* See {@link ol.source.Vector#clear source.clear()} for exceptions.
* @event ol.source.VectorEvent#removefeature
* @api stable
*/
REMOVEFEATURE: 'removefeature'
};
/**
* @classdesc
* Provides a source of features for vector layers. Vector features provided
* by this source are suitable for editing. See {@link ol.source.VectorTile} for
* vector data that is optimized for rendering.
*
* @constructor
* @extends {ol.source.Source}
* @fires ol.source.VectorEvent
* @param {olx.source.VectorOptions=} opt_options Vector source options.
* @api stable
*/
ol.source.Vector = function(opt_options) {
var options = opt_options || {};
goog.base(this, {
attributions: options.attributions,
logo: options.logo,
projection: undefined,
state: ol.source.State.READY,
wrapX: options.wrapX !== undefined ? options.wrapX : true
});
/**
* @private
* @type {ol.FeatureLoader}
*/
this.loader_ = ol.nullFunction;
Iif (options.loader !== undefined) {
this.loader_ = options.loader;
} else Iif (options.url !== undefined) {
goog.asserts.assert(options.format !== undefined,
'format must be set when url is set');
// create a XHR feature loader for "url" and "format"
this.loader_ = ol.featureloader.xhr(options.url, options.format);
}
/**
* @private
* @type {ol.LoadingStrategy}
*/
this.strategy_ = options.strategy !== undefined ? options.strategy :
ol.loadingstrategy.all;
var useSpatialIndex =
options.useSpatialIndex !== undefined ? options.useSpatialIndex : true;
/**
* @private
* @type {ol.structs.RBush.<ol.Feature>}
*/
this.featuresRtree_ = useSpatialIndex ? new ol.structs.RBush() : null;
/**
* @private
* @type {ol.structs.RBush.<{extent: ol.Extent}>}
*/
this.loadedExtentsRtree_ = new ol.structs.RBush();
/**
* @private
* @type {Object.<string, ol.Feature>}
*/
this.nullGeometryFeatures_ = {};
/**
* A lookup of features by id (the return from feature.getId()).
* @private
* @type {Object.<string, ol.Feature>}
*/
this.idIndex_ = {};
/**
* A lookup of features without id (keyed by goog.getUid(feature)).
* @private
* @type {Object.<string, ol.Feature>}
*/
this.undefIdIndex_ = {};
/**
* @private
* @type {Object.<string, Array.<goog.events.Key>>}
*/
this.featureChangeKeys_ = {};
/**
* @private
* @type {ol.Collection.<ol.Feature>}
*/
this.featuresCollection_ = null;
var collection, features;
if (options.features instanceof ol.Collection) {
collection = options.features;
features = collection.getArray();
} else if (goog.isArray(options.features)) {
features = options.features;
}
if (!useSpatialIndex && collection === undefined) {
collection = new ol.Collection(features);
}
if (features !== undefined) {
this.addFeaturesInternal(features);
}
if (collection !== undefined) {
this.bindFeaturesCollection_(collection);
}
};
goog.inherits(ol.source.Vector, ol.source.Source);
/**
* Add a single feature to the source. If you want to add a batch of features
* at once, call {@link ol.source.Vector#addFeatures source.addFeatures()}
* instead.
* @param {ol.Feature} feature Feature to add.
* @api stable
*/
ol.source.Vector.prototype.addFeature = function(feature) {
this.addFeatureInternal(feature);
this.changed();
};
/**
* Add a feature without firing a `change` event.
* @param {ol.Feature} feature Feature.
* @protected
*/
ol.source.Vector.prototype.addFeatureInternal = function(feature) {
var featureKey = goog.getUid(feature).toString();
if (!this.addToIndex_(featureKey, feature)) {
return;
}
this.setupChangeEvents_(featureKey, feature);
var geometry = feature.getGeometry();
if (geometry) {
var extent = geometry.getExtent();
if (this.featuresRtree_) {
this.featuresRtree_.insert(extent, feature);
}
} else {
this.nullGeometryFeatures_[featureKey] = feature;
}
this.dispatchEvent(
new ol.source.VectorEvent(ol.source.VectorEventType.ADDFEATURE, feature));
};
/**
* @param {string} featureKey
* @param {ol.Feature} feature
* @private
*/
ol.source.Vector.prototype.setupChangeEvents_ = function(featureKey, feature) {
goog.asserts.assert(!(featureKey in this.featureChangeKeys_),
'key (%s) not yet registered in featureChangeKey', featureKey);
this.featureChangeKeys_[featureKey] = [
goog.events.listen(feature,
goog.events.EventType.CHANGE,
this.handleFeatureChange_, false, this),
goog.events.listen(feature,
ol.ObjectEventType.PROPERTYCHANGE,
this.handleFeatureChange_, false, this)
];
};
/**
* @param {string} featureKey
* @param {ol.Feature} feature
* @return {boolean} `true` if the feature is "valid", in the sense that it is
* also a candidate for insertion into the Rtree, otherwise `false`.
* @private
*/
ol.source.Vector.prototype.addToIndex_ = function(featureKey, feature) {
var valid = true;
var id = feature.getId();
if (id !== undefined) {
if (!(id.toString() in this.idIndex_)) {
this.idIndex_[id.toString()] = feature;
} else {
valid = false;
}
} else {
goog.asserts.assert(!(featureKey in this.undefIdIndex_),
'Feature already added to the source');
this.undefIdIndex_[featureKey] = feature;
}
return valid;
};
/**
* Add a batch of features to the source.
* @param {Array.<ol.Feature>} features Features to add.
* @api stable
*/
ol.source.Vector.prototype.addFeatures = function(features) {
this.addFeaturesInternal(features);
this.changed();
};
/**
* Add features without firing a `change` event.
* @param {Array.<ol.Feature>} features Features.
* @protected
*/
ol.source.Vector.prototype.addFeaturesInternal = function(features) {
var featureKey, i, length, feature;
var extents = [];
var newFeatures = [];
var geometryFeatures = [];
for (i = 0, length = features.length; i < length; i++) {
feature = features[i];
featureKey = goog.getUid(feature).toString();
Eif (this.addToIndex_(featureKey, feature)) {
newFeatures.push(feature);
}
}
for (i = 0, length = newFeatures.length; i < length; i++) {
feature = newFeatures[i];
featureKey = goog.getUid(feature).toString();
this.setupChangeEvents_(featureKey, feature);
var geometry = feature.getGeometry();
if (geometry) {
var extent = geometry.getExtent();
extents.push(extent);
geometryFeatures.push(feature);
} else {
this.nullGeometryFeatures_[featureKey] = feature;
}
}
if (this.featuresRtree_) {
this.featuresRtree_.load(extents, geometryFeatures);
}
for (i = 0, length = newFeatures.length; i < length; i++) {
this.dispatchEvent(new ol.source.VectorEvent(
ol.source.VectorEventType.ADDFEATURE, newFeatures[i]));
}
};
/**
* @param {!ol.Collection.<ol.Feature>} collection Collection.
* @private
*/
ol.source.Vector.prototype.bindFeaturesCollection_ = function(collection) {
goog.asserts.assert(!this.featuresCollection_,
'bindFeaturesCollection can only be called once');
var modifyingCollection = false;
goog.events.listen(this, ol.source.VectorEventType.ADDFEATURE,
function(evt) {
if (!modifyingCollection) {
modifyingCollection = true;
collection.push(evt.feature);
modifyingCollection = false;
}
});
goog.events.listen(this, ol.source.VectorEventType.REMOVEFEATURE,
function(evt) {
if (!modifyingCollection) {
modifyingCollection = true;
collection.remove(evt.feature);
modifyingCollection = false;
}
});
goog.events.listen(collection, ol.CollectionEventType.ADD,
function(evt) {
if (!modifyingCollection) {
var feature = evt.element;
goog.asserts.assertInstanceof(feature, ol.Feature);
modifyingCollection = true;
this.addFeature(feature);
modifyingCollection = false;
}
}, false, this);
goog.events.listen(collection, ol.CollectionEventType.REMOVE,
function(evt) {
if (!modifyingCollection) {
var feature = evt.element;
goog.asserts.assertInstanceof(feature, ol.Feature);
modifyingCollection = true;
this.removeFeature(feature);
modifyingCollection = false;
}
}, false, this);
this.featuresCollection_ = collection;
};
/**
* Remove all features from the source.
* @param {boolean=} opt_fast Skip dispatching of {@link removefeature} events.
* @api stable
*/
ol.source.Vector.prototype.clear = function(opt_fast) {
if (opt_fast) {
for (var featureId in this.featureChangeKeys_) {
var keys = this.featureChangeKeys_[featureId];
keys.forEach(goog.events.unlistenByKey);
}
if (!this.featuresCollection_) {
this.featureChangeKeys_ = {};
this.idIndex_ = {};
this.undefIdIndex_ = {};
}
} else {
var rmFeatureInternal = this.removeFeatureInternal;
if (this.featuresRtree_) {
this.featuresRtree_.forEach(rmFeatureInternal, this);
goog.object.forEach(this.nullGeometryFeatures_, rmFeatureInternal, this);
}
}
if (this.featuresCollection_) {
this.featuresCollection_.clear();
}
goog.asserts.assert(goog.object.isEmpty(this.featureChangeKeys_),
'featureChangeKeys is an empty object now');
goog.asserts.assert(goog.object.isEmpty(this.idIndex_),
'idIndex is an empty object now');
goog.asserts.assert(goog.object.isEmpty(this.undefIdIndex_),
'undefIdIndex is an empty object now');
if (this.featuresRtree_) {
this.featuresRtree_.clear();
}
this.loadedExtentsRtree_.clear();
this.nullGeometryFeatures_ = {};
var clearEvent = new ol.source.VectorEvent(ol.source.VectorEventType.CLEAR);
this.dispatchEvent(clearEvent);
this.changed();
};
/**
* Iterate through all features on the source, calling the provided callback
* with each one. If the callback returns any "truthy" value, iteration will
* stop and the function will return the same value.
*
* @param {function(this: T, ol.Feature): S} callback Called with each feature
* on the source. Return a truthy value to stop iteration.
* @param {T=} opt_this The object to use as `this` in the callback.
* @return {S|undefined} The return value from the last call to the callback.
* @template T,S
* @api stable
*/
ol.source.Vector.prototype.forEachFeature = function(callback, opt_this) {
if (this.featuresRtree_) {
return this.featuresRtree_.forEach(callback, opt_this);
} else if (this.featuresCollection_) {
return this.featuresCollection_.forEach(callback, opt_this);
}
};
/**
* Iterate through all features whose geometries contain the provided
* coordinate, calling the callback with each feature. If the callback returns
* a "truthy" value, iteration will stop and the function will return the same
* value.
*
* @param {ol.Coordinate} coordinate Coordinate.
* @param {function(this: T, ol.Feature): S} callback Called with each feature
* whose goemetry contains the provided coordinate.
* @param {T=} opt_this The object to use as `this` in the callback.
* @return {S|undefined} The return value from the last call to the callback.
* @template T,S
*/
ol.source.Vector.prototype.forEachFeatureAtCoordinateDirect =
function(coordinate, callback, opt_this) {
var extent = [coordinate[0], coordinate[1], coordinate[0], coordinate[1]];
return this.forEachFeatureInExtent(extent, function(feature) {
var geometry = feature.getGeometry();
goog.asserts.assert(geometry, 'feature geometry is defined and not null');
if (geometry.containsCoordinate(coordinate)) {
return callback.call(opt_this, feature);
} else {
return undefined;
}
});
};
/**
* Iterate through all features whose bounding box intersects the provided
* extent (note that the feature's geometry may not intersect the extent),
* calling the callback with each feature. If the callback returns a "truthy"
* value, iteration will stop and the function will return the same value.
*
* If you are interested in features whose geometry intersects an extent, call
* the {@link ol.source.Vector#forEachFeatureIntersectingExtent
* source.forEachFeatureIntersectingExtent()} method instead.
*
* When `useSpatialIndex` is set to false, this method will loop through all
* features, equivalent to {@link ol.source.Vector#forEachFeature}.
*
* @param {ol.Extent} extent Extent.
* @param {function(this: T, ol.Feature): S} callback Called with each feature
* whose bounding box intersects the provided extent.
* @param {T=} opt_this The object to use as `this` in the callback.
* @return {S|undefined} The return value from the last call to the callback.
* @template T,S
* @api
*/
ol.source.Vector.prototype.forEachFeatureInExtent =
function(extent, callback, opt_this) {
if (this.featuresRtree_) {
return this.featuresRtree_.forEachInExtent(extent, callback, opt_this);
} else Eif (this.featuresCollection_) {
return this.featuresCollection_.forEach(callback, opt_this);
}
};
/**
* Iterate through all features whose geometry intersects the provided extent,
* calling the callback with each feature. If the callback returns a "truthy"
* value, iteration will stop and the function will return the same value.
*
* If you only want to test for bounding box intersection, call the
* {@link ol.source.Vector#forEachFeatureInExtent
* source.forEachFeatureInExtent()} method instead.
*
* @param {ol.Extent} extent Extent.
* @param {function(this: T, ol.Feature): S} callback Called with each feature
* whose geometry intersects the provided extent.
* @param {T=} opt_this The object to use as `this` in the callback.
* @return {S|undefined} The return value from the last call to the callback.
* @template T,S
* @api
*/
ol.source.Vector.prototype.forEachFeatureIntersectingExtent =
function(extent, callback, opt_this) {
return this.forEachFeatureInExtent(extent,
/**
* @param {ol.Feature} feature Feature.
* @return {S|undefined}
* @template S
*/
function(feature) {
var geometry = feature.getGeometry();
goog.asserts.assert(geometry,
'feature geometry is defined and not null');
if (geometry.intersectsExtent(extent)) {
var result = callback.call(opt_this, feature);
if (result) {
return result;
}
}
});
};
/**
* Get the features collection associated with this source. Will be `null`
* unless the source was configured with `useSpatialIndex` set to `false`, or
* with an {@link ol.Collection} as `features`.
* @return {ol.Collection.<ol.Feature>}
* @api
*/
ol.source.Vector.prototype.getFeaturesCollection = function() {
return this.featuresCollection_;
};
/**
* Get all features on the source.
* @return {Array.<ol.Feature>} Features.
* @api stable
*/
ol.source.Vector.prototype.getFeatures = function() {
var features;
if (this.featuresCollection_) {
features = this.featuresCollection_.getArray();
} else Eif (this.featuresRtree_) {
features = this.featuresRtree_.getAll();
if (!goog.object.isEmpty(this.nullGeometryFeatures_)) {
goog.array.extend(
features, goog.object.getValues(this.nullGeometryFeatures_));
}
}
goog.asserts.assert(features !== undefined,
'Neither featuresRtree_ nor featuresCollection_ are available');
return features;
};
/**
* Get all features whose geometry intersects the provided coordinate.
* @param {ol.Coordinate} coordinate Coordinate.
* @return {Array.<ol.Feature>} Features.
* @api stable
*/
ol.source.Vector.prototype.getFeaturesAtCoordinate = function(coordinate) {
var features = [];
this.forEachFeatureAtCoordinateDirect(coordinate, function(feature) {
features.push(feature);
});
return features;
};
/**
* Get all features in the provided extent. Note that this returns all features
* whose bounding boxes intersect the given extent (so it may include features
* whose geometries do not intersect the extent).
*
* This method is not available when the source is configured with
* `useSpatialIndex` set to `false`.
* @param {ol.Extent} extent Extent.
* @return {Array.<ol.Feature>} Features.
* @api
*/
ol.source.Vector.prototype.getFeaturesInExtent = function(extent) {
goog.asserts.assert(this.featuresRtree_,
'getFeaturesInExtent does not work when useSpatialIndex is set to false');
return this.featuresRtree_.getInExtent(extent);
};
/**
* Get the closest feature to the provided coordinate.
*
* This method is not available when the source is configured with
* `useSpatialIndex` set to `false`.
* @param {ol.Coordinate} coordinate Coordinate.
* @return {ol.Feature} Closest feature.
* @api stable
*/
ol.source.Vector.prototype.getClosestFeatureToCoordinate =
function(coordinate) {
// Find the closest feature using branch and bound. We start searching an
// infinite extent, and find the distance from the first feature found. This
// becomes the closest feature. We then compute a smaller extent which any
// closer feature must intersect. We continue searching with this smaller
// extent, trying to find a closer feature. Every time we find a closer
// feature, we update the extent being searched so that any even closer
// feature must intersect it. We continue until we run out of features.
var x = coordinate[0];
var y = coordinate[1];
var closestFeature = null;
var closestPoint = [NaN, NaN];
var minSquaredDistance = Infinity;
var extent = [-Infinity, -Infinity, Infinity, Infinity];
goog.asserts.assert(this.featuresRtree_,
'getClosestFeatureToCoordinate does not work with useSpatialIndex set ' +
'to false');
this.featuresRtree_.forEachInExtent(extent,
/**
* @param {ol.Feature} feature Feature.
*/
function(feature) {
var geometry = feature.getGeometry();
goog.asserts.assert(geometry,
'feature geometry is defined and not null');
var previousMinSquaredDistance = minSquaredDistance;
minSquaredDistance = geometry.closestPointXY(
x, y, closestPoint, minSquaredDistance);
if (minSquaredDistance < previousMinSquaredDistance) {
closestFeature = feature;
// This is sneaky. Reduce the extent that it is currently being
// searched while the R-Tree traversal using this same extent object
// is still in progress. This is safe because the new extent is
// strictly contained by the old extent.
var minDistance = Math.sqrt(minSquaredDistance);
extent[0] = x - minDistance;
extent[1] = y - minDistance;
extent[2] = x + minDistance;
extent[3] = y + minDistance;
}
});
return closestFeature;
};
/**
* Get the extent of the features currently in the source.
*
* This method is not available when the source is configured with
* `useSpatialIndex` set to `false`.
* @return {!ol.Extent} Extent.
* @api stable
*/
ol.source.Vector.prototype.getExtent = function() {
goog.asserts.assert(this.featuresRtree_,
'getExtent does not work when useSpatialIndex is set to false');
return this.featuresRtree_.getExtent();
};
/**
* Get a feature by its identifier (the value returned by feature.getId()).
* Note that the index treats string and numeric identifiers as the same. So
* `source.getFeatureById(2)` will return a feature with id `'2'` or `2`.
*
* @param {string|number} id Feature identifier.
* @return {ol.Feature} The feature (or `null` if not found).
* @api stable
*/
ol.source.Vector.prototype.getFeatureById = function(id) {
var feature = this.idIndex_[id.toString()];
return feature !== undefined ? feature : null;
};
/**
* @param {goog.events.Event} event Event.
* @private
*/
ol.source.Vector.prototype.handleFeatureChange_ = function(event) {
var feature = /** @type {ol.Feature} */ (event.target);
var featureKey = goog.getUid(feature).toString();
var geometry = feature.getGeometry();
if (!geometry) {
if (!(featureKey in this.nullGeometryFeatures_)) {
Eif (this.featuresRtree_) {
this.featuresRtree_.remove(feature);
}
this.nullGeometryFeatures_[featureKey] = feature;
}
} else {
var extent = geometry.getExtent();
if (featureKey in this.nullGeometryFeatures_) {
delete this.nullGeometryFeatures_[featureKey];
Eif (this.featuresRtree_) {
this.featuresRtree_.insert(extent, feature);
}
} else {
if (this.featuresRtree_) {
this.featuresRtree_.update(extent, feature);
}
}
}
var id = feature.getId();
var removed;
if (id !== undefined) {
var sid = id.toString();
if (featureKey in this.undefIdIndex_) {
delete this.undefIdIndex_[featureKey];
this.idIndex_[sid] = feature;
} else {
Eif (this.idIndex_[sid] !== feature) {
removed = this.removeFromIdIndex_(feature);
goog.asserts.assert(removed,
'Expected feature to be removed from index');
this.idIndex_[sid] = feature;
}
}
} else {
if (!(featureKey in this.undefIdIndex_)) {
removed = this.removeFromIdIndex_(feature);
goog.asserts.assert(removed,
'Expected feature to be removed from index');
this.undefIdIndex_[featureKey] = feature;
} else {
goog.asserts.assert(this.undefIdIndex_[featureKey] === feature,
'feature keyed under %s in undefIdKeys', featureKey);
}
}
this.changed();
this.dispatchEvent(new ol.source.VectorEvent(
ol.source.VectorEventType.CHANGEFEATURE, feature));
};
/**
* @return {boolean} Is empty.
*/
ol.source.Vector.prototype.isEmpty = function() {
return this.featuresRtree_.isEmpty() &&
goog.object.isEmpty(this.nullGeometryFeatures_);
};
/**
* @param {ol.Extent} extent Extent.
* @param {number} resolution Resolution.
* @param {ol.proj.Projection} projection Projection.
*/
ol.source.Vector.prototype.loadFeatures = function(
extent, resolution, projection) {
var loadedExtentsRtree = this.loadedExtentsRtree_;
var extentsToLoad = this.strategy_(extent, resolution);
var i, ii;
for (i = 0, ii = extentsToLoad.length; i < ii; ++i) {
var extentToLoad = extentsToLoad[i];
var alreadyLoaded = loadedExtentsRtree.forEachInExtent(extentToLoad,
/**
* @param {{extent: ol.Extent}} object Object.
* @return {boolean} Contains.
*/
function(object) {
return ol.extent.containsExtent(object.extent, extentToLoad);
});
if (!alreadyLoaded) {
this.loader_.call(this, extentToLoad, resolution, projection);
loadedExtentsRtree.insert(extentToLoad, {extent: extentToLoad.slice()});
}
}
};
/**
* Remove a single feature from the source. If you want to remove all features
* at once, use the {@link ol.source.Vector#clear source.clear()} method
* instead.
* @param {ol.Feature} feature Feature to remove.
* @api stable
*/
ol.source.Vector.prototype.removeFeature = function(feature) {
var featureKey = goog.getUid(feature).toString();
if (featureKey in this.nullGeometryFeatures_) {
delete this.nullGeometryFeatures_[featureKey];
} else {
if (this.featuresRtree_) {
this.featuresRtree_.remove(feature);
}
}
this.removeFeatureInternal(feature);
this.changed();
};
/**
* Remove feature without firing a `change` event.
* @param {ol.Feature} feature Feature.
* @protected
*/
ol.source.Vector.prototype.removeFeatureInternal = function(feature) {
var featureKey = goog.getUid(feature).toString();
goog.asserts.assert(featureKey in this.featureChangeKeys_,
'featureKey exists in featureChangeKeys');
this.featureChangeKeys_[featureKey].forEach(goog.events.unlistenByKey);
delete this.featureChangeKeys_[featureKey];
var id = feature.getId();
if (id !== undefined) {
delete this.idIndex_[id.toString()];
} else {
delete this.undefIdIndex_[featureKey];
}
this.dispatchEvent(new ol.source.VectorEvent(
ol.source.VectorEventType.REMOVEFEATURE, feature));
};
/**
* Remove a feature from the id index. Called internally when the feature id
* may have changed.
* @param {ol.Feature} feature The feature.
* @return {boolean} Removed the feature from the index.
* @private
*/
ol.source.Vector.prototype.removeFromIdIndex_ = function(feature) {
var removed = false;
for (var id in this.idIndex_) {
if (this.idIndex_[id] === feature) {
delete this.idIndex_[id];
removed = true;
break;
}
}
return removed;
};
/**
* @classdesc
* Events emitted by {@link ol.source.Vector} instances are instances of this
* type.
*
* @constructor
* @extends {goog.events.Event}
* @implements {oli.source.VectorEvent}
* @param {string} type Type.
* @param {ol.Feature=} opt_feature Feature.
*/
ol.source.VectorEvent = function(type, opt_feature) {
goog.base(this, type);
/**
* The feature being added or removed.
* @type {ol.Feature|undefined}
* @api stable
*/
this.feature = opt_feature;
};
goog.inherits(ol.source.VectorEvent, goog.events.Event);
|