Skip to content

Commit b35657f

Browse files
authored
chore: Simplify cog-basic example (#119)
In #117 we split the existing cog-basic example into a new one specific to land-cover. Now we can simplify some of the existing setup around cog-basic.
1 parent 8f9f12d commit b35657f

File tree

1 file changed

+18
-168
lines changed

1 file changed

+18
-168
lines changed

examples/cog-basic/src/App.tsx

Lines changed: 18 additions & 168 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,7 @@
11
import type { DeckProps } from "@deck.gl/core";
22
import { MapboxOverlay } from "@deck.gl/mapbox";
3-
import type { Device, Texture } from "@luma.gl/core";
4-
import { ShaderModule } from "@luma.gl/shadertools";
5-
import {
6-
COGLayer,
7-
parseColormap,
8-
proj,
9-
} from "@developmentseed/deck.gl-geotiff";
10-
import type {
11-
GeoTIFF,
12-
GeoTIFFImage,
13-
TypedArrayArrayWithDimensions,
14-
} from "geotiff";
15-
import { RasterLayerProps } from "@developmentseed/deck.gl-raster";
3+
import { COGLayer, proj } from "@developmentseed/deck.gl-geotiff";
4+
import type { GeoTIFF } from "geotiff";
165
import { fromUrl, Pool } from "geotiff";
176
import { toProj4 } from "geotiff-geokeys-to-proj4";
187
import "maplibre-gl/dist/maplibre-gl.css";
@@ -82,102 +71,17 @@ async function getCogBounds(
8271
// const COG_URL =
8372
// "https://nz-imagery.s3-ap-southeast-2.amazonaws.com/new-zealand/new-zealand_2024-2025_10m/rgb/2193/CC11.tiff";
8473

85-
// const COG_URL =
86-
// "https://ds-wheels.s3.us-east-1.amazonaws.com/m_4007307_sw_18_060_20220803.tif";
87-
8874
const COG_URL =
89-
"https://ds-wheels.s3.us-east-1.amazonaws.com/Annual_NLCD_LndCov_2023_CU_C1V0.tif";
90-
91-
export type LandCoverProps = {
92-
colormap_texture: Texture;
93-
};
94-
95-
const landCoverModule = {
96-
name: "landCover",
97-
} as const satisfies ShaderModule<LandCoverProps>;
98-
99-
async function loadLandCoverTexture(
100-
image: GeoTIFFImage,
101-
options: {
102-
device: Device;
103-
window: [number, number, number, number];
104-
signal?: AbortSignal;
105-
pool: Pool;
106-
},
107-
colormapTexture: Texture,
108-
): Promise<{
109-
texture: ImageData | Texture;
110-
shaders?: RasterLayerProps["shaders"];
111-
height: number;
112-
width: number;
113-
}> {
114-
const { device, window, signal, pool } = options;
115-
116-
const {
117-
[0]: data,
118-
width,
119-
height,
120-
} = (await image.readRasters({
121-
window,
122-
samples: [0],
123-
pool,
124-
signal,
125-
})) as TypedArrayArrayWithDimensions;
126-
127-
const texture = device.createTexture({
128-
format: "r8unorm",
129-
dimension: "2d",
130-
width,
131-
height,
132-
data,
133-
});
134-
135-
// Hard coded NoData value but this ideally would be fetched from COG metadata
136-
const nodataVal = 250;
137-
// Since values are 0-1 for unorm textures,
138-
const noDataScaled = nodataVal / 255.0;
139-
140-
return {
141-
texture,
142-
// For colormap rendering:
143-
shaders: {
144-
inject: {
145-
"fs:#decl": `
146-
uniform sampler2D colormap_texture;
147-
`,
148-
"fs:DECKGL_FILTER_COLOR": `
149-
float value = color.r;
150-
vec3 pickingval = vec3(value, 0., 0.);
151-
if (value == ${noDataScaled}) {
152-
discard;
153-
} else {
154-
vec4 color_val = texture(colormap_texture, vec2(value, 0.));
155-
color = color_val;
156-
}
157-
`,
158-
},
159-
modules: [landCoverModule],
160-
shaderProps: {
161-
landCover: {
162-
colormap_texture: colormapTexture,
163-
},
164-
},
165-
},
166-
height,
167-
width,
168-
};
169-
}
75+
"https://ds-wheels.s3.us-east-1.amazonaws.com/m_4007307_sw_18_060_20220803.tif";
17076

17177
export default function App() {
17278
const mapRef = useRef<MapRef>(null);
173-
const [device, setDevice] = useState<Device | null>(null);
17479
const [geotiff, setGeotiff] = useState<GeoTIFF | null>(null);
17580
const [error, setError] = useState<string | null>(null);
17681
const [loading, setLoading] = useState(true);
17782
const [debug, setDebug] = useState(false);
17883
const [debugOpacity, setDebugOpacity] = useState(0.25);
17984
const [pool] = useState<Pool>(new Pool());
180-
const [colormapTexture, setColormapTexture] = useState<Texture | null>(null);
18185

18286
useEffect(() => {
18387
let mounted = true;
@@ -221,51 +125,20 @@ export default function App() {
221125
};
222126
}, []);
223127

224-
// Once device exists, create global colormap texture
225-
useEffect(() => {
226-
async function createColormapTexture() {
227-
if (device && geotiff) {
228-
const image = await geotiff.getImage();
229-
const { data, width, height } = parseColormap(
230-
image.fileDirectory.ColorMap,
231-
);
232-
const colorMapTexture = device.createTexture({
233-
data,
234-
format: "rgba8unorm",
235-
width,
236-
height,
237-
sampler: {
238-
minFilter: "nearest",
239-
magFilter: "nearest",
240-
addressModeU: "clamp-to-edge",
241-
addressModeV: "clamp-to-edge",
242-
},
243-
});
244-
245-
setColormapTexture(colorMapTexture);
246-
}
247-
}
248-
249-
createColormapTexture();
250-
}, [geotiff, device]);
251-
252-
const layers =
253-
geotiff && colormapTexture
254-
? [
255-
new COGLayer({
256-
id: "cog-layer",
257-
geotiff,
258-
maxError: 0.125,
259-
debug,
260-
debugOpacity,
261-
geoKeysParser,
262-
pool,
263-
loadTexture: (image, options) =>
264-
loadLandCoverTexture(image, options, colormapTexture),
265-
beforeId: "aeroway-runway",
266-
}),
267-
]
268-
: [];
128+
const layers = geotiff
129+
? [
130+
new COGLayer({
131+
id: "cog-layer",
132+
geotiff,
133+
maxError: 0.125,
134+
debug,
135+
debugOpacity,
136+
geoKeysParser,
137+
pool,
138+
beforeId: "aeroway-runway",
139+
}),
140+
]
141+
: [];
269142

270143
return (
271144
<div style={{ position: "relative", width: "100%", height: "100%" }}>
@@ -280,11 +153,7 @@ export default function App() {
280153
}}
281154
mapStyle="https://basemaps.cartocdn.com/gl/dark-matter-gl-style/style.json"
282155
>
283-
<DeckGLOverlay
284-
layers={layers}
285-
interleaved
286-
onDeviceInitialized={(device) => setDevice(device)}
287-
/>
156+
<DeckGLOverlay layers={layers} interleaved />
288157
</Map>
289158

290159
{/* UI Overlay Container */}
@@ -364,25 +233,6 @@ export default function App() {
364233
marginTop: "12px",
365234
}}
366235
>
367-
{/* <label
368-
style={{
369-
display: "flex",
370-
alignItems: "center",
371-
gap: "8px",
372-
fontSize: "14px",
373-
cursor: "pointer",
374-
marginBottom: "12px",
375-
}}
376-
>
377-
<input
378-
type="checkbox"
379-
checked={renderAsTiled}
380-
onChange={(e) => setRenderAsTiled(e.target.checked)}
381-
style={{ cursor: "pointer" }}
382-
/>
383-
<span>Render as tiled</span>
384-
</label> */}
385-
386236
<label
387237
style={{
388238
display: "flex",

0 commit comments

Comments
 (0)