Skip to content

Commit 6c3c4e1

Browse files
committed
Started on creating config maps for legend generation
1 parent e8d7edf commit 6c3c4e1

File tree

5 files changed

+190
-1
lines changed

5 files changed

+190
-1
lines changed

api/v2beta1/wms_conversion.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ func V3HubFromV2(src *WMS, target *pdoknlv3.WMS) {
5252
dst := target
5353

5454
dst.ObjectMeta = src.ObjectMeta
55+
if dst.Annotations == nil {
56+
dst.Annotations = make(map[string]string)
57+
}
58+
5559
dst.Annotations[SERVICE_METADATA_IDENTIFIER_ANNOTATION] = src.Spec.Service.MetadataIdentifier
5660

5761
// Set LifeCycle if defined
@@ -322,6 +326,7 @@ func (v2Layer WMSLayer) MapToV3(v2Service WMSService) pdoknlv3.Layer {
322326
BoundingBoxes: []pdoknlv3.WMSBoundingBox{},
323327
MinScaleDenominator: nil,
324328
MaxScaleDenominator: nil,
329+
Visible: &v2Layer.Visible,
325330
}
326331

327332
if v2Layer.SourceMetadataIdentifier != nil {
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/env bash
2+
set -eo pipefail
3+
echo "creating legends for root and group layers by concatenating data layers"
4+
input_filepath="/input/input"
5+
remove_filepath="/input/remove"
6+
config_filepath="/input/ogc-webservice-proxy-config.yaml"
7+
legend_dir="/var/www/legend"
8+
< "${input_filepath}" xargs -n 2 echo | while read -r layer style; do
9+
export layer
10+
# shellcheck disable=SC2016 # dollar is for yq
11+
if ! < "${config_filepath}" yq -e 'env(layer) as $layer | .grouplayers | keys | contains([$layer])' &>/dev/null; then
12+
continue
13+
fi
14+
export grouplayer="${layer}"
15+
grouplayer_style_filepath="${legend_dir}/${grouplayer}/${style}.png"
16+
# shellcheck disable=SC2016 # dollar is for yq
17+
datalayers=$(< "${config_filepath}" yq 'env(grouplayer) as $foo | .grouplayers[$foo][]')
18+
datalayer_style_filepaths=()
19+
for datalayer in $datalayers; do
20+
datalayer_style_filepath="${legend_dir}/${datalayer}/${style}.png"
21+
if [[ -f "${datalayer_style_filepath}" ]]; then
22+
datalayer_style_filepaths+=("${datalayer_style_filepath}")
23+
fi
24+
done
25+
if [[ -n "${datalayer_style_filepaths[*]}" ]]; then
26+
echo "concatenating ${grouplayer_style_filepath}"
27+
gm convert -append "${datalayer_style_filepaths[@]}" "${grouplayer_style_filepath}"
28+
else
29+
echo "no data for ${grouplayer_style_filepath}"
30+
fi
31+
done
32+
< "${remove_filepath}" xargs -n 2 echo | while read -r layer style; do
33+
remove_legend_file="${legend_dir}/${layer}/${style}.png"
34+
echo removing $remove_legend_file
35+
rm $remove_legend_file
36+
done
37+
echo "done"
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package legendgenerator
2+
3+
import (
4+
"fmt"
5+
pdoknlv3 "github.com/pdok/mapserver-operator/api/v3"
6+
"github.com/pdok/mapserver-operator/internal/controller"
7+
smoothoperatorutils "github.com/pdok/smooth-operator/pkg/util"
8+
corev1 "k8s.io/api/core/v1"
9+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
10+
"os"
11+
"sigs.k8s.io/yaml"
12+
"strings"
13+
)
14+
15+
const (
16+
defaultMapserverConf = `CONFIG
17+
ENV
18+
MS_MAP_NO_PATH "true"
19+
END
20+
END
21+
`
22+
)
23+
24+
type LegendReference struct {
25+
Layer string `yaml:"layer" json:"layer"`
26+
Style string `yaml:"style" json:"style"`
27+
}
28+
29+
func getBareConfigMapLegendGenerator(obj metav1.Object) *corev1.ConfigMap {
30+
return &corev1.ConfigMap{
31+
ObjectMeta: metav1.ObjectMeta{
32+
Name: obj.GetName() + "-legend-generator",
33+
Namespace: obj.GetNamespace(),
34+
},
35+
}
36+
}
37+
38+
func GetLegendGeneratorConfigMap(wms *pdoknlv3.WMS) *corev1.ConfigMap {
39+
result := getBareConfigMapLegendGenerator(wms)
40+
labels := controller.AddCommonLabels(wms, smoothoperatorutils.CloneOrEmptyMap(wms.GetLabels()))
41+
result.Labels = labels
42+
43+
result.Immutable = smoothoperatorutils.Pointer(true)
44+
result.Data = map[string]string{}
45+
result.Data["default_mapserver.conf"] = defaultMapserverConf
46+
47+
addLayerInput(wms, result.Data)
48+
49+
if wms.Spec.Options.RewriteGroupToDataLayers != nil && *wms.Spec.Options.RewriteGroupToDataLayers {
50+
addLegendFixerConfig(wms, result.Data)
51+
}
52+
53+
return result
54+
}
55+
56+
func addLayerInput(wms *pdoknlv3.WMS, data map[string]string) {
57+
legendReferences := make([]LegendReference, 0)
58+
59+
if wms.Spec.Service.Layer.Layers != nil {
60+
for _, layer := range *wms.Spec.Service.Layer.Layers {
61+
processLayer(&layer, &legendReferences)
62+
}
63+
}
64+
65+
sb := strings.Builder{}
66+
for _, reference := range legendReferences {
67+
sb.WriteString(fmt.Sprintf("\"%s\" \"%s\"\n", reference.Layer, reference.Style))
68+
}
69+
70+
data["input"] = sb.String()
71+
referencesYaml, err := yaml.Marshal(legendReferences)
72+
if err == nil {
73+
data["input2"] = string(referencesYaml)
74+
}
75+
76+
}
77+
78+
func processLayer(layer *pdoknlv3.Layer, legendReferences *[]LegendReference) {
79+
if layer.Visible == nil || !*layer.Visible {
80+
return
81+
}
82+
for _, style := range layer.Styles {
83+
if style.Legend == nil {
84+
*legendReferences = append(*legendReferences, LegendReference{
85+
Layer: layer.Name,
86+
Style: style.Name,
87+
})
88+
}
89+
}
90+
91+
if layer.Layers != nil {
92+
for _, innerLayer := range *layer.Layers {
93+
processLayer(&innerLayer, legendReferences)
94+
}
95+
}
96+
}
97+
98+
func addLegendFixerConfig(wms *pdoknlv3.WMS, data map[string]string) {
99+
fileBytes, err := os.ReadFile("./legend-fixer.sh")
100+
if err == nil {
101+
data["legend-fixer.sh"] = string(fileBytes)
102+
}
103+
104+
//TODO: Identify middle layers
105+
if wms.Spec.Service.Layer.Layers != nil {
106+
for _, layer := range *wms.Spec.Service.Layer.Layers {
107+
if layer.Layers != nil && len(*layer.Layers) > 0 {
108+
println(layer.Name)
109+
}
110+
}
111+
}
112+
}

internal/controller/legendgenerator/mapper_test.go

Lines changed: 35 additions & 0 deletions
Large diffs are not rendered by default.

internal/controller/shared_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ func AddCommonLabels[O pdoknlv3.WMSWFS](obj O, labels map[string]string) map[str
631631
case *pdoknlv3.WFS:
632632
inspire = any(obj).(*pdoknlv3.WFS).Spec.Service.Inspire != nil
633633
case *pdoknlv3.WMS:
634-
inspire = any(obj).(*pdoknlv3.WFS).Spec.Service.Inspire != nil
634+
inspire = any(obj).(*pdoknlv3.WMS).Spec.Service.Inspire != nil
635635
}
636636

637637
labels["inspire"] = strconv.FormatBool(inspire)

0 commit comments

Comments
 (0)