Display a graticule on a Map.

Color
100% © U.S. Geological Survey

The Map can display a fully configurable graticule.

index.js
import colormap from 'colormap';

import { Color } from 'three';
import { MapControls } from 'three/examples/jsm/controls/MapControls.js';

import XYZ from 'ol/source/XYZ.js';

import Extent from '@giro3d/giro3d/core/geographic/Extent.js';
import Instance from '@giro3d/giro3d/core/Instance.js';
import ElevationLayer from '@giro3d/giro3d/core/layer/ElevationLayer.js';
import TiledImageSource from '@giro3d/giro3d/sources/TiledImageSource.js';
import Map from '@giro3d/giro3d/entities/Map.js';
import Inspector from '@giro3d/giro3d/gui/Inspector.js';
import Interpretation from '@giro3d/giro3d/core/layer/Interpretation.js';
import GeoTIFFFormat from '@giro3d/giro3d/formats/GeoTIFFFormat.js';
import ColorMap, { ColorMapMode } from '@giro3d/giro3d/core/layer/ColorMap.js';

const x = -13602000;
const y = 5812000;
const halfWidth = 25000;

// Defines geographic extent: CRS, min/max X, min/max Y
const extent = new Extent('EPSG:3857', x - halfWidth, x + halfWidth, y - halfWidth, y + halfWidth);

// `viewerDiv` will contain Giro3D' rendering area (the canvas element)
const viewerDiv = document.getElementById('viewerDiv');

// Creates a Giro3D instance
const instance = new Instance(viewerDiv, {
    crs: extent.crs(),
    renderer: {
        clearColor: 0x0a3b59,
    },
});

// Creates a map that will contain the layer
const map = new Map('planar', {
    extent,
    hillshading: true,
    discardNoData: true,
    doubleSided: true,
    backgroundColor: 'white',
    graticule: {
        enabled: true,
        color: new Color('white'),
        xStep: 500,
        yStep: 500,
        xOffset: 0,
        yOffset: 0,
        opacity: 1,
        thickness: 20,
    },
});

instance.add(map);

const source = new TiledImageSource({
    source: new XYZ({
        minZoom: 10,
        maxZoom: 16,
        url: 'https://3d.oslandia.com/dem/MtStHelens-tiles/{z}/{x}/{y}.tif',
    }),
    format: new GeoTIFFFormat(),
});

const floor = 1100;
const ceiling = 2500;

const values = colormap({ colormap: 'viridis', nshades: 256 });
const colors = values.map(v => new Color(v));

const dem = new ElevationLayer({
    name: 'dem',
    extent,
    interpretation: Interpretation.Raw,
    source,
    colorMap: new ColorMap(colors, floor, ceiling, ColorMapMode.Elevation),
});

map.addLayer(dem);

instance.camera.camera3D.position.set(-13600394, 5818579, 11832);

// Instanciates controls
const controls = new MapControls(instance.camera.camera3D, instance.domElement);

controls.target.set(-13603000, 5811000, 0);

instance.useTHREEControls(controls);

// GUI
function bindSlider(id, callback) {
    const slider = document.getElementById(id);
    slider.oninput = function oninput() {
        callback(slider.value);
        instance.notifyChange(map);
    };
}

function bindToggle(id, callback) {
    const toggle = document.getElementById(id);

    toggle.oninput = function oninput() {
        callback(toggle.checked);
        instance.notifyChange(map);
    };
}

function bindDropdown(id, callback) {
    document.getElementById(id).addEventListener('change', e => {
        callback(e.target.value);
        instance.notifyChange(map);
    });
}

bindToggle('toggle-graticule', v => (map.materialOptions.graticule.enabled = v));

bindSlider('x-step', v => (map.materialOptions.graticule.xStep = v));
bindSlider('y-step', v => (map.materialOptions.graticule.yStep = v));
bindSlider('x-offset', v => (map.materialOptions.graticule.xOffset = v));
bindSlider('y-offset', v => (map.materialOptions.graticule.yOffset = v));
bindSlider('opacity', v => (map.materialOptions.graticule.opacity = v));
bindSlider('thickness', v => (map.materialOptions.graticule.thickness = v));

bindDropdown('color', v => (map.materialOptions.graticule.color = new Color(v)));

Inspector.attach(document.getElementById('panelDiv'), instance);
index.html
<!doctype html>
<html lang="en">
    <head>
        <title>Graticule</title>
        <meta charset="UTF-8" />
        <meta name="name" content="graticule" />
        <meta name="description" content="Display a graticule on a Map." />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />

        <link rel="icon" href="https://giro3d.org/images/favicon.svg" />
        <link href="https://giro3d.org/assets/bootstrap-custom.css" rel="stylesheet" />
        <script src="https://giro3d.org/assets/bootstrap.bundle.min.js"></script>
        <link
            rel="stylesheet"
            type="text/css"
            href="https://giro3d.org/latest/css/example.css"
        />

        
    </head>

    <body>
        <div id="viewerDiv" class="m-0 p-0 w-100 h-100"></div>
        <div id="panelDiv" class="position-absolute top-0 start-0 mh-100 overflow-auto"></div>

        <div class="side-pane-with-status-bar">
            <div class="card">
                <div class="card-body">
                    <!-- Show/Hide graticule -->
                    <div class="form-check form-switch">
                        <input
                            class="form-check-input"
                            type="checkbox"
                            checked="true"
                            role="switch"
                            id="toggle-graticule"
                            autocomplete="off"
                        />
                        <label class="form-check-label" for="toggle-graticule">Show graticule</label>
                    </div>

                    <div class="my-2"></div>

                    <!-- X step -->
                    <label for="x-step" class="form-label">X-axis step</label>
                    <div class="input-group">
                        <input
                            type="range"
                            min="100"
                            max="3000"
                            value="1000"
                            step="100"
                            class="form-range"
                            id="x-step"
                            autocomplete="off"
                        />
                    </div>

                    <div class="my-2"></div>

                    <!-- Y step -->
                    <label for="y-step" class="form-label">Y-axis step</label>
                    <div class="input-group">
                        <input
                            type="range"
                            min="100"
                            max="3000"
                            value="1000"
                            step="100"
                            class="form-range"
                            id="y-step"
                            autocomplete="off"
                        />
                    </div>

                    <div class="my-2"></div>

                    <!-- X offset -->
                    <label for="x-offset" class="form-label">X-axis offset</label>
                    <div class="input-group">
                        <input
                            type="range"
                            min="100"
                            max="3000"
                            value="200"
                            step="100"
                            class="form-range"
                            id="x-offset"
                            autocomplete="off"
                        />
                    </div>

                    <div class="my-2"></div>

                    <!-- Y offset -->
                    <label for="y-offset" class="form-label">Y-axis offset</label>
                    <div class="input-group">
                        <input
                            type="range"
                            min="100"
                            max="3000"
                            value="200"
                            step="100"
                            class="form-range"
                            id="y-offset"
                            autocomplete="off"
                        />
                    </div>

                    <div class="my-2"></div>

                    <!-- Opacity -->
                    <label for="opacity" class="form-label">Opacity</label>
                    <div class="input-group">
                        <input
                            type="range"
                            min="0"
                            max="1"
                            value="1"
                            step="0.01"
                            class="form-range"
                            id="opacity"
                            autocomplete="off"
                        />
                    </div>

                    <!-- Thickness -->
                    <label for="thickness" class="form-label">Thickness</label>
                    <div class="input-group">
                        <input
                            type="range"
                            min="1"
                            max="100"
                            value="20"
                            step="1"
                            class="form-range"
                            id="thickness"
                            autocomplete="off"
                        />
                    </div>

                    <div class="my-2"></div>

                    <!-- Color dropdown -->
                    <div class="input-group">
                        <span class="input-group-text flex-grow-1">Color</span>
                        <select class="btn btn-outline-primary btn-sm" id="color" autocomplete="off">
                            <option selected value="white">White</option>
                            <option value="black">Black</option>
                            <option value="yellow">Yellow</option>
                            <option value="red">Red</option>
                        </select>
                    </div>
                </div>
            </div>
        </div>

        <script type="module" src="index.js"></script>
        <script>
            /* activate popovers */
            const popoverTriggerList = [].slice.call(
                document.querySelectorAll('[data-bs-toggle="popover"]'),
            );
            popoverTriggerList.map(
                // bootstrap is used as script in the template, disable warning about undef
                // eslint-disable-next-line no-undef
                popoverTriggerEl =>
                    new bootstrap.Popover(popoverTriggerEl, {
                        trigger: 'hover',
                        placement: 'left',
                        content: document.getElementById(
                            popoverTriggerEl.getAttribute('data-bs-content'),
                        ).innerHTML,
                        html: true,
                    }),
            );
        </script>
    </body>
</html>
package.json
{
    "name": "graticule",
    "dependencies": {
        "colormap": "^2.3.2",
        "@giro3d/giro3d": "0.37.3"
    },
    "devDependencies": {
        "vite": "^3.2.3"
    },
    "scripts": {
        "start": "vite",
        "build": "vite build"
    }
}