Visualization chooser (continued)

The final missing piece of our COG explorer is to make it so the user can choose the imagery source. While this section is already getting quite long, you may be able to tell that there is not that much more work involved in adding an image chooser. Let's do it!

The first thing we need to do is come up with a list of images that should be displayed. For each image, we want to display a name to show the user in a <select> element, and we need to have a base for the GeoTIFF URL. So we'll want to add something like this to your main.js:

const images = [
    name: 'Buenos Aires',
    base: '',
    name: 'Minneapolis',
    base: '',
    name: 'Cape Town',
    base: '',

Next, we'll add another <select> element to let the user choose the image. In your index.html, adjust the controls to look like this:

<div id="controls">
  <select id="image"></select>
  <select id="visualization"></select>

Back to the JavaScript, we need to populate that <select> element with one <option> for each imagery source. In your main.js add the following:

const imageSelector = document.getElementById('image');
images.forEach((image) => {
  const option = document.createElement('option');
  option.textContent =;

Now we just need to make a minor adjustment to the function that updates our visualization so it grabs the base URL from the selected imagery source. In main.js edit the updateVisualization function and surrounding lines so it looks something like this:

let previousBase;
function updateVisualization() {
  const visualization = visualizations[visualizationSelector.selectedIndex];
  const base = images[imageSelector.selectedIndex].base;
  const newBase = base !== previousBase;
  previousBase = base;

  const layer = createLayer(base, visualization);

  if (newBase) {

visualizationSelector.addEventListener('change', updateVisualization);
imageSelector.addEventListener('change', updateVisualization);

Note that we also only now update the view if the user has selected a new imagery source. This is nicer than zooming out to the full extent whenever a new visualization type is selected.

Reload http://localhost:5173/. Boom!

COG explorer
COG explorer

results matching ""

    No results matching ""