Files
grafana/public/app/plugins/panel/geomap/utils/getLayersExtent.ts
Drew Slobodnjak 6e85dfa25a Geomap: Add dynamic initial view options (#54419)
* Geomap: Add dynamic initial view options

* Add control options for dynamic initial view

* Add handling for last only scope

* Remove stale todos

* Only reinitialize map view during data if fit

* Add options for data fit

In map init, remove dependency on layers input.

Add a boolean to map view config to handle all layers, with default
set to true. Also, add layer string to handle currently selected layer.

Change some verbage.

In map view editor, add controls for data extent options. Only display
layer selection when all layers is not selected.

Update layer extent function to handle all options permutations. When
all layers is selected, return all data for extent calculation. When
last only is selected, only return last data point in matching layer for
extent calculation. Otherwise, return all data points in matching layer
for extent calculation.

* Change padding tooltip and hide for last only

* Simplify getLayersExtent call

* Add enums for data scope options

* Update GeomapPanel for refactor merge

* Move view init functions back into geomap class

Re-apply data change handling and extent calculation handling.

* Update padding tooltip verbage

* Ensure geomap panel options layers are initialized

* Clean up GeomapPanel file (betterer)

* refactors / clean up

* more cleanup

Co-authored-by: nmarrs <nathanielmarrs@gmail.com>
2022-09-21 13:11:44 -07:00

57 lines
1.6 KiB
TypeScript

import { createEmpty, extend, Extent } from 'ol/extent';
import LayerGroup from 'ol/layer/Group';
import VectorLayer from 'ol/layer/Vector';
import { MapLayerState } from '../types';
export function getLayersExtent(
layers: MapLayerState[] = [],
allLayers = false,
lastOnly = false,
layer: string | undefined
): Extent {
return layers
.filter((l) => l.layer instanceof VectorLayer || l.layer instanceof LayerGroup)
.flatMap((ll) => {
const l = ll.layer;
if (l instanceof LayerGroup) {
return getLayerGroupExtent(l);
} else if (l instanceof VectorLayer) {
if (allLayers) {
// Return everything from all layers
return [l.getSource().getExtent()] ?? [];
} else if (lastOnly && layer === ll.options.name) {
// Return last only for selected layer
const feat = l.getSource().getFeatures();
const featOfInterest = feat[feat.length - 1];
const geo = featOfInterest?.getGeometry();
if (geo) {
return [geo.getExtent()] ?? [];
}
return [];
} else if (!lastOnly && layer === ll.options.name) {
// Return all points for selected layer
return [l.getSource().getExtent()] ?? [];
}
return [];
} else {
return [];
}
})
.reduce(extend, createEmpty());
}
export function getLayerGroupExtent(lg: LayerGroup) {
return lg
.getLayers()
.getArray()
.filter((l) => l instanceof VectorLayer)
.map((l) => {
if (l instanceof VectorLayer) {
return l.getSource().getExtent() ?? [];
} else {
return [];
}
});
}