mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Geomap: default basemap config cleanup (#37069)
This commit is contained in:
parent
f26f76df5a
commit
e604e69d93
@ -1013,8 +1013,8 @@ default_timezone = browser
|
||||
enabled = true
|
||||
|
||||
[geomap]
|
||||
# Set the default base layer in GeoMaps plugin
|
||||
default_baselayer =
|
||||
# Set the JSON configuration for the default basemap
|
||||
default_baselayer_config =
|
||||
|
||||
# Enable or disable loading other base map layers
|
||||
disable_custom_baselayers = false
|
||||
enable_custom_baselayers = true
|
||||
|
@ -993,8 +993,14 @@
|
||||
;enabled = true
|
||||
|
||||
[geomap]
|
||||
# Set the default base layer in GeoMaps plugin
|
||||
;default_baselayer =
|
||||
# Set the JSON configuration for the default basemap
|
||||
;default_baselayer_config = `{
|
||||
"type": "xyz",
|
||||
"config": {
|
||||
"attribution": "Open street map",
|
||||
"url": "https://tile.openstreetmap.org/{z}/{x}/{y}.png"
|
||||
}
|
||||
}`
|
||||
|
||||
# Enable or disable loading other base map layers
|
||||
;disable_custom_baselayers = false
|
||||
;enable_custom_baselayers = true
|
||||
|
@ -1677,21 +1677,21 @@ Set this to `false` to disable expressions and hide them in the Grafana UI. Defa
|
||||
|
||||
This section controls the defaults settings for Geomap Plugin.
|
||||
|
||||
### default_baselayer
|
||||
### default_baselayer_config
|
||||
|
||||
The json config used to define the default base map. Four base map options to choose from are `carto`, `esriXYZTiles`, `xyzTiles`, `standard`.
|
||||
For example, to set cartoDB light as the default base layer:
|
||||
|
||||
```ini
|
||||
geomap_default_baselayer = `{
|
||||
"type": "carto",
|
||||
"config": {
|
||||
"theme": "light",
|
||||
"showLabels": true
|
||||
}
|
||||
}`
|
||||
default_baselayer_config = `{
|
||||
"type": "xyz",
|
||||
"config": {
|
||||
"attribution": "Open street map",
|
||||
"url": "https://tile.openstreetmap.org/{z}/{x}/{y}.png"
|
||||
}
|
||||
}`
|
||||
```
|
||||
|
||||
### disable_custom_baselayers
|
||||
### enable_custom_baselayers
|
||||
|
||||
Set this to `true` to disable loading other custom base maps and hide them in the Grafana UI. Default is `false`.
|
@ -88,7 +88,7 @@ export class GrafanaBootConfig implements GrafanaConfig {
|
||||
caching = {
|
||||
enabled: false,
|
||||
};
|
||||
geomapDefaultBaseLayer?: MapLayerOptions;
|
||||
geomapDefaultBaseLayerConfig?: MapLayerOptions;
|
||||
geomapDisableCustomBaseLayer?: boolean;
|
||||
|
||||
constructor(options: GrafanaBootConfig) {
|
||||
|
@ -259,8 +259,13 @@ func (hs *HTTPServer) getFrontendSettingsMap(c *models.ReqContext) (map[string]i
|
||||
"caching": map[string]bool{
|
||||
"enabled": hs.Cfg.SectionWithEnvOverrides("caching").Key("enabled").MustBool(true),
|
||||
},
|
||||
"geomapDefaultBaseLayer": hs.Cfg.DefaultBaseLayer,
|
||||
"geomapDisableCustomBaseLayer": hs.Cfg.DisableCustomBaseLayers,
|
||||
}
|
||||
|
||||
if hs.Cfg.GeomapDefaultBaseLayerConfig != nil {
|
||||
jsonObj["geomapDefaultBaseLayerConfig"] = hs.Cfg.GeomapDefaultBaseLayerConfig
|
||||
}
|
||||
if !hs.Cfg.GeomapEnableCustomBaseLayers {
|
||||
jsonObj["geomapDisableCustomBaseLayer"] = true
|
||||
}
|
||||
|
||||
return jsonObj, nil
|
||||
|
@ -402,9 +402,9 @@ type Cfg struct {
|
||||
// Grafana.com URL
|
||||
GrafanaComURL string
|
||||
|
||||
// Geomap plugin tile server
|
||||
DefaultBaseLayer map[string]interface{}
|
||||
DisableCustomBaseLayers bool
|
||||
// Geomap base layer config
|
||||
GeomapDefaultBaseLayerConfig map[string]interface{}
|
||||
GeomapEnableCustomBaseLayers bool
|
||||
}
|
||||
|
||||
// IsLiveConfigEnabled returns true if live should be able to save configs to SQL tables
|
||||
@ -972,16 +972,17 @@ func (cfg *Cfg) Load(args *CommandLineArgs) error {
|
||||
}
|
||||
|
||||
geomapSection := iniFile.Section("geomap")
|
||||
basemapJSON := valueAsString(geomapSection, "default_baselayer", "")
|
||||
cfg.DefaultBaseLayer = make(map[string]interface{})
|
||||
basemapJSON := valueAsString(geomapSection, "default_baselayer_config", "")
|
||||
if basemapJSON != "" {
|
||||
err = json.Unmarshal([]byte(basemapJSON), &cfg.DefaultBaseLayer)
|
||||
layer := make(map[string]interface{})
|
||||
err = json.Unmarshal([]byte(basemapJSON), &layer)
|
||||
if err != nil {
|
||||
cfg.Logger.Error(fmt.Sprintf("Error parsing JSON string: %s", err))
|
||||
cfg.DefaultBaseLayer = nil
|
||||
cfg.Logger.Error("Error reading json from default_baselayer_config", "error", err)
|
||||
} else {
|
||||
cfg.GeomapDefaultBaseLayerConfig = layer
|
||||
}
|
||||
}
|
||||
cfg.DisableCustomBaseLayers = geomapSection.Key("disable_custom_baselayers").MustBool(false)
|
||||
cfg.GeomapEnableCustomBaseLayers = geomapSection.Key("enable_custom_baselayers").MustBool(true)
|
||||
|
||||
cfg.readDateFormats()
|
||||
cfg.readSentryConfig()
|
||||
|
@ -1,5 +1,5 @@
|
||||
import React, { Component } from 'react';
|
||||
import { geomapLayerRegistry } from './layers/registry';
|
||||
import { DEFAULT_BASEMAP_CONFIG, geomapLayerRegistry, defaultBaseLayer } from './layers/registry';
|
||||
import { Map, View } from 'ol';
|
||||
import Attribution from 'ol/control/Attribution';
|
||||
import Zoom from 'ol/control/Zoom';
|
||||
@ -12,7 +12,6 @@ import { PanelData, MapLayerHandler, MapLayerOptions, PanelProps, GrafanaTheme }
|
||||
import { config } from '@grafana/runtime';
|
||||
|
||||
import { ControlsOptions, GeomapPanelOptions, MapViewConfig } from './types';
|
||||
import { defaultGrafanaThemedMap } from './layers/basemaps';
|
||||
import { centerPointRegistry, MapCenterID } from './view';
|
||||
import { fromLonLat } from 'ol/proj';
|
||||
import { Coordinate } from 'ol/coordinate';
|
||||
@ -153,10 +152,11 @@ export class GeomapPanel extends Component<Props> {
|
||||
if (!this.map) {
|
||||
return;
|
||||
}
|
||||
if (!cfg) {
|
||||
cfg = { type: defaultGrafanaThemedMap.id };
|
||||
|
||||
if (!cfg?.type || config.geomapDisableCustomBaseLayer) {
|
||||
cfg = DEFAULT_BASEMAP_CONFIG;
|
||||
}
|
||||
const item = geomapLayerRegistry.getIfExists(cfg.type) ?? defaultGrafanaThemedMap;
|
||||
const item = geomapLayerRegistry.getIfExists(cfg.type) ?? defaultBaseLayer;
|
||||
const layer = item.create(this.map, cfg, config.theme2).init();
|
||||
if (this.basemap) {
|
||||
this.map.removeLayer(this.basemap);
|
||||
|
@ -2,7 +2,7 @@ import React, { FC } from 'react';
|
||||
import { StandardEditorProps, MapLayerOptions, MapLayerRegistryItem, PluginState } from '@grafana/data';
|
||||
import { GeomapPanelOptions } from '../types';
|
||||
import { LayerEditor } from './LayerEditor';
|
||||
import { hasAlphaPanels } from 'app/core/config';
|
||||
import { config, hasAlphaPanels } from 'app/core/config';
|
||||
|
||||
function baseMapFilter(layer: MapLayerRegistryItem): boolean {
|
||||
if (!layer.isBaseMap) {
|
||||
@ -19,5 +19,9 @@ export const BaseLayerEditor: FC<StandardEditorProps<MapLayerOptions, any, Geoma
|
||||
onChange,
|
||||
context,
|
||||
}) => {
|
||||
if (config.geomapDisableCustomBaseLayer) {
|
||||
return <div>The base layer is configured by the server admin.</div>;
|
||||
}
|
||||
|
||||
return <LayerEditor options={value} data={context.data} onChange={onChange} filter={baseMapFilter} />;
|
||||
};
|
||||
|
@ -10,8 +10,7 @@ import {
|
||||
FieldType,
|
||||
Field,
|
||||
} from '@grafana/data';
|
||||
import { geomapLayerRegistry } from '../layers/registry';
|
||||
import { defaultGrafanaThemedMap } from '../layers/basemaps';
|
||||
import { DEFAULT_BASEMAP_CONFIG, geomapLayerRegistry } from '../layers/registry';
|
||||
import { OptionsPaneCategoryDescriptor } from 'app/features/dashboard/components/PanelEditor/OptionsPaneCategoryDescriptor';
|
||||
import { setOptionImmutably } from 'app/features/dashboard/components/PanelEditor/utils';
|
||||
import { fillOptionsPaneItems } from 'app/features/dashboard/components/PanelEditor/getVizualizationOptions';
|
||||
@ -29,7 +28,7 @@ export const LayerEditor: FC<LayerEditorProps> = ({ options, onChange, data, fil
|
||||
return geomapLayerRegistry.selectOptions(
|
||||
options?.type // the selected value
|
||||
? [options.type] // as an array
|
||||
: [defaultGrafanaThemedMap.id],
|
||||
: [DEFAULT_BASEMAP_CONFIG.type],
|
||||
filter
|
||||
);
|
||||
}, [options?.type, filter]);
|
||||
|
@ -1,30 +0,0 @@
|
||||
import { MapLayerRegistryItem, MapLayerOptions, GrafanaTheme2 } from '@grafana/data';
|
||||
import Map from 'ol/Map';
|
||||
import { carto } from './carto';
|
||||
import { esriXYZTiles } from './esri';
|
||||
import { xyzTiles } from './generic';
|
||||
import { standard } from './osm';
|
||||
import { config } from 'app/core/config';
|
||||
|
||||
// Array of base map options to search through
|
||||
const baseLayers = [carto, esriXYZTiles, xyzTiles, standard];
|
||||
|
||||
// Default base layer depending on the server setting
|
||||
export const defaultBaseLayer: MapLayerRegistryItem = {
|
||||
id: 'default',
|
||||
name: 'Default base layer',
|
||||
isBaseMap: true,
|
||||
|
||||
create: (map: Map, options: MapLayerOptions, theme: GrafanaTheme2) => {
|
||||
// Use Carto as the default base layer if not set from server
|
||||
let layer: any = carto;
|
||||
if (config.geomapDefaultBaseLayer && config.geomapDefaultBaseLayer.type) {
|
||||
options = config.geomapDefaultBaseLayer; // options from server
|
||||
layer = baseLayers.find((baseLayer) => baseLayer.id === options.type);
|
||||
if (!layer) {
|
||||
throw new Error('Invalid default base map type');
|
||||
}
|
||||
}
|
||||
return layer.create(map, options, theme);
|
||||
},
|
||||
};
|
@ -2,19 +2,11 @@ import { cartoLayers } from './carto';
|
||||
import { esriLayers } from './esri';
|
||||
import { genericLayers } from './generic';
|
||||
import { osmLayers } from './osm';
|
||||
import { defaultBaseLayer } from './default';
|
||||
|
||||
export const defaultGrafanaThemedMap = {
|
||||
...defaultBaseLayer,
|
||||
id: 'default',
|
||||
name: 'Default base layer',
|
||||
};
|
||||
|
||||
/**
|
||||
* Registry for layer handlers
|
||||
*/
|
||||
export const basemapLayers = [
|
||||
defaultGrafanaThemedMap,
|
||||
...osmLayers,
|
||||
...cartoLayers,
|
||||
...esriLayers, // keep formatting
|
||||
|
@ -1,12 +1,41 @@
|
||||
import { MapLayerRegistryItem, Registry } from '@grafana/data';
|
||||
|
||||
import { MapLayerRegistryItem, Registry, MapLayerOptions, GrafanaTheme2 } from '@grafana/data';
|
||||
import Map from 'ol/Map';
|
||||
import { carto } from './basemaps/carto';
|
||||
import { config } from 'app/core/config';
|
||||
import { basemapLayers } from './basemaps';
|
||||
import { dataLayers } from './data';
|
||||
|
||||
export const DEFAULT_BASEMAP_CONFIG: MapLayerOptions = {
|
||||
type: 'default',
|
||||
config: {},
|
||||
};
|
||||
|
||||
// Default base layer depending on the server setting
|
||||
export const defaultBaseLayer: MapLayerRegistryItem = {
|
||||
id: DEFAULT_BASEMAP_CONFIG.type,
|
||||
name: 'Default base layer',
|
||||
isBaseMap: true,
|
||||
|
||||
create: (map: Map, options: MapLayerOptions, theme: GrafanaTheme2) => {
|
||||
const serverLayerType = config?.geomapDefaultBaseLayerConfig?.type;
|
||||
if (serverLayerType) {
|
||||
const layer = geomapLayerRegistry.getIfExists(serverLayerType);
|
||||
if (!layer) {
|
||||
throw new Error('Invalid basemap configuraiton on server');
|
||||
}
|
||||
return layer.create(map, config.geomapDefaultBaseLayerConfig!, theme);
|
||||
}
|
||||
|
||||
// For now use carto as our default basemap
|
||||
return carto.create(map, options, theme);
|
||||
},
|
||||
};
|
||||
|
||||
/**
|
||||
* Registry for layer handlers
|
||||
*/
|
||||
export const geomapLayerRegistry = new Registry<MapLayerRegistryItem<any>>(() => [
|
||||
defaultBaseLayer,
|
||||
...basemapLayers, // simple basemaps
|
||||
...dataLayers, // Layers with update functions
|
||||
]);
|
||||
|
@ -5,9 +5,8 @@ import { GeomapPanel } from './GeomapPanel';
|
||||
import { MapViewEditor } from './editor/MapViewEditor';
|
||||
import { defaultView, GeomapPanelOptions } from './types';
|
||||
import { mapPanelChangedHandler } from './migrations';
|
||||
import { defaultGrafanaThemedMap } from './layers/basemaps';
|
||||
import { defaultMarkersConfig } from './layers/data/markersLayer';
|
||||
import { config } from 'app/core/config';
|
||||
import { DEFAULT_BASEMAP_CONFIG } from './layers/registry';
|
||||
|
||||
export const plugin = new PanelPlugin<GeomapPanelOptions>(GeomapPanel)
|
||||
.setNoPadding()
|
||||
@ -33,20 +32,14 @@ export const plugin = new PanelPlugin<GeomapPanelOptions>(GeomapPanel)
|
||||
defaultValue: defaultView.shared,
|
||||
});
|
||||
|
||||
// Nested
|
||||
if (!config.geomapDisableCustomBaseLayer) {
|
||||
builder.addCustomEditor({
|
||||
category: ['Base Layer'],
|
||||
id: 'basemap',
|
||||
path: 'basemap',
|
||||
name: 'Base Layer',
|
||||
editor: BaseLayerEditor,
|
||||
defaultValue: {
|
||||
type: defaultGrafanaThemedMap.id,
|
||||
config: defaultGrafanaThemedMap.defaultOptions,
|
||||
},
|
||||
});
|
||||
}
|
||||
builder.addCustomEditor({
|
||||
category: ['Base Layer'],
|
||||
id: 'basemap',
|
||||
path: 'basemap',
|
||||
name: 'Base Layer',
|
||||
editor: BaseLayerEditor,
|
||||
defaultValue: DEFAULT_BASEMAP_CONFIG,
|
||||
});
|
||||
|
||||
builder.addCustomEditor({
|
||||
category: ['Data Layer'],
|
||||
|
Loading…
Reference in New Issue
Block a user