Skip to content

Commit fac17ae

Browse files
authored
website: show children/parent in hierarchy example
2 parents 864c46c + b47904e commit fac17ae

2 files changed

Lines changed: 37 additions & 11 deletions

File tree

examples/website/hierarchy/app.tsx

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {createRoot} from 'react-dom/client';
33
import 'maplibre-gl/dist/maplibre-gl.css';
44
import {Map} from 'react-map-gl/maplibre';
55
import {ScatterplotLayer, ArcLayer, LineLayer} from '@deck.gl/layers';
6-
import {lonLatToCell, cellToBoundary} from 'a5';
6+
import {lonLatToCell, cellToBoundary, cellToChildren, cellToParent} from 'a5';
77
import DeckGL from '@deck.gl/react';
88
import {MapView} from '@deck.gl/core';
99

@@ -19,6 +19,8 @@ const A5GREEN_DARK = [0, 128, 64] as [number, number, number];
1919
const App: React.FC<{showCellId?: boolean}> = ({showCellId = true}) => {
2020
const [viewState, setViewState] = useState(INITIAL_VIEW_STATE);
2121
const [cellLocation, setCellLocation] = useState([INITIAL_VIEW_STATE.longitude, INITIAL_VIEW_STATE.latitude]);
22+
const [showChildren, setShowChildren] = useState(false);
23+
const [showParent, setShowParent] = useState(false);
2224

2325
const onViewStateChange = useCallback(({viewState}) => {
2426
const [longitude, latitude] = cellLocation;
@@ -38,8 +40,10 @@ const App: React.FC<{showCellId?: boolean}> = ({showCellId = true}) => {
3840
// Memoize the entire cells calculation
3941
const data = useMemo(() => {
4042
const cellId = lonLatToCell(cellLocation, resolution);
41-
return {cellId, children: [cellId]};
42-
}, [resolution, cellLocation]);
43+
const children = showChildren ? cellToChildren(cellId) : [];
44+
const parent = showParent ? cellToParent(cellId) : null;
45+
return {cellId, children: [cellId, ...children, ...(parent ? [parent] : [])]};
46+
}, [resolution, cellLocation, showChildren, showParent]);
4347

4448
// Convert cell boundaries to great circle arcs
4549
const arcs = useMemo(() => {
@@ -54,14 +58,18 @@ const App: React.FC<{showCellId?: boolean}> = ({showCellId = true}) => {
5458
}).flat();
5559
}, [data.children]);
5660

61+
const getColor = (_, info) => {
62+
return info.index < 5 ? A5GREEN : [160, 160, 160, 255];
63+
}
64+
5765
const polygonProps: any = {
5866
data: arcs,
5967
getSourcePosition: d => d.source,
6068
getTargetPosition: d => d.target,
61-
getSourceColor: A5GREEN,
62-
getTargetColor: A5GREEN,
63-
getColor: A5GREEN,
64-
getWidth: 2,
69+
getSourceColor: getColor,
70+
getTargetColor: getColor,
71+
getColor: getColor,
72+
getWidth: (_, info) => info.index < 5 ? 2 : 1,
6573
getHeight: 0,
6674
greatCircle: true,
6775
widthUnits: 'pixels'
@@ -154,6 +162,24 @@ const App: React.FC<{showCellId?: boolean}> = ({showCellId = true}) => {
154162
<div>Cell ID (Hex): {`0x${data.cellId.toString(16).padStart(16, '0')}`}</div>
155163
<div>Resolution: {resolution}</div>
156164
<div>Location: [{cellLocation[0].toFixed(4)}, {cellLocation[1].toFixed(4)}]</div>
165+
<div style={{ marginTop: '10px' }}>
166+
<label style={{ marginRight: '15px' }}>
167+
<input
168+
type="checkbox"
169+
checked={showChildren}
170+
onChange={(e) => setShowChildren(e.target.checked)}
171+
/>
172+
Show children
173+
</label>
174+
<label>
175+
<input
176+
type="checkbox"
177+
checked={showParent}
178+
onChange={(e) => setShowParent(e.target.checked)}
179+
/>
180+
Show parent
181+
</label>
182+
</div>
157183
</div>
158184
)}
159185
</>

website/src/examples/hierarchy.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ class HierarchyDemo extends Component {
1414
static renderInfo(meta) {
1515
return (
1616
<div>
17-
<p>A visualization of the A5 pentagon hierarchy on a globe.</p>
18-
<p>Zooming in/out on the page will change the resolution level of the pentagon at the center of the map.</p>
19-
<p>Click any location on the globe to see the pentagon hierarchy at that location.</p>
20-
<p>Notice how cells at similar locations have similar cell ids.</p>
17+
<p>A5 cells for a given location on the globe.</p>
18+
<p>Zoom in/out to change the resolution level of the A5 pentagon at the center of the map.</p>
19+
<p>Click to see the pentagon hierarchy at that location. Notice how cells at similar locations have similar cell ids.</p>
20+
<p>Parent cells do not exactly cover children cells, but are guaranteed to be close.</p>
2121
</div>
2222
);
2323
}

0 commit comments

Comments
 (0)