Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return ref layers in featuresAt, refs #847 #901

Merged
merged 2 commits into from
Jan 5, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion js/data/feature_tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ FeatureTree.prototype.query = function(args, callback) {
};

FeatureTree.prototype.formatResults = function(bucketInfo) {
return {
var results = {
$type: bucketInfo.$type,
layer: {
id: bucketInfo.id,
Expand All @@ -57,6 +57,8 @@ FeatureTree.prototype.formatResults = function(bucketInfo) {
layout: bucketInfo.layout
}
};
if (bucketInfo.ref) results.layer.ref = bucketInfo.ref;
return results;
};

FeatureTree.prototype.queryFeatures = function(matching, x, y, radius, params, callback) {
Expand Down
37 changes: 29 additions & 8 deletions js/source/worker_tile.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,22 +152,23 @@ function sortTileIntoBuckets(tile, data, bucketInfo) {

var sourceLayers = {},
buckets = {},
layerName;
layerName,
refs = [];

// For each source layer, find a list of buckets that use data from it
for (var i = 0; i < bucketInfo.length; i++) {
var info = bucketInfo[i];
function matchTileToBucket(info) {
var bucketName = info.id;

var minZoom = info.minzoom;
var maxZoom = info.maxzoom;

if (info.source !== tile.source) continue;
if (minZoom && tile.zoom < minZoom && minZoom < tile.maxZoom) continue;
if (maxZoom && tile.zoom >= maxZoom) continue;
if (info.ref) refs.push(info);

if (info.source !== tile.source) return;
if (minZoom && tile.zoom < minZoom && minZoom < tile.maxZoom) return;
if (maxZoom && tile.zoom >= maxZoom) return;

var bucket = createBucket(info, tile.buffers, tile.collision);
if (!bucket) continue;
if (!bucket) return;
bucket.features = [];
bucket.name = bucketName;
buckets[bucketName] = bucket;
Expand All @@ -182,6 +183,26 @@ function sortTileIntoBuckets(tile, data, bucketInfo) {
sourceLayers[bucketName] = info;
}
}
// For each source layer, find a list of buckets that use data from it
for (var i = 0; i < bucketInfo.length; i++) {
var info = bucketInfo[i];
matchTileToBucket(info);
}

while (refs.length) {
var l = refs.shift();
var refLayer = sourceLayers[l.ref][l.ref];

Object.keys(refLayer).forEach(key => {
if (key !== 'paint' && !l[key]) l[key] = refLayer[key];
});
sourceLayers[l.ref][l.id] = l;

var bucket = createBucket(l, tile.buffers, tile.collision);
bucket.features = [];
bucket.name = l.id;
buckets[l.id] = bucket;
}

// read each layer, and sort its features into buckets
if (data.layers) {
Expand Down
26 changes: 13 additions & 13 deletions js/style/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,9 @@ Style.prototype = util.inherit(Evented, {
var i,
layer,
id,
prop,
prop,
paintProp;

var constants = this.stylesheet.constants;
var globalTrans = this.stylesheet.transition;

Expand All @@ -248,7 +248,7 @@ Style.prototype = util.inherit(Evented, {
if (layer.layers) {
buckets = getBuckets(buckets, ordered, layer.layers);
}
if (!layer.source || !layer.type) {
if (!layer.ref && (!layer.source || !layer.type)) {
continue;
}
var bucket = {id: layer.id};
Expand Down Expand Up @@ -285,7 +285,7 @@ Style.prototype = util.inherit(Evented, {
for (i = 0; i < flattened.length; i++) {
flattened[i] = resolveLayer(layerMap, flattened[i]);
}

// pre-calculate style declarations and transition properties for all layers x all classes
var processedPaintProps = this.processedPaintProps = {};
for (i = 0; i < flattened.length; i++) {
Expand All @@ -297,9 +297,9 @@ Style.prototype = util.inherit(Evented, {
for (prop in layer) {
if (!(/^paint/).test(prop)) continue;
var paint = StyleConstant.resolve(layer[prop], constants);

// makes "" the key for the default paint property, which is a bit
// unusual, but is valid JS and should work in all browsers
// unusual, but is valid JS and should work in all browsers
var className = (prop === "paint") ? "" : prop.slice(6);
var classProps = processedPaintProps[id][className] = {};
for (paintProp in paint) {
Expand All @@ -309,10 +309,10 @@ Style.prototype = util.inherit(Evented, {
classProps[match[1]].transition = paint[paintProp];
} else {
if (!classProps[paintProp]) classProps[paintProp] = {};
classProps[paintProp].styleDeclaration = new StyleDeclaration(renderType, paintProp, paint[paintProp]);
classProps[paintProp].styleDeclaration = new StyleDeclaration(renderType, paintProp, paint[paintProp]);
}
}

// do a second pass to fill in missing transition properties & remove
// transition properties without matching style declaration
for (paintProp in classProps) {
Expand All @@ -321,19 +321,19 @@ Style.prototype = util.inherit(Evented, {
} else {
var trans = classProps[paintProp].transition;
var newTrans = {};
newTrans.duration = trans && trans.duration >= 0 ? trans.duration :
newTrans.duration = trans && trans.duration >= 0 ? trans.duration :
globalTrans && globalTrans.duration >= 0 ? globalTrans.duration : 300;
newTrans.delay = trans && trans.delay >= 0 ? trans.delay :
newTrans.delay = trans && trans.delay >= 0 ? trans.delay :
globalTrans && globalTrans.delay >= 0 ? globalTrans.delay : 0;
classProps[paintProp].transition = newTrans;
}
}
}
}

this.layerGroups = this._groupLayers(this.stylesheet.layers);
this.cascadeClasses(options);

// Resolve layer references.
function resolveLayer(layerMap, layer) {
if (!layer.ref || !layerMap[layer.ref]) return layer;
Expand Down Expand Up @@ -379,7 +379,7 @@ Style.prototype = util.inherit(Evented, {
var layer = flattened[i];
var id = layer.id;
transitions[id] = {};

for (var className in processedPaintProps[id]) {
if (!(className === "" || classes[className])) continue;
var paintProps = processedPaintProps[id][className];
Expand Down
34 changes: 34 additions & 0 deletions test/js/style/style.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,12 @@ test('Style#featuresAt', function(t) {
"paint": {
"line-color": "red"
}
}, {
"id": "landref",
"ref": "land",
"paint": {
"line-color": "blue"
}
}]
});

Expand All @@ -219,6 +225,19 @@ test('Style#featuresAt', function(t) {
'line-cap': 'round'
}
}
}, {
$type: 'Polygon',
layer: {
id: 'landref',
ref: 'land',
type: 'line',
layout: {
'line-cap': 'round'
},
paint: {
'line-color': 'blue'
}
}
}]);
};

Expand Down Expand Up @@ -258,6 +277,21 @@ test('Style#featuresAt', function(t) {
});
});

t.test('ref layer inherits properties', function(t) {
style.featuresAt([256, 256], {}, function(err, results) {
t.error(err);

var layer = results[0].layer;
var refLayer = results[1].layer;
t.deepEqual(layer.layout, refLayer.layout);
t.deepEqual(layer.type, refLayer.type);
t.deepEqual(layer.id, refLayer.ref);
t.notEqual(layer.paint, refLayer.paint);

t.end();
});
});

t.end();
});
});
Expand Down