|
| 1 | +/** |
| 2 | +* Copyright 2012-2019, Plotly, Inc. |
| 3 | +* All rights reserved. |
| 4 | +* |
| 5 | +* This source code is licensed under the MIT license found in the |
| 6 | +* LICENSE file in the root directory of this source tree. |
| 7 | +*/ |
| 8 | + |
| 9 | +'use strict'; |
| 10 | + |
| 11 | +var isNumeric = require('fast-isnumeric'); |
| 12 | + |
| 13 | +var Lib = require('../../lib'); |
| 14 | +var Color = require('../../components/color'); |
| 15 | +var Colorscale = require('../../components/colorscale'); |
| 16 | + |
| 17 | +var BADNUM = require('../../constants/numerical').BADNUM; |
| 18 | +var makeBlank = require('../../lib/geojson_utils').makeBlank; |
| 19 | + |
| 20 | +module.exports = function convert(calcTrace) { |
| 21 | + var trace = calcTrace[0].trace; |
| 22 | + var isVisible = (trace.visible === true && trace._length !== 0); |
| 23 | + |
| 24 | + var heatmap = { |
| 25 | + layout: {visibility: 'none'}, |
| 26 | + paint: {} |
| 27 | + }; |
| 28 | + |
| 29 | + var opts = trace._opts = { |
| 30 | + heatmap: heatmap, |
| 31 | + geojson: makeBlank() |
| 32 | + }; |
| 33 | + |
| 34 | + // early return if not visible or placeholder |
| 35 | + if(!isVisible) return opts; |
| 36 | + |
| 37 | + var features = []; |
| 38 | + var i; |
| 39 | + |
| 40 | + var z = trace.z; |
| 41 | + var radius = trace.radius; |
| 42 | + var hasZ = Lib.isArrayOrTypedArray(z) && z.length; |
| 43 | + var hasArrayRadius = Lib.isArrayOrTypedArray(radius); |
| 44 | + |
| 45 | + for(i = 0; i < calcTrace.length; i++) { |
| 46 | + var cdi = calcTrace[i]; |
| 47 | + var lonlat = cdi.lonlat; |
| 48 | + |
| 49 | + if(lonlat[0] !== BADNUM) { |
| 50 | + var props = {}; |
| 51 | + |
| 52 | + if(hasZ) { |
| 53 | + var zi = cdi.z; |
| 54 | + props.z = zi !== BADNUM ? zi : 0; |
| 55 | + } |
| 56 | + if(hasArrayRadius) { |
| 57 | + props.r = (isNumeric(radius[i]) && radius[i] > 0) ? +radius[i] : 0; |
| 58 | + } |
| 59 | + |
| 60 | + features.push({ |
| 61 | + type: 'Feature', |
| 62 | + geometry: {type: 'Point', coordinates: lonlat}, |
| 63 | + properties: props |
| 64 | + }); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + var cOpts = Colorscale.extractOpts(trace); |
| 69 | + var scl = cOpts.reversescale ? |
| 70 | + Colorscale.flipScale(cOpts.colorscale) : |
| 71 | + cOpts.colorscale; |
| 72 | + |
| 73 | + // Add alpha channel to first colorscale step. |
| 74 | + // If not, we would essentially color the entire map. |
| 75 | + // See https://docs.mapbox.com/mapbox-gl-js/example/heatmap-layer/ |
| 76 | + var scl01 = scl[0][1]; |
| 77 | + var color0 = Color.opacity(scl01) < 1 ? scl01 : Color.addOpacity(scl01, 0); |
| 78 | + |
| 79 | + var heatmapColor = [ |
| 80 | + 'interpolate', ['linear'], |
| 81 | + ['heatmap-density'], |
| 82 | + 0, color0 |
| 83 | + ]; |
| 84 | + for(i = 1; i < scl.length; i++) { |
| 85 | + heatmapColor.push(scl[i][0], scl[i][1]); |
| 86 | + } |
| 87 | + |
| 88 | + // Those "weights" have to be in [0, 1], we can do this either: |
| 89 | + // - as here using a mapbox-gl expression |
| 90 | + // - or, scale the 'z' property in the feature loop |
| 91 | + var zExp = [ |
| 92 | + 'interpolate', ['linear'], |
| 93 | + ['get', 'z'], |
| 94 | + cOpts.min, 0, |
| 95 | + cOpts.max, 1 |
| 96 | + ]; |
| 97 | + |
| 98 | + Lib.extendFlat(opts.heatmap.paint, { |
| 99 | + 'heatmap-weight': hasZ ? zExp : 1 / (cOpts.max - cOpts.min), |
| 100 | + |
| 101 | + 'heatmap-color': heatmapColor, |
| 102 | + |
| 103 | + 'heatmap-radius': hasArrayRadius ? |
| 104 | + {type: 'identity', property: 'r'} : |
| 105 | + trace.radius, |
| 106 | + |
| 107 | + 'heatmap-opacity': trace.opacity |
| 108 | + }); |
| 109 | + |
| 110 | + opts.geojson = {type: 'FeatureCollection', features: features}; |
| 111 | + opts.heatmap.layout.visibility = 'visible'; |
| 112 | + opts.below = trace.below; |
| 113 | + |
| 114 | + return opts; |
| 115 | +}; |
0 commit comments