Skip to content

Commit

Permalink
read list of palettes from Thredds server layer metadata service
Browse files Browse the repository at this point in the history
  • Loading branch information
sixlighthouses committed Feb 8, 2024
1 parent b1b8753 commit e09266a
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 4 deletions.
6 changes: 6 additions & 0 deletions lib/Models/Catalog/Ows/WebMapServiceCapabilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ export type CapabilitiesDimension = string & {
readonly current?: boolean;
};

export type Palettes = {
palettes: ReadonlyArray<string>;
defaultPalette: string;
scaleRange: number[];
};

export type CapabilitiesExtent = string & {
readonly name: string;
readonly default?: string;
Expand Down
38 changes: 37 additions & 1 deletion lib/Models/Catalog/Ows/WebMapServiceCapabilitiesStratum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import WebMapServiceCatalogItemTraits, {
SUPPORTED_CRS_4326,
WebMapServiceAvailableLayerDimensionsTraits,
WebMapServiceAvailableLayerStylesTraits,
WebMapServiceAvailablePaletteTraits,
WebMapServiceAvailableStyleTraits
} from "../../../Traits/TraitsClasses/WebMapServiceCatalogItemTraits";
import LoadableStratum from "../../Definition/LoadableStratum";
Expand Down Expand Up @@ -144,6 +145,27 @@ export default class WebMapServiceCapabilitiesStratum extends LoadableStratum(
}
}

@computed
get availablePalettes(): StratumFromTraits<WebMapServiceAvailablePaletteTraits>[] {
const result: StratumFromTraits<WebMapServiceAvailablePaletteTraits>[] = [];
const availableStyles = this.catalogItem.availableStyles || [];
const layerStyle = availableStyles[0];
if (this.catalogItem.isThredds) {
if (layerStyle?.styles[0].abstract) {
const urlRegex = /(https?:\/\/[^\s]+)/g;
const urls = layerStyle?.styles[0].abstract.match(urlRegex);
let paletteUrl: uri.URI | undefined;
if (urls) {
paletteUrl = URI(proxyCatalogItemUrl(this.catalogItem, urls[0]));
result.push({
url: paletteUrl.toString()
});
}
}
}

return result;
}
/**
* **How we determine WMS legends (in order)**
1. Defined manually in catalog JSON
Expand All @@ -154,6 +176,7 @@ export default class WebMapServiceCapabilitiesStratum extends LoadableStratum(
@computed
get legends(): StratumFromTraits<LegendTraits>[] | undefined {
const availableStyles = this.catalogItem.availableStyles || [];
const availablePalettes = this.catalogItem.availablePalettes || [];
const layers = this.catalogItem.layersArray;
const styles = this.catalogItem.stylesArray;

Expand All @@ -172,12 +195,22 @@ export default class WebMapServiceCapabilitiesStratum extends LoadableStratum(
)?.styles;

let layerStyle: Model<WebMapServiceAvailableStyleTraits> | undefined;
let paletteUrl: string | undefined;

if (isDefined(style)) {
// Attempt to find layer style based on AvailableStyleTraits
layerStyle = layerAvailableStyles?.find(
(candidate) => candidate.name === style
);
if (this.catalogItem.isThredds) {
if (layerStyle?.abstract) {
const urlRegex = /(https?:\/\/[^\s]+)/g;
const urls = layerStyle.abstract.match(urlRegex);
if (urls) {
paletteUrl = proxyCatalogItemUrl(this.catalogItem, urls[0]);
}
}
}
}

// If no style is selected and this WMS doesn't support GetLegendGraphics - we must use the first style if none is explicitly specified.
Expand Down Expand Up @@ -250,6 +283,8 @@ export default class WebMapServiceCapabilitiesStratum extends LoadableStratum(
legendUri.setQuery("transparent", "true");
}

i;

// Add colour scale range params if supported
if (
this.catalogItem.supportsColorScaleRange &&
Expand All @@ -264,7 +299,8 @@ export default class WebMapServiceCapabilitiesStratum extends LoadableStratum(
createStratumInstance(LegendTraits, {
url: legendUri.toString(),
urlMimeType: legendUrlMimeType,
imageScaling: legendScaling
imageScaling: legendScaling,
paletteUrl: paletteUrl
})
);
}
Expand Down
1 change: 1 addition & 0 deletions lib/ReactViews/Analytics/ParameterEditor.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ const ParameterEditor = createReactClass({
renderEditor() {
for (let i = 0; i < ParameterEditor.parameterTypeConverters.length; ++i) {
const converter = ParameterEditor.parameterTypeConverters[i];

const editor = converter.parameterTypeToDiv(
this.props.parameter.type,
this
Expand Down
2 changes: 1 addition & 1 deletion lib/ReactViews/Workbench/Controls/Legend.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import LegendOwnerTraits from "../../../Traits/TraitsClasses/LegendOwnerTraits";
import LegendTraits, {
LegendItemTraits
} from "../../../Traits/TraitsClasses/LegendTraits";
import Styles from "./legend.scss";
import Styles, { legend } from "./legend.scss";

/* A lookup map for displayable mime types */
const DISPLAYABLE_MIME_TYPES = [
Expand Down
50 changes: 50 additions & 0 deletions lib/ReactViews/Workbench/Controls/Palettes.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import React, { useEffect, useState } from "react";
import isDefined from "../../../Core/isDefined";
import WebMapServiceCapabilitiesStratum from "../../../Models/Catalog/Ows/WebMapServiceCapabilitiesStratum";
import Box from "../../../Styled/Box";

interface PalettesProps {
item: WebMapServiceCapabilitiesStratum;
}

// @observable
const Palettes: React.FC<PalettesProps> = ({ item }) => {
const [palettes, setPalettes] = useState([]);

useEffect(() => {
const fetchPalettes = async (item: WebMapServiceCapabilitiesStratum) => {
try {
const paletteUrl = item.availablePalettes[0]?.url;
if (isDefined(paletteUrl)) {
const response = await fetch(paletteUrl);
const data = await response.json();
setPalettes(data.palettes);
}
} catch (error) {
console.error("Error fetching palettes:", error);
}
};

fetchPalettes(item);
}, [item]);

const handlePaletteChange = (event: React.ChangeEvent<HTMLSelectElement>) => {
//get the value of the selected option
const selectedPalette = event.target.value;
item.setTrait("user", "selectedPalette", selectedPalette);

Check failure on line 34 in lib/ReactViews/Workbench/Controls/Palettes.tsx

View workflow job for this annotation

GitHub Actions / build

Property 'setTrait' does not exist on type 'WebMapServiceCapabilitiesStratum'.

Check failure on line 34 in lib/ReactViews/Workbench/Controls/Palettes.tsx

View workflow job for this annotation

GitHub Actions / deploy

Property 'setTrait' does not exist on type 'WebMapServiceCapabilitiesStratum'.
console.log("Selected palette:", selectedPalette);
};

return (
<Box displayInlineBlock fullWidth>
<p>Palettes: </p>
<select onChange={handlePaletteChange}>
{palettes.map((palette: any) => (
<option key={palette}>{palette}</option>
))}
</select>
</Box>
);
};

export default Palettes;
9 changes: 7 additions & 2 deletions lib/ReactViews/Workbench/Controls/WorkbenchItemControls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import DimensionSelectorSection from "./SelectableDimensionSection";
import ShortReport from "./ShortReport";
import TimerSection from "./TimerSection";
import ViewingControls from "./ViewingControls";
import Palettes from "./Palettes";

type WorkbenchControls = {
viewingControls?: boolean;
Expand All @@ -40,6 +41,7 @@ type WorkbenchControls = {
colorScaleRange?: boolean;
shortReport?: boolean;
legend?: boolean;
palettes?: boolean;
};

type WorkbenchItemControlsProps = {
Expand All @@ -62,7 +64,8 @@ export const defaultControls: Complete<WorkbenchControls> = {
selectableDimensions: true,
colorScaleRange: true,
shortReport: true,
legend: true
legend: true,
palettes: true
};

export const hideAllControls: Complete<WorkbenchControls> = {
Expand All @@ -78,7 +81,8 @@ export const hideAllControls: Complete<WorkbenchControls> = {
selectableDimensions: false,
colorScaleRange: false,
shortReport: false,
legend: false
legend: false,
palettes: false
};

const WorkbenchItemControls: React.FC<WorkbenchItemControlsProps> = observer(
Expand Down Expand Up @@ -111,6 +115,7 @@ const WorkbenchItemControls: React.FC<WorkbenchItemControlsProps> = observer(
{controls?.selectableDimensions ? (
<DimensionSelectorSection item={item} placement={DEFAULT_PLACEMENT} />
) : null}
{controls?.palettes ? <Palettes item={item} /> : null}

Check failure on line 118 in lib/ReactViews/Workbench/Controls/WorkbenchItemControls.tsx

View workflow job for this annotation

GitHub Actions / build

Type 'BaseModel' is missing the following properties from type 'WebMapServiceCapabilitiesStratum': catalogItem, capabilities, duplicateLoadableStratum, metadataUrls, and 105 more.

Check failure on line 118 in lib/ReactViews/Workbench/Controls/WorkbenchItemControls.tsx

View workflow job for this annotation

GitHub Actions / deploy

Type 'BaseModel' is missing the following properties from type 'WebMapServiceCapabilitiesStratum': catalogItem, capabilities, duplicateLoadableStratum, metadataUrls, and 105 more.
{
<GeneratedControlSection
item={item}
Expand Down
8 changes: 8 additions & 0 deletions lib/Traits/TraitsClasses/LegendTraits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,14 @@ export default class LegendTraits extends ModelTraits {
})
url?: string;

@primitiveTrait({
type: "string",
name: "Palettes URL",
description:
"The URL of the endpoint which returns the list of Thredds palette"
})
paletteUrl?: string;

@primitiveTrait({
type: "number",
name: "Scaling",
Expand Down
18 changes: 18 additions & 0 deletions lib/Traits/TraitsClasses/WebMapServiceCatalogItemTraits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,16 @@ export class WebMapServiceAvailableLayerStylesTraits extends ModelTraits {
styles?: WebMapServiceAvailableStyleTraits[];
}

export class WebMapServiceAvailablePaletteTraits extends ModelTraits {
@primitiveTrait({
type: "string",
name: "URL",
description:
"The URL of the GetMetadata request for the palette. This is used to get the palettes available for a layer."
})
url?: string;
}

export class WebMapServiceAvailableDimensionTraits extends ModelTraits {
@primitiveTrait({
type: "string",
Expand Down Expand Up @@ -216,6 +226,14 @@ export default class WebMapServiceCatalogItemTraits extends mixTraits(
})
availableStyles?: WebMapServiceAvailableLayerStylesTraits[];

@objectArrayTrait({
type: WebMapServiceAvailablePaletteTraits,
name: "Available Palettes",
description: "The available palettes.",
idProperty: "index"
})
availablePalettes?: WebMapServiceAvailablePaletteTraits[];

@objectArrayTrait({
type: WebMapServiceAvailableLayerDimensionsTraits,
name: "Available Dimensions",
Expand Down

0 comments on commit e09266a

Please sign in to comment.