Use viridis colormap for roads' count layers

This commit is contained in:
Paul Bienkowski 2022-03-16 22:30:12 +01:00
parent 36fd8c492c
commit 6893d7b56f
4 changed files with 13 additions and 6 deletions

View file

@ -4,12 +4,13 @@ type ColorMap = [number, string][]
import styles from './ColorMapLegend.module.less' import styles from './ColorMapLegend.module.less'
export default function ColorMapLegend({map}: {map: ColorMap}) { export default function ColorMapLegend({map, twoTicks = false}: {map: ColorMap, twoTicks?: boolean}) {
const min = map[0][0] const min = map[0][0]
const max = map[map.length - 1][0] const max = map[map.length - 1][0]
const normalizeValue = (v) => (v - min) / (max - min) const normalizeValue = (v) => (v - min) / (max - min)
const gradientId = useMemo(() => `gradient${Math.floor(Math.random() * 1000000)}`, []); const gradientId = useMemo(() => `gradient${Math.floor(Math.random() * 1000000)}`, []);
const gradientUrl = `url(#${gradientId})`; const gradientUrl = `url(#${gradientId})`;
const tickValues = twoTicks ? [map[0], map[map.length-1]] : map
return ( return (
<div className={styles.colorMapLegend}> <div className={styles.colorMapLegend}>
<svg width="100%" height="20" version="1.1" xmlns="http://www.w3.org/2000/svg"> <svg width="100%" height="20" version="1.1" xmlns="http://www.w3.org/2000/svg">
@ -23,7 +24,7 @@ export default function ColorMapLegend({map}: {map: ColorMap}) {
<rect id="rect1" x="0" y="0" width="100%" height="100%" fill={gradientUrl} /> <rect id="rect1" x="0" y="0" width="100%" height="100%" fill={gradientUrl} />
</svg> </svg>
{map.map(([value]) => ( {tickValues.map(([value]) => (
<span className={styles.tick} key={value} style={{left: normalizeValue(value) * 100 + '%'}}> <span className={styles.tick} key={value} style={{left: normalizeValue(value) * 100 + '%'}}>
{value.toFixed(2)} {value.toFixed(2)}
</span> </span>

View file

@ -22,6 +22,10 @@ function rgbArrayToColor(arr) {
return ['rgb', ...arr.map((v) => Math.round(v * 255))] return ['rgb', ...arr.map((v) => Math.round(v * 255))]
} }
function rgbArrayToHtml(arr) {
return "#" + arr.map((v) => Math.round(v * 255).toString(16)).map(v => (v.length == 1 ? '0' : '') + v).join('')
}
export function colormapToScale(colormap, value, min, max) { export function colormapToScale(colormap, value, min, max) {
return [ return [
'interpolate-hcl', 'interpolate-hcl',
@ -32,6 +36,7 @@ export function colormapToScale(colormap, value, min, max) {
} }
export const viridis = simplifyColormap(viridisBase.map(rgbArrayToColor), 20) export const viridis = simplifyColormap(viridisBase.map(rgbArrayToColor), 20)
export const viridisSimpleHtml = simplifyColormap(viridisBase.map(rgbArrayToHtml), 10)
export const grayscale = ['#FFFFFF', '#000000'] export const grayscale = ['#FFFFFF', '#000000']
export const reds = [ export const reds = [
'rgba( 255, 0, 0, 0)', 'rgba( 255, 0, 0, 0)',

View file

@ -8,7 +8,7 @@ import {
setMapConfigFlag as setMapConfigFlagAction, setMapConfigFlag as setMapConfigFlagAction,
initialState as defaultMapConfig, initialState as defaultMapConfig,
} from 'reducers/mapConfig' } from 'reducers/mapConfig'
import {colorByDistance, colorByCount, reds} from 'mapstyles' import {colorByDistance, colorByCount, viridisSimpleHtml} from 'mapstyles'
import {ColorMapLegend} from 'components' import {ColorMapLegend} from 'components'
const BASEMAP_STYLE_OPTIONS = [ const BASEMAP_STYLE_OPTIONS = [
@ -93,7 +93,7 @@ function LayerSidebar({
/> />
</List.Item> </List.Item>
<List.Item> <List.Item>
<ColorMapLegend map={_.chunk(colorByCount('obsRoads.maxCount',mapConfig.obsRoads.maxCount, reds).slice(3), 2)} /> <ColorMapLegend map={_.chunk(colorByCount('obsRoads.maxCount', mapConfig.obsRoads.maxCount, viridisSimpleHtml ).slice(3), 2)} twoTicks />
</List.Item></> </List.Item></>
) : ) :
( (

View file

@ -44,7 +44,8 @@ const getRoadsLayer = (colorAttribute, maxCount) =>
produce(untaggedRoadsLayer, (draft) => { produce(untaggedRoadsLayer, (draft) => {
draft.id = 'obs_roads_normal' draft.id = 'obs_roads_normal'
if (colorAttribute.endsWith('_count')) { if (colorAttribute.endsWith('_count')) {
delete draft.filter // delete draft.filter
draft.filter = ['to-boolean', ['get', colorAttribute]]
} else { } else {
draft.filter = draft.filter[1] // remove '!' draft.filter = draft.filter[1] // remove '!'
} }
@ -52,7 +53,7 @@ const getRoadsLayer = (colorAttribute, maxCount) =>
draft.paint['line-color'] = colorAttribute.startsWith('distance_') draft.paint['line-color'] = colorAttribute.startsWith('distance_')
? colorByDistance(colorAttribute) ? colorByDistance(colorAttribute)
: colorAttribute.endsWith('_count') : colorAttribute.endsWith('_count')
? colorByCount(colorAttribute, maxCount, reds) ? colorByCount(colorAttribute, maxCount)
: '#DDD' : '#DDD'
draft.paint['line-opacity'][3] = 12 draft.paint['line-opacity'][3] = 12
draft.paint['line-opacity'][5] = 13 draft.paint['line-opacity'][5] = 13