Only pass the feature to the attribute getter

This commit is contained in:
Tim Schaub
2022-07-21 13:43:53 -07:00
parent bd9e73a534
commit 7e424be66b
8 changed files with 13 additions and 41 deletions

View File

@@ -18,8 +18,8 @@ class WebGLLayer extends Layer {
className: this.getClassName(),
fill: {
attributes: {
[DefaultAttributes.COLOR]: function (feature, properties) {
const color = asArray(properties.COLOR || '#eee');
[DefaultAttributes.COLOR]: function (feature) {
const color = asArray(feature.get('COLOR') || '#eee');
color[3] = 0.85;
return packColor(color);
},
@@ -30,8 +30,8 @@ class WebGLLayer extends Layer {
},
stroke: {
attributes: {
[DefaultAttributes.COLOR]: function (feature, properties) {
const color = [...asArray(properties.COLOR || '#eee')];
[DefaultAttributes.COLOR]: function (feature) {
const color = [...asArray(feature.get('COLOR') || '#eee')];
color.forEach((_, i) => (color[i] = Math.round(color[i] * 0.75))); // darken slightly
return packColor(color);
},

View File

@@ -14,8 +14,8 @@ import {
* @typedef {Object} CustomAttribute A description of a custom attribute to be passed on to the GPU, with a value different
* for each feature.
* @property {string} name Attribute name.
* @property {function(import("../../Feature").default, Object<string, *>):number} callback This callback computes the numerical value of the
* attribute for a given feature (properties are available as 2nd arg for quicker access).
* @property {function(import("../../Feature").default):number} callback This callback computes the numerical value of the
* attribute for a given feature.
*/
let workerMessageCounter = 0;

View File

@@ -96,10 +96,7 @@ class LineStringBatchRenderer extends AbstractBatchRenderer {
// custom attributes
for (let k = 0, kk = this.customAttributes.length; k < kk; k++) {
value = this.customAttributes[k].callback(
batchEntry.feature,
batchEntry.properties
);
value = this.customAttributes[k].callback(batchEntry.feature);
batch.renderInstructions[renderIndex++] = value;
}

View File

@@ -9,7 +9,6 @@ import {getUid} from '../../util.js';
/**
* @typedef {Object} GeometryBatchItem Object that holds a reference to a feature as well as the raw coordinates of its various geometries
* @property {import("../../Feature").default} feature Feature
* @property {Object<string, *>} properties Feature properties
* @property {Array<Array<number>>} flatCoordss Array of flat coordinates arrays, one for each geometry related to the feature
* @property {number} [verticesCount] Only defined for linestring and polygon batches
* @property {number} [ringsCount] Only defined for polygon batches
@@ -162,7 +161,6 @@ class MixedGeometryBatch {
if (!(uid in this.pointBatch.entries)) {
this.pointBatch.entries[uid] = {
feature: feature,
properties: feature.getProperties(),
flatCoordss: [],
};
}
@@ -179,7 +177,6 @@ class MixedGeometryBatch {
if (!(uid in this.lineStringBatch.entries)) {
this.lineStringBatch.entries[uid] = {
feature: feature,
properties: feature.getProperties(),
flatCoordss: [],
verticesCount: 0,
};
@@ -197,7 +194,6 @@ class MixedGeometryBatch {
if (!(uid in this.polygonBatch.entries)) {
this.polygonBatch.entries[uid] = {
feature: feature,
properties: feature.getProperties(),
flatCoordss: [],
verticesCount: 0,
ringsCount: 0,

View File

@@ -86,10 +86,7 @@ class PointBatchRenderer extends AbstractBatchRenderer {
// pushing custom attributes
for (let j = 0, jj = this.customAttributes.length; j < jj; j++) {
value = this.customAttributes[j].callback(
batchEntry.feature,
batchEntry.properties
);
value = this.customAttributes[j].callback(batchEntry.feature);
batch.renderInstructions[renderIndex++] = value;
}
}

View File

@@ -86,10 +86,7 @@ class PolygonBatchRenderer extends AbstractBatchRenderer {
// custom attributes
for (let k = 0, kk = this.customAttributes.length; k < kk; k++) {
value = this.customAttributes[k].callback(
batchEntry.feature,
batchEntry.properties
);
value = this.customAttributes[k].callback(batchEntry.feature);
batch.renderInstructions[renderIndex++] = value;
}

View File

@@ -39,7 +39,7 @@ describe('Batch renderers', function () {
{
name: 'test',
callback: function (feature, properties) {
return properties.test;
return feature.get('test');
},
},
];

View File

@@ -62,12 +62,10 @@ describe('MixedGeometryBatch', function () {
expect(keys).to.eql([uid1, uid2]);
expect(mixedBatch.pointBatch.entries[uid1]).to.eql({
feature: feature1,
properties: feature1.getProperties(),
flatCoordss: [[0, 1]],
});
expect(mixedBatch.pointBatch.entries[uid2]).to.eql({
feature: feature2,
properties: feature2.getProperties(),
flatCoordss: [[2, 3]],
});
});
@@ -95,7 +93,7 @@ describe('MixedGeometryBatch', function () {
});
it('updates the modified properties and geometry in the point batch', () => {
const entry = mixedBatch.pointBatch.entries[getUid(feature1)];
expect(entry.properties.prop1).to.eql('changed');
expect(entry.feature.get('prop1')).to.eql('changed');
});
it('keeps geometry count the same', () => {
expect(mixedBatch.pointBatch.geometriesCount).to.be(2);
@@ -173,13 +171,11 @@ describe('MixedGeometryBatch', function () {
expect(keys).to.eql([uid1, uid2]);
expect(mixedBatch.lineStringBatch.entries[uid1]).to.eql({
feature: feature1,
properties: feature1.getProperties(),
flatCoordss: [[0, 1, 2, 3, 4, 5, 6, 7]],
verticesCount: 4,
});
expect(mixedBatch.lineStringBatch.entries[uid2]).to.eql({
feature: feature2,
properties: feature2.getProperties(),
flatCoordss: [[8, 9, 10, 11, 12, 13]],
verticesCount: 3,
});
@@ -208,7 +204,7 @@ describe('MixedGeometryBatch', function () {
});
it('updates the modified properties and geometry in the linestring batch', () => {
const entry = mixedBatch.lineStringBatch.entries[getUid(feature1)];
expect(entry.properties.prop1).to.eql('changed');
expect(entry.feature.get('prop1')).to.eql('changed');
expect(entry.verticesCount).to.eql(6);
expect(entry.flatCoordss).to.eql([
[0, 1, 2, 3, 4, 5, 6, 7, 100, 101, 102, 103],
@@ -316,7 +312,6 @@ describe('MixedGeometryBatch', function () {
expect(keys).to.eql([uid1, uid2]);
expect(mixedBatch.polygonBatch.entries[uid1]).to.eql({
feature: feature1,
properties: feature1.getProperties(),
flatCoordss: [[0, 1, 2, 3, 4, 5, 6, 7, 20, 21, 22, 23, 24, 25]],
verticesCount: 7,
ringsCount: 2,
@@ -324,7 +319,6 @@ describe('MixedGeometryBatch', function () {
});
expect(mixedBatch.polygonBatch.entries[uid2]).to.eql({
feature: feature2,
properties: feature2.getProperties(),
flatCoordss: [
[
8, 9, 10, 11, 12, 13, 30, 31, 32, 33, 34, 35, 40, 41, 42, 43, 44,
@@ -346,7 +340,6 @@ describe('MixedGeometryBatch', function () {
expect(keys).to.eql([getUid(feature1), getUid(feature2)]);
expect(mixedBatch.lineStringBatch.entries[getUid(feature1)]).to.eql({
feature: feature1,
properties: feature1.getProperties(),
flatCoordss: [
[0, 1, 2, 3, 4, 5, 6, 7],
[20, 21, 22, 23, 24, 25],
@@ -355,7 +348,6 @@ describe('MixedGeometryBatch', function () {
});
expect(mixedBatch.lineStringBatch.entries[getUid(feature2)]).to.eql({
feature: feature2,
properties: feature2.getProperties(),
flatCoordss: [
[8, 9, 10, 11, 12, 13],
[30, 31, 32, 33, 34, 35],
@@ -393,7 +385,7 @@ describe('MixedGeometryBatch', function () {
});
it('updates the modified properties and geometry in the polygon batch', () => {
const entry = mixedBatch.polygonBatch.entries[getUid(feature1)];
expect(entry.properties.prop1).to.eql('changed');
expect(entry.feature.get('prop1')).to.eql('changed');
expect(entry.verticesCount).to.eql(11);
expect(entry.ringsCount).to.eql(3);
expect(entry.ringsVerticesCounts).to.eql([[4, 3, 4]]);
@@ -529,7 +521,6 @@ describe('MixedGeometryBatch', function () {
const uid = getUid(feature);
expect(mixedBatch.polygonBatch.entries[uid]).to.eql({
feature: feature,
properties: feature.getProperties(),
flatCoordss: [
[0, 1, 2, 3, 4, 5, 6, 7, 20, 21, 22, 23, 24, 25],
[
@@ -549,7 +540,6 @@ describe('MixedGeometryBatch', function () {
const uid = getUid(feature);
expect(mixedBatch.lineStringBatch.entries[uid]).to.eql({
feature: feature,
properties: feature.getProperties(),
flatCoordss: [
[0, 1, 2, 3, 4, 5, 6, 7],
[20, 21, 22, 23, 24, 25],
@@ -566,7 +556,6 @@ describe('MixedGeometryBatch', function () {
const uid = getUid(feature);
expect(mixedBatch.pointBatch.entries[uid]).to.eql({
feature: feature,
properties: feature.getProperties(),
flatCoordss: [
[101, 102],
[201, 202],
@@ -619,7 +608,6 @@ describe('MixedGeometryBatch', function () {
const entry = mixedBatch.polygonBatch.entries[getUid(feature)];
expect(entry).to.eql({
feature: feature,
properties: feature.getProperties(),
flatCoordss: [
[0, 1, 2, 3, 4, 5, 6, 7, 20, 21, 22, 23, 24, 25],
[
@@ -637,7 +625,6 @@ describe('MixedGeometryBatch', function () {
const entry = mixedBatch.lineStringBatch.entries[getUid(feature)];
expect(entry).to.eql({
feature: feature,
properties: feature.getProperties(),
flatCoordss: [
[0, 1, 2, 3, 4, 5, 6, 7],
[20, 21, 22, 23, 24, 25],
@@ -680,7 +667,6 @@ describe('MixedGeometryBatch', function () {
const entry = mixedBatch.polygonBatch.entries[getUid(feature)];
expect(entry).to.eql({
feature: feature,
properties: feature.getProperties(),
flatCoordss: [[201, 202, 203, 204, 205, 206, 207, 208]],
verticesCount: 4,
ringsCount: 1,
@@ -691,7 +677,6 @@ describe('MixedGeometryBatch', function () {
const entry = mixedBatch.lineStringBatch.entries[getUid(feature)];
expect(entry).to.eql({
feature: feature,
properties: feature.getProperties(),
flatCoordss: [[201, 202, 203, 204, 205, 206, 207, 208]],
verticesCount: 4,
});