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 | 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 318× 318× 318× 318× 318× 318× 318× 318× 318× 318× 318× 318× 318× 318× 318× 318× 318× 318× 318× 5× 5× 313× 73× 318× 102× 318× 78× 318× 107× 1× 1× 205× 204× 1× 205× 205× 2× 202× 202× 202× 179× 179× 129× 23× 202× 1× 895× 1× 898× 898× 898× 18× 16× 2× 880× 879× 897× 1× 215× 215× 1× 293× 293× 293× 293× 293× 693× 693× 693× 693× 293× 693× 693× 693× 693× 693× 676× 676× 676× 17× 293× 95× 293× 693× 1× 107× 107× 484× 456× 456× 456× 107× 429× 11× 11× 11× 107× 484× 28× 28× 28× 107× 429× 418× 418× 418× 107× 1× 237× 229× 420× 420× 229× 1× 1× 1× 8× 7× 7× 3× 237× 230× 237× 9× 237× 237× 237× 237× 237× 1× 1× 1× 155× 153× 2× 2× 1× 1× 79× 1× 54× 54× 8× 46× 46× 46× 6× 54× 1× 1× 27× 1× 2× 2× 2× 2× 2× 2× 2× 2× 9× 5× 5× 5× 5× 3× 3× 3× 3× 3× 3× 2× 1× 1× 22× 22× 1× 1× 151× 1× 1× 1× 342× 342× 342× 342× 8× 2× 1× 2× 334× 334× 1× 1× 1× 333× 21× 342× 342× 3× 3× 1× 1× 2× 2× 2× 339× 1× 1× 342× 342× 1× 4× 1× 154× 154× 154× 154× 154× 154× 5× 154× 149× 149× 1× 442× 442× 12× 430× 11× 442× 442× 1× 455× 455× 455× 455× 455× 3× 452× 455× 1× 3× 3× 4× 3× 3× 3× 3× 1× 1929× 1929× 1× | // FIXME bulk feature upload - suppress events // FIXME make change-detection more refined (notably, geometry hint) goog.provide('ol.source.Vector'); goog.require('ol'); goog.require('ol.Collection'); goog.require('ol.CollectionEventType'); goog.require('ol.ObjectEventType'); goog.require('ol.array'); goog.require('ol.asserts'); goog.require('ol.events'); goog.require('ol.events.Event'); goog.require('ol.events.EventType'); goog.require('ol.extent'); goog.require('ol.featureloader'); goog.require('ol.functions'); goog.require('ol.loadingstrategy'); goog.require('ol.obj'); goog.require('ol.source.Source'); goog.require('ol.source.State'); goog.require('ol.source.VectorEventType'); goog.require('ol.structs.RBush'); /** * @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.Vector.Event * @param {olx.source.VectorOptions=} opt_options Vector source options. * @api */ ol.source.Vector = function(opt_options) { var options = opt_options || {}; ol.source.Source.call(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; /** * @private * @type {ol.format.Feature|undefined} */ this.format_ = options.format; /** * @private * @type {boolean} */ this.overlaps_ = options.overlaps == undefined ? true : options.overlaps; /** * @private * @type {string|ol.FeatureUrlFunction|undefined} */ this.url_ = options.url; Iif (options.loader !== undefined) { this.loader_ = options.loader; } else Iif (this.url_ !== undefined) { ol.asserts.assert(this.format_, 7); // `format` must be set when `url` is set // create a XHR feature loader for "url" and "format" this.loader_ = ol.featureloader.xhr(this.url_, /** @type {ol.format.Feature} */ (this.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 ol.getUid(feature)). * @private * @type {Object.<string, ol.Feature>} */ this.undefIdIndex_ = {}; /** * @private * @type {Object.<string, Array.<ol.EventsKey>>} */ 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 (Array.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); } }; ol.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. A feature will not be added to the source if feature with * the same id is already there. The reason for this behavior is to avoid * feature duplication when using bbox or tile loading strategies. * @param {ol.Feature} feature Feature to add. * @api */ 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 = ol.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.Vector.Event(ol.source.VectorEventType.ADDFEATURE, feature)); }; /** * @param {string} featureKey Unique identifier for the feature. * @param {ol.Feature} feature The feature. * @private */ ol.source.Vector.prototype.setupChangeEvents_ = function(featureKey, feature) { this.featureChangeKeys_[featureKey] = [ ol.events.listen(feature, ol.events.EventType.CHANGE, this.handleFeatureChange_, this), ol.events.listen(feature, ol.ObjectEventType.PROPERTYCHANGE, this.handleFeatureChange_, this) ]; }; /** * @param {string} featureKey Unique identifier for the feature. * @param {ol.Feature} feature The feature. * @return {boolean} The feature is "valid", in the sense that it is also a * candidate for insertion into the Rtree. * @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 { ol.asserts.assert(!(featureKey in this.undefIdIndex_), 30); // The passed `feature` was 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 */ 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 = ol.getUid(feature).toString(); Eif (this.addToIndex_(featureKey, feature)) { newFeatures.push(feature); } } for (i = 0, length = newFeatures.length; i < length; i++) { feature = newFeatures[i]; featureKey = ol.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.Vector.Event( ol.source.VectorEventType.ADDFEATURE, newFeatures[i])); } }; /** * @param {!ol.Collection.<ol.Feature>} collection Collection. * @private */ ol.source.Vector.prototype.bindFeaturesCollection_ = function(collection) { var modifyingCollection = false; ol.events.listen(this, ol.source.VectorEventType.ADDFEATURE, function(evt) { if (!modifyingCollection) { modifyingCollection = true; collection.push(evt.feature); modifyingCollection = false; } }); ol.events.listen(this, ol.source.VectorEventType.REMOVEFEATURE, function(evt) { if (!modifyingCollection) { modifyingCollection = true; collection.remove(evt.feature); modifyingCollection = false; } }); ol.events.listen(collection, ol.CollectionEventType.ADD, function(evt) { if (!modifyingCollection) { modifyingCollection = true; this.addFeature(/** @type {ol.Feature} */ (evt.element)); modifyingCollection = false; } }, this); ol.events.listen(collection, ol.CollectionEventType.REMOVE, function(evt) { if (!modifyingCollection) { modifyingCollection = true; this.removeFeature(/** @type {ol.Feature} */ (evt.element)); modifyingCollection = false; } }, this); this.featuresCollection_ = collection; }; /** * Remove all features from the source. * @param {boolean=} opt_fast Skip dispatching of {@link removefeature} events. * @api */ ol.source.Vector.prototype.clear = function(opt_fast) { if (opt_fast) { for (var featureId in this.featureChangeKeys_) { var keys = this.featureChangeKeys_[featureId]; keys.forEach(ol.events.unlistenByKey); } if (!this.featuresCollection_) { this.featureChangeKeys_ = {}; this.idIndex_ = {}; this.undefIdIndex_ = {}; } } else { if (this.featuresRtree_) { this.featuresRtree_.forEach(this.removeFeatureInternal, this); for (var id in this.nullGeometryFeatures_) { this.removeFeatureInternal(this.nullGeometryFeatures_[id]); } } } if (this.featuresCollection_) { this.featuresCollection_.clear(); } if (this.featuresRtree_) { this.featuresRtree_.clear(); } this.loadedExtentsRtree_.clear(); this.nullGeometryFeatures_ = {}; var clearEvent = new ol.source.Vector.Event(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 */ 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(); if (geometry.intersectsCoordinate(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} The return value from the last call to the callback. * @template S */ function(feature) { var geometry = feature.getGeometry(); 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>} The collection of features. * @api */ ol.source.Vector.prototype.getFeaturesCollection = function() { return this.featuresCollection_; }; /** * Get all features on the source in random order. * @return {Array.<ol.Feature>} Features. * @api */ ol.source.Vector.prototype.getFeatures = function() { var features; if (this.featuresCollection_) { features = this.featuresCollection_.getArray(); } else Eif (this.featuresRtree_) { features = this.featuresRtree_.getAll(); if (!ol.obj.isEmpty(this.nullGeometryFeatures_)) { ol.array.extend( features, ol.obj.getValues(this.nullGeometryFeatures_)); } } return /** @type {Array.<ol.Feature>} */ (features); }; /** * Get all features whose geometry intersects the provided coordinate. * @param {ol.Coordinate} coordinate Coordinate. * @return {Array.<ol.Feature>} Features. * @api */ 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 an array of * all features intersecting the given extent in random order (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) { 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. * @param {function(ol.Feature):boolean=} opt_filter Feature filter function. * The filter function will receive one argument, the {@link ol.Feature feature} * and it should return a boolean value. By default, no filtering is made. * @return {ol.Feature} Closest feature. * @api */ ol.source.Vector.prototype.getClosestFeatureToCoordinate = function(coordinate, opt_filter) { // 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]; var filter = opt_filter ? opt_filter : ol.functions.TRUE; this.featuresRtree_.forEachInExtent(extent, /** * @param {ol.Feature} feature Feature. */ function(feature) { if (filter(feature)) { var geometry = feature.getGeometry(); 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`. * @param {ol.Extent=} opt_extent Destination extent. If provided, no new extent * will be created. Instead, that extent's coordinates will be overwritten. * @return {ol.Extent} Extent. * @api */ ol.source.Vector.prototype.getExtent = function(opt_extent) { return this.featuresRtree_.getExtent(opt_extent); }; /** * 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 */ ol.source.Vector.prototype.getFeatureById = function(id) { var feature = this.idIndex_[id.toString()]; return feature !== undefined ? feature : null; }; /** * Get the format associated with this source. * * @return {ol.format.Feature|undefined} The feature format. * @api */ ol.source.Vector.prototype.getFormat = function() { return this.format_; }; /** * @return {boolean} The source can have overlapping geometries. */ ol.source.Vector.prototype.getOverlaps = function() { return this.overlaps_; }; /** * @override */ ol.source.Vector.prototype.getResolutions = function() {}; /** * Get the url associated with this source. * * @return {string|ol.FeatureUrlFunction|undefined} The url. * @api */ ol.source.Vector.prototype.getUrl = function() { return this.url_; }; /** * @param {ol.events.Event} event Event. * @private */ ol.source.Vector.prototype.handleFeatureChange_ = function(event) { var feature = /** @type {ol.Feature} */ (event.target); var featureKey = ol.getUid(feature).toString(); var geometry = feature.getGeometry(); if (!geometry) { if (!(featureKey in this.nullGeometryFeatures_)) { if (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(); 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) { this.removeFromIdIndex_(feature); this.idIndex_[sid] = feature; } } } else { if (!(featureKey in this.undefIdIndex_)) { this.removeFromIdIndex_(feature); this.undefIdIndex_[featureKey] = feature; } } this.changed(); this.dispatchEvent(new ol.source.Vector.Event( ol.source.VectorEventType.CHANGEFEATURE, feature)); }; /** * @return {boolean} Is empty. */ ol.source.Vector.prototype.isEmpty = function() { return this.featuresRtree_.isEmpty() && ol.obj.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 */ ol.source.Vector.prototype.removeFeature = function(feature) { var featureKey = ol.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 = ol.getUid(feature).toString(); this.featureChangeKeys_[featureKey].forEach(ol.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.Vector.Event( 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 {ol.events.Event} * @implements {oli.source.Vector.Event} * @param {string} type Type. * @param {ol.Feature=} opt_feature Feature. */ ol.source.Vector.Event = function(type, opt_feature) { ol.events.Event.call(this, type); /** * The feature being added or removed. * @type {ol.Feature|undefined} * @api */ this.feature = opt_feature; }; ol.inherits(ol.source.Vector.Event, ol.events.Event); |