changeset 4325:124a5a7fe8d6

enchange wms styling * Enable opacity in SLD templates by splitting the corresponding `_fill` value. The place of the change is in the handlers which are specfic to geoserver templates. Adding it there keeps the way how opacity is styled consistent with WFS styles towards the default values in the database and the client.
author Bernhard Reiter <bernhard@intevation.de>
date Wed, 04 Sep 2019 15:30:08 +0200
parents 36d384326407
children f2f0b776ba12 b372fbe15300
files pkg/geoserver/templates.go
diffstat 1 files changed, 39 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/pkg/geoserver/templates.go	Wed Sep 04 14:39:51 2019 +0200
+++ b/pkg/geoserver/templates.go	Wed Sep 04 15:30:08 2019 +0200
@@ -11,6 +11,7 @@
 // Author(s):
 //  * Sascha L. Teichmann <sascha.teichmann@intevation.de>
 //  * Markus Kottländer <markus.kottlaender@intevation.de>
+//  * Bernhard Reiter <bernhard.reiter@intevation.de>
 
 package geoserver
 
@@ -18,6 +19,7 @@
 	"context"
 	"database/sql"
 	"regexp"
+	"strconv"
 	"strings"
 	"text/template"
 
@@ -54,6 +56,9 @@
 }
 
 func templateConfigValues(tmplTxt string) (string, error) {
+	// As SLDs cannot handle opacity as hex part of the color setting
+	// we split out the 7-8 chars of keys ending on "_fill" color settings
+	// into keys ending on "_fill_opacity"
 	tmpl, err := template.New("template").Parse(tmplTxt)
 	if err != nil {
 		return "", err
@@ -61,11 +66,45 @@
 
 	// Try to extract the needed keys from the template.
 	keys := extractKeysFromTemplate(tmplTxt)
+
+	// filter out keys ending on "_opacity" and put them in their own slice
+	var opacityKeys []string
+	n := 0
+	for _, key := range keys {
+		if strings.HasSuffix(key, "_opacity") {
+			opacityKeys = append(opacityKeys, key)
+		} else {
+			keys[n] = key
+			n++
+		}
+	}
+	keys = keys[:n]
+
 	kv, err := loadConfigValues(keys)
 	if err != nil {
 		return "", err
 	}
 
+	// if there convert opacity hex value into float between 0-1
+	// otherwise just use 1.0
+	for _, opacityKey := range opacityKeys {
+		fillKey := opacityKey[0 : len(opacityKey)-8]
+		fillValue := kv[fillKey]
+		if fillValue != "" && len(fillValue) == 9 {
+			opacity, err := strconv.ParseInt(fillValue[7:9], 16, 0)
+			if err == nil {
+				kv[opacityKey] = strconv.FormatFloat(
+					float64(opacity)/255, 'f', 2, 64)
+				kv[fillKey] = kv[fillKey][0:7]
+			} else {
+				return "", err
+			}
+
+		} else {
+			kv[opacityKey] = "1.0"
+		}
+	}
+
 	var buf strings.Builder
 	if err = tmpl.Execute(&buf, kv); err != nil {
 		return "", err