Source: modules/action/Symbology.js

  1. /**
  2. * @module action/Symbology.js
  3. * @name SymbologyAction
  4. * @copyright 2023 3Liz
  5. * @author DHONT René-Luc
  6. * @license MPL-2.0
  7. */
  8. import WMS from './../WMS.js';
  9. import {LayerTreeLayerState, LayerTreeGroupState} from './../state/LayerTree.js'
  10. /**
  11. * @param {LayerTreeLayerState[]} treeLayers - The tree layer group in which tree layers will be updated
  12. * @returns {Promise} Promise object represents the tree layers updated
  13. */
  14. export async function updateLayerTreeLayersSymbology(treeLayers) {
  15. if (!Array.isArray(treeLayers)) {
  16. throw new TypeError('`updateLayerTreeLayersSymbology` method required an array as parameter!');
  17. }
  18. // If the tree layers is empty
  19. // nothing to do
  20. if (treeLayers.length == 0) {
  21. return treeLayers;
  22. }
  23. const wmsNames = treeLayers.map(layer => layer.wmsName);
  24. const wmsStyles = treeLayers.map(layer => layer.wmsSelectedStyleName);
  25. let treeLayersByName = {};
  26. treeLayers.forEach(treeLayer => treeLayersByName[treeLayer.wmsName] = treeLayer);
  27. const wms = new WMS();
  28. const wmsParams = {
  29. LAYER: wmsNames,
  30. STYLES: wmsStyles,
  31. };
  32. await wms.getLegendGraphic(wmsParams).then((response) => {
  33. for (const node of response.nodes) {
  34. // If the layer has no symbology, there is no type property
  35. if (node.hasOwnProperty('type')) {
  36. treeLayersByName[node.name].symbology = node;
  37. }
  38. }
  39. }).catch(console.error);
  40. return treeLayers;
  41. }
  42. /**
  43. * @param {LayerTreeLayerState} treeLayer - The tree layer to be updated
  44. * @returns {Promise} Promise object represents the tree layer updated
  45. */
  46. export async function updateLayerTreeLayerSymbology(treeLayer) {
  47. if (!(treeLayer instanceof LayerTreeLayerState)) {
  48. throw new TypeError('`updateLayerTreeLayerSymbology` method required a LayerTreeLayerState as parameter!');
  49. }
  50. return updateLayerTreeLayersSymbology([treeLayer])[0];
  51. }
  52. /**
  53. * @param {LayerTreeGroupState} treeGroup - The tree layer group in which tree layers will be updated
  54. * @returns {Promise} Promise object represents the tree layers updated
  55. */
  56. export async function updateLayerTreeGroupLayersSymbology(treeGroup) {
  57. if (!(treeGroup instanceof LayerTreeGroupState)) {
  58. throw new TypeError('`updateLayerTreeGroupLayersSymbology` method required a LayerTreeGroupState as parameter!');
  59. }
  60. return updateLayerTreeLayersSymbology(treeGroup.findTreeLayers());
  61. }