Skip to content

Commit

Permalink
Implement zoom to layer
Browse files Browse the repository at this point in the history
  • Loading branch information
gjmooney committed Jan 8, 2025
1 parent 0d65aa9 commit e556b3d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
11 changes: 10 additions & 1 deletion packages/base/src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -834,13 +834,22 @@ export function addCommands(
});

commands.addCommand(CommandIDs.zoomToLayer, {
label: trans.__('Select the completion suggestion.'),
label: trans.__('Zoom to Layer'),
execute: () => {
const currentWidget = tracker.currentWidget;
if (!currentWidget || !completionProviderManager) {
return;
}
console.log('zooming');
const model = tracker.currentWidget.context.model;
const selectedItems = model.localState?.selected.value;

if (!selectedItems) {
return;
}

const layerId = Object.keys(selectedItems)[0];
model.centerOnAnnotation(layerId);
}
});
}
Expand Down
28 changes: 28 additions & 0 deletions packages/base/src/mainview/mainView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ import AnnotationFloater from '../annotations/components/AnnotationFloater';
import { CommandIDs } from '../constants';
import { FollowIndicator } from './FollowIndicator';
import CollaboratorPointers, { ClientPointer } from './CollaboratorPointers';
import TileSource from 'ol/source/Tile';

interface IProps {
viewModel: MainViewModel;
Expand Down Expand Up @@ -1241,10 +1242,37 @@ export class MainView extends React.Component<IProps, IStates> {
}

private _onZoomToAnnotation(_: IJupyterGISModel, id: string) {
// annotation?
const annotation = this._model.annotationModel?.getAnnotation(id);
if (annotation) {
this._moveToPosition(annotation.position, annotation.zoom);
}

// layer?
const olLayer = this.getLayer(id) as Layer;
const source = olLayer?.getSource();

let extent;

if (source instanceof VectorSource) {
extent = source.getExtent();
}

if (source instanceof TileSource) {
// Tiled sources don't have getExtent() so we get it from the grid
const tileGrid = source.getTileGrid();
extent = tileGrid?.getExtent();
}

if (extent) {
this._Map.getView().fit(extent, {
size: this._Map.getSize(), // Ensure the map view fits within the map size
maxZoom: 16, // Optional: Set a maximum zoom level
duration: 500
});
} else {
console.warn('Layer has no extent.');
}
}

private _moveToPosition(
Expand Down

0 comments on commit e556b3d

Please sign in to comment.