class TopoJSONLayer extends deck.CompositeLayer {
static layerName = "TopoJSONLayer";
static defaultProps = {
...deck.GeoJsonLayer.defaultProps,
topologyObject: null,
preProcessTopology: { type: "function", value: null, optional: true }
};
updateState({ props, oldProps, changeFlags }) {
super.updateState({ props, oldProps, changeFlags });
if (
changeFlags.dataChanged ||
props.topologyObject !== oldProps.topologyObject
) {
const { data, topologyObject, preProcessTopology } = props;
if (!data) {
this.setState({ geojson: null });
return;
}
let topology = data;
if (preProcessTopology) {
topology = preProcessTopology(topology);
}
const objectName =
topologyObject || this._selectDefaultTopologyObject(topology);
const geojson = topojson.feature(topology, topology.objects[objectName]);
this.setState({ geojson });
}
}
_selectDefaultTopologyObject(topology) {
const objects = Object.keys(topology.objects);
if (objects.length === 0) {
throw new Error("TopoJSON data contains no objects");
}
return objects[0];
}
renderLayers() {
const { geojson } = this.state;
if (!geojson) return null;
return new deck.GeoJsonLayer(
this.getSubLayerProps({
id: "geojson",
data: geojson,
...this.props
})
);
}
}