MyCustomLayer = {
class MyCustomLayer extends deck.CompositeLayer {
static layerName = 'MyCustomLayer' + Date.now()
static defaultProps = {
getColor: {type: 'accessor', value: [0, 0, 0]}
}
updateState({props, oldProps, changeFlags}) {
const labels = [];
turfjs.segmentEach(props.data, f => {
const len = turfjs.length(f);
if (len > 500) {
const p0 = f.geometry.coordinates[0]
const p1 = f.geometry.coordinates[1]
const bearing = turfjs.rhumbBearing(p0, p1)
const midPoint = [
(p0[0] + p1[0]) / 2,
(p0[1] + p1[1]) / 2,
]
labels.push({
position: midPoint,
angle: bearing,
text: f.properties.name,
color: this.props.getColor(f)
})
}
});
this.setState({labels})
}
renderLayers() {
return [
new deck.GeoJsonLayer(this.getSubLayerProps({
id: 'lines',
data: this.props.data,
getLineColor: this.props.getColor,
getLineWidth: 1,
lineWidthUnits: 'pixels'
})),
new deck.TextLayer(this.getSubLayerProps({
id: 'labels',
data: this.state.labels,
getText: d => d.text,
getPosition: d => d.position,
getSize: 20000,
getAngle: d => 90 - d.angle,
getColor: [255, 255, 255],
background: true,
getBackgroundColor: d => d.color,
sizeUnits: 'meters'
}))
];
}
}
return MyCustomLayer
}