From 7a12ac2868757506fc3fe3aa01745411a57c2f2e Mon Sep 17 00:00:00 2001 From: Vladimir Agafonkin Date: Fri, 4 Mar 2016 18:47:43 +0200 Subject: [PATCH] pass GeoJSON in stringified form This is a reapplication of #2001 without changing the API, and serves as the middle ground for improving `setData` performance as discussed in #1504. --- js/source/geojson_source.js | 17 ++++++++++------- js/source/worker.js | 11 ++++++----- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/js/source/geojson_source.js b/js/source/geojson_source.js index f12d6912bb9..e6b683f67d2 100644 --- a/js/source/geojson_source.js +++ b/js/source/geojson_source.js @@ -142,18 +142,21 @@ GeoJSONSource.prototype = util.inherit(Evented, /** @lends GeoJSONSource.prototy _updateData: function() { this._dirty = false; - var data = this._data; - if (typeof data === 'string' && typeof window != 'undefined') { - data = urlResolve(window.location.href, data); - } - this.workerID = this.dispatcher.send('parse geojson', { - data: data, + var options = { tileSize: this.tileSize, source: this.id, geojsonVtOptions: this.geojsonVtOptions, cluster: this.cluster, superclusterOptions: this.superclusterOptions - }, function(err) { + }; + + var data = this._data; + if (typeof data === 'string') { + options.url = typeof window != 'undefined' ? urlResolve(window.location.href, data) : data; + } else { + options.data = JSON.stringify(data); + } + this.workerID = this.dispatcher.send('parse geojson', options, function(err) { this._loaded = true; if (err) { this.fire('error', {error: err}); diff --git a/js/source/worker.js b/js/source/worker.js index 6add94ae8ff..b1f31798aeb 100644 --- a/js/source/worker.js +++ b/js/source/worker.js @@ -126,16 +126,17 @@ util.extend(Worker.prototype, { callback(null); }.bind(this); - // TODO accept params.url for urls instead - // Not, because of same origin issues, urls must either include an // explicit origin or absolute path. // ie: /foo/bar.json or http://example.com/bar.json // but not ../foo/bar.json - if (typeof params.data === 'string') { - ajax.getJSON(params.data, indexData); + if (params.url) { + ajax.getJSON(params.url, indexData); + } else if (typeof params.data === 'string') { + indexData(null, JSON.parse(params.data)); + } else { + return callback(new Error("Input data is not a valid GeoJSON object.")); } - else indexData(null, params.data); }, 'load geojson tile': function(params, callback) {