Mercurial > gemma
view pkg/controllers/search.go @ 928:0a0013fbda4a geo-style
octree2contour: Merge multi linestrings in user land before sending them to database.
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Mon, 08 Oct 2018 13:09:02 +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 }