-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: adds wms styling using geostyler #1771
base: next
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import React, { | ||
useEffect, useState, useCallback | ||
} from 'react'; | ||
|
||
import { | ||
CardStyle, CardStyleProps | ||
} from 'geostyler/dist/Component/CardStyle/CardStyle'; | ||
import SLDParser from 'geostyler-sld-parser'; | ||
import { Style as GsStyle } from 'geostyler-style'; | ||
import Layer from 'ol/layer/Layer'; | ||
import { | ||
ImageWMS, TileWMS | ||
} from 'ol/source'; | ||
|
||
import { Logger } from '@terrestris/base-util'; | ||
import { MapUtil } from '@terrestris/ol-util'; | ||
import { useMap } from '@terrestris/react-util/dist/Hooks/useMap/useMap'; | ||
|
||
import useAppSelector from '../../../hooks/useAppSelector'; | ||
import useSHOGunAPIClient from '../../../hooks/useSHOGunAPIClient'; | ||
import { | ||
fetchGeoserverStyle, fetchWorkspaceFromGetCapabilities, getLayerUrl | ||
} from '../../../utils/geoserverUtils'; | ||
|
||
export type SldStylingPanelProps = CardStyleProps; | ||
|
||
const SldStylingPanel: React.FC<SldStylingPanelProps> = (props): JSX.Element => { | ||
const [style, setStyle] = useState<GsStyle | undefined>(); | ||
const layerUid = useAppSelector(state => state.stylingDrawerLayerUid); | ||
const map = useMap(); | ||
const client = useSHOGunAPIClient(); | ||
|
||
const getStyle = useCallback(async () => { | ||
if (!map || !layerUid) { | ||
return; | ||
} | ||
|
||
const layer = MapUtil.getLayerByOlUid(map, layerUid) as Layer; | ||
if (!layer) { | ||
return; | ||
} | ||
|
||
const source = layer.getSource(); | ||
if (!(source instanceof ImageWMS || source instanceof TileWMS)) { | ||
return; | ||
} | ||
|
||
const layerUrl = getLayerUrl(source); | ||
if (!layerUrl) { | ||
return; | ||
} | ||
|
||
try { | ||
const workspaceInfo = await fetchWorkspaceFromGetCapabilities(layerUrl, client); | ||
if (!workspaceInfo) {return;} | ||
|
||
const geoserverStyle = await fetchGeoserverStyle(workspaceInfo.workspace, workspaceInfo.layerName, layerUrl, client); | ||
if (geoserverStyle) { | ||
const parser = new SLDParser(); | ||
const { output: sldObject } = await parser.readStyle(geoserverStyle); | ||
setStyle(sldObject); | ||
} | ||
} catch (error) { | ||
Logger.error('Error: ', error); | ||
} | ||
}, [map, layerUid, client]); | ||
|
||
useEffect(() => { | ||
getStyle(); | ||
}, [getStyle]); | ||
|
||
useEffect(() => { | ||
if (!map || !style) { | ||
return; | ||
} | ||
|
||
const layer = MapUtil.getLayerByOlUid(map, layerUid) as Layer; | ||
const source = layer?.getSource() as ImageWMS; | ||
if (!source) { | ||
return; | ||
} | ||
|
||
style.name = source.getParams().LAYERS; | ||
|
||
const parser = new SLDParser(); | ||
parser.writeStyle(style).then(({ output: sld }) => { | ||
if (sld) { | ||
source.updateParams({ | ||
SLD_BODY: sld, | ||
STYLES: style.name | ||
}); | ||
Comment on lines
+88
to
+91
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just wondering, but what happens if:
What style would be shown in the styler UI? The default one or the updated one? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good hint! The default style will be used in that case. Maybe we want to check if a layer has a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we also want to store the |
||
} | ||
}); | ||
}, [style, map, layerUid]); | ||
|
||
if (!map || !style) { | ||
return <></>; | ||
} | ||
|
||
return ( | ||
<CardStyle | ||
style={style} | ||
onStyleChange={setStyle} | ||
{...props} | ||
/> | ||
); | ||
}; | ||
|
||
export default SldStylingPanel; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { | ||
createSlice, PayloadAction | ||
} from '@reduxjs/toolkit'; | ||
|
||
const initialState = ''; | ||
|
||
export const slice = createSlice({ | ||
name: 'stylingDrawerLayer', | ||
initialState, | ||
reducers: { | ||
setStylingDrawerLayerUid: (state, action: PayloadAction<string>) => { | ||
return action.payload; | ||
}, | ||
clearStylingDrawerLayerUid: () => { | ||
return ''; | ||
} | ||
} | ||
}); | ||
|
||
export const { | ||
setStylingDrawerLayerUid, | ||
clearStylingDrawerLayerUid | ||
} = slice.actions; | ||
|
||
export default slice.reducer; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just wondering why this is needed. Usually the workspace is already party of the layer name, e.g.
MYWORKSPACE:MYLAYER
. This can probably enhanced by checking if the name contains the workspace already and if not getting it from the capabilities.