|
| 1 | +import maplibregl from 'maplibre-gl'; |
| 2 | +import {Deck} from '@deck.gl/core'; |
| 3 | +import {H3TileLayer} from '@deck.gl/carto'; |
| 4 | +import {Filter, h3TableSource, H3TableSourceResponse} from '@carto/api-client'; |
| 5 | +import '../components/index.js'; |
| 6 | +import type {Widget, FilterEvent} from '../components/index.js'; |
| 7 | + |
| 8 | +/************************************************************************** |
| 9 | + * REACTIVE STATE |
| 10 | + */ |
| 11 | + |
| 12 | +let data: Promise<H3TableSourceResponse>; |
| 13 | +let viewState = {latitude: 37.375, longitude: -5.996, zoom: 6}; |
| 14 | +let filters: Record<string, Filter> = {}; |
| 15 | + |
| 16 | +/************************************************************************** |
| 17 | + * DECK.GL |
| 18 | + */ |
| 19 | + |
| 20 | +const deck = new Deck({ |
| 21 | + canvas: 'deck-canvas', |
| 22 | + initialViewState: viewState, |
| 23 | + controller: true, |
| 24 | + layers: [], |
| 25 | +}); |
| 26 | + |
| 27 | +const map = new maplibregl.Map({ |
| 28 | + container: 'map', |
| 29 | + style: |
| 30 | + 'https://basemaps.cartocdn.com/gl/positron-nolabels-gl-style/style.json', |
| 31 | + interactive: false, |
| 32 | +}); |
| 33 | + |
| 34 | +deck.setProps({ |
| 35 | + onViewStateChange: (params) => { |
| 36 | + viewState = params.viewState; |
| 37 | + const {longitude, latitude, ...rest} = viewState; |
| 38 | + map.jumpTo({center: [longitude, latitude], ...rest}); |
| 39 | + updateWidgets(); |
| 40 | + }, |
| 41 | +}); |
| 42 | + |
| 43 | +const widgets: Widget[] = [ |
| 44 | + bindWidget('#formula'), |
| 45 | + bindWidget('#category'), |
| 46 | + bindWidget('#pie'), |
| 47 | + bindWidget('#table'), |
| 48 | + bindWidget('#scatter'), |
| 49 | + bindWidget('#histogram'), |
| 50 | +]; |
| 51 | + |
| 52 | +updateSources(); |
| 53 | + |
| 54 | +/************************************************************************** |
| 55 | + * UPDATES |
| 56 | + */ |
| 57 | + |
| 58 | +function updateSources() { |
| 59 | + data = h3TableSource({ |
| 60 | + accessToken: import.meta.env.VITE_CARTO_ACCESS_TOKEN, |
| 61 | + connectionName: 'carto_dw', |
| 62 | + tableName: |
| 63 | + 'carto-demo-data.demo_tables.derived_spatialfeatures_esp_h3res8_v1_yearly_v2', |
| 64 | + filters, |
| 65 | + aggregationExp: 'sum(population) as population', |
| 66 | + }); |
| 67 | + |
| 68 | + updateLayers(); |
| 69 | + updateWidgets(); |
| 70 | +} |
| 71 | + |
| 72 | +function updateLayers() { |
| 73 | + const layer = new H3TileLayer({ |
| 74 | + id: 'retail_stores', |
| 75 | + data, |
| 76 | + pointRadiusMinPixels: 4, |
| 77 | + getFillColor: [200, 0, 80], |
| 78 | + }); |
| 79 | + |
| 80 | + deck.setProps({layers: [layer]}); |
| 81 | + data.then(({attribution}) => { |
| 82 | + document.querySelector('#footer')!.innerHTML = attribution; |
| 83 | + }); |
| 84 | +} |
| 85 | + |
| 86 | +function updateWidgets() { |
| 87 | + for (const widget of widgets) { |
| 88 | + widget.data = data; |
| 89 | + widget.viewState = viewState; |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +/************************************************************************** |
| 94 | + * INITIALIZATION |
| 95 | + */ |
| 96 | + |
| 97 | +function bindWidget(selector: string): Widget { |
| 98 | + const widget = document.querySelector<Widget>(selector)!; |
| 99 | + |
| 100 | + widget.addEventListener('filter', (event) => { |
| 101 | + filters = (event as FilterEvent).detail.filters; |
| 102 | + updateSources(); |
| 103 | + }); |
| 104 | + |
| 105 | + return widget; |
| 106 | +} |
0 commit comments