Skip to content

Commit

Permalink
feat: selecting imagery features (#450)
Browse files Browse the repository at this point in the history
* feat: support select feature

* feat: support selecting mvt feature
  • Loading branch information
keiya01 authored Feb 10, 2023
1 parent 4061743 commit f24ef53
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/core/Map/Layers/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ export default function useHooks({

const walk = useCallback(
<T>(fn: (layer: LazyLayer, index: number, parents: LazyLayer[]) => T | void): T | undefined => {
return walkLayers(layersRef() ?? [], (l, i, p) => {
return walkLayers([...(layersRef() ?? []), ...tempLayersRef.current], (l, i, p) => {
const ll = findById(l.id);
if (!ll) return;
return fn(
Expand Down
1 change: 1 addition & 0 deletions src/core/Map/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ function Map(
selectedLayerId={{ layerId: selectedLayer.layerId, featureId: selectedLayer.featureId }}
layerSelectionReason={selectedLayer.reason}
onLayerSelect={handleEngineLayerSelect}
layersRef={layersRef}
{...props}>
<Layers
ref={layersRef}
Expand Down
8 changes: 7 additions & 1 deletion src/core/Map/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ import type {
} from "react";

import type { LatLngHeight, Camera, Rect, LatLng, DataType } from "../../mantle";
import type { FeatureComponentType, ClusterComponentType, LayerSelectionReason } from "../Layers";
import type {
FeatureComponentType,
ClusterComponentType,
LayerSelectionReason,
Ref as LayersRef,
} from "../Layers";

export type {
FeatureComponentProps,
Expand Down Expand Up @@ -97,6 +102,7 @@ export type EngineProps = {
isLayerDragging?: boolean;
shouldRender?: boolean;
meta?: Record<string, unknown>;
layersRef?: RefObject<LayersRef>;
onLayerSelect?: (
layerId: string | undefined,
featureId?: string,
Expand Down
35 changes: 33 additions & 2 deletions src/core/engines/Cesium/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import { Color, Entity, Cesium3DTileFeature, Cartesian3, Ion, Cesium3DTileset }
import type { Viewer as CesiumViewer } from "cesium";
import CesiumDnD, { Context } from "cesium-dnd";
import { isEqual } from "lodash-es";
import { useCallback, useEffect, useMemo, useRef } from "react";
import { RefObject, useCallback, useEffect, useMemo, useRef } from "react";
import type { CesiumComponentRef, CesiumMovementEvent, RootEventTarget } from "resium";
import { useCustomCompareCallback } from "use-custom-compare";

import { e2eAccessToken, setE2ECesiumViewer } from "@reearth/config";
import { ComputedFeature, DataType } from "@reearth/core/mantle";
import { LayersRef } from "@reearth/core/Map";

import type {
Camera,
Expand All @@ -33,6 +35,7 @@ export default ({
selectionReason,
isLayerDraggable,
meta,
layersRef,
onLayerSelect,
onCameraChange,
onLayerDrag,
Expand All @@ -46,6 +49,7 @@ export default ({
layerId?: string;
featureId?: string;
};
layersRef?: RefObject<LayersRef>;
selectionReason?: LayerSelectionReason;
isLayerDraggable?: boolean;
meta?: Record<string, unknown>;
Expand Down Expand Up @@ -155,6 +159,33 @@ export default ({
if (!viewer || viewer.isDestroyed()) return;

const entity = findEntity(viewer, selectedLayerId?.layerId, selectedLayerId?.featureId);
if (!entity) {
// Find ImageryLayerFeature
const ImageryLayerDataTypes: DataType[] = ["mvt"];
const layers = layersRef?.current?.findAll(
layer =>
layer.type === "simple" &&
!!layer.data?.type &&
ImageryLayerDataTypes.includes(layer.data?.type),
);

if (layers?.length) {
// TODO: Get ImageryLayerFeature from `viewer.imageryLayers`.(But currently didn't find the way)
const [feature, layerId] =
((): [ComputedFeature, string] | void => {
for (const layer of layers) {
const f = layer.computed?.features.find(
feature => feature.id !== selectedLayerId?.featureId,
);
if (f) {
return [f, layer.id];
}
}
})() || [];
onLayerSelect?.(layerId, feature?.id);
}
}

if (prevSelectedEntity.current === entity) return;
prevSelectedEntity.current = entity;

Expand Down Expand Up @@ -182,7 +213,7 @@ export default ({
if (!entity || entity instanceof Entity) {
viewer.selectedEntity = entity;
}
}, [cesium, selectedLayerId, onLayerSelect]);
}, [cesium, selectedLayerId, onLayerSelect, layersRef]);

const handleMouseEvent = useCallback(
(type: keyof MouseEvents, e: CesiumMovementEvent, target: RootEventTarget) => {
Expand Down
2 changes: 2 additions & 0 deletions src/core/engines/Cesium/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const Cesium: React.ForwardRefRenderFunction<EngineRef, EngineProps> = (
shouldRender,
layerSelectionReason,
meta,
layersRef,
onLayerSelect,
onCameraChange,
onLayerDrag,
Expand Down Expand Up @@ -73,6 +74,7 @@ const Cesium: React.ForwardRefRenderFunction<EngineRef, EngineProps> = (
selectionReason: layerSelectionReason,
isLayerDraggable,
meta,
layersRef,
onLayerSelect,
onCameraChange,
onLayerDrag,
Expand Down

0 comments on commit f24ef53

Please sign in to comment.