Skip to content

Commit

Permalink
add synchronous version of queryRenderedFeatures
Browse files Browse the repository at this point in the history
  • Loading branch information
ansis committed Mar 4, 2016
1 parent fb88553 commit 9b3a74d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 26 deletions.
18 changes: 7 additions & 11 deletions js/source/source.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ exports._getVisibleCoordinates = function() {
else return this._pyramid.renderedIDs().map(TileCoord.fromID);
};

exports._queryRenderedVectorFeatures = function(queryGeometry, params, classes, zoom, bearing, callback) {
exports._queryRenderedVectorFeatures = function(resultFeatures, queryGeometry, params, classes, zoom, bearing, callback) {
if (!this._pyramid)
return callback(null, []);

Expand All @@ -76,10 +76,10 @@ exports._queryRenderedVectorFeatures = function(queryGeometry, params, classes,
return callback(null, []);

var styleLayers = this.map.style._layers;
var features = [];

var async = true;
if (async) {
var isAsync = callback !== undefined;

if (isAsync) {
util.asyncAll(tilesIn, function(tileIn, callback) {

if (!tileIn.tile.loaded || !tileIn.tile.featureTree) return callback();
Expand All @@ -101,29 +101,25 @@ exports._queryRenderedVectorFeatures = function(queryGeometry, params, classes,
collisionTile: collisionTile.data,
rawTileData: tileIn.tile.rawTileData.slice()
}, function(err_, data) {
if (data) tileIn.tile.featureTree.makeGeoJSON(features, data, styleLayers);
if (data) tileIn.tile.featureTree.makeGeoJSON(resultFeatures, data, styleLayers);
callback();
}, tileIn.tile.workerID);
}.bind(this), function() {
callback(null, features);
callback(null);
});

} else {
for (var r = 0; r < tilesIn.length; r++) {
var tileIn = tilesIn[r];
if (!tileIn.tile.loaded || !tileIn.tile.featureTree) continue;
tileIn.tile.featureTree.query(features, {
tileIn.tile.featureTree.query(resultFeatures, {
queryGeometry: tileIn.queryGeometry,
scale: tileIn.scale,
tileSize: tileIn.tile.tileSize,
bearing: bearing,
params: params
}, styleLayers, true);
}

setTimeout(function() {
callback(null, features);
}, 0);
}
};

Expand Down
31 changes: 20 additions & 11 deletions js/style/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -403,24 +403,33 @@ Style.prototype = util.inherit(Evented, {
},

queryRenderedFeatures: function(queryGeometry, params, classes, zoom, bearing, callback) {
var features = [];
var resultFeatures = [];
var error = null;

if (params.layer) {
params.layerIDs = Array.isArray(params.layer) ? params.layer : [params.layer];
}

util.asyncAll(Object.keys(this.sources), function(id, callback) {
var source = this.sources[id];
source.queryRenderedFeatures(queryGeometry, params, classes, zoom, bearing, function(err, result) {
if (result) features = features.concat(result);
if (err) error = err;
callback();
var isAsync = callback !== undefined;

if (isAsync) {
util.asyncAll(Object.keys(this.sources), function(id, callback) {
var source = this.sources[id];
source.queryRenderedFeatures(resultFeatures, queryGeometry, params, classes, zoom, bearing, function(err) {
if (err) error = err;
callback();
});
}.bind(this), function() {
if (error) return callback(error);
callback(null, resultFeatures);
});
}.bind(this), function() {
if (error) return callback(error);
callback(null, features);
});
} else {
for (var id in this.sources) {
this.sources[id].queryRenderedFeatures(resultFeatures, queryGeometry, params, classes, zoom, bearing);
}

return resultFeatures;
}
},

_remove: function() {
Expand Down
24 changes: 20 additions & 4 deletions js/ui/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -327,9 +327,27 @@ util.extend(Map.prototype, /** @lends Map.prototype */{
* });
*/
queryRenderedFeatures: function(pointOrBox, params, callback) {
if (callback === undefined) {
if (!(pointOrBox instanceof Point || Array.isArray(pointOrBox))) {
callback = params;
params = pointOrBox;
pointOrBox = undefined;
}
var queryGeometry = this._makeQueryGeometry(pointOrBox);
this.style.queryRenderedFeatures(queryGeometry, params, this._classes, this.transform.zoom, this.transform.angle, callback);
return this;
},

queryRenderedFeaturesSync: function(pointOrBox, params) {
if (!(pointOrBox instanceof Point || Array.isArray(pointOrBox))) {
params = pointOrBox;
pointOrBox = undefined;
}
var queryGeometry = this._makeQueryGeometry(pointOrBox);
return this.style.queryRenderedFeatures(queryGeometry, params, this._classes, this.transform.zoom, this.transform.angle);
},

_makeQueryGeometry: function(pointOrBox) {
if (pointOrBox === undefined) {
// bounds was omitted: use full viewport
pointOrBox = [
Point.convert([0, 0]),
Expand Down Expand Up @@ -358,11 +376,9 @@ util.extend(Map.prototype, /** @lends Map.prototype */{
return this.transform.locationCoordinate(this.transform.pointLocation(p).wrap());
}.bind(this));

this.style.queryRenderedFeatures(queryGeometry, params, this._classes, this.transform.zoom, this.transform.angle, callback);
return this;
return queryGeometry;
},


/**
* Get data from vector tiles as an array of GeoJSON Features.
*
Expand Down

0 comments on commit 9b3a74d

Please sign in to comment.