|
| 1 | +import React, { useState, useEffect } from "react"; |
| 2 | +import { createRoot } from "react-dom/client"; |
| 3 | +import { StaticMap, MapContext, NavigationControl } from "react-map-gl"; |
| 4 | +import DeckGL, { Layer, PickingInfo } from "deck.gl/typed"; |
| 5 | +import { GeoArrowTripsLayer } from "@geoarrow/deck.gl-layers"; |
| 6 | +import * as arrow from "apache-arrow"; |
| 7 | + |
| 8 | +const GEOARROW_POINT_DATA = "http://localhost:8080/trips.feather"; |
| 9 | + |
| 10 | +const INITIAL_VIEW_STATE = { |
| 11 | + longitude: -74, |
| 12 | + latitude: 40.72, |
| 13 | + zoom: 13, |
| 14 | + pitch: 45, |
| 15 | + bearing: 0, |
| 16 | +}; |
| 17 | + |
| 18 | +const MAP_STYLE = |
| 19 | + "https://basemaps.cartocdn.com/gl/positron-nolabels-gl-style/style.json"; |
| 20 | +const NAV_CONTROL_STYLE = { |
| 21 | + position: "absolute", |
| 22 | + top: 10, |
| 23 | + left: 10, |
| 24 | +}; |
| 25 | + |
| 26 | +function Root() { |
| 27 | + const trailLength = 180; |
| 28 | + // unit corresponds to the timestamp in source data |
| 29 | + const loopLength = 1800; |
| 30 | + const animationSpeed = 1; |
| 31 | + |
| 32 | + const [time, setTime] = useState(0); |
| 33 | + const [animation] = useState<{ id: number }>({ id: 0 }); |
| 34 | + |
| 35 | + const animate = () => { |
| 36 | + setTime((t) => (t + animationSpeed) % loopLength); |
| 37 | + animation.id = window.requestAnimationFrame(animate); |
| 38 | + }; |
| 39 | + |
| 40 | + useEffect(() => { |
| 41 | + animation.id = window.requestAnimationFrame(animate); |
| 42 | + return () => window.cancelAnimationFrame(animation.id); |
| 43 | + }, [animation]); |
| 44 | + |
| 45 | + const onClick = (info: PickingInfo) => { |
| 46 | + if (info.object) { |
| 47 | + console.log(JSON.stringify(info.object.toJSON())); |
| 48 | + } |
| 49 | + }; |
| 50 | + |
| 51 | + const [table, setTable] = useState<arrow.Table | null>(null); |
| 52 | + |
| 53 | + useEffect(() => { |
| 54 | + // declare the data fetching function |
| 55 | + const fetchData = async () => { |
| 56 | + const data = await fetch(GEOARROW_POINT_DATA); |
| 57 | + const buffer = await data.arrayBuffer(); |
| 58 | + const table = arrow.tableFromIPC(buffer); |
| 59 | + setTable(table); |
| 60 | + }; |
| 61 | + |
| 62 | + if (!table) { |
| 63 | + fetchData().catch(console.error); |
| 64 | + } |
| 65 | + }); |
| 66 | + |
| 67 | + const layers: Layer[] = []; |
| 68 | + |
| 69 | + table && |
| 70 | + layers.push( |
| 71 | + new GeoArrowTripsLayer({ |
| 72 | + id: "geoarrow-linestring", |
| 73 | + data: table, |
| 74 | + getColor: [255, 0, 0], |
| 75 | + getPath: table.getChild("geometry")!, |
| 76 | + getTimestamps: table.getChild("timestamps")!, |
| 77 | + widthMinPixels: 2, |
| 78 | + pickable: true, |
| 79 | + trailLength, |
| 80 | + currentTime: time, |
| 81 | + }), |
| 82 | + ); |
| 83 | + |
| 84 | + return ( |
| 85 | + <DeckGL |
| 86 | + initialViewState={INITIAL_VIEW_STATE} |
| 87 | + controller={true} |
| 88 | + layers={layers} |
| 89 | + ContextProvider={MapContext.Provider} |
| 90 | + onClick={onClick} |
| 91 | + glOptions={{ |
| 92 | + powerPreference: "high-performance", |
| 93 | + }} |
| 94 | + > |
| 95 | + <StaticMap mapStyle={MAP_STYLE} /> |
| 96 | + <NavigationControl style={NAV_CONTROL_STYLE} /> |
| 97 | + </DeckGL> |
| 98 | + ); |
| 99 | +} |
| 100 | + |
| 101 | +/* global document */ |
| 102 | +const container = document.body.appendChild(document.createElement("div")); |
| 103 | +createRoot(container).render(<Root />); |
0 commit comments