Skip to content
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

feat: add extra maps #304

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
REACT_APP_MAPBOX_TOKEN=pk.eyJ1IjoiZHNhdmlub3YiLCJhIjoiY2xsd2VocjRsMjVpbzNwcDhsc3E1ZzAzNiJ9.3xSNq_gwuOMHPUTDkO4OzQ
REACT_APP_GOOGLE_MAPS_API_KEY=
REACT_APP_GOOGLE_MAP_VECTOR_ID=
17,170 changes: 17,170 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

21 changes: 19 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,44 @@
"author": "",
"license": "ISC",
"dependencies": {
"@arcgis/core": "^4.27.6",
"@deck.gl/arcgis": "^8.9.31",
"@deck.gl/core": "^8.9.31",
"@deck.gl/geo-layers": "^8.9.31",
"@deck.gl/google-maps": "^8.9.31",
"@deck.gl/layers": "^8.9.31",
"@deck.gl/mesh-layers": "^8.9.31",
"@deck.gl/react": "^8.9.31",
"@emotion/react": "^11.11.1",
"@emotion/styled": "^11.11.0",
"@fortawesome/fontawesome-svg-core": "^1.2.36",
"@fortawesome/free-solid-svg-icons": "^5.15.4",
"@fortawesome/react-fontawesome": "^0.1.17",
"@googlemaps/react-wrapper": "^1.1.35",
"@hyperjump/json-schema": "^0.23.2",
"@loaders.gl/3d-tiles": "^4.0.0-beta.2",
"@loaders.gl/core": "^4.0.0-beta.2",
"@loaders.gl/i3s": "^4.0.0-beta.2",
"@loaders.gl/images": "^4.0.0-beta.2",
"@loaders.gl/loader-utils": "^4.0.0-beta.2",
"@loaders.gl/schema": "^4.0.0-beta.2",
"@loaders.gl/tiles": "^4.0.0-beta.2",
"@luma.gl/core": "^8.5.14",
"@math.gl/proj4": "^3.6.3",
"@probe.gl/stats": "^4.0.4",
"@reduxjs/toolkit": "^1.9.5",
"deck.gl": "^8.8.3",
"@types/google.maps": "^3.54.4",
"apache-arrow": "^13.0.0",
"dotenv": "^16.3.1",
"html2canvas": "^1.4.1",
"mapbox-gl": "^2.15.0",
"maplibre-gl": "^3.5.0",
"normalize.css": "^8.0.1",
"prop-types": "^15.8.1",
"react": "^17.0.2",
"react-color": "^2.19.3",
"react-dom": "^17.0.2",
"react-map-gl": "^5.0.0",
"react-map-gl": "^7.1.0",
"react-redux": "^8.1.1",
"react-responsive": "^9.0.0-beta.8",
"react-router-dom": "^6.2.1",
Expand Down
31 changes: 31 additions & 0 deletions src/components/arcgis-wrapper/arcgis-wrapper.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { useEffect, useRef } from "react";
import { useArcgis } from "../../hooks/use-arcgis-hook/use-arcgis-hook";
import { StyledMapContainer } from "../common-styled";

/* eslint-disable-next-line */
export interface ArcgisWrapperProps {}

export function ArcgisWrapper({}: ArcgisWrapperProps) {
const mapContainer = useRef<HTMLDivElement | null>(null);
const map: unknown | null = useArcgis(mapContainer);

useEffect(() => {
if (!map) {
return;
}

const layers = [];
// @ts-expect-error @deck.gl/arcgis has no types
map.deck.set({ layers });
}, [map]);

return (
<StyledMapContainer
ref={mapContainer}
className="map-container"
data-testid="ArcgisContainer"
/>
);
}

export default ArcgisWrapper;
10 changes: 10 additions & 0 deletions src/components/common-styled.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import styled from "styled-components";

export const StyledMapContainer = styled.div`
overflow: hidden;
position: absolute;
width: 100%;
height: 100%;
left: 0;
top: 0;
`;
140 changes: 114 additions & 26 deletions src/components/deck-gl-wrapper/deck-gl-wrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ import {
View,
} from "@deck.gl/core";
import { useEffect, useMemo, useState, useRef } from "react";
import { StaticMap } from "react-map-gl";
import { CONTRAST_MAP_STYLES } from "../../constants/map-styles";
import {
NormalsDebugData,
ViewStateSet,
Expand Down Expand Up @@ -58,10 +56,26 @@ import {
selectBoundingVolumeColorMode,
selectBoundingVolumeType,
} from "../../redux/slices/debug-options-slice";
import { selectBaseMaps, selectSelectedBaseMapId } from "../../redux/slices/base-maps-slice";
import {
selectBaseMaps,
selectSelectedBaseMapId,
} from "../../redux/slices/base-maps-slice";
import {
BaseMapProviderId,
MAP_PROVIDER_PROPERTIES,
} from "../../constants/base-map-providers";
import {
Source as MaplibreSource,
MapRef as MaplibreMapRef,
} from "react-map-gl/maplibre";
import { Source as MapboxSource, MapRef as MapboxMapRef } from "react-map-gl";
import { selectMapProvider } from "../../redux/slices/app.slice";
import { mapActions } from "../../redux/slices/map.slice";
import { MapWrapper } from "../map-wrapper/map-wrapper";
import { GoogleMapsWrapper } from "../google-maps-wrapper/google-maps-wrapper";

const TRANSITION_DURAITON = 4000;
const INITIAL_VIEW_STATE = {
export const INITIAL_VIEW_STATE = {
longitude: -120,
latitude: 34,
pitch: 45,
Expand Down Expand Up @@ -212,8 +226,69 @@ export const DeckGlWrapper = ({
const baseMaps = useAppSelector(selectBaseMaps);
const selectedBaseMapId = useAppSelector(selectSelectedBaseMapId);
const selectedBaseMap = baseMaps.find((map) => map.id === selectedBaseMapId);
const showTerrain=selectedBaseMap?.id === "Terrain";
const mapStyle=selectedBaseMap?.mapUrl;
const showTerrain = selectedBaseMap?.id === "Terrain";
const mapStyle = selectedBaseMap?.mapUrl;
const baseMapProvider = useAppSelector(selectMapProvider);
const baseMapProviderId = baseMapProvider.id;
const mapRef = useRef<MaplibreMapRef | MapboxMapRef>(null);
let terrainState = showTerrain;
if (
baseMapProvider.id === BaseMapProviderId.arcgis ||
baseMapProvider.id === BaseMapProviderId.googleMaps
) {
terrainState = false;
}
const mapProviderProps = useMemo(
() => MAP_PROVIDER_PROPERTIES[baseMapProviderId],
[baseMapProviderId]
);

const getTerrainElevation = (
baseMapProviderId:
| BaseMapProviderId.maplibre
| BaseMapProviderId.mapbox2
| BaseMapProviderId.googleMaps
| BaseMapProviderId.arcgis,
terrainState: boolean,
mapRef: MaplibreMapRef | MapboxMapRef | null
) => {
let extraElevation = 0;
if (terrainState) {
if (baseMapProviderId === BaseMapProviderId.mapbox2) {
const center = mapRef?.getCenter();
if (center) {
const result = mapRef?.queryTerrainElevation(center);
if (typeof result === "number") {
extraElevation = result;
}
}
} else if (baseMapProviderId === BaseMapProviderId.maplibre) {
const map = mapRef?.getMap();
// @ts-expect-error transform is not typed
extraElevation = map?.transform.elevation || 0;
}
}
return extraElevation;
};

const onSourceDataHandler = () => {
const extraElevation = getTerrainElevation(
baseMapProviderId,
terrainState,
mapRef.current
);
dispatch(
mapActions.setMapState({
...viewState,
position: [
viewState.main.latitude,
viewState.main.longitude,
viewState.main.zoom,
],
})
);
};

const VIEWS = useMemo(
() => [
new MapView({
Expand Down Expand Up @@ -757,27 +832,40 @@ export const DeckGlWrapper = ({
getTooltip={getTooltip}
onClick={onClick}
>
{({ viewport }) => {
currentViewport = viewport;
}}
{!showTerrain && (
<StaticMap
reuseMaps
mapStyle={mapStyle}
preventStyleDiffing
preserveDrawingBuffer
/>
)}
{mapStyle && (
<View id="minimap">
<StaticMap
reuseMaps
mapStyle={CONTRAST_MAP_STYLES[mapStyle]}
preventStyleDiffing
preserveDrawingBuffer
<mapProviderProps.Map
// @ ts-expect-error Maplibre & Mapbox types are different
ref={mapRef}
mapboxAccessToken={mapProviderProps.accessToken}
mapStyle={mapProviderProps.mapStyle}
terrain={
terrainState
? { source: "dem-data-source", exaggeration: 1 }
: undefined
}
onSourceData={onSourceDataHandler}
>
{baseMapProviderId === BaseMapProviderId.maplibre && (
<MaplibreSource
id={mapProviderProps.terrainProps.id}
type={mapProviderProps.terrainProps.type}
tiles={[
"https://s3.amazonaws.com/elevation-tiles-prod/terrarium/{z}/{x}/{y}.png",
]}
encoding="terrarium"
tileSize={256}
maxzoom={12}
/>
)}
{baseMapProviderId === BaseMapProviderId.mapbox2 && (
<MapboxSource
id={mapProviderProps.terrainProps.id}
type={mapProviderProps.terrainProps.type}
url="mapbox://mapbox.mapbox-terrain-dem-v1"
tileSize={512}
maxzoom={14}
Comment on lines +863 to +865
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess all these pros are in mapProviderProps

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

replaced to {mapProviderProps[BaseMapProviderId.mapbox2].terrainProps.XXX} and {mapProviderProps[BaseMapProviderId.maplibre].terrainProps.XXX} accordingly.

/>
</View>
)}
)}
</mapProviderProps.Map>
</DeckGL>
);
};
77 changes: 77 additions & 0 deletions src/components/google-maps-wrapper/google-maps-wrapper.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { useState, useEffect, useMemo } from "react";
import { Wrapper, Status } from "@googlemaps/react-wrapper";
import { GoogleMapsOverlay as DeckOverlay } from "@deck.gl/google-maps/typed";
import { StyledMapContainer } from "../common-styled";
import { useAppDispatch, useAppSelector } from "../../redux/hooks";

const googleMapsApiToken = ""; //process.env.REACT_APP_GOOGLE_MAPS_API_KEY;
const googleMapsMapId = ""; //process.env.REACT_APP_GOOGLE_MAP_VECTOR_ID;

/* eslint-disable-next-line */
interface GoogleMapsWrapperProps {
interleaved?: boolean;
}

const renderMap = (status: Status) => {
if (status === Status.LOADING) return <h3>{status} ..</h3>;
if (status === Status.FAILURE) return <h3>{status} ...</h3>;
return <h3>{status} .</h3>;
};

export function GoogleMapsWrapper({
interleaved = false,
}: GoogleMapsWrapperProps) {
const dispatch = useAppDispatch();
const [map, setMap] = useState<google.maps.Map | null>(null);
const [mapContainer, setMapContainer] = useState<HTMLDivElement | null>(null);
const { longitude, latitude, zoom, pitch, bearing } = {
longitude: 10,
latitude: 20,
zoom: 10,
pitch: 1,
bearing: 1,
};
/// const { longitude, latitude, zoom, pitch, bearing } =
/// useAppSelector(selectMapState);

const overlay = useMemo(
() =>
new DeckOverlay({
interleaved,
layers: [],
}),
[interleaved]
);

useEffect(() => {
if (map) {
map.setCenter({ lat: latitude, lng: longitude });
map.setZoom(zoom);
map.setHeading(bearing);
map.setTilt(pitch);
overlay.setMap(map);
}
}, [map, latitude, longitude, bearing, pitch, zoom, overlay]);

useEffect(() => {
if (!mapContainer) {
return;
}
const mapInstance = new google.maps.Map(mapContainer, {
mapId: googleMapsMapId,
});
setMap(mapInstance);
}, [mapContainer]);

const setRef = (element: HTMLDivElement) => {
setMapContainer(element);
};

return (
<Wrapper apiKey={googleMapsApiToken} render={renderMap}>
<StyledMapContainer ref={setRef} id="map" />
</Wrapper>
);
}

export default GoogleMapsWrapper;
Loading