From 56414305908daf246459c64fccfa099c83041856 Mon Sep 17 00:00:00 2001 From: geonux Date: Mon, 6 Jul 2015 14:43:05 +0200 Subject: [PATCH 1/3] RemoveFromLoadedExtent to remove extent to the list of loaded extent in case of server error/restriction. --- src/ol/source/vector.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/ol/source/vector.js b/src/ol/source/vector.js index fc56d22d6c..8100f8e302 100644 --- a/src/ol/source/vector.js +++ b/src/ol/source/vector.js @@ -762,6 +762,27 @@ ol.source.Vector.prototype.loadFeatures = function( }; +/** + * Remove the current extent from the list of loaded extent. + * @param {ol.Extent} extent Extent. + * @api + */ +ol.source.Vector.prototype.removeFromLoadedExtent = function( + extent) { + var loadedExtentsRtree = this.loadedExtentsRtree_; + var obj; + loadedExtentsRtree.forEachInExtent(extent, function(object) { + if (ol.extent.equals(object.extent, extent)) { + obj = object; + return true; + } + }); + if (obj) { + loadedExtentsRtree.remove(obj); + } +}; + + /** * 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 From 8bc61504c8cb7a8a23cbf3504d619627eb4cf85f Mon Sep 17 00:00:00 2001 From: Andreas Hocevar Date: Wed, 6 Dec 2017 13:16:42 +0100 Subject: [PATCH 2/3] Give method a better name and add docs --- externs/olx.js | 31 +++++++++++++++++++++++++++++++ src/ol/source/vector.js | 5 ++--- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/externs/olx.js b/externs/olx.js index 32dadbe6cf..7deab6bc85 100644 --- a/externs/olx.js +++ b/externs/olx.js @@ -6684,6 +6684,37 @@ olx.source.VectorOptions.prototype.format; * The loader function used to load features, from a remote source for example. * If this is not set and `url` is set, the source will create and use an XHR * feature loader. + * + * Example: + * + * ```js + * var vectorSource = new ol.source.Vector({ + * format: new ol.format.GeoJSON(), + * loader: function(extent, resolution, projection) { + * var proj = projection.getCode(); + * var url = 'https://ahocevar.com/geoserver/wfs?service=WFS&' + + * 'version=1.1.0&request=GetFeature&typename=osm:water_areas&' + + * 'outputFormat=application/json&srsname=' + proj + '&' + + * 'bbox=' + extent.join(',') + ',' + proj; + * var xhr = new XMLHttpRequest(); + * xhr.open('GET', url); + * var onError = function() { + * vectorSource.removeLoadedExtent(extent); + * } + * xhr.onerror = onError; + * xhr.onload = function() { + * if (xhr.status == 200) { + * vectorSource.addFeatures( + * vectorSource.getFormat().readFeatures(xhr.responseText)); + * } else { + * onError(); + * } + * } + * xhr.send(); + * }, + * strategy: ol.loadingstrategy.bbox + * }); + * ``` * @type {ol.FeatureLoader|undefined} * @api */ diff --git a/src/ol/source/vector.js b/src/ol/source/vector.js index 8100f8e302..9d2916eddb 100644 --- a/src/ol/source/vector.js +++ b/src/ol/source/vector.js @@ -763,12 +763,11 @@ ol.source.Vector.prototype.loadFeatures = function( /** - * Remove the current extent from the list of loaded extent. + * Remove an extent from the list of loaded extents. * @param {ol.Extent} extent Extent. * @api */ -ol.source.Vector.prototype.removeFromLoadedExtent = function( - extent) { +ol.source.Vector.prototype.removeLoadedExtent = function(extent) { var loadedExtentsRtree = this.loadedExtentsRtree_; var obj; loadedExtentsRtree.forEachInExtent(extent, function(object) { From 45ae731aa63426aba4ac96d7081db5ee0629aaf2 Mon Sep 17 00:00:00 2001 From: Andreas Hocevar Date: Wed, 6 Dec 2017 13:39:35 +0100 Subject: [PATCH 3/3] Add test --- test/spec/ol/source/vector.test.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/spec/ol/source/vector.test.js b/test/spec/ol/source/vector.test.js index 13ea161baf..8b6db3027d 100644 --- a/test/spec/ol/source/vector.test.js +++ b/test/spec/ol/source/vector.test.js @@ -451,6 +451,19 @@ describe('ol.source.Vector', function() { expect(count1).to.eql(1); expect(count2).to.eql(1); }); + + it('removes extents with #removeLoadedExtent()', function(done) { + var source = new ol.source.Vector(); + source.setLoader(function(bbox, resolution, projection) { + setTimeout(function() { + expect(source.loadedExtentsRtree_.getAll()).to.have.length(1); + source.removeLoadedExtent(bbox); + expect(source.loadedExtentsRtree_.getAll()).to.have.length(0); + done(); + }, 0); + }); + source.loadFeatures([-10000, -10000, 10000, 10000], 1, ol.proj.get('EPSG:3857')); + }); }); });