Skip to content

Commit

Permalink
Allow GeoJSON to passed as stringify JSON.
Browse files Browse the repository at this point in the history
  • Loading branch information
dcervelli committed Jan 27, 2016
1 parent 58a2ce5 commit a4510a5
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 4 deletions.
6 changes: 3 additions & 3 deletions js/source/geojson_source.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module.exports = GeoJSONSource;
* Create a GeoJSON data source instance given an options object
* @class GeoJSONSource
* @param {Object} [options]
* @param {Object|string} options.data A GeoJSON data object or URL to it. The latter is preferable in case of large GeoJSON files.
* @param {Object|string} options.data A GeoJSON data object, a JSON.stringified GeoJSON data object (slightly faster than former option), or URL to it (preferable in case of large GeoJSON files).
* @param {number} [options.maxzoom=14] Maximum zoom to preserve detail at.
* @param {number} [options.buffer] Tile buffer on each side.
* @param {number} [options.tolerance] Simplification tolerance (higher means simpler).
Expand Down Expand Up @@ -81,7 +81,7 @@ GeoJSONSource.prototype = util.inherit(Evented, /** @lends GeoJSONSource.prototy
/**
* Update source geojson data and rerender map
*
* @param {Object|string} data A GeoJSON data object or URL to it. The latter is preferable in case of large GeoJSON files.
* @param {Object|string} data A GeoJSON data object, a JSON.stringified GeoJSON data object (slightly faster than former option), or URL to it (preferable in case of large GeoJSON files).
* @returns {GeoJSONSource} this
*/
setData: function(data) {
Expand Down Expand Up @@ -129,7 +129,7 @@ GeoJSONSource.prototype = util.inherit(Evented, /** @lends GeoJSONSource.prototy
_updateData: function() {
this._dirty = false;
var data = this._data;
if (typeof data === 'string' && typeof window != 'undefined') {
if (typeof data === 'string' && typeof window != 'undefined' && data.charAt(0) !== '{') {
data = urlResolve(window.location.href, data);
}
this.workerID = this.dispatcher.send('parse geojson', {
Expand Down
6 changes: 5 additions & 1 deletion js/source/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,11 @@ util.extend(Worker.prototype, {
// 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.data.charAt(0) === '{') {
indexData(null, JSON.parse(params.data));
} else {
ajax.getJSON(params.data, indexData);
}
}
else indexData(null, params.data);
},
Expand Down
48 changes: 48 additions & 0 deletions test/js/source/worker.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,54 @@ test('remove tile', function(t) {
});
});

test('geojson', function(t) {
var geojson = {
"type": "FeatureCollection",
"features": [{
"type": "Feature",
"properties": {},
"geometry": {
"type": "LineString",
"coordinates": [
[-122.48369693756104, 37.83381888486939],
[-122.48348236083984, 37.83317489144141]
]
}
}]
};

// test parsing of GeoJSON in native and stringified form.
[geojson, JSON.stringify(geojson)].forEach(function (data) {
t.test('parses geojson', function(t) {
var worker = new Worker(_self);

worker.loaded = {
source: {
'0': {}
}
};

t.deepEqual(worker.geoJSONIndexes, {});
worker['parse geojson']({
data: data,
tileSize: 512,
source: "test",
geojsonVtOptions: { maxZoom: 20 },
cluster: false,
superclusterOptions: {
maxZoom: 19,
extent: 4096,
radius: 400,
log: false
}
}, function() {
t.notDeepEqual(worker.geoJSONIndexes, {});
t.end();
});
});
});
});

test('after', function(t) {
server.close(t.end);
});

0 comments on commit a4510a5

Please sign in to comment.