view pkg/controllers/search.go @ 935:430d52c2f6ef

client: move layer isolines to be drawn at the top * Move layer isolones to be drawn last (and thus being "on top") so that the bottleneck (position) layer will not interfere that much with the colours. It also allows to set a white background with high opacity on the bottleneck polygon in order to get highly visible isolines.
author Bernhard Reiter <bernhard@intevation.de>
date Mon, 08 Oct 2018 17:20:42 +0200
parents 5616c6bfb186
children e03fab882b47
line wrap: on
line source

package controllers

import (
	"database/sql"
	"net/http"
	"regexp"
	"strconv"
	"strings"

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

const (
	searchHectometreSQL = `SELECT COALESCE(json_agg(r),'[]')
FROM (SELECT (location_code).hectometre || ' rhm' AS name,
             ST_AsGeoJSON(geom)::json AS geom
      FROM waterway.distance_marks
      WHERE (location_code).hectometre = $1) r`
	searchBottleneckSQL = `SELECT COALESCE(json_agg(r),'[]')
FROM (SELECT objnam AS name,
             ST_AsGeoJSON(ST_Centroid(area))::json AS geom
      FROM waterway.bottlenecks
      WHERE objnam ILIKE $1) r`
)

var rkmRegex = regexp.MustCompile(
	"^[[:space:]]*([0-9]+)([,.]([0-9]))?[[:space:]]*$",
)

func searchFeature(
	input interface{},
	req *http.Request,
	db *sql.Conn,
) (jr JSONResult, err error) {

	s := input.(*models.SearchRequest)

	if len(s.SearchString) == 0 {
		err = JSONError{http.StatusBadRequest,
			"error: empty search string"}
		return
	}

	var result string

	m := rkmRegex.FindStringSubmatch(s.SearchString)
	if len(m) != 0 {
		// Handle search for river kilometre:
		var hectometre int
		if hectometre, err = strconv.Atoi(m[1]); err != nil {
			return
		}

		hectometre *= 10
		if m[3] != "" {
			var h int
			if h, err = strconv.Atoi(m[3]); err != nil {
				return
			}
			hectometre += h
		}

		err = db.QueryRowContext(
			req.Context(),
			searchHectometreSQL,
			hectometre,
		).Scan(&result)
	} else {
		// Hande search for bottlencks:
		err = db.QueryRowContext(
			req.Context(),
			searchBottleneckSQL,
			"%"+s.SearchString+"%",
		).Scan(&result)
	}

	if err != nil {
		return
	}

	jr.Result = strings.NewReader(result)

	return
}