Color ramp resampling (color ramp)

This example shows how to resample a color ramp and use it to visualize a point layer. Download the sample data here.

Select between different resampling methods or no resampling to compare point layer visualizations.

NPM module setup


npm install --save @maptiler/sdk
import { Map, MapStyle, config, ColorRampCollection } from '@maptiler/sdk';
import '@maptiler/sdk/dist/maptiler-sdk.css';

config.apiKey = 'YOUR_MAPTILER_API_KEY_HERE';
const map = new Map({
    container: 'map',
    zoom: 11,
    center: [-73.97402, 40.73302],
    style: MapStyle.DATAVIZ.LIGHT
});

const ramp = ColorRampCollection.PORTLAND.scale(300, 4000);
const ramp_sqrt = ColorRampCollection.PORTLAND.scale(300, 4000).resample("ease-out-sqrt");
const ramp_exp = ColorRampCollection.PORTLAND.scale(300, 4000).resample("ease-out-exp");

let layers;

map.on('load', async function () {
  await createColorRampLayer(ramp_sqrt, 'sqrt');
});

document.getElementById('resample-method').addEventListener('change', function(evt) {
  const method = this.value;
  mapRemoveLayers();
  switch (method) {
    case 'lineal':
      createColorRampLayer(ramp, method);
      break;
    case 'sqrt':
      createColorRampLayer(ramp_sqrt, method);
      break;
    case 'exp':
      createColorRampLayer(ramp_exp, method);
      break;
    default:
      createColorRampLayer(ramp, "lineal");
      break;
  }
});

async function createColorRampLayer(colorRamp, name) { 
  const rampLayers = await maptilersdk.helpers.addPoint(map, {
    layerId: `school_color_ramp_${name}`,
    data: 'schools.geojson',
    property: "students",
    pointColor: colorRamp,
    pointOpacity: 0.7,
    minPointRadius: 15,
    showLabel: true,
  });
  layers = rampLayers;
}

function mapRemoveLayers() {
  Object.keys(layers).forEach(item => {
    if (layers[item] !== "") {
      if (map.getLayer(layers[item])) map.removeLayer(layers[item]);
    } 
  })
}
Custom color ramp (color ramp)

Custom color ramp (color ramp)

Examples

Visualize point layers by creating custom map color ramps.

Polygon color ramp symbol (polygon helper)

Polygon color ramp symbol (polygon helper)

Examples

Style polygon fill color based on zoom level.

Line color ramp symbol (polyline helper)

Line color ramp symbol (polyline helper)

Examples

Style line color based on map zoom level.

Was this helpful?