Mercurial > gemma
changeset 3831:0ffea636d6b0 sld-colors
Prepared class break templating mechanism to work with differences, too. Needs an adjusted template.
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Thu, 04 Jul 2019 11:26:11 +0200 |
parents | 464a6a6e05e6 |
children | cbced21c6d94 |
files | pkg/geoserver/templates.go |
diffstat | 1 files changed, 64 insertions(+), 49 deletions(-) [+] |
line wrap: on
line diff
--- a/pkg/geoserver/templates.go Thu Jul 04 11:03:36 2019 +0200 +++ b/pkg/geoserver/templates.go Thu Jul 04 11:26:11 2019 +0200 @@ -30,21 +30,65 @@ const ( selectClassBreaksSQL = ` SELECT config_val FROM sys_admin.system_config -WHERE config_key = 'morphology_classbreaks'` +WHERE config_key = $1` ) func init() { RegisterStylePreprocessor( "sounding_results_contour_lines_geoserver", - templateSRContourLines) + templateContourLinesFunc("morphology_classbreaks")) + RegisterStylePreprocessor( + "sounding_differences", + templateContourLinesFunc("morphology_classbreaks_compare")) } -type colorClass struct { - value float64 - color color.RGBA +type ( + colorClass struct { + value float64 + color color.RGBA + } + + colorClasses []colorClass + + classBreak struct { + High float64 + HasHigh bool + Low float64 + HasLow bool + color color.RGBA + } +) + +func (cb *classBreak) Color() string { + return fmt.Sprintf("#%02x%02x%02x", + cb.color.R, + cb.color.G, + cb.color.B, + ) } -type colorClasses []colorClass +func (cc colorClasses) toClassBreaks() []classBreak { + + cbs := make([]classBreak, len(cc), len(cc)+1) + for i := range cc { + if i > 0 { + cbs[i].Low = cc[i-1].value + cbs[i].HasLow = true + } + cbs[i].High = cc[i].value + cbs[i].HasHigh = true + cbs[i].color = cc[i].color + } + if len(cc) > 0 { + cbs = append(cbs, classBreak{ + color: cc[len(cc)-1].color, + Low: cc[len(cc)-1].value, + HasLow: true, + }) + } + + return cbs +} func (cc colorClasses) interpolate(v float64) (color.RGBA, bool) { if len(cc) == 0 || v < cc[0].value || v > cc[len(cc)-1].value { @@ -183,7 +227,13 @@ return final, err } -func templateSRContourLines(data string) (string, error) { +func templateContourLinesFunc(configKey string) func(string) (string, error) { + return func(data string) (string, error) { + return templateContourLines(data, configKey) + } +} + +func templateContourLines(data, configKey string) (string, error) { tmpl, err := template.New("template").Parse(data) if err != nil { return "", err @@ -191,7 +241,7 @@ var cb []classBreak - if cb, err = getSRContoursClassBreaks(); err != nil { + if cb, err = countourLinesClassBreaks(configKey); err != nil { return "", err } @@ -202,46 +252,7 @@ return buf.String(), nil } -type classBreak struct { - High float64 - HasHigh bool - Low float64 - HasLow bool - color color.RGBA -} - -func (cb *classBreak) Color() string { - return fmt.Sprintf("#%02x%02x%02x", - cb.color.R, - cb.color.G, - cb.color.B, - ) -} - -func (cc colorClasses) toClassBreaks() []classBreak { - - cbs := make([]classBreak, len(cc), len(cc)+1) - for i := range cc { - if i > 0 { - cbs[i].Low = cc[i-1].value - cbs[i].HasLow = true - } - cbs[i].High = cc[i].value - cbs[i].HasHigh = true - cbs[i].color = cc[i].color - } - if len(cc) > 0 { - cbs = append(cbs, classBreak{ - color: cc[len(cc)-1].color, - Low: cc[len(cc)-1].value, - HasLow: true, - }) - } - - return cbs -} - -func getSRContoursClassBreaks() ([]classBreak, error) { +func countourLinesClassBreaks(configKey string) ([]classBreak, error) { var config string ctx := context.Background() @@ -249,7 +260,11 @@ ctx, "sys_admin", func(conn *sql.Conn) error { - return conn.QueryRowContext(ctx, selectClassBreaksSQL).Scan(&config) + return conn.QueryRowContext( + ctx, + selectClassBreaksSQL, + configKey, + ).Scan(&config) }, ); err != nil { return nil, err