Skip to content

Commit

Permalink
Merge bebfbba into 46e3931
Browse files Browse the repository at this point in the history
  • Loading branch information
1chandu committed Dec 10, 2019
2 parents 46e3931 + bebfbba commit 3a21d21
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 77 deletions.
9 changes: 9 additions & 0 deletions docs/layers/contour-layer.md
Expand Up @@ -131,6 +131,15 @@ Method called to retrieve the position of each point.

Method called to retrieve weight of each point. By default each point will use a weight of `1`.


## Sub Layers

The `ContourLayer` renders the following sublayers:

* `lines` - For Isolines, rendered by [LineLayer](/docs/layers/line-layer.md)
* `bands` - For Isobands, rendered by [SolidPolygonLayer](/docs/layers/solid-polygon-layer.md)


## Source

[modules/aggregation-layers/src/contour-layer](https://github.com/uber/deck.gl/tree/master/modules/aggregation-layers/src/contour-layer)
45 changes: 8 additions & 37 deletions modules/aggregation-layers/src/contour-layer/contour-layer.js
Expand Up @@ -18,7 +18,6 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

import {equals} from 'math.gl';
import GL from '@luma.gl/constants';
import {LineLayer, SolidPolygonLayer} from '@deck.gl/layers';
import {generateContours} from './contour-utils';
Expand Down Expand Up @@ -102,14 +101,14 @@ export default class ContourLayer extends GridAggregationLayer {
contourSegments.length > 0 &&
new LinesSubLayerClass(
this.getSubLayerProps({
id: 'contour-line-layer'
id: 'lines'
}),
{
data: this.state.contourData.contourSegments,
getSourcePosition: d => d.start,
getTargetPosition: d => d.end,
getColor: this._onGetSublayerColor.bind(this),
getWidth: this._onGetSublayerStrokeWidth.bind(this)
getColor: d => d.contour.color || DEFAULT_COLOR,
getWidth: d => d.contour.strokeWidth || DEFAULT_STROKE_WIDTH
}
);

Expand All @@ -119,12 +118,12 @@ export default class ContourLayer extends GridAggregationLayer {
contourPolygons.length > 0 &&
new BandsSubLayerClass(
this.getSubLayerProps({
id: 'contour-solid-polygon-layer'
id: 'bands'
}),
{
data: this.state.contourData.contourPolygons,
getPolygon: d => d.vertices,
getFillColor: this._onGetSublayerColor.bind(this)
getFillColor: d => d.contour.color || DEFAULT_COLOR
}
);

Expand Down Expand Up @@ -184,43 +183,15 @@ export default class ContourLayer extends GridAggregationLayer {
const count = contours.length;
const thresholdData = new Array(count);
for (let i = 0; i < count; i++) {
const {threshold, zIndex} = contours[i];
const contour = contours[i];
thresholdData[i] = {
threshold,
zIndex: zIndex || i,
contour,
zIndex: contour.zIndex || i,
zOffset
};
}
this.setState({thresholdData});
}

// Private (Sublayers)

_onGetSublayerColor(element) {
// element is either a line segment or polygon
const {contours} = this.props;
let color = DEFAULT_COLOR;
contours.forEach(data => {
if (equals(data.threshold, element.threshold)) {
color = data.color || DEFAULT_COLOR;
}
});
return color;
}

_onGetSublayerStrokeWidth(segment) {
const {contours} = this.props;
let strokeWidth = DEFAULT_STROKE_WIDTH;
// Linearly searches the contours, but there should only be few contours
contours.some(contour => {
if (contour.threshold === segment.threshold) {
strokeWidth = contour.strokeWidth || DEFAULT_STROKE_WIDTH;
return true;
}
return false;
});
return strokeWidth;
}
}

ContourLayer.layerName = 'ContourLayer';
Expand Down
25 changes: 14 additions & 11 deletions modules/aggregation-layers/src/contour-layer/contour-utils.js
Expand Up @@ -14,9 +14,12 @@ export function generateContours({
const contourPolygons = [];
const width = gridSize[0];
const height = gridSize[1];
let segmentIndex = 0;
let polygonIndex = 0;

thresholdData.forEach((data, index) => {
const {threshold} = data;
for (const data of thresholdData) {
const {contour} = data;
const {threshold} = contour;
for (let x = -1; x < width; x++) {
for (let y = -1; y < height; y++) {
// Get the MarchingSquares code based on neighbor cell weights.
Expand All @@ -42,27 +45,27 @@ export function generateContours({
if (Array.isArray(threshold)) {
opts.type = CONTOUR_TYPE.ISO_BANDS;
const polygons = getVertices(opts);
polygons.forEach(polygon => {
contourPolygons.push({
for (const polygon of polygons) {
contourPolygons[polygonIndex++] = {
vertices: polygon,
threshold
});
});
contour
};
}
} else {
// Get the intersection vertices based on MarchingSquares code.
opts.type = CONTOUR_TYPE.ISO_LINES;
const vertices = getVertices(opts);
for (let i = 0; i < vertices.length; i += 2) {
contourSegments.push({
contourSegments[segmentIndex++] = {
start: vertices[i],
end: vertices[i + 1],
threshold
});
contour
};
}
}
}
}
});
}
return {contourSegments, contourPolygons};
}
/* eslint-enable max-depth */
Expand Up @@ -18,7 +18,6 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
import test from 'tape-catch';
import {makeSpy} from '@probe.gl/test-utils';

import * as FIXTURES from 'deck.gl-test/data';

Expand Down Expand Up @@ -62,8 +61,6 @@ test('ContourLayer', t => {
});

test('ContourLayer#renderSubLayer', t => {
makeSpy(ContourLayer.prototype, '_onGetSublayerColor');

const layer = new ContourLayer({
id: 'contourLayer',
data: FIXTURES.points,
Expand All @@ -82,9 +79,6 @@ test('ContourLayer#renderSubLayer', t => {
t.ok(subLayers[0] instanceof LineLayer, 'Sublayer Line layer rendered');
t.ok(subLayers[1] instanceof SolidPolygonLayer, 'Sublayer SolidPolygon layer rendered');

t.ok(ContourLayer.prototype._onGetSublayerColor.called, 'should call _onGetSublayerColor');
ContourLayer.prototype._onGetSublayerColor.restore();

t.end();
});

Expand Down Expand Up @@ -140,32 +134,21 @@ test('ContourLayer#updates', t => {
updateProps: {
cellSize: 500 // changed from 400 to 500
},
spies: ['_onGetSublayerColor', '_updateAggregation', '_generateContours'],
spies: ['_updateAggregation', '_generateContours'],
onAfterUpdate({layer, subLayers, spies}) {
t.ok(subLayers.length === 2, 'Sublayers rendered');

t.ok(spies._updateAggregation.called, 'should re-aggregate data on cellSize change');
t.ok(spies._generateContours.called, 'should re-generate contours on cellSize change');
t.ok(
spies._onGetSublayerColor.called,
'should call _onGetSublayerColor on cellSize change'
);
spies._updateAggregation.restore();
spies._generateContours.restore();
spies._onGetSublayerColor.restore();
}
},
{
updateProps: {
contours: CONTOURS2
},
spies: [
'_updateThresholdData',
'_generateContours',
'_updateAggregation',
'_onGetSublayerStrokeWidth',
'_onGetSublayerColor'
],
spies: ['_updateThresholdData', '_generateContours', '_updateAggregation'],
onAfterUpdate({subLayers, spies}) {
t.ok(subLayers.length === 2, 'Sublayers rendered');

Expand All @@ -178,20 +161,10 @@ test('ContourLayer#updates', t => {
!spies._updateAggregation.called,
'should NOT re-aggregate data on countours count change'
);
t.ok(
spies._onGetSublayerColor.called,
'should call _onGetSublayerColor on contours change'
);
t.ok(
spies._onGetSublayerStrokeWidth.called,
'should call _onGetSublayerStrokeWidth on contours change'
);

spies._updateThresholdData.restore();
spies._generateContours.restore();
spies._updateAggregation.restore();
spies._onGetSublayerColor.restore();
spies._onGetSublayerStrokeWidth.restore();
}
}
]
Expand Down

0 comments on commit 3a21d21

Please sign in to comment.