有没有什么元素或函数可以用来获取SLD中的当前层名称?我们正在请求多个层,并希望使用每一个相同的样式,但有小的自定义,如颜色变化等取决于层。
如果不为每个层类型创建一个新的SLD,我还找不到任何文档来支持这样的用例。
发布于 2020-04-02 01:09:42
WFS
在这里,我为您创建了一个示例,向您展示如何执行您提到的简单修改。
在下面的示例中,我没有修改图层样式,而是修改了features样式,这与我刚才发现易于举例说明的事情是一样的。因此,为了在您的情况下工作,您只需要获得层样式,并以与示例相同的方式进行更改。
<!doctype html>
<html lang="en">
<head>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.1.1/css/ol.css" type="text/css">
<style>
.map {
height: 400px;
width: 100%;
}
</style>
<script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.1.1/build/ol.js"></script>
<title>Simple Style Change</title>
</head>
<body>
<div id="map" class="map"></div>
<script type="text/javascript">
// base style default of features
// features
const radius = 20000;
const cx = -80;
const cy = 35;
const fonts = [
'1.2em "Fira Sans", sans-serif',
'italic 1.2em "Fira Sans", serif',
'italic small-caps bold 16px/2 cursive',
'small-caps bold 24px/1 sans-serif',
'caption'
];
const baseStyle = new ol.style.Style({
stroke: new ol.style.Stroke({
color: 'rgba(0, 0, 0, 1)',
width: 1
}),
fill: new ol.style.Fill({
color: 'rgba(0, 0, 0, 0.1)'
}),
text: new ol.style.Text({
stroke: new ol.style.Stroke({color: '#ffffff', width: 3})
})
});
// map
const view = new ol.View({
center: ol.proj.fromLonLat([cx-6, cy]),
zoom: 7
});
const vlayer = new ol.layer.Vector({
source: new ol.source.Vector(),
style: baseStyle
});
const map = new ol.Map({
target: 'map',
layers: [
new ol.layer.Tile({
source: new ol.source.OSM()
}),
vlayer
],
view
});
// add features
const features = [];
let feat, style, fill, text;
for (let i = 0; i < 20; i++) {
feat = new ol.Feature({
geometry: ol.geom.Polygon.circular([cx-i, cy], radius)
.transform('EPSG:4326', 'EPSG:3857')
});
style = vlayer.getStyle().clone();
fill = vlayer.getStyle().getFill().clone();
fill.setColor(`rgba(${i*12},${i*12},${i*12},0.5)`)
style.setFill(fill);
text = vlayer.getStyle().getText().clone();
text.setFont(fonts[i % fonts.length]);
text.setText(`${i}`);
style.setText(text);
feat.setStyle(style);
features.push(feat);
}
vlayer.getSource().addFeatures(features);
</script>
</body>
</html>
WMS更新
对于WMS,您可以使用两个参数来根据请求动态更改样式。
在OL上,您可以使用选项参数params或方法updateParams在创建时指定此参数(假设您使用的是TileWMS )。
https://stackoverflow.com/questions/60952109
复制相似问题