-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Raster colorization via "raster-color"
#12368
Closed
Closed
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
3f19039
Initial implementation of `raster-color`
rreusser 309430e
Clean up example and change order of operations in shader
rreusser 9104e72
Move scale factors to a better location
rreusser f1418b5
Clean up raster color configuration
rreusser 859f99a
Formatting
rreusser f728ba6
Build out demo page
rreusser 50f841b
Fix test
rreusser c4fbb78
Move uniforms inside ifdef
rreusser baceb47
Update docs and minor code tweak for clarity
rreusser 4608565
Apply lint fixes
rreusser 24bab92
Retrigger CI
rreusser 59819b9
Partial linter, flow, and style spec fixes
rreusser 89b4c30
Fix flow type errors
rreusser 7191240
Apply PR feedback
rreusser File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,310 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>Mapbox GL JS debug page</title> | ||
<meta charset="utf-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> | ||
<link rel="stylesheet" href="../dist/mapbox-gl.css" /> | ||
<style> | ||
body { margin: 0; padding: 0; } | ||
html, body { height: 100%; } | ||
#map { height: calc(100% - 40px); } | ||
#controls { | ||
position: fixed; | ||
z-index: 1; | ||
top: 5px; | ||
left: 5px; | ||
} | ||
#gradientbg, #gradient { | ||
position: fixed; | ||
bottom: 0; | ||
width: 100%; | ||
height: 40px; | ||
} | ||
#gradientbg { | ||
z-index: 1; | ||
background: repeating-conic-gradient(#ccc 0% 25%, white 0% 50%) 50% / 20px 20px; | ||
} | ||
#gradient { | ||
transition: all 0.3s ease-in-out; | ||
} | ||
</style> | ||
</head> | ||
|
||
<body> | ||
<div id="map"></div> | ||
<div id="controls"></div> | ||
<div id="gradientbg"><div id="gradient"></div></div> | ||
|
||
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/dat-gui/0.7.7/dat.gui.min.js"></script> | ||
<script type="text/javascript" src="https://unpkg.com/d3@7.6.1/dist/d3.min.js"></script> | ||
<script src="../dist/mapbox-gl-dev.js"></script> | ||
<script src="../debug/access_token_generated.js"></script> | ||
<script> | ||
|
||
/*global d3*/ | ||
|
||
const presets = { | ||
Satellite: { | ||
tileset: "mapbox.satellite", | ||
rasterColor: "Cubehelix", | ||
rasterColorMix: "luminosity", | ||
rasterColorRange: "[0, 1]" | ||
}, | ||
Terrain: { | ||
tileset: "mapbox.terrain-rgb", | ||
rasterColor: "Hypsometric", | ||
rasterColorMix: "decode terrain-rgb", | ||
rasterColorRange: "[0, 8848]", | ||
forcePngraw: true | ||
}, | ||
"Population log-density": { | ||
tileset: "rreusser.796swou6 (pop log-density)", | ||
rasterColor: "PopDensity", | ||
rasterColorMix: "decode population log-density", | ||
rasterColorRange: "[-3, 4]", | ||
forbid2x: true, | ||
forcePngraw: true | ||
}, | ||
'Clouds (GMGSI LWIR)': { | ||
tileset: "rreusser.dv2uv1dw (GMGSI clouds)", | ||
rasterColor: "Transparent white", | ||
rasterColorMix: "luminosity", | ||
rasterColorRange: "[0, 1]" | ||
}, | ||
'Radar composite reflectivity (GFS)': { | ||
tileset: "rreusser.b3c8wj9i (GFS refc)", | ||
rasterColor: "Radar", | ||
rasterColorMix: "luminosity", | ||
rasterColorRange: "[0, 1]" | ||
}, | ||
'Radar composite reflectivity (HRRR)': { | ||
tileset: "rreusser.5vzfcdjn (HRRR refc)", | ||
rasterColor: "Radar", | ||
rasterColorMix: "luminosity", | ||
rasterColorRange: "[0, 1]" | ||
}, | ||
}; | ||
|
||
function GUIParams () { | ||
this.preset = "Satellite"; | ||
this.opacity = 1; | ||
Object.assign(this, presets[this.preset]); | ||
} | ||
|
||
const guiParams = new GUIParams(); | ||
|
||
const mixes = { | ||
luminosity: [ 0.2126, 0.7152, 0.0722, 0 ], | ||
average: [1 / 3, 1 / 3, 1 / 3, 0], | ||
red: [1, 0, 0, 0], | ||
green: [0, 1, 0, 0], | ||
blue: [0, 0, 1, 0], | ||
"decode terrain-rgb": [ | ||
256 * 256 * 256 * 0.1, | ||
256 * 256 * 0.1, | ||
256 * 0.1, | ||
-10000 | ||
], | ||
"decode population log-density": [ | ||
256 * 256 * 256 * 8.348993603394451e-07, | ||
256 * 256 * 8.348993603394451e-07, | ||
256 * 8.348993603394451e-07, | ||
-9 | ||
], | ||
}; | ||
|
||
const ranges = { | ||
"[0, 1]": [0, 1], | ||
"[0, 8848]": [0, 8848], | ||
"[-3, 4]": [-3, 4], | ||
}; | ||
|
||
const tilesets = { | ||
"mapbox.terrain-rgb": "mapbox.terrain-rgb", | ||
"mapbox.satellite": "mapbox.satellite", | ||
"rreusser.796swou6 (pop log-density)": "rreusser.796swou6", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Included the sake of illustration and debugging, but we should probably remove tilesets on my account from the final PR. |
||
"rreusser.dv2uv1dw (GMGSI clouds)": "rreusser.dv2uv1dw", | ||
"rreusser.5vzfcdjn (HRRR refc)": "rreusser.5vzfcdjn", | ||
"rreusser.b3c8wj9i (GFS refc)": "rreusser.b3c8wj9i", | ||
}; | ||
|
||
const quantize = (interpolator, opac = x => 1, n = 100) => d3.quantize(interpolator, n).map((c, i) => { | ||
const col = d3.rgb(c); | ||
const t = i / (n - 1); | ||
return [ t, `rgba(${col.r},${col.g},${col.b},${col.opacity * opac(t)})` ]; | ||
}); | ||
|
||
const colorScales = { | ||
Viridis: quantize(d3.interpolateViridis), | ||
Greys: quantize(x => d3.interpolateGreys(1 - x)), | ||
Turbo: quantize(d3.interpolateTurbo), | ||
Inferno: quantize(d3.interpolateInferno), | ||
Magma: quantize(d3.interpolateMagma), | ||
Plasma: quantize(d3.interpolatePlasma), | ||
Cividis: quantize(d3.interpolateCividis), | ||
Cool: quantize(d3.interpolateCool), | ||
Warm: quantize(d3.interpolateWarm), | ||
Cubehelix: quantize(d3.interpolateCubehelixDefault), | ||
RdPu: quantize(x => d3.interpolateRdPu(1 - x)), | ||
YlGnBu: quantize(x => d3.interpolateYlGnBu(1 - x)), | ||
Rainbow: quantize(d3.interpolateRainbow), | ||
Sinebow: quantize(d3.interpolateSinebow), | ||
RdBu: quantize(d3.interpolateRdBu), | ||
PopDensity: quantize(d3.interpolateMagma, t => 3 * t * t - 2 * t * t * t), | ||
Hypsometric: [ | ||
[ 0.004, "rgb(48, 167, 228)" ], | ||
[ 0.005, "rgb(57, 143, 83)" ], | ||
[ 0.031, "rgb(116, 166, 129)" ], | ||
[ 0.055, "rgb(178, 205, 174)" ], | ||
[ 0.076, "rgb(188, 195, 169)" ], | ||
[ 0.108, "rgb(221, 207, 153)" ], | ||
[ 0.153, "rgb(211, 174, 114)" ], | ||
[ 0.205, "rgb(207, 155, 103)" ], | ||
[ 0.277, "rgb(179, 120, 85)" ], | ||
[ 0.375, "rgb(227, 210, 197)" ], | ||
[ 0.660, "rgb(255, 255, 255)" ] | ||
], | ||
'Transparent white': [ | ||
[ 0.0, "rgba(255, 255, 255, 0)" ], | ||
[ 1.0, "rgba(255, 255, 255, 1)" ], | ||
], | ||
Radar: [ | ||
[ 0.0000, "rgba(35,23,27,0)" ], | ||
[ 0.0714, "rgba(68, 188, 116, 0.1)" ], | ||
[ 0.1428, "rgba(30, 183, 66, 0.3)" ], | ||
[ 0.2142, "rgba(45, 193, 81, 0.5)" ], | ||
[ 0.2857, "rgba(38, 208, 43, 0.7)" ], | ||
[ 0.3571, "rgba(58, 237, 55, 0.9)" ], | ||
[ 0.4285, "rgb(143, 252, 95)" ], | ||
[ 0.5000, "rgb(190, 251, 81)" ], | ||
[ 0.5714, "rgb(200, 235, 26)" ], | ||
[ 0.6428, "rgb(245,199,43)" ], | ||
[ 0.7142, "rgb(255,155,33)" ], | ||
[ 0.7857, "rgb(251,105,25)" ], | ||
[ 0.8571, "rgb(214,57,15)" ], | ||
[ 0.9285, "rgb(168,22,4)" ], | ||
[ 1.0000, "rgb(183, 44, 204)" ] | ||
] | ||
}; | ||
|
||
function setStyle (tileset) { | ||
map.setStyle({ | ||
version: 8, | ||
sources: { | ||
mapbox: { | ||
type: "raster", | ||
url: "mapbox://mapbox.satellite", | ||
tileSize: 256 | ||
}, | ||
raster: { | ||
type: "raster", | ||
tiles: [ `https://api.mapbox.com/v4/${tilesets[tileset]}/{z}/{x}/{y}@2x.webp` ], | ||
maxzoom: 14, | ||
tileSize: 256 | ||
} | ||
}, | ||
layers: [{ | ||
"id": "background", | ||
"type": "background", | ||
"paint": { | ||
"background-color": "rgb(4,7,14)" | ||
} | ||
}, { | ||
"id": "satellite", | ||
"type": "raster", | ||
"source": "mapbox", | ||
"source-layer": "mapbox_satellite_full", | ||
"paint": { | ||
"raster-saturation": -0.3, | ||
"raster-brightness-max": 0.5, | ||
} | ||
}, { | ||
"type": "raster", | ||
"source": "raster", | ||
"id": "raster", | ||
"paint": { | ||
"raster-opacity": guiParams.opacity | ||
} | ||
}] | ||
}); | ||
} | ||
|
||
var map = window.map = new mapboxgl.Map({ | ||
container: "map", | ||
style: {version: 8, layers: [], sources: {}}, | ||
hash: true, | ||
transformRequest (url, type) { | ||
if (type !== "Tile") return; | ||
const preset = presets[guiParams.preset]; | ||
if (preset.forbid2x) url = url.replace('@2x', ''); | ||
if (preset.forcePngraw) url = url.replace('.webp', '.pngraw'); | ||
return {url}; | ||
} | ||
}); | ||
|
||
map.once("load", function () { | ||
var gui = window.gui = new dat.GUI(); // eslint-disable-line | ||
|
||
const preset = gui.add(guiParams, "preset", Object.keys(presets)); | ||
const tileset = gui.add(guiParams, "tileset", Object.keys(tilesets)); | ||
const scale = gui.add(guiParams, "rasterColor", Object.keys(colorScales)); | ||
const mix = gui.add(guiParams, "rasterColorMix", Object.keys(mixes)); | ||
const range = gui.add(guiParams, "rasterColorRange", Object.keys(ranges)); | ||
const opacity = gui.add(guiParams, "opacity", 0, 1); | ||
|
||
function setColorScale(scale) { | ||
const range = ranges[guiParams.rasterColorRange]; | ||
const cs = colorScales[scale]; | ||
map.setPaintProperty("raster", "raster-color", [ | ||
"interpolate", | ||
["linear"], | ||
["raster-value"], | ||
...cs.map(([pos, col]) => [ | ||
range[0] + (range[1] - range[0]) * pos, | ||
col | ||
]).flat(), | ||
]); | ||
document.getElementById("gradient").style.background = `linear-gradient(90deg, ${cs.map(([pos, col]) => `${col} ${(pos * 100).toFixed(1)}%`).join(",")})`; | ||
document.getElementById("gradient").style.opacity = guiParams.opacity; | ||
} | ||
|
||
function setColorMix(mix) { | ||
map.setPaintProperty("raster", "raster-color-mix", mixes[mix]); | ||
} | ||
|
||
function setColorRange(range) { | ||
map.setPaintProperty("raster", "raster-color-range", ranges[range]); | ||
setColorScale(guiParams.rasterColor); | ||
} | ||
|
||
function setTileset(range) { | ||
setStyle(guiParams.tileset); | ||
setColorMix(guiParams.rasterColorMix); | ||
setColorScale(guiParams.rasterColor); | ||
setColorRange(guiParams.rasterColorRange); | ||
} | ||
|
||
function setPreset(presetName) { | ||
const preset = presets[presetName]; | ||
Object.assign(guiParams, preset); | ||
setTileset(guiParams.tileset); | ||
} | ||
|
||
mix.onChange(setColorMix).listen(); | ||
scale.onChange(setColorScale).listen(); | ||
range.onChange(setColorRange).listen(); | ||
tileset.onChange(setTileset).listen(); | ||
preset.onChange(setPreset); | ||
|
||
opacity.onChange(opac => { | ||
map.setPaintProperty("raster", "raster-opacity", opac); | ||
document.getElementById("gradient").style.opacity = opac; | ||
}); | ||
|
||
setTileset(guiParams.tileset); | ||
}); | ||
|
||
</script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check warning
Code scanning / CodeQL
Inclusion of functionality from an untrusted source