deck.gl icon indicating copy to clipboard operation
deck.gl copied to clipboard

Hexagonal layer can change height scale to log as kepler.gl

Open jadelmag opened this issue 6 years ago • 3 comments

I've created a HexagonLayer but I would like to show hexagon cells with log height scale as kepler. It is possible?

Because, I was reading documentation and I understand that could not change it. elevationDomain: Default: [0, max(count)]

Elevation scale input domain. The elevation scale is a linear scale that maps number of counts to elevation. By default it is set to between 0 and max of point counts in each hexagon. This property is extremely handy when you want to render different data input with the same elevation scale for comparison.

class DeckGLOverlay extends Component {
    constructor(props) {
        super(props);
        this.state = {
            x: null,
            y: null,
            hoveredObject: null,
            id: uuidv4(),
        };
    }

    getCoordinates = () => {
        let coordinates = [];
        const { data3d } = this.props;
        data3d.forEach(supplypoint => {
            if (
                !supplypoint.geojson ||
                (supplypoint.geojson &&
                    supplypoint.geojson &&
                    !supplypoint.geojson.coordinates)
            ) {
                return;
            }
            let feature = {
                COORDINATES: supplypoint.geojson.coordinates,
                VOLUME: supplypoint.value,
                UNITS: supplypoint.units,
                DATA: {
                    supplypointId: supplypoint.supplypointId,
                    address: supplypoint.address,
                    client: supplypoint.client,
                    diameter: supplypoint.diameter,
                    endusetype: supplypoint.endusetype,
                    usetype: supplypoint.usetype,
                },
            };
            coordinates.push(feature);
        });
        return coordinates;
    };

    sumElevations = values => {
        if (values.length >= 1) {
            let totalVolumes = 0;
            for (let item of values) {
                if (item.hasOwnProperty("VOLUME")) {
                    totalVolumes += item["VOLUME"];
                }
            }
            return totalVolumes;
        }
        return 0;
    };

    render() {
        const { viewport } = this.props;
        const layers = [];
        if (this.props.data3d) {
            layers.push(
                new HexagonLayer({
                    id: `hexagon-layer-${uuidv4()}`,
                    data: this.getCoordinates(),
                    pickable: true,
                    extruded: true,
                    material: material,
                    radius: 5,
                    elevationRange: [0, 1000],
                    elevationScale: 1,
                    elevationAggregation: "SUM",
                    colorRange: BLUE_COLORS,
                    colorScaleType: "quantile",
                    colorAggregation: "SUM",
                    getPosition: d => d.COORDINATES,
                    getElevationValue: d => this.sumElevations(d),
                })
            );
        }
        return (
            <DeckGL layers={layers} onHover={this.onHoverDeckgl} viewState={viewport} />
        );
    }
}
export default injectIntl(DeckGLOverlay);

kepler

jadelmag avatar Mar 17 '20 17:03 jadelmag

Use getElevationValue.

Pessimistress avatar Mar 17 '20 22:03 Pessimistress

I'm using getElevationValue, but in getElevationValue I'm adding all the volumes.

So, you said that I' have to use getElevationValue and calculate log function.

And where is log size scale function?

new HexagonLayer({
    id: `hexagon-layer-${uuidv4()}`,
    data: this.getCoordinates(),
    pickable: true,
    extruded: true,
    material: material,
    radius: 5,
    elevationRange: [0, 1000],
    elevationScale: 1,
    elevationAggregation: "SUM",
    colorRange: BLUE_COLORS,
    colorScaleType: "quantile",
    colorAggregation: "SUM",
    getPosition: d => d.COORDINATES,
    getElevationValue: d => this.sumElevations(d),
})
sumElevations = values => {
        if (values.length >= 1) {
            let totalVolumes = 0;
            for (let item of values) {
                if (item.hasOwnProperty("VOLUME")) {
                    totalVolumes += item["VOLUME"];
                }
            }
            return totalVolumes;
        }
        return 0;
    };

jadelmag avatar Mar 18 '20 06:03 jadelmag

I solved @Pessimistress :D

I've imported scaleLag from d3-scale, from d3-scale library.

class DeckGLOverlay extends Component {
    constructor(props) {
        super(props);
        this.state = {
            x: null,
            y: null,
            hoveredObject: null,
            id: uuidv4(),
        };

        this.logScale = scaleLog()
            .domain([10, 100000])
            .range([0, 500]);
    }

    getCoordinates = () => {
        ...
        return coordinates;
    };

    sumElevations = values => {
        if (values.length >= 1) {
            let totalVolumes = 0;
            for (let item of values) {
                if (item.hasOwnProperty("VOLUME")) {
                    totalVolumes += item.VOLUME;
                }
            }
            return totalVolumes;
        }
        return 0;
    };

    elevationByLogScale = values => {
        let totalVolumes = this.sumElevations(values);
        let scale = parseInt(this.logScale(totalVolumes));
        if (isNaN(scale)) return 0;
        return scale;
    };

    render() {
        const { viewport } = this.props;
        const layers = [];
        if (this.props.data3d) {
            layers.push(
                new HexagonLayer({
                    id: `hexagon-layer-${uuidv4()}`,
                    data: this.getCoordinates(),
                    pickable: true,
                    extruded: true,
                    material: lightingEffect,
                    radius: 5,
                    elevationRange: [0, 500],
                    elevationScale: 1,
                    colorRange: BLUE_COLORS,
                    getPosition: d => d.COORDINATES,
                    getElevationValue: d => this.elevationByLogScale(d),
                })
            );
        }
        return <DeckGL layers={layers} viewState={viewport}></DeckGL>;
    }
}

jadelmag avatar Mar 18 '20 13:03 jadelmag