-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Mask the layers by the selected location
- Loading branch information
Showing
20 changed files
with
689 additions
and
98 deletions.
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
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
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
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,52 @@ | ||
import { GeoJsonLayer } from "@deck.gl/layers"; | ||
import { useContext, useEffect, useMemo } from "react"; | ||
|
||
import useLocation from "@/hooks/use-location"; | ||
import { useLocationGeometry } from "@/hooks/use-location-geometry"; | ||
|
||
import { DeckGLMapboxOverlayContext } from "../deckgl-mapbox-provider"; | ||
|
||
interface MaskLayerProps { | ||
beforeId: string; | ||
} | ||
|
||
const MaskLayer = ({ beforeId }: MaskLayerProps) => { | ||
const [location] = useLocation(); | ||
const { data, isLoading } = useLocationGeometry(location.code.slice(-1)[0]); | ||
const geometry = useMemo(() => { | ||
if (isLoading || data === undefined || data === null) { | ||
// We return an empty feature collection so that while the geometry is loading, we don't show | ||
// anything instead of the layers unmasked | ||
return { | ||
type: "FeatureCollection", | ||
features: [], | ||
}; | ||
} | ||
|
||
return data; | ||
}, [data, isLoading]); | ||
|
||
const { addLayer, removeLayer } = useContext(DeckGLMapboxOverlayContext); | ||
|
||
useEffect(() => { | ||
const layer = new GeoJsonLayer({ | ||
id: "mask", | ||
beforeId, | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
data: geometry, | ||
stroked: false, | ||
operation: "mask", | ||
}); | ||
|
||
addLayer(layer); | ||
|
||
return () => { | ||
removeLayer("mask"); | ||
}; | ||
}, [addLayer, beforeId, geometry, removeLayer]); | ||
|
||
return null; | ||
}; | ||
|
||
export default MaskLayer; |
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,69 @@ | ||
import { MaskExtension } from "@deck.gl/extensions"; | ||
import { TileLayer } from "@deck.gl/geo-layers"; | ||
import { BitmapLayer } from "@deck.gl/layers"; | ||
import { useContext, useEffect } from "react"; | ||
import { RasterLayer as IRasterLayer, RasterSource as IRasterSource } from "react-map-gl"; | ||
|
||
import { LayerConfig } from "@/types/layer"; | ||
|
||
import { DeckGLMapboxOverlayContext } from "../deckgl-mapbox-provider"; | ||
|
||
interface RasterLayerProps { | ||
config: LayerConfig; | ||
beforeId: string; | ||
} | ||
|
||
const RasterLayer = ({ config, beforeId }: RasterLayerProps) => { | ||
const { addLayer, removeLayer } = useContext(DeckGLMapboxOverlayContext); | ||
|
||
useEffect(() => { | ||
const style = config.styles[0] as IRasterLayer; | ||
const source = config.source as IRasterSource; | ||
|
||
const layer = new TileLayer({ | ||
id: style.id, | ||
beforeId, | ||
data: source.tiles, | ||
tileSize: source.tileSize, | ||
minZoom: source.minzoom, | ||
maxZoom: source.maxzoom, | ||
visible: style.layout?.visibility !== "none", | ||
opacity: style.paint?.["raster-opacity"] as number, | ||
renderSubLayers: (subLayer) => { | ||
if (!subLayer || !subLayer.data || !subLayer.tile) { | ||
return null; | ||
} | ||
|
||
return new BitmapLayer({ | ||
id: subLayer.id, | ||
bounds: [ | ||
subLayer.tile.boundingBox[0][0], | ||
subLayer.tile.boundingBox[0][1], | ||
subLayer.tile.boundingBox[1][0], | ||
subLayer.tile.boundingBox[1][1], | ||
], | ||
visible: subLayer.visible, | ||
opacity: subLayer.opacity, | ||
textureParameters: { | ||
minFilter: "nearest", | ||
magFilter: "nearest", | ||
mipmapFilter: undefined, | ||
}, | ||
image: subLayer.data, | ||
extensions: [new MaskExtension()], | ||
maskId: "mask", | ||
}); | ||
}, | ||
}); | ||
|
||
addLayer(layer); | ||
|
||
return () => { | ||
removeLayer(config.styles[0].id); | ||
}; | ||
}, [config, beforeId, addLayer, removeLayer]); | ||
|
||
return null; | ||
}; | ||
|
||
export default RasterLayer; |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.