Mercurial > gemma
annotate pkg/geoserver/templates.go @ 4768:a2f16bbcc846 direct-diff
Morph differences: Directly raster A and subtract B as a raster.
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Mon, 21 Oct 2019 02:01:56 +0200 |
parents | 44b032028e48 |
children | 24156a964eaa |
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( |
4584
44b032028e48
Re-enable templating for sounding differences on config changes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4579
diff
changeset
|
41 "sounding_differences", |
44b032028e48
Re-enable templating for sounding differences on config changes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4579
diff
changeset
|
42 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
|
43 RegisterStylePreprocessor( |
8aff98c84a5a
Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3854
diff
changeset
|
44 "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
|
45 templateConfigValues) |
4247
152b9eb5ca47
styles-config: implement styling of WMS-Layers
Fadi Abbud <fadi.abbud@intevation.de>
parents:
4232
diff
changeset
|
46 RegisterStylePreprocessor( |
152b9eb5ca47
styles-config: implement styling of WMS-Layers
Fadi Abbud <fadi.abbud@intevation.de>
parents:
4232
diff
changeset
|
47 "distance_marks_ashore_geoserver", |
152b9eb5ca47
styles-config: implement styling of WMS-Layers
Fadi Abbud <fadi.abbud@intevation.de>
parents:
4232
diff
changeset
|
48 templateConfigValues) |
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 "waterway_area", |
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_axis", |
152b9eb5ca47
styles-config: implement styling of WMS-Layers
Fadi Abbud <fadi.abbud@intevation.de>
parents:
4232
diff
changeset
|
54 templateConfigValues) |
4232
8aff98c84a5a
Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3854
diff
changeset
|
55 // 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
|
56 } |
8aff98c84a5a
Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3854
diff
changeset
|
57 |
8aff98c84a5a
Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3854
diff
changeset
|
58 func templateConfigValues(tmplTxt string) (string, error) { |
4325
124a5a7fe8d6
enchange wms styling
Bernhard Reiter <bernhard@intevation.de>
parents:
4247
diff
changeset
|
59 // 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
|
60 // 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
|
61 // 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
|
62 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
|
63 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
|
64 return "", err |
8aff98c84a5a
Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3854
diff
changeset
|
65 } |
8aff98c84a5a
Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3854
diff
changeset
|
66 |
8aff98c84a5a
Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3854
diff
changeset
|
67 // 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
|
68 keys := extractKeysFromTemplate(tmplTxt) |
4325
124a5a7fe8d6
enchange wms styling
Bernhard Reiter <bernhard@intevation.de>
parents:
4247
diff
changeset
|
69 |
124a5a7fe8d6
enchange wms styling
Bernhard Reiter <bernhard@intevation.de>
parents:
4247
diff
changeset
|
70 // 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
|
71 var opacityKeys []string |
124a5a7fe8d6
enchange wms styling
Bernhard Reiter <bernhard@intevation.de>
parents:
4247
diff
changeset
|
72 n := 0 |
124a5a7fe8d6
enchange wms styling
Bernhard Reiter <bernhard@intevation.de>
parents:
4247
diff
changeset
|
73 for _, key := range keys { |
124a5a7fe8d6
enchange wms styling
Bernhard Reiter <bernhard@intevation.de>
parents:
4247
diff
changeset
|
74 if strings.HasSuffix(key, "_opacity") { |
124a5a7fe8d6
enchange wms styling
Bernhard Reiter <bernhard@intevation.de>
parents:
4247
diff
changeset
|
75 opacityKeys = append(opacityKeys, key) |
124a5a7fe8d6
enchange wms styling
Bernhard Reiter <bernhard@intevation.de>
parents:
4247
diff
changeset
|
76 } else { |
124a5a7fe8d6
enchange wms styling
Bernhard Reiter <bernhard@intevation.de>
parents:
4247
diff
changeset
|
77 keys[n] = key |
124a5a7fe8d6
enchange wms styling
Bernhard Reiter <bernhard@intevation.de>
parents:
4247
diff
changeset
|
78 n++ |
124a5a7fe8d6
enchange wms styling
Bernhard Reiter <bernhard@intevation.de>
parents:
4247
diff
changeset
|
79 } |
124a5a7fe8d6
enchange wms styling
Bernhard Reiter <bernhard@intevation.de>
parents:
4247
diff
changeset
|
80 } |
124a5a7fe8d6
enchange wms styling
Bernhard Reiter <bernhard@intevation.de>
parents:
4247
diff
changeset
|
81 keys = keys[:n] |
124a5a7fe8d6
enchange wms styling
Bernhard Reiter <bernhard@intevation.de>
parents:
4247
diff
changeset
|
82 |
4232
8aff98c84a5a
Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3854
diff
changeset
|
83 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
|
84 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
|
85 return "", err |
8aff98c84a5a
Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3854
diff
changeset
|
86 } |
8aff98c84a5a
Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3854
diff
changeset
|
87 |
4325
124a5a7fe8d6
enchange wms styling
Bernhard Reiter <bernhard@intevation.de>
parents:
4247
diff
changeset
|
88 // if there convert opacity hex value into float between 0-1 |
124a5a7fe8d6
enchange wms styling
Bernhard Reiter <bernhard@intevation.de>
parents:
4247
diff
changeset
|
89 // otherwise just use 1.0 |
124a5a7fe8d6
enchange wms styling
Bernhard Reiter <bernhard@intevation.de>
parents:
4247
diff
changeset
|
90 for _, opacityKey := range opacityKeys { |
124a5a7fe8d6
enchange wms styling
Bernhard Reiter <bernhard@intevation.de>
parents:
4247
diff
changeset
|
91 fillKey := opacityKey[0 : len(opacityKey)-8] |
124a5a7fe8d6
enchange wms styling
Bernhard Reiter <bernhard@intevation.de>
parents:
4247
diff
changeset
|
92 fillValue := kv[fillKey] |
124a5a7fe8d6
enchange wms styling
Bernhard Reiter <bernhard@intevation.de>
parents:
4247
diff
changeset
|
93 if fillValue != "" && len(fillValue) == 9 { |
124a5a7fe8d6
enchange wms styling
Bernhard Reiter <bernhard@intevation.de>
parents:
4247
diff
changeset
|
94 opacity, err := strconv.ParseInt(fillValue[7:9], 16, 0) |
124a5a7fe8d6
enchange wms styling
Bernhard Reiter <bernhard@intevation.de>
parents:
4247
diff
changeset
|
95 if err == nil { |
124a5a7fe8d6
enchange wms styling
Bernhard Reiter <bernhard@intevation.de>
parents:
4247
diff
changeset
|
96 kv[opacityKey] = strconv.FormatFloat( |
124a5a7fe8d6
enchange wms styling
Bernhard Reiter <bernhard@intevation.de>
parents:
4247
diff
changeset
|
97 float64(opacity)/255, 'f', 2, 64) |
124a5a7fe8d6
enchange wms styling
Bernhard Reiter <bernhard@intevation.de>
parents:
4247
diff
changeset
|
98 kv[fillKey] = kv[fillKey][0:7] |
124a5a7fe8d6
enchange wms styling
Bernhard Reiter <bernhard@intevation.de>
parents:
4247
diff
changeset
|
99 } else { |
124a5a7fe8d6
enchange wms styling
Bernhard Reiter <bernhard@intevation.de>
parents:
4247
diff
changeset
|
100 return "", err |
124a5a7fe8d6
enchange wms styling
Bernhard Reiter <bernhard@intevation.de>
parents:
4247
diff
changeset
|
101 } |
124a5a7fe8d6
enchange wms styling
Bernhard Reiter <bernhard@intevation.de>
parents:
4247
diff
changeset
|
102 |
124a5a7fe8d6
enchange wms styling
Bernhard Reiter <bernhard@intevation.de>
parents:
4247
diff
changeset
|
103 } else { |
124a5a7fe8d6
enchange wms styling
Bernhard Reiter <bernhard@intevation.de>
parents:
4247
diff
changeset
|
104 kv[opacityKey] = "1.0" |
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 } |
124a5a7fe8d6
enchange wms styling
Bernhard Reiter <bernhard@intevation.de>
parents:
4247
diff
changeset
|
107 |
4232
8aff98c84a5a
Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3854
diff
changeset
|
108 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
|
109 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
|
110 return "", err |
8aff98c84a5a
Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3854
diff
changeset
|
111 } |
8aff98c84a5a
Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3854
diff
changeset
|
112 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
|
113 |
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 |
8aff98c84a5a
Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3854
diff
changeset
|
116 // 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
|
117 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
|
118 |
8aff98c84a5a
Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3854
diff
changeset
|
119 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
|
120 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
|
121 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
|
122 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
|
123 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
|
124 } |
8aff98c84a5a
Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3854
diff
changeset
|
125 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
|
126 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
|
127 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
|
128 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
|
129 i++ |
8aff98c84a5a
Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3854
diff
changeset
|
130 } |
8aff98c84a5a
Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3854
diff
changeset
|
131 return out |
8aff98c84a5a
Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3854
diff
changeset
|
132 } |
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 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
|
135 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
|
136 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
|
137 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
|
138 } |
8aff98c84a5a
Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3854
diff
changeset
|
139 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
|
140 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
|
141 ctx, |
8aff98c84a5a
Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3854
diff
changeset
|
142 "sys_admin", |
8aff98c84a5a
Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3854
diff
changeset
|
143 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
|
144 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
|
145 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
|
146 return err |
8aff98c84a5a
Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3854
diff
changeset
|
147 } |
8aff98c84a5a
Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3854
diff
changeset
|
148 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
|
149 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
|
150 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
|
151 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
|
152 return err |
8aff98c84a5a
Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3854
diff
changeset
|
153 } |
8aff98c84a5a
Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3854
diff
changeset
|
154 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
|
155 } |
8aff98c84a5a
Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3854
diff
changeset
|
156 return nil |
8aff98c84a5a
Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3854
diff
changeset
|
157 }, |
8aff98c84a5a
Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3854
diff
changeset
|
158 ); err != nil { |
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, err |
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 |
8aff98c84a5a
Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3854
diff
changeset
|
162 return kv, nil |
3828
885eb90255ff
Cleanup templating of styles [WIP].
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3826
diff
changeset
|
163 } |
885eb90255ff
Cleanup templating of styles [WIP].
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3826
diff
changeset
|
164 |
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
|
165 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
|
166 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
|
167 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
|
168 } |
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 } |
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 |
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 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
|
172 tmpl, err := template.New("template").Parse(data) |
885eb90255ff
Cleanup templating of styles [WIP].
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3826
diff
changeset
|
173 if err != nil { |
885eb90255ff
Cleanup templating of styles [WIP].
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3826
diff
changeset
|
174 return "", err |
885eb90255ff
Cleanup templating of styles [WIP].
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3826
diff
changeset
|
175 } |
885eb90255ff
Cleanup templating of styles [WIP].
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3826
diff
changeset
|
176 |
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
|
177 var cb []models.ClassBreak |
3828
885eb90255ff
Cleanup templating of styles [WIP].
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3826
diff
changeset
|
178 |
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
|
179 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
|
180 return "", err |
885eb90255ff
Cleanup templating of styles [WIP].
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3826
diff
changeset
|
181 } |
885eb90255ff
Cleanup templating of styles [WIP].
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3826
diff
changeset
|
182 |
885eb90255ff
Cleanup templating of styles [WIP].
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3826
diff
changeset
|
183 var buf strings.Builder |
885eb90255ff
Cleanup templating of styles [WIP].
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3826
diff
changeset
|
184 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
|
185 return "", err |
885eb90255ff
Cleanup templating of styles [WIP].
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3826
diff
changeset
|
186 } |
885eb90255ff
Cleanup templating of styles [WIP].
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3826
diff
changeset
|
187 return buf.String(), nil |
885eb90255ff
Cleanup templating of styles [WIP].
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3826
diff
changeset
|
188 } |
885eb90255ff
Cleanup templating of styles [WIP].
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3826
diff
changeset
|
189 |
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
|
190 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
|
191 |
885eb90255ff
Cleanup templating of styles [WIP].
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3826
diff
changeset
|
192 var config string |
885eb90255ff
Cleanup templating of styles [WIP].
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3826
diff
changeset
|
193 ctx := context.Background() |
885eb90255ff
Cleanup templating of styles [WIP].
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3826
diff
changeset
|
194 if err := auth.RunAs( |
885eb90255ff
Cleanup templating of styles [WIP].
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3826
diff
changeset
|
195 ctx, |
885eb90255ff
Cleanup templating of styles [WIP].
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3826
diff
changeset
|
196 "sys_admin", |
885eb90255ff
Cleanup templating of styles [WIP].
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3826
diff
changeset
|
197 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
|
198 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
|
199 ctx, |
4232
8aff98c84a5a
Example how to style distance_marks WMS layer with templating.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3854
diff
changeset
|
200 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
|
201 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
|
202 ).Scan(&config) |
3828
885eb90255ff
Cleanup templating of styles [WIP].
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3826
diff
changeset
|
203 }, |
885eb90255ff
Cleanup templating of styles [WIP].
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3826
diff
changeset
|
204 ); err != nil { |
885eb90255ff
Cleanup templating of styles [WIP].
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3826
diff
changeset
|
205 return nil, err |
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 |
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
|
208 cc, err := models.ParseColorValues(config) |
3828
885eb90255ff
Cleanup templating of styles [WIP].
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3826
diff
changeset
|
209 if err != nil { |
885eb90255ff
Cleanup templating of styles [WIP].
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3826
diff
changeset
|
210 return nil, err |
885eb90255ff
Cleanup templating of styles [WIP].
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3826
diff
changeset
|
211 } |
885eb90255ff
Cleanup templating of styles [WIP].
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3826
diff
changeset
|
212 |
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
|
213 return cc.ClassBreaks(), nil |
3828
885eb90255ff
Cleanup templating of styles [WIP].
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3826
diff
changeset
|
214 } |