Skip to content

Commit

Permalink
DRY bucket code with util function
Browse files Browse the repository at this point in the history
  • Loading branch information
Molly Lloyd committed Aug 22, 2018
1 parent 55ed053 commit 79bcb4f
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 184 deletions.
66 changes: 5 additions & 61 deletions src/data/bucket/fill_bucket.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ import { members as layoutAttributes } from './fill_attributes';
import SegmentVector from '../segment';
import { ProgramConfigurationSet } from '../program_configuration';
import { LineIndexArray, TriangleIndexArray } from '../index_array_type';
import loadGeometry from '../load_geometry';
import earcut from 'earcut';
import classifyRings from '../../util/classify_rings';
import assert from 'assert';
const EARCUT_MAX_RINGS = 500;
import { register } from '../../util/web_worker_transfer';
import EvaluationParameters from '../../style/evaluation_parameters';
import addPatternFeatures from './pattern_bucket_features';

import type {
Bucket,
Expand Down Expand Up @@ -70,65 +69,10 @@ class FillBucket implements Bucket {
}

populate(features: Array<IndexedFeature>, options: PopulateParameters) {
const patterns = options.patternDependencies;
this.features = [];

for (const layer of this.layers) {
const patternProperty = layer.paint.get('fill-pattern');
if (!patternProperty.isConstant()) {
this.hasPattern = true;
}

const constantPattern = patternProperty.constantOr(null);
if (constantPattern) {
this.hasPattern = true;
patterns[constantPattern.to] = true;
patterns[constantPattern.from] = true;
}
}

for (const {feature, index, sourceLayerIndex} of features) {
if (!this.layers[0]._featureFilter(new EvaluationParameters(this.zoom), feature)) continue;

const geometry = loadGeometry(feature);

const bucketFeature: BucketFeature = {
sourceLayerIndex: sourceLayerIndex,
index: index,
geometry: geometry,
properties: feature.properties,
type: feature.type,
patterns: {}
};

if (typeof feature.id !== 'undefined') {
bucketFeature.id = feature.id;
}

if (this.hasPattern) {
for (const layer of this.layers) {
const patternProperty = layer.paint.get('fill-pattern');

const patternPropertyValue = patternProperty.value;
if (patternPropertyValue.kind !== "constant") {
const min = patternPropertyValue.evaluate({zoom: this.zoom - 1}, feature, {});
const mid = patternPropertyValue.evaluate({zoom: this.zoom}, feature, {});
const max = patternPropertyValue.evaluate({zoom: this.zoom + 1}, feature, {});
// add to patternDependencies
patterns[min] = true;
patterns[mid] = true;
patterns[max] = true;

// save for layout
bucketFeature.patterns[layer.id] = { min, mid, max };
}
}
this.features.push(bucketFeature);
} else {
this.addFeature(bucketFeature, geometry, index, {});
}

options.featureIndex.insert(feature, geometry, index, sourceLayerIndex, this.index);
this.features = addPatternFeatures('fill', this.zoom, this.layers, features,
options, this.index, this.addFeature.bind(this));
if (this.features.length) {
this.hasPattern = true;
}
}

Expand Down
66 changes: 5 additions & 61 deletions src/data/bucket/fill_extrusion_bucket.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ import { members as layoutAttributes } from './fill_extrusion_attributes';
import SegmentVector from '../segment';
import { ProgramConfigurationSet } from '../program_configuration';
import { TriangleIndexArray } from '../index_array_type';
import loadGeometry from '../load_geometry';
import EXTENT from '../extent';
import earcut from 'earcut';
import classifyRings from '../../util/classify_rings';
import assert from 'assert';
const EARCUT_MAX_RINGS = 500;
import { register } from '../../util/web_worker_transfer';
import EvaluationParameters from '../../style/evaluation_parameters';
import addPatternFeatures from './pattern_bucket_features';

import type {
Bucket,
Expand Down Expand Up @@ -84,65 +83,10 @@ class FillExtrusionBucket implements Bucket {
}

populate(features: Array<IndexedFeature>, options: PopulateParameters) {
const patterns = options.patternDependencies;
this.features = [];

for (const layer of this.layers) {
const patternProperty = layer.paint.get('fill-extrusion-pattern');
if (!patternProperty.isConstant()) {
this.hasPattern = true;
}

const constantPattern = patternProperty.constantOr(null);
if (constantPattern) {
this.hasPattern = true;
patterns[constantPattern.to] = true;
patterns[constantPattern.from] = true;
}
}

for (const {feature, index, sourceLayerIndex} of features) {
if (!this.layers[0]._featureFilter(new EvaluationParameters(this.zoom), feature)) continue;

const geometry = loadGeometry(feature);

const bucketFeature: BucketFeature = {
sourceLayerIndex: sourceLayerIndex,
index: index,
geometry: geometry,
properties: feature.properties,
type: feature.type,
patterns: {}
};

if (typeof feature.id !== 'undefined') {
bucketFeature.id = feature.id;
}

if (this.hasPattern) {
for (const layer of this.layers) {
const patternProperty = layer.paint.get('fill-extrusion-pattern');

const patternPropertyValue = patternProperty.value;
if (patternPropertyValue.kind !== "constant") {
const min = patternPropertyValue.evaluate({zoom: this.zoom - 1}, feature, {});
const mid = patternPropertyValue.evaluate({zoom: this.zoom}, feature, {});
const max = patternPropertyValue.evaluate({zoom: this.zoom + 1}, feature, {});
// add to patternDependencies
patterns[min] = true;
patterns[mid] = true;
patterns[max] = true;

// save for layout
bucketFeature.patterns[layer.id] = { min, mid, max };
}
}
this.features.push(bucketFeature);
} else {
this.addFeature(bucketFeature, geometry, index, {});
}

options.featureIndex.insert(feature, geometry, index, sourceLayerIndex, this.index);
this.features = addPatternFeatures('fill-extrusion', this.zoom, this.layers,
features, options, this.index, this.addFeature.bind(this));
if (this.features.length) {
this.hasPattern = true;
}
}

Expand Down
66 changes: 5 additions & 61 deletions src/data/bucket/line_bucket.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ import { members as layoutAttributes } from './line_attributes';
import SegmentVector from '../segment';
import { ProgramConfigurationSet } from '../program_configuration';
import { TriangleIndexArray } from '../index_array_type';
import loadGeometry from '../load_geometry';
import EXTENT from '../extent';
import mvt from '@mapbox/vector-tile';
const vectorTileFeatureTypes = mvt.VectorTileFeature.types;
import { register } from '../../util/web_worker_transfer';
import EvaluationParameters from '../../style/evaluation_parameters';
import addPatternFeatures from './pattern_bucket_features';

import type {
Bucket,
Expand Down Expand Up @@ -127,65 +126,10 @@ class LineBucket implements Bucket {
}

populate(features: Array<IndexedFeature>, options: PopulateParameters) {
const patterns = options.patternDependencies;
this.features = [];

for (const layer of this.layers) {
const patternProperty = layer.paint.get('line-pattern');
if (!patternProperty.isConstant()) {
this.hasPattern = true;
}

const constantPattern = patternProperty.constantOr(null);
if (constantPattern) {
this.hasPattern = true;
patterns[constantPattern.to] = true;
patterns[constantPattern.from] = true;
}
}

for (const {feature, index, sourceLayerIndex} of features) {
if (!this.layers[0]._featureFilter(new EvaluationParameters(this.zoom), feature)) continue;

const geometry = loadGeometry(feature);

const bucketFeature: BucketFeature = {
sourceLayerIndex: sourceLayerIndex,
index: index,
geometry: geometry,
properties: feature.properties,
type: feature.type,
patterns: {}
};

if (typeof feature.id !== 'undefined') {
bucketFeature.id = feature.id;
}

if (this.hasPattern) {
for (const layer of this.layers) {
const patternProperty = layer.paint.get('line-pattern');

const patternPropertyValue = patternProperty.value;
if (patternPropertyValue.kind !== "constant") {
const min = patternPropertyValue.evaluate({zoom: this.zoom - 1}, feature, {});
const mid = patternPropertyValue.evaluate({zoom: this.zoom}, feature, {});
const max = patternPropertyValue.evaluate({zoom: this.zoom + 1}, feature, {});
// add to patternDependencies
patterns[min] = true;
patterns[mid] = true;
patterns[max] = true;

// save for layout
bucketFeature.patterns[layer.id] = { min, mid, max };
}
}
this.features.push(bucketFeature);
} else {
this.addFeature(bucketFeature, geometry, index, {});
}

options.featureIndex.insert(feature, geometry, index, sourceLayerIndex, this.index);
this.features = addPatternFeatures('line', this.zoom, this.layers, features,
options, this.index, this.addFeature.bind(this));
if (this.features.length) {
this.hasPattern = true;
}
}

Expand Down
87 changes: 87 additions & 0 deletions src/data/bucket/pattern_bucket_features.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// @flow
import EvaluationParameters from '../../style/evaluation_parameters';
import loadGeometry from '../load_geometry';

import type FillStyleLayer from '../../style/style_layer/fill_style_layer';
import type FillExtrusionStyleLayer from '../../style/style_layer/fill_extrusion_style_layer';
import type LineStyleLayer from '../../style/style_layer/line_style_layer';
import type {ImagePosition} from '../../render/image_atlas';
import type Point from '@mapbox/point-geometry';

import type {
BucketFeature,
IndexedFeature,
PopulateParameters
} from '../bucket';

type PatternStyleLayers =
Array<LineStyleLayer> |
Array<FillStyleLayer> |
Array<FillExtrusionStyleLayer>;

export default function addPatternFeatures(type: string, zoom: number, layers: PatternStyleLayers, features: Array<IndexedFeature>, options: PopulateParameters, bucketIndex: number, addFeature: (BucketFeature, Array<Array<Point>>, number, {[string]: ImagePosition}) => void) {

const patterns = options.patternDependencies;
const patternFeatures = [];
let hasPattern = false;

for (const layer of layers) {
const patternProperty = layer.paint.get(`${type}-pattern`);
if (!patternProperty.isConstant()) {
hasPattern = true;
}

const constantPattern = patternProperty.constantOr(null);
if (constantPattern) {
hasPattern = true;
patterns[constantPattern.to] = true;
patterns[constantPattern.from] = true;
}
}

for (const {feature, index, sourceLayerIndex} of features) {
if (!layers[0]._featureFilter(new EvaluationParameters(zoom), feature)) continue;

const geometry = loadGeometry(feature);

const patternFeature: BucketFeature = {
sourceLayerIndex: sourceLayerIndex,
index: index,
geometry: geometry,
properties: feature.properties,
type: feature.type,
patterns: {}
};

if (typeof feature.id !== 'undefined') {
patternFeature.id = feature.id;
}

if (hasPattern) {
for (const layer of layers) {
const patternProperty = layer.paint.get(`${type}-pattern`);

const patternPropertyValue = patternProperty.value;
if (patternPropertyValue.kind !== "constant") {
const min = patternPropertyValue.evaluate({zoom: zoom - 1}, feature, {});
const mid = patternPropertyValue.evaluate({zoom: zoom}, feature, {});
const max = patternPropertyValue.evaluate({zoom: zoom + 1}, feature, {});
// add to patternDependencies
patterns[min] = true;
patterns[mid] = true;
patterns[max] = true;

// save for layout
patternFeature.patterns[layer.id] = { min, mid, max };
}
}
patternFeatures.push(patternFeature);
} else {
addFeature(patternFeature, geometry, index, {});
}

options.featureIndex.insert(feature, geometry, index, sourceLayerIndex, bucketIndex);
}

return patternFeatures;
}
1 change: 1 addition & 0 deletions src/source/worker_tile.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ class WorkerTile {
sourceLayerIndex: sourceLayerIndex,
sourceID: this.source
});

bucket.populate(features, options);
featureIndex.bucketLayerIDs.push(family.map((l) => l.id));
}
Expand Down
2 changes: 1 addition & 1 deletion src/style/style_layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ class StyleLayer extends Evented {
}

recalculate(parameters: EvaluationParameters) {
this._crossfadeParameters = parameters.getCrossfadeParameters();
this._crossfadeParameters = parameters.getCrossfadeParameters ? parameters.getCrossfadeParameters() : null;
if (this._unevaluatedLayout) {
(this: any).layout = this._unevaluatedLayout.possiblyEvaluate(parameters);
}
Expand Down

0 comments on commit 79bcb4f

Please sign in to comment.