geostyler
Version:
Framework for styling geodata
100 lines (99 loc) • 2.42 kB
JavaScript
import { jsx as h } from "react/jsx-runtime";
import { useState as w, useRef as k, useCallback as A, useEffect as H } from "react";
import { SldStyleParser as P } from "geostyler-sld-parser";
/* empty css */
import B from "../../../Util/HTTPUtil.js";
import I from "./LoadingIcon.js";
import { useGeoStylerComposition as N } from "../../../context/GeoStylerContext/GeoStylerContext.js";
const O = new P(), V = (L) => {
const T = N("SLDRenderer"), t = { ...L, ...T }, {
requestDelay: c = 500,
width: s = 150,
height: o = 100,
wmsBaseUrl: d,
layer: a,
rasterLayer: p,
additionalHeaders: u,
wmsParams: y,
onClick: R,
symbolizers: i
} = t;
if (!t.wmsBaseUrl || !t.layer)
throw new Error(
'"wmsBaseUrl" or "layer" are required as SLDRenderer props or in the GeoStylerContext.composition.SLDRenderer'
);
const [D, E] = w(), [U, f] = w(), n = k(void 0), S = A((r) => {
f(I), n && clearTimeout(n.current), n.current = setTimeout(async () => {
var g;
const b = {
name: "sld-renderer-style",
rules: [{
name: "",
symbolizers: r
}]
};
let l;
r.some((e) => e.kind === "Raster") ? l = p || a : l = a;
const {
output: C,
errors: m = []
} = await O.writeStyle(b);
try {
const e = await B.post({
url: d,
params: {
SERVICE: "WMS",
VERSION: "1.3.0",
REQUEST: "GetLegendGraphic",
FORMAT: "image/png",
TRANSPARENT: "true",
LAYER: l,
SLD_BODY: C,
WIDTH: s,
HEIGHT: o,
...y
},
additionalHeaders: u
});
e && e.ok && e.blob().then((G) => {
f(window.URL.createObjectURL(G));
});
} catch (e) {
m.push(e);
}
m.length > 0 && E((g = m[0]) == null ? void 0 : g.message);
}, c);
}, [
u,
o,
a,
p,
c,
s,
d,
y
]);
return H(() => {
S(i);
}, [i, S]), /* @__PURE__ */ h(
"div",
{
onClick: (r) => {
R && R(i, r);
},
className: "gs-symbolizer-sldrenderer",
children: /* @__PURE__ */ h(
"img",
{
width: s,
height: o,
src: U,
alt: D
}
)
}
);
};
export {
V as SLDRenderer
};