-
Notifications
You must be signed in to change notification settings - Fork 39
feat: Switch to using MapboxOverlay #890
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,8 +2,12 @@ import * as React from "react"; | |
import { useEffect, useCallback, useState } from "react"; | ||
import { createRender, useModelState, useModel } from "@anywidget/react"; | ||
import type { Initialize, Render } from "@anywidget/types"; | ||
import Map from "react-map-gl/maplibre"; | ||
import DeckGL from "@deck.gl/react"; | ||
import Map, { | ||
useControl, | ||
FullscreenControl, | ||
NavigationControl, | ||
ScaleControl, | ||
} from "react-map-gl/maplibre"; | ||
import { MapViewState, PickingInfo, type Layer } from "@deck.gl/core"; | ||
import { BaseLayerModel, initializeLayer } from "./model/index.js"; | ||
import type { WidgetModel } from "@jupyter-widgets/base"; | ||
|
@@ -13,6 +17,10 @@ import { v4 as uuidv4 } from "uuid"; | |
import { Message } from "./types.js"; | ||
import { flyTo } from "./actions/fly-to.js"; | ||
import { useViewStateDebounced } from "./state"; | ||
import { | ||
MapboxOverlay as DeckOverlay, | ||
MapboxOverlayProps, | ||
} from "@deck.gl/mapbox"; | ||
|
||
import { MachineContext, MachineProvider } from "./xstate"; | ||
import * as selectors from "./xstate/selectors"; | ||
|
@@ -38,6 +46,13 @@ const DEFAULT_INITIAL_VIEW_STATE = { | |
const DEFAULT_MAP_STYLE = | ||
"https://basemaps.cartocdn.com/gl/positron-nolabels-gl-style/style.json"; | ||
|
||
function DeckGLOverlay(props: MapboxOverlayProps) { | ||
const overlay = useControl(() => new DeckOverlay(props)); | ||
|
||
overlay.setProps(props); | ||
return null; | ||
} | ||
|
||
async function getChildModelState( | ||
childModels: WidgetModel[], | ||
childLayerIds: string[], | ||
|
@@ -237,57 +252,65 @@ function App() { | |
/> | ||
)} | ||
<div className="bg-red-800 h-full w-full relative"> | ||
<DeckGL | ||
style={{ width: "100%", height: "100%" }} | ||
<Map | ||
reuseMaps | ||
id={`map-${mapId}`} | ||
initialViewState={ | ||
["longitude", "latitude", "zoom"].every((key) => | ||
Object.keys(initialViewState).includes(key), | ||
) | ||
? initialViewState | ||
: DEFAULT_INITIAL_VIEW_STATE | ||
} | ||
controller={true} | ||
layers={ | ||
bboxSelectPolygonLayer | ||
? layers.concat(bboxSelectPolygonLayer) | ||
: layers | ||
} | ||
getTooltip={(showTooltip && getTooltip) || undefined} | ||
getCursor={() => (isDrawingBBoxSelection ? "crosshair" : "grab")} | ||
pickingRadius={pickingRadius} | ||
onClick={onMapClickHandler} | ||
onHover={onMapHoverHandler} | ||
useDevicePixels={ | ||
isDefined(useDevicePixels) ? useDevicePixels : true | ||
} | ||
// https://deck.gl/docs/api-reference/core/deck#_typedarraymanagerprops | ||
_typedArrayManagerProps={{ | ||
overAlloc: 1, | ||
poolSize: 0, | ||
mapStyle={mapStyle || DEFAULT_MAP_STYLE} | ||
attributionControl={{ | ||
customAttribution, | ||
}} | ||
onViewStateChange={(event) => { | ||
const { viewState } = event; | ||
|
||
// This condition is necessary to confirm that the viewState is | ||
// of type MapViewState. | ||
if ("latitude" in viewState) { | ||
const { longitude, latitude, zoom, pitch, bearing } = viewState; | ||
setViewState({ | ||
longitude, | ||
latitude, | ||
zoom, | ||
pitch, | ||
bearing, | ||
}); | ||
} | ||
}} | ||
parameters={parameters || {}} | ||
> | ||
<Map | ||
mapStyle={mapStyle || DEFAULT_MAP_STYLE} | ||
customAttribution={customAttribution} | ||
></Map> | ||
</DeckGL> | ||
<FullscreenControl position="top-left" /> | ||
<NavigationControl position="top-left" /> | ||
<ScaleControl position="bottom-left" /> | ||
<DeckGLOverlay | ||
// interleaved | ||
style={{ width: "100%", height: "100%" }} | ||
layers={ | ||
bboxSelectPolygonLayer | ||
? layers.concat(bboxSelectPolygonLayer) | ||
: layers | ||
} | ||
getTooltip={(showTooltip && getTooltip) || undefined} | ||
getCursor={() => (isDrawingBBoxSelection ? "crosshair" : "grab")} | ||
pickingRadius={pickingRadius} | ||
onClick={onMapClickHandler} | ||
onHover={onMapHoverHandler} | ||
useDevicePixels={ | ||
isDefined(useDevicePixels) ? useDevicePixels : true | ||
} | ||
// https://deck.gl/docs/api-reference/core/deck#_typedarraymanagerprops | ||
_typedArrayManagerProps={{ | ||
overAlloc: 1, | ||
poolSize: 0, | ||
}} | ||
onViewStateChange={(event) => { | ||
const { viewState } = event; | ||
|
||
// This condition is necessary to confirm that the viewState is | ||
// of type MapViewState. | ||
if ("latitude" in viewState) { | ||
const { longitude, latitude, zoom, pitch, bearing } = | ||
viewState; | ||
setViewState({ | ||
longitude, | ||
latitude, | ||
zoom, | ||
pitch, | ||
bearing, | ||
}); | ||
} | ||
}} | ||
Comment on lines
+294
to
+310
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. todo: I need to check that deck.gl still sends these events even when the view state is managed by maplibre. For background: This sets the current view state on the model so that it can be updated on the Python side, which can be accessed via |
||
parameters={parameters || {}} | ||
/> | ||
</Map> | ||
</div> | ||
</div> | ||
</div> | ||
|
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.
Is this the standard way to use
DeckOverlay
in react? @felixpalmerThere 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.
Yes https://deck.gl/docs/api-reference/mapbox/mapbox-overlay