view pkg/geoserver/templates.go @ 4017:639bdb17c3f2

Fixed offset for fairway box This was broken by changeset: 4080:bf86f9a08733 user: Thomas Junk <thomas.junk@intevation.de> Date: Thu Jul 18 15:04:30 2019 +0200 summary: improve fairwaydiagram printing positioning For the record: I think the current implementation exceptionally flawed. Instead of adding extra offset parameters to the diagram elements the whole building block with all contained elements should be translated in one step, that would be less cluttered and less error prone...
author Sascha Wilde <wilde@intevation.de>
date Fri, 19 Jul 2019 16:59:25 +0200
parents 3fcc4e11fc00
children 8aff98c84a5a
line wrap: on
line source

// This is Free Software under GNU Affero General Public License v >= 3.0
// without warranty, see README.md and license for details.
//
// SPDX-License-Identifier: AGPL-3.0-or-later
// License-Filename: LICENSES/AGPL-3.0.txt
//
// Copyright (C) 2019 by via donau
//   – Österreichische Wasserstraßen-Gesellschaft mbH
// Software engineering by Intevation GmbH
//
// Author(s):
//  * Sascha L. Teichmann <sascha.teichmann@intevation.de>
//  * Markus Kottländer <markus.kottlaender@intevation.de>

package geoserver

import (
	"context"
	"database/sql"
	"strings"
	"text/template"

	"gemma.intevation.de/gemma/pkg/auth"
	"gemma.intevation.de/gemma/pkg/models"
)

const (
	selectClassBreaksSQL = `
SELECT config_val FROM sys_admin.system_config
WHERE config_key = $1`
)

func init() {
	RegisterStylePreprocessor(
		"sounding_results_contour_lines_geoserver",
		templateContourLinesFunc("morphology_classbreaks"))
	RegisterStylePreprocessor(
		"sounding_differences",
		templateContourLinesFunc("morphology_classbreaks_compare"))
}

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
	}

	var cb []models.ClassBreak

	if cb, err = countourLinesClassBreaks(configKey); err != nil {
		return "", err
	}

	var buf strings.Builder
	if err = tmpl.Execute(&buf, cb); err != nil {
		return "", err
	}
	return buf.String(), nil
}

func countourLinesClassBreaks(configKey string) ([]models.ClassBreak, error) {

	var config string
	ctx := context.Background()
	if err := auth.RunAs(
		ctx,
		"sys_admin",
		func(conn *sql.Conn) error {
			return conn.QueryRowContext(
				ctx,
				selectClassBreaksSQL,
				configKey,
			).Scan(&config)
		},
	); err != nil {
		return nil, err
	}

	cc, err := models.ParseColorValues(config)
	if err != nil {
		return nil, err
	}

	return cc.ClassBreaks(), nil
}