diff --git a/src/components/map/FeatureLayerPopup.jsx b/src/components/map/FeatureLayerPopup.jsx index 2d0eacb3..261c80f7 100644 --- a/src/components/map/FeatureLayerPopup.jsx +++ b/src/components/map/FeatureLayerPopup.jsx @@ -1,6 +1,7 @@ import React from 'react' import PropTypes from 'prop-types' import LayerIcon from '../icons/LayerIcon' +import {latest, expression, function as styleFunction} from '@mapbox/mapbox-gl-style-spec' function groupFeaturesBySourceLayer(features) { const sources = {} @@ -28,7 +29,59 @@ function groupFeaturesBySourceLayer(features) { class FeatureLayerPopup extends React.Component { static propTypes = { onLayerSelect: PropTypes.func.isRequired, - features: PropTypes.array + features: PropTypes.array, + zoom: PropTypes.number, + } + + _getFeatureColor(feature, zoom) { + try { + const paintProps = feature.layer.paint; + let propName; + + if(paintProps.hasOwnProperty("text-color") && paintProps["text-color"]) { + propName = "text-color"; + } + else if (paintProps.hasOwnProperty("fill-color") && paintProps["fill-color"]) { + propName = "fill-color"; + } + else if (paintProps.hasOwnProperty("line-color") && paintProps["line-color"]) { + propName = "line-color"; + } + else if (paintProps.hasOwnProperty("fill-extrusion-color") && paintProps["fill-extrusion-color"]) { + propName = "fill-extrusion-color"; + } + + if(propName) { + const propertySpec = latest["paint_"+feature.layer.type][propName]; + + let color = feature.layer.paint[propName]; + + if(typeof(color) === "object") { + if(color.stops) { + color = styleFunction.convertFunction(color, propertySpec); + } + + const exprResult = expression.createExpression(color, propertySpec); + const val = exprResult.value.evaluate({ + zoom: zoom + }, feature); + return val.toString(); + } + else { + return color; + } + } + else { + // Default color + return "black"; + } + } + // This is quite complex, just incase there's an edgecase we're missing + // always return black if we get an unexpected error. + catch (err) { + console.error("Unable to get feature color, error:", err); + return "black"; + } } render() { @@ -36,21 +89,31 @@ class FeatureLayerPopup extends React.Component { const items = Object.keys(sources).map(vectorLayerId => { const layers = sources[vectorLayerId].map((feature, idx) => { - return