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

Cesium2d viewer #7234

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#### next release (8.7.9)

- Allow to use Cesium in `SCENE2D` mode (and WebMercator projection) as map viewer.
- Add a new configuration parameter `mapViewers` to specify which map viewers to show in `SettingPanel`.
- [The next improvement]

#### 8.7.8 - 2024-11-01
Expand Down
34 changes: 26 additions & 8 deletions lib/Models/Cesium.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,11 @@ import TerriaFeature from "./Feature/Feature";
import GlobeOrMap from "./GlobeOrMap";
import Terria from "./Terria";
import UserDrawing from "./UserDrawing";
import { setViewerMode } from "./ViewerMode";
import ViewerMode, { setViewerMode } from "./ViewerMode";
import ScreenSpaceEventHandler from "terriajs-cesium/Source/Core/ScreenSpaceEventHandler";
import SceneMode from "terriajs-cesium/Source/Scene/SceneMode";
import GeographicProjection from "terriajs-cesium/Source/Core/GeographicProjection";
import WebMercatorProjection from "terriajs-cesium/Source/Core/WebMercatorProjection";
import I3SDataProvider from "terriajs-cesium/Source/Scene/I3SDataProvider";

//import Cesium3DTilesInspector from "terriajs-cesium/Source/Widgets/Cesium3DTilesInspector/Cesium3DTilesInspector";
Expand Down Expand Up @@ -190,7 +193,15 @@ export default class Cesium extends GlobeOrMap {
SingleTileImageryProvider.fromUrl(img),
{}
),
scene3DOnly: true,
scene3DOnly: false,
sceneMode:
terriaViewer.viewerMode && terriaViewer.viewerMode === ViewerMode.Cesium
? SceneMode.SCENE3D
: SceneMode.SCENE2D,
mapProjection:
terriaViewer.viewerMode && terriaViewer.viewerMode === ViewerMode.Cesium
? new GeographicProjection()
: new WebMercatorProjection(),
shadows: true,
useBrowserRecommendedResolution: !this.terria.useNativeResolution
};
Expand Down Expand Up @@ -867,6 +878,12 @@ export default class Cesium extends GlobeOrMap {
duration: flightDurationSeconds,
destination: finalDestination
});
} else if (defined(target.rectangle)) {
// target has a rectangle
return flyToPromise(camera, {
duration: flightDurationSeconds,
destination: target.rectangle
});
} else if (defined(target.entities)) {
// target is some DataSource
return waitForDataSourceToLoad(target).then(() => {
Expand Down Expand Up @@ -922,12 +939,6 @@ export default class Cesium extends GlobeOrMap {
} else {
return Promise.resolve();
}
} else if (defined(target.rectangle)) {
// target has a rectangle
return flyToPromise(camera, {
duration: flightDurationSeconds,
destination: target.rectangle
});
} else if (
defined(target.imageryProvider) &&
defined(target.imageryProvider.rectangle)
Expand Down Expand Up @@ -1009,6 +1020,13 @@ export default class Cesium extends GlobeOrMap {
const scene = this.scene;
const camera = scene.camera;

if (scene.mode === SceneMode.SCENE2D) {
const rect = camera.computeViewRectangle();
if (rect) {
return new CameraView(rect);
}
}

const width = scene.canvas.clientWidth;
const height = scene.canvas.clientHeight;

Expand Down
8 changes: 7 additions & 1 deletion lib/Models/Terria.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,11 @@ export interface ConfigParameters {
*/
searchBarConfig?: ModelPropertiesFromTraits<SearchBarTraits>;
searchProviders: ModelPropertiesFromTraits<SearchProviderTraits>[];

/**
* List of the enabled MapViewers: 3d, 3dsmooth, 2d, cesium2d
Copy link
Contributor

Choose a reason for hiding this comment

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

The type says this is an array rather than a list. The JSDoc for the other array-options seems to start with just "The", so "The enabled MapViewers .." in this case.

*/
mapViewers: string[];
}

interface StartOptions {
Expand Down Expand Up @@ -595,7 +600,8 @@ export default class Terria {
aboutButtonHrefUrl: "about.html",
plugins: undefined,
searchBarConfig: undefined,
searchProviders: []
searchProviders: [],
mapViewers: ["3d", "3dsmooth", "2d"]
};

@observable
Expand Down
10 changes: 10 additions & 0 deletions lib/Models/ViewerMode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import TerriaViewer from "../ViewModels/TerriaViewer";

enum ViewerMode {
Cesium = "cesium",
Cesium2D = "cesium2d",
Leaflet = "leaflet"
}

Expand All @@ -24,6 +25,12 @@ export const MapViewers = Object.seal({
terrain: false,
label: "settingPanel.viewerModeLabels.Leaflet",
available: true
},
cesium2d: {
viewerMode: ViewerMode.Cesium2D,
terrain: false,
label: "settingPanel.viewerModeLabels.Leaflet",
Copy link
Contributor

Choose a reason for hiding this comment

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

The other entries have unique labels, so I'm guessing this entry shouldn't use the already used Leaflet-label.

available: true
}
});

Expand All @@ -38,6 +45,9 @@ export function setViewerMode(
if (viewerMode === "3d" || viewerMode === "3dsmooth") {
viewer.viewerMode = ViewerMode.Cesium;
viewer.viewerOptions.useTerrain = viewerMode === "3d";
} else if (viewerMode === "cesium2d") {
viewer.viewerMode = ViewerMode.Cesium2D;
viewer.viewerOptions.useTerrain = false;
} else if (viewerMode === "2d") {
viewer.viewerMode = ViewerMode.Leaflet;
} else {
Expand Down
28 changes: 18 additions & 10 deletions lib/ReactViews/Map/Panels/SettingPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ class SettingPanel extends React.Component<PropTypes> {
constructor(props: PropTypes) {
super(props);
makeObservable(this);

Object.entries(MapViewers).forEach(([key, elem]) => {
elem.available = props.terria.configParameters.mapViewers.includes(key);
});
}

@observable _hoverBaseMap = null;
Expand Down Expand Up @@ -168,7 +172,9 @@ class SettingPanel extends React.Component<PropTypes> {
2: t("settingPanel.qualityLabels.lowerPerformance")
};
const currentViewer =
this.props.terria.mainViewer.viewerMode === ViewerMode.Cesium
this.props.terria.mainViewer.viewerMode === ViewerMode.Cesium2D
? ViewerMode.Cesium2D
: this.props.terria.mainViewer.viewerMode === ViewerMode.Cesium
? this.props.terria.mainViewer.viewerOptions.useTerrain
? "3d"
: "3dsmooth"
Expand Down Expand Up @@ -247,15 +253,17 @@ class SettingPanel extends React.Component<PropTypes> {
<Text as="label">{t("settingPanel.mapView")}</Text>
</Box>
<FlexGrid gap={1} elementsNo={3}>
{Object.entries(MapViewers).map(([key, viewerMode]) => (
<SettingsButton
key={key}
isActive={key === currentViewer}
onClick={(event: any) => this.selectViewer(key as any, event)}
>
<Text mini>{t(viewerMode.label)}</Text>
</SettingsButton>
))}
{Object.entries(MapViewers)
Copy link
Contributor

Choose a reason for hiding this comment

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

The previous line contains elementsNo={3}, which coincides with MapViewers.length. Does this work as intended when the filtering filters out some objects?

And even without filtering, why does the previous line not need to be updated to contain 4 since there is one more item in MapViewers in this PR?

.filter(([_, viewerMode]) => viewerMode.available)
.map(([key, viewerMode]) => (
<SettingsButton
key={key}
isActive={key === currentViewer}
onClick={(event: any) => this.selectViewer(key as any, event)}
>
<Text mini>{t(viewerMode.label)}</Text>
</SettingsButton>
))}
</FlexGrid>
{!!supportsSide && (
<>
Expand Down
8 changes: 8 additions & 0 deletions lib/ViewModels/TerriaViewer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ const cesiumFromPromise = computed(
{ keepAlive: true }
);

const cesium2dFromPromise = computed(
() =>
fromPromise(import("../Models/Cesium").then((Cesium) => Cesium.default)),
{ keepAlive: true }
);

// Viewer options. Designed to be easily serialisable
interface ViewerOptions {
useTerrain: boolean;
Expand Down Expand Up @@ -173,6 +179,8 @@ export default class TerriaViewer {
viewerFromPromise = leafletFromPromise.get();
} else if (this.attached && this.viewerMode === ViewerMode.Cesium) {
viewerFromPromise = cesiumFromPromise.get();
} else if (this.attached && this.viewerMode === ViewerMode.Cesium2D) {
viewerFromPromise = cesium2dFromPromise.get();
Copy link
Contributor

Choose a reason for hiding this comment

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

To me, the body of cesiumFromPromise looks identical to the body of cesium2dFromPromise. Can't this entire else-if be folded into the previous else if, but with the condition this.attached && (this.viewerMode === ViewerMode.Cesium || this.viewerMode === ViewerMode.Cesium2D)?

}
return viewerFromPromise;
}
Expand Down