annotate pkg/geoserver/templates.go @ 5694:3bc15e38c7e8 sr-v2

Typo fix
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Mon, 12 Feb 2024 14:43:31 +0100
parents 24156a964eaa
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3826
4b1184fa0326 Added templating and a failing test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
1 // This is Free Software under GNU Affero General Public License v >= 3.0
4b1184fa0326 Added templating and a failing test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
2 // without warranty, see README.md and license for details.
4b1184fa0326 Added templating and a failing test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
3 //
4b1184fa0326 Added templating and a failing test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
4 // SPDX-License-Identifier: AGPL-3.0-or-later
4b1184fa0326 Added templating and a failing test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
5 // License-Filename: LICENSES/AGPL-3.0.txt
4b1184fa0326 Added templating and a failing test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
6 //
3854
3fcc4e11fc00 Validate the config values of the morpho classes when saving. Also don't trigger the expensive re-calculation of the contour lines if only the colors changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3831
diff changeset
7 // Copyright (C) 2019 by via donau
3826
4b1184fa0326 Added templating and a failing test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
8 // – Österreichische Wasserstraßen-Gesellschaft mbH
4b1184fa0326 Added templating and a failing test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
9 // Software engineering by Intevation GmbH
4b1184fa0326 Added templating and a failing test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
10 //
4b1184fa0326 Added templating and a failing test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
11 // Author(s):
4b1184fa0326 Added templating and a failing test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
12 // * Sascha L. Teichmann <sascha.teichmann@intevation.de>
3854
3fcc4e11fc00 Validate the config values of the morpho classes when saving. Also don't trigger the expensive re-calculation of the contour lines if only the colors changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3831
diff changeset
13 // * Markus Kottländer <markus.kottlaender@intevation.de>
4325
124a5a7fe8d6 enchange wms styling
Bernhard Reiter <bernhard@intevation.de>
parents: 4247
diff changeset
14 // * Bernhard Reiter <bernhard.reiter@intevation.de>
3826
4b1184fa0326 Added templating and a failing test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
15
4b1184fa0326 Added templating and a failing test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
16 package geoserver
4b1184fa0326 Added templating and a failing test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
17
4b1184fa0326 Added templating and a failing test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
18 import (
3828
885eb90255ff Cleanup templating of styles [WIP].
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3826
diff changeset
19 "context"
885eb90255ff Cleanup templating of styles [WIP].
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3826
diff changeset
20 "database/sql"
4232
8aff98c84a5a Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3854
diff changeset
21 "regexp"
4325
124a5a7fe8d6 enchange wms styling
Bernhard Reiter <bernhard@intevation.de>
parents: 4247
diff changeset
22 "strconv"
3826
4b1184fa0326 Added templating and a failing test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
23 "strings"
3828
885eb90255ff Cleanup templating of styles [WIP].
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3826
diff changeset
24 "text/template"
885eb90255ff Cleanup templating of styles [WIP].
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3826
diff changeset
25
885eb90255ff Cleanup templating of styles [WIP].
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3826
diff changeset
26 "gemma.intevation.de/gemma/pkg/auth"
3854
3fcc4e11fc00 Validate the config values of the morpho classes when saving. Also don't trigger the expensive re-calculation of the contour lines if only the colors changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3831
diff changeset
27 "gemma.intevation.de/gemma/pkg/models"
3826
4b1184fa0326 Added templating and a failing test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
28 )
4b1184fa0326 Added templating and a failing test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
29
3828
885eb90255ff Cleanup templating of styles [WIP].
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3826
diff changeset
30 const (
4232
8aff98c84a5a Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3854
diff changeset
31 selectConfigValSQL = `
3828
885eb90255ff Cleanup templating of styles [WIP].
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3826
diff changeset
32 SELECT config_val FROM sys_admin.system_config
3831
0ffea636d6b0 Prepared class break templating mechanism to work with differences, too. Needs an adjusted template.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3830
diff changeset
33 WHERE config_key = $1`
3828
885eb90255ff Cleanup templating of styles [WIP].
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3826
diff changeset
34 )
885eb90255ff Cleanup templating of styles [WIP].
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3826
diff changeset
35
885eb90255ff Cleanup templating of styles [WIP].
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3826
diff changeset
36 func init() {
885eb90255ff Cleanup templating of styles [WIP].
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3826
diff changeset
37 RegisterStylePreprocessor(
4579
4c56682f83f7 Adjusted templating for iso areas.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4325
diff changeset
38 "sounding_results_areas_geoserver",
3831
0ffea636d6b0 Prepared class break templating mechanism to work with differences, too. Needs an adjusted template.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3830
diff changeset
39 templateContourLinesFunc("morphology_classbreaks"))
0ffea636d6b0 Prepared class break templating mechanism to work with differences, too. Needs an adjusted template.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3830
diff changeset
40 RegisterStylePreprocessor(
5423
24156a964eaa [WIP] Add support for scan marking points in geoserver.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4584
diff changeset
41 "sounding_results_marking_points_geoserver",
24156a964eaa [WIP] Add support for scan marking points in geoserver.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4584
diff changeset
42 templateContourLinesFunc("morphology_classbreaks"))
24156a964eaa [WIP] Add support for scan marking points in geoserver.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4584
diff changeset
43 RegisterStylePreprocessor(
4584
44b032028e48 Re-enable templating for sounding differences on config changes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4579
diff changeset
44 "sounding_differences",
44b032028e48 Re-enable templating for sounding differences on config changes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4579
diff changeset
45 templateContourLinesFunc("morphology_classbreaks_compare"))
4232
8aff98c84a5a Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3854
diff changeset
46 RegisterStylePreprocessor(
8aff98c84a5a Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3854
diff changeset
47 "distance_marks_geoserver",
8aff98c84a5a Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3854
diff changeset
48 templateConfigValues)
4247
152b9eb5ca47 styles-config: implement styling of WMS-Layers
Fadi Abbud <fadi.abbud@intevation.de>
parents: 4232
diff changeset
49 RegisterStylePreprocessor(
152b9eb5ca47 styles-config: implement styling of WMS-Layers
Fadi Abbud <fadi.abbud@intevation.de>
parents: 4232
diff changeset
50 "distance_marks_ashore_geoserver",
152b9eb5ca47 styles-config: implement styling of WMS-Layers
Fadi Abbud <fadi.abbud@intevation.de>
parents: 4232
diff changeset
51 templateConfigValues)
152b9eb5ca47 styles-config: implement styling of WMS-Layers
Fadi Abbud <fadi.abbud@intevation.de>
parents: 4232
diff changeset
52 RegisterStylePreprocessor(
152b9eb5ca47 styles-config: implement styling of WMS-Layers
Fadi Abbud <fadi.abbud@intevation.de>
parents: 4232
diff changeset
53 "waterway_area",
152b9eb5ca47 styles-config: implement styling of WMS-Layers
Fadi Abbud <fadi.abbud@intevation.de>
parents: 4232
diff changeset
54 templateConfigValues)
152b9eb5ca47 styles-config: implement styling of WMS-Layers
Fadi Abbud <fadi.abbud@intevation.de>
parents: 4232
diff changeset
55 RegisterStylePreprocessor(
152b9eb5ca47 styles-config: implement styling of WMS-Layers
Fadi Abbud <fadi.abbud@intevation.de>
parents: 4232
diff changeset
56 "waterway_axis",
152b9eb5ca47 styles-config: implement styling of WMS-Layers
Fadi Abbud <fadi.abbud@intevation.de>
parents: 4232
diff changeset
57 templateConfigValues)
4232
8aff98c84a5a Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3854
diff changeset
58 // TODO: Add more layers.
8aff98c84a5a Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3854
diff changeset
59 }
8aff98c84a5a Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3854
diff changeset
60
8aff98c84a5a Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3854
diff changeset
61 func templateConfigValues(tmplTxt string) (string, error) {
4325
124a5a7fe8d6 enchange wms styling
Bernhard Reiter <bernhard@intevation.de>
parents: 4247
diff changeset
62 // As SLDs cannot handle opacity as hex part of the color setting
124a5a7fe8d6 enchange wms styling
Bernhard Reiter <bernhard@intevation.de>
parents: 4247
diff changeset
63 // we split out the 7-8 chars of keys ending on "_fill" color settings
124a5a7fe8d6 enchange wms styling
Bernhard Reiter <bernhard@intevation.de>
parents: 4247
diff changeset
64 // into keys ending on "_fill_opacity"
4232
8aff98c84a5a Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3854
diff changeset
65 tmpl, err := template.New("template").Parse(tmplTxt)
8aff98c84a5a Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3854
diff changeset
66 if err != nil {
8aff98c84a5a Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3854
diff changeset
67 return "", err
8aff98c84a5a Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3854
diff changeset
68 }
8aff98c84a5a Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3854
diff changeset
69
8aff98c84a5a Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3854
diff changeset
70 // Try to extract the needed keys from the template.
8aff98c84a5a Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3854
diff changeset
71 keys := extractKeysFromTemplate(tmplTxt)
4325
124a5a7fe8d6 enchange wms styling
Bernhard Reiter <bernhard@intevation.de>
parents: 4247
diff changeset
72
124a5a7fe8d6 enchange wms styling
Bernhard Reiter <bernhard@intevation.de>
parents: 4247
diff changeset
73 // filter out keys ending on "_opacity" and put them in their own slice
124a5a7fe8d6 enchange wms styling
Bernhard Reiter <bernhard@intevation.de>
parents: 4247
diff changeset
74 var opacityKeys []string
124a5a7fe8d6 enchange wms styling
Bernhard Reiter <bernhard@intevation.de>
parents: 4247
diff changeset
75 n := 0
124a5a7fe8d6 enchange wms styling
Bernhard Reiter <bernhard@intevation.de>
parents: 4247
diff changeset
76 for _, key := range keys {
124a5a7fe8d6 enchange wms styling
Bernhard Reiter <bernhard@intevation.de>
parents: 4247
diff changeset
77 if strings.HasSuffix(key, "_opacity") {
124a5a7fe8d6 enchange wms styling
Bernhard Reiter <bernhard@intevation.de>
parents: 4247
diff changeset
78 opacityKeys = append(opacityKeys, key)
124a5a7fe8d6 enchange wms styling
Bernhard Reiter <bernhard@intevation.de>
parents: 4247
diff changeset
79 } else {
124a5a7fe8d6 enchange wms styling
Bernhard Reiter <bernhard@intevation.de>
parents: 4247
diff changeset
80 keys[n] = key
124a5a7fe8d6 enchange wms styling
Bernhard Reiter <bernhard@intevation.de>
parents: 4247
diff changeset
81 n++
124a5a7fe8d6 enchange wms styling
Bernhard Reiter <bernhard@intevation.de>
parents: 4247
diff changeset
82 }
124a5a7fe8d6 enchange wms styling
Bernhard Reiter <bernhard@intevation.de>
parents: 4247
diff changeset
83 }
124a5a7fe8d6 enchange wms styling
Bernhard Reiter <bernhard@intevation.de>
parents: 4247
diff changeset
84 keys = keys[:n]
124a5a7fe8d6 enchange wms styling
Bernhard Reiter <bernhard@intevation.de>
parents: 4247
diff changeset
85
4232
8aff98c84a5a Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3854
diff changeset
86 kv, err := loadConfigValues(keys)
8aff98c84a5a Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3854
diff changeset
87 if err != nil {
8aff98c84a5a Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3854
diff changeset
88 return "", err
8aff98c84a5a Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3854
diff changeset
89 }
8aff98c84a5a Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3854
diff changeset
90
4325
124a5a7fe8d6 enchange wms styling
Bernhard Reiter <bernhard@intevation.de>
parents: 4247
diff changeset
91 // if there convert opacity hex value into float between 0-1
124a5a7fe8d6 enchange wms styling
Bernhard Reiter <bernhard@intevation.de>
parents: 4247
diff changeset
92 // otherwise just use 1.0
124a5a7fe8d6 enchange wms styling
Bernhard Reiter <bernhard@intevation.de>
parents: 4247
diff changeset
93 for _, opacityKey := range opacityKeys {
124a5a7fe8d6 enchange wms styling
Bernhard Reiter <bernhard@intevation.de>
parents: 4247
diff changeset
94 fillKey := opacityKey[0 : len(opacityKey)-8]
124a5a7fe8d6 enchange wms styling
Bernhard Reiter <bernhard@intevation.de>
parents: 4247
diff changeset
95 fillValue := kv[fillKey]
124a5a7fe8d6 enchange wms styling
Bernhard Reiter <bernhard@intevation.de>
parents: 4247
diff changeset
96 if fillValue != "" && len(fillValue) == 9 {
124a5a7fe8d6 enchange wms styling
Bernhard Reiter <bernhard@intevation.de>
parents: 4247
diff changeset
97 opacity, err := strconv.ParseInt(fillValue[7:9], 16, 0)
124a5a7fe8d6 enchange wms styling
Bernhard Reiter <bernhard@intevation.de>
parents: 4247
diff changeset
98 if err == nil {
124a5a7fe8d6 enchange wms styling
Bernhard Reiter <bernhard@intevation.de>
parents: 4247
diff changeset
99 kv[opacityKey] = strconv.FormatFloat(
124a5a7fe8d6 enchange wms styling
Bernhard Reiter <bernhard@intevation.de>
parents: 4247
diff changeset
100 float64(opacity)/255, 'f', 2, 64)
124a5a7fe8d6 enchange wms styling
Bernhard Reiter <bernhard@intevation.de>
parents: 4247
diff changeset
101 kv[fillKey] = kv[fillKey][0:7]
124a5a7fe8d6 enchange wms styling
Bernhard Reiter <bernhard@intevation.de>
parents: 4247
diff changeset
102 } else {
124a5a7fe8d6 enchange wms styling
Bernhard Reiter <bernhard@intevation.de>
parents: 4247
diff changeset
103 return "", err
124a5a7fe8d6 enchange wms styling
Bernhard Reiter <bernhard@intevation.de>
parents: 4247
diff changeset
104 }
124a5a7fe8d6 enchange wms styling
Bernhard Reiter <bernhard@intevation.de>
parents: 4247
diff changeset
105
124a5a7fe8d6 enchange wms styling
Bernhard Reiter <bernhard@intevation.de>
parents: 4247
diff changeset
106 } else {
124a5a7fe8d6 enchange wms styling
Bernhard Reiter <bernhard@intevation.de>
parents: 4247
diff changeset
107 kv[opacityKey] = "1.0"
124a5a7fe8d6 enchange wms styling
Bernhard Reiter <bernhard@intevation.de>
parents: 4247
diff changeset
108 }
124a5a7fe8d6 enchange wms styling
Bernhard Reiter <bernhard@intevation.de>
parents: 4247
diff changeset
109 }
124a5a7fe8d6 enchange wms styling
Bernhard Reiter <bernhard@intevation.de>
parents: 4247
diff changeset
110
4232
8aff98c84a5a Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3854
diff changeset
111 var buf strings.Builder
8aff98c84a5a Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3854
diff changeset
112 if err = tmpl.Execute(&buf, kv); err != nil {
8aff98c84a5a Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3854
diff changeset
113 return "", err
8aff98c84a5a Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3854
diff changeset
114 }
8aff98c84a5a Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3854
diff changeset
115 return buf.String(), nil
8aff98c84a5a Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3854
diff changeset
116
8aff98c84a5a Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3854
diff changeset
117 }
8aff98c84a5a Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3854
diff changeset
118
8aff98c84a5a Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3854
diff changeset
119 // TODO: Use the parse tree of the template to extract keys.
8aff98c84a5a Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3854
diff changeset
120 var findKeysRe = regexp.MustCompile(`{{[-]?\s*\.([^-.\s]+)\s*[-]?}}`)
8aff98c84a5a Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3854
diff changeset
121
8aff98c84a5a Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3854
diff changeset
122 func extractKeysFromTemplate(tmpl string) []string {
8aff98c84a5a Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3854
diff changeset
123 parts := findKeysRe.FindAllStringSubmatch(tmpl, -1)
8aff98c84a5a Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3854
diff changeset
124 keys := make(map[string]struct{})
8aff98c84a5a Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3854
diff changeset
125 for _, part := range parts {
8aff98c84a5a Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3854
diff changeset
126 keys[part[1]] = struct{}{}
8aff98c84a5a Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3854
diff changeset
127 }
8aff98c84a5a Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3854
diff changeset
128 out := make([]string, len(keys))
8aff98c84a5a Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3854
diff changeset
129 var i int
8aff98c84a5a Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3854
diff changeset
130 for key := range keys {
8aff98c84a5a Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3854
diff changeset
131 out[i] = key
8aff98c84a5a Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3854
diff changeset
132 i++
8aff98c84a5a Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3854
diff changeset
133 }
8aff98c84a5a Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3854
diff changeset
134 return out
8aff98c84a5a Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3854
diff changeset
135 }
8aff98c84a5a Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3854
diff changeset
136
8aff98c84a5a Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3854
diff changeset
137 func loadConfigValues(keys []string) (map[string]string, error) {
8aff98c84a5a Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3854
diff changeset
138 kv := make(map[string]string, len(keys))
8aff98c84a5a Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3854
diff changeset
139 if len(keys) == 0 {
8aff98c84a5a Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3854
diff changeset
140 return kv, nil
8aff98c84a5a Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3854
diff changeset
141 }
8aff98c84a5a Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3854
diff changeset
142 ctx := context.Background()
8aff98c84a5a Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3854
diff changeset
143 if err := auth.RunAs(
8aff98c84a5a Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3854
diff changeset
144 ctx,
8aff98c84a5a Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3854
diff changeset
145 "sys_admin",
8aff98c84a5a Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3854
diff changeset
146 func(conn *sql.Conn) error {
8aff98c84a5a Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3854
diff changeset
147 stmt, err := conn.PrepareContext(ctx, selectConfigValSQL)
8aff98c84a5a Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3854
diff changeset
148 if err != nil {
8aff98c84a5a Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3854
diff changeset
149 return err
8aff98c84a5a Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3854
diff changeset
150 }
8aff98c84a5a Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3854
diff changeset
151 defer stmt.Close()
8aff98c84a5a Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3854
diff changeset
152 for _, key := range keys {
8aff98c84a5a Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3854
diff changeset
153 var val string
8aff98c84a5a Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3854
diff changeset
154 if err := stmt.QueryRowContext(ctx, key).Scan(&val); err != nil {
8aff98c84a5a Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3854
diff changeset
155 return err
8aff98c84a5a Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3854
diff changeset
156 }
8aff98c84a5a Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3854
diff changeset
157 kv[key] = val
8aff98c84a5a Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3854
diff changeset
158 }
8aff98c84a5a Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3854
diff changeset
159 return nil
8aff98c84a5a Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3854
diff changeset
160 },
8aff98c84a5a Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3854
diff changeset
161 ); err != nil {
8aff98c84a5a Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3854
diff changeset
162 return nil, err
8aff98c84a5a Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3854
diff changeset
163 }
8aff98c84a5a Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3854
diff changeset
164
8aff98c84a5a Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3854
diff changeset
165 return kv, nil
3828
885eb90255ff Cleanup templating of styles [WIP].
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3826
diff changeset
166 }
885eb90255ff Cleanup templating of styles [WIP].
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3826
diff changeset
167
3831
0ffea636d6b0 Prepared class break templating mechanism to work with differences, too. Needs an adjusted template.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3830
diff changeset
168 func templateContourLinesFunc(configKey string) func(string) (string, error) {
0ffea636d6b0 Prepared class break templating mechanism to work with differences, too. Needs an adjusted template.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3830
diff changeset
169 return func(data string) (string, error) {
0ffea636d6b0 Prepared class break templating mechanism to work with differences, too. Needs an adjusted template.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3830
diff changeset
170 return templateContourLines(data, configKey)
0ffea636d6b0 Prepared class break templating mechanism to work with differences, too. Needs an adjusted template.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3830
diff changeset
171 }
0ffea636d6b0 Prepared class break templating mechanism to work with differences, too. Needs an adjusted template.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3830
diff changeset
172 }
0ffea636d6b0 Prepared class break templating mechanism to work with differences, too. Needs an adjusted template.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3830
diff changeset
173
0ffea636d6b0 Prepared class break templating mechanism to work with differences, too. Needs an adjusted template.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3830
diff changeset
174 func templateContourLines(data, configKey string) (string, error) {
3828
885eb90255ff Cleanup templating of styles [WIP].
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3826
diff changeset
175 tmpl, err := template.New("template").Parse(data)
885eb90255ff Cleanup templating of styles [WIP].
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3826
diff changeset
176 if err != nil {
885eb90255ff Cleanup templating of styles [WIP].
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3826
diff changeset
177 return "", err
885eb90255ff Cleanup templating of styles [WIP].
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3826
diff changeset
178 }
885eb90255ff Cleanup templating of styles [WIP].
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3826
diff changeset
179
3854
3fcc4e11fc00 Validate the config values of the morpho classes when saving. Also don't trigger the expensive re-calculation of the contour lines if only the colors changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3831
diff changeset
180 var cb []models.ClassBreak
3828
885eb90255ff Cleanup templating of styles [WIP].
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3826
diff changeset
181
3831
0ffea636d6b0 Prepared class break templating mechanism to work with differences, too. Needs an adjusted template.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3830
diff changeset
182 if cb, err = countourLinesClassBreaks(configKey); err != nil {
3828
885eb90255ff Cleanup templating of styles [WIP].
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3826
diff changeset
183 return "", err
885eb90255ff Cleanup templating of styles [WIP].
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3826
diff changeset
184 }
885eb90255ff Cleanup templating of styles [WIP].
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3826
diff changeset
185
885eb90255ff Cleanup templating of styles [WIP].
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3826
diff changeset
186 var buf strings.Builder
885eb90255ff Cleanup templating of styles [WIP].
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3826
diff changeset
187 if err = tmpl.Execute(&buf, cb); err != nil {
885eb90255ff Cleanup templating of styles [WIP].
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3826
diff changeset
188 return "", err
885eb90255ff Cleanup templating of styles [WIP].
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3826
diff changeset
189 }
885eb90255ff Cleanup templating of styles [WIP].
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3826
diff changeset
190 return buf.String(), nil
885eb90255ff Cleanup templating of styles [WIP].
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3826
diff changeset
191 }
885eb90255ff Cleanup templating of styles [WIP].
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3826
diff changeset
192
3854
3fcc4e11fc00 Validate the config values of the morpho classes when saving. Also don't trigger the expensive re-calculation of the contour lines if only the colors changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3831
diff changeset
193 func countourLinesClassBreaks(configKey string) ([]models.ClassBreak, error) {
3828
885eb90255ff Cleanup templating of styles [WIP].
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3826
diff changeset
194
885eb90255ff Cleanup templating of styles [WIP].
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3826
diff changeset
195 var config string
885eb90255ff Cleanup templating of styles [WIP].
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3826
diff changeset
196 ctx := context.Background()
885eb90255ff Cleanup templating of styles [WIP].
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3826
diff changeset
197 if err := auth.RunAs(
885eb90255ff Cleanup templating of styles [WIP].
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3826
diff changeset
198 ctx,
885eb90255ff Cleanup templating of styles [WIP].
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3826
diff changeset
199 "sys_admin",
885eb90255ff Cleanup templating of styles [WIP].
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3826
diff changeset
200 func(conn *sql.Conn) error {
3831
0ffea636d6b0 Prepared class break templating mechanism to work with differences, too. Needs an adjusted template.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3830
diff changeset
201 return conn.QueryRowContext(
0ffea636d6b0 Prepared class break templating mechanism to work with differences, too. Needs an adjusted template.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3830
diff changeset
202 ctx,
4232
8aff98c84a5a Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3854
diff changeset
203 selectConfigValSQL,
3831
0ffea636d6b0 Prepared class break templating mechanism to work with differences, too. Needs an adjusted template.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3830
diff changeset
204 configKey,
0ffea636d6b0 Prepared class break templating mechanism to work with differences, too. Needs an adjusted template.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3830
diff changeset
205 ).Scan(&config)
3828
885eb90255ff Cleanup templating of styles [WIP].
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3826
diff changeset
206 },
885eb90255ff Cleanup templating of styles [WIP].
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3826
diff changeset
207 ); err != nil {
885eb90255ff Cleanup templating of styles [WIP].
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3826
diff changeset
208 return nil, err
885eb90255ff Cleanup templating of styles [WIP].
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3826
diff changeset
209 }
885eb90255ff Cleanup templating of styles [WIP].
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3826
diff changeset
210
3854
3fcc4e11fc00 Validate the config values of the morpho classes when saving. Also don't trigger the expensive re-calculation of the contour lines if only the colors changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3831
diff changeset
211 cc, err := models.ParseColorValues(config)
3828
885eb90255ff Cleanup templating of styles [WIP].
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3826
diff changeset
212 if err != nil {
885eb90255ff Cleanup templating of styles [WIP].
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3826
diff changeset
213 return nil, err
885eb90255ff Cleanup templating of styles [WIP].
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3826
diff changeset
214 }
885eb90255ff Cleanup templating of styles [WIP].
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3826
diff changeset
215
3854
3fcc4e11fc00 Validate the config values of the morpho classes when saving. Also don't trigger the expensive re-calculation of the contour lines if only the colors changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3831
diff changeset
216 return cc.ClassBreaks(), nil
3828
885eb90255ff Cleanup templating of styles [WIP].
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3826
diff changeset
217 }