-
Notifications
You must be signed in to change notification settings - Fork 1
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
tileset tab in examples #46
Draft
juandjara
wants to merge
11
commits into
main
Choose a base branch
from
feature/sc-461331/tileset-example
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+679
−10
Draft
Changes from 7 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
d3778cf
WIP tileset example
juandjara 37dcb49
dropped features data
juandjara 67cbc28
complete incomeView in react with warnings
juandjara 09b826d
use same tileset as deck.gl example
juandjara 6dda023
adapt new widgets interface
juandjara b92d767
first try at histogram widget for app templates
juandjara a0e0dc0
update histogram example
juandjara 47ac4ea
fix viewstate update bug
juandjara 55eb017
fix linter warning
juandjara 2588c87
Merge branch 'main' into feature/sc-461331/tileset-example
juandjara c42b74e
fix yarn.lock
juandjara 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
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
237 changes: 237 additions & 0 deletions
237
packages/create-react/src/components/views/RiversView.tsx
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,237 @@ | ||
import { MapView, MapViewState, WebMercatorViewport } from "@deck.gl/core"; | ||
import { useContext, useEffect, useMemo, useState } from "react"; | ||
import { AppContext } from "../../context"; | ||
import { useDebouncedState } from "../../hooks/useDebouncedState"; | ||
import { createViewportSpatialFilter, vectorTilesetSource } from "@carto/api-client"; | ||
import { BASEMAP, VectorTileLayer } from "@deck.gl/carto"; | ||
import { Card } from "../Card"; | ||
import { FormulaWidget } from "../widgets/FormulaWidget"; | ||
import DeckGL from "@deck.gl/react"; | ||
import { Map } from 'react-map-gl/maplibre'; | ||
import { Layers } from "../Layers"; | ||
import { HistogramWidget } from "../widgets/HistogramWidget"; | ||
|
||
const CONNECTION_NAME = 'amanzanares-pm-bq'; | ||
const TILESET_NAME = 'cartodb-on-gcp-pm-team.amanzanares_opensource_demo.national_water_model_tileset_final_test_4'; | ||
const MAP_VIEW = new MapView({ repeat: true }); | ||
|
||
const INITIAL_VIEW_STATE: MapViewState = { | ||
latitude: 31.8028, | ||
longitude: -103.0078, | ||
zoom: 4, | ||
}; | ||
|
||
const histogramTicks = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; | ||
|
||
function hexToRgb(hex: string) { | ||
const r = parseInt(hex.slice(1, 3), 16); | ||
const g = parseInt(hex.slice(3, 5), 16); | ||
const b = parseInt(hex.slice(5, 7), 16); | ||
return [r, g, b]; | ||
} | ||
|
||
/** | ||
* Example application page, showing U.S. streams network. | ||
*/ | ||
export default function IncomeView() { | ||
// With authentication enabled, access token may change. | ||
const { accessToken, apiBaseUrl } = useContext(AppContext); | ||
const [attributionHTML, setAttributionHTML] = useState(''); | ||
|
||
// data to calculate feature dropping for each zoom level | ||
const [fractionsDropped, setFractionsDropped] = useState<number[]>([]); | ||
const [minZoom, setMinZoom] = useState(0); | ||
const [maxZoom, setMaxZoom] = useState(20); | ||
const [tilesLoaded, setTilesLoaded] = useState(false); | ||
|
||
// Debounce view state to avoid excessive re-renders during pan and zoom. | ||
const [viewState, setViewState] = useDebouncedState(INITIAL_VIEW_STATE, 200); | ||
|
||
/**************************************************************************** | ||
* Sources (https://deck.gl/docs/api-reference/carto/data-sources) | ||
*/ | ||
|
||
const data = useMemo( | ||
() => | ||
vectorTilesetSource({ | ||
accessToken, | ||
apiBaseUrl, | ||
connectionName: CONNECTION_NAME, | ||
tableName: TILESET_NAME, | ||
}), | ||
[accessToken, apiBaseUrl], | ||
); | ||
|
||
/**************************************************************************** | ||
* Layers (https://deck.gl/docs/api-reference/carto/overview#carto-layers) | ||
*/ | ||
|
||
const LAYER_ID = 'Income by block group' | ||
|
||
// Layer visibility represented as name/visibility pairs, managed by the Layers component. | ||
const [layerVisibility, setLayerVisibility] = useState< | ||
Record<string, boolean> | ||
>({ | ||
[LAYER_ID]: true, | ||
}); | ||
|
||
// Update layers when data or visualization parameters change. | ||
const layers = useMemo(() => { | ||
return [ | ||
new VectorTileLayer({ | ||
id: LAYER_ID, | ||
pickable: true, | ||
visible: layerVisibility[LAYER_ID], | ||
data, | ||
getLineColor: d => { | ||
const [r, g, b] = hexToRgb('#d5d5d7'); | ||
const n = d.properties.streamOrder; | ||
const alphaPart = Math.min(n / 10, 1); | ||
const alpha = 120 + 128 * alphaPart; | ||
return [r, g, b, alpha]; | ||
}, | ||
getLineWidth: d => { | ||
const n = d.properties.streamOrder; | ||
return n * 0.5; | ||
}, | ||
lineWidthUnits: 'pixels', | ||
lineWidthMinPixels: 1, | ||
onViewportLoad(tiles) { | ||
data?.then((res) => { | ||
setTilesLoaded(true) | ||
res.widgetSource.loadTiles(tiles) | ||
const bbox = new WebMercatorViewport(viewState).getBounds() | ||
const spatialFilter = createViewportSpatialFilter(bbox) | ||
if (spatialFilter) { | ||
res.widgetSource.extractTileFeatures({ spatialFilter }) | ||
} | ||
}) | ||
}, | ||
}), | ||
]; | ||
}, [data, layerVisibility]); | ||
|
||
/**************************************************************************** | ||
* Attribution | ||
*/ | ||
|
||
useEffect(() => { | ||
data?.then((res) => { | ||
const { fraction_dropped_per_zoom, minzoom, maxzoom, attribution } = res | ||
setFractionsDropped(fraction_dropped_per_zoom ?? []) | ||
setMinZoom(minzoom ?? 0) | ||
setMaxZoom(maxzoom ?? 20) | ||
setAttributionHTML(attribution) | ||
}) | ||
}, [data]); | ||
|
||
useEffect(() => { | ||
if (data && viewState && tilesLoaded) { | ||
data?.then((res) => { | ||
const bbox = new WebMercatorViewport(viewState).getBounds() | ||
const spatialFilter = createViewportSpatialFilter(bbox) | ||
if (spatialFilter) { | ||
res.widgetSource.extractTileFeatures({ spatialFilter }) | ||
} | ||
}) | ||
} | ||
}, [data, viewState, tilesLoaded]) | ||
|
||
function clamp(n: number, min: number, max: number) { | ||
return Math.min(Math.max(n, min), max); | ||
} | ||
|
||
const droppingPercent = useMemo(() => { | ||
if (!fractionsDropped.length) { | ||
return 0 | ||
} | ||
const roundedZoom = Math.round(viewState.zoom) | ||
const clampedZoom = clamp(roundedZoom, minZoom, maxZoom) | ||
const percent = fractionsDropped[clampedZoom] | ||
return percent | ||
}, [minZoom, maxZoom, fractionsDropped, viewState.zoom]) | ||
|
||
return ( | ||
<> | ||
<aside className="sidebar"> | ||
<Card> | ||
<p className="overline">✨👀 You're viewing</p> | ||
<h1 className="title">U.S. Streams Network</h1> | ||
<p className="body1"> | ||
Cheesecake caramels sesame snaps gummi bears oat cake chupa chups. | ||
Chupa chups sugar plum tootsie roll powder candy canes. Biscuit cake | ||
gummies cheesecake cupcake biscuit bear claw icing. Jelly topping | ||
caramels gummi bears carrot cake pudding. | ||
</p> | ||
<p className="body1"> | ||
Bear claw marshmallow gingerbread muffin sweet roll bear claw ice | ||
cream cake macaroon. Lollipop brownie ice cream pudding sweet gummi | ||
bears jelly jelly-o tart. | ||
</p> | ||
</Card> | ||
<span className="flex-space" /> | ||
{tilesLoaded && ( | ||
<> | ||
{droppingPercent > 0 && droppingPercent <= 0.05 && ( | ||
<section className='caption' style={{ padding: '4px 8px' }}> | ||
<strong>Warning:</strong> There may be some data ({(droppingPercent * 100).toFixed(2)}%) missing at this zoom level ({Math.round(viewState.zoom)}) because of the tileset dropping features. | ||
</section> | ||
)} | ||
{droppingPercent > 0.05 && ( | ||
<section className='caption' style={{ padding: '4px 8px' }}> | ||
<strong>Warning:</strong> There is an important amount of data ({(droppingPercent * 100).toFixed(2)}%) missing at this zoom level ({Math.round(viewState.zoom)}) because of the tileset dropping features. Widget calculations will not be accurate. | ||
</section> | ||
)} | ||
<Card title="Stream count"> | ||
<FormulaWidget | ||
data={data} | ||
column={'*'} | ||
operation={'count'} | ||
viewState={viewState} | ||
/> | ||
</Card> | ||
<Card title="Stream count by stream order"> | ||
<HistogramWidget | ||
data={data} | ||
column='streamOrder' | ||
ticks={histogramTicks} | ||
viewState={viewState} | ||
operation="count" | ||
/> | ||
</Card> | ||
</> | ||
)} | ||
</aside> | ||
<main className="map"> | ||
<DeckGL | ||
layers={layers} | ||
views={MAP_VIEW} | ||
initialViewState={viewState} | ||
controller={{ dragRotate: false }} | ||
onViewStateChange={({ viewState }) => setViewState(viewState)} | ||
> | ||
<Map mapStyle={BASEMAP.DARK_MATTER} /> | ||
</DeckGL> | ||
<Layers | ||
layers={layers} | ||
layerVisibility={layerVisibility} | ||
onLayerVisibilityChange={setLayerVisibility} | ||
/> | ||
{/* <Card title="Legend" className="legend"> | ||
<LegendEntryCategorical | ||
title="Block group" | ||
subtitle="By income per capita" | ||
values={histogramTicks.map(String)} | ||
getSwatchColor={(value: string) => | ||
colors[histogramTicks.indexOf(Number(value))] as Color | ||
} | ||
/> | ||
</Card> */} | ||
<aside | ||
className="map-footer" | ||
dangerouslySetInnerHTML={{ __html: attributionHTML }} | ||
></aside> | ||
</main> | ||
</> | ||
) | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing a useMemo dependency for
viewState
here – the tiles may be filtered by an old viewport, returning incorrect or missing data.The other thing to consider is that the 'onViewportLoad' event could happen either before or after the (debounced) 'onViewStateChange' event, when the user moves the map. If 'onViewportLoad' is second, then there is nothing that cues the widgets to recalculate, and they'll keep showing old results.
We could fix that by adding an imperative
.refresh()
method to the widgets, but that feels like "bad style" in React... maybe the simplest thing is just to force-update the viewState once more, after tiles load? Then we can remove the extractTileFeatures call here, and most of the time avoid filtering twice:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great! This fixes it!
I think the initial using of
data.then
everywhere is what threw me off when reading the async flows as I just copied that from other examples. But this seems to work so great