Mercurial > gemma
annotate pkg/controllers/search.go @ 804:21609ba97357
client: improve legendelements for layerselection
* Add a special hint to the layer data, so that we can detect
points and specify which resolution we want for the legend.
* Improve LineString to have a bend.
* Actually take styles of the layer into account.
* Do not act on TILE layers.
author | Bernhard Reiter <bernhard@intevation.de> |
---|---|
date | Thu, 27 Sep 2018 15:00:52 +0200 |
parents | 5616c6bfb186 |
children | e03fab882b47 |
rev | line source |
---|---|
743
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
1 package controllers |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
2 |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
3 import ( |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
4 "database/sql" |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
5 "net/http" |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
6 "regexp" |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
7 "strconv" |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
8 "strings" |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
9 |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
10 "gemma.intevation.de/gemma/pkg/models" |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
11 ) |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
12 |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
13 const ( |
749
802ecaae8410
Search back end: simply return emty array for empty result sets.
Sascha Wilde <wilde@intevation.de>
parents:
748
diff
changeset
|
14 searchHectometreSQL = `SELECT COALESCE(json_agg(r),'[]') |
784
5616c6bfb186
Search: show more human readable results for rkm.
Sascha Wilde <wilde@intevation.de>
parents:
751
diff
changeset
|
15 FROM (SELECT (location_code).hectometre || ' rhm' AS name, |
744
6fe6839f5ce6
Make search return an array of features, including name.
Sascha Wilde <wilde@intevation.de>
parents:
743
diff
changeset
|
16 ST_AsGeoJSON(geom)::json AS geom |
6fe6839f5ce6
Make search return an array of features, including name.
Sascha Wilde <wilde@intevation.de>
parents:
743
diff
changeset
|
17 FROM waterway.distance_marks |
748
3cb012d4d9ef
Added search for bottlenecks
Sascha Wilde <wilde@intevation.de>
parents:
744
diff
changeset
|
18 WHERE (location_code).hectometre = $1) r` |
749
802ecaae8410
Search back end: simply return emty array for empty result sets.
Sascha Wilde <wilde@intevation.de>
parents:
748
diff
changeset
|
19 searchBottleneckSQL = `SELECT COALESCE(json_agg(r),'[]') |
748
3cb012d4d9ef
Added search for bottlenecks
Sascha Wilde <wilde@intevation.de>
parents:
744
diff
changeset
|
20 FROM (SELECT objnam AS name, |
3cb012d4d9ef
Added search for bottlenecks
Sascha Wilde <wilde@intevation.de>
parents:
744
diff
changeset
|
21 ST_AsGeoJSON(ST_Centroid(area))::json AS geom |
3cb012d4d9ef
Added search for bottlenecks
Sascha Wilde <wilde@intevation.de>
parents:
744
diff
changeset
|
22 FROM waterway.bottlenecks |
3cb012d4d9ef
Added search for bottlenecks
Sascha Wilde <wilde@intevation.de>
parents:
744
diff
changeset
|
23 WHERE objnam ILIKE $1) r` |
743
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
24 ) |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
25 |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
26 var rkmRegex = regexp.MustCompile( |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
27 "^[[:space:]]*([0-9]+)([,.]([0-9]))?[[:space:]]*$", |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
28 ) |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
29 |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
30 func searchFeature( |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
31 input interface{}, |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
32 req *http.Request, |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
33 db *sql.Conn, |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
34 ) (jr JSONResult, err error) { |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
35 |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
36 s := input.(*models.SearchRequest) |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
37 |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
38 if len(s.SearchString) == 0 { |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
39 err = JSONError{http.StatusBadRequest, |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
40 "error: empty search string"} |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
41 return |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
42 } |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
43 |
751
21dd10f26206
Some refactoring in search endpoint.
Sascha Wilde <wilde@intevation.de>
parents:
750
diff
changeset
|
44 var result string |
743
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
45 |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
46 m := rkmRegex.FindStringSubmatch(s.SearchString) |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
47 if len(m) != 0 { |
751
21dd10f26206
Some refactoring in search endpoint.
Sascha Wilde <wilde@intevation.de>
parents:
750
diff
changeset
|
48 // Handle search for river kilometre: |
743
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
49 var hectometre int |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
50 if hectometre, err = strconv.Atoi(m[1]); err != nil { |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
51 return |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
52 } |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
53 |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
54 hectometre *= 10 |
750
cf37bf6c28c6
Search: Fixed parsing of numbers w/o decimal.
Sascha Wilde <wilde@intevation.de>
parents:
749
diff
changeset
|
55 if m[3] != "" { |
743
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
56 var h int |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
57 if h, err = strconv.Atoi(m[3]); err != nil { |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
58 return |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
59 } |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
60 hectometre += h |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
61 } |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
62 |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
63 err = db.QueryRowContext( |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
64 req.Context(), |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
65 searchHectometreSQL, |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
66 hectometre, |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
67 ).Scan(&result) |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
68 } else { |
751
21dd10f26206
Some refactoring in search endpoint.
Sascha Wilde <wilde@intevation.de>
parents:
750
diff
changeset
|
69 // Hande search for bottlencks: |
748
3cb012d4d9ef
Added search for bottlenecks
Sascha Wilde <wilde@intevation.de>
parents:
744
diff
changeset
|
70 err = db.QueryRowContext( |
3cb012d4d9ef
Added search for bottlenecks
Sascha Wilde <wilde@intevation.de>
parents:
744
diff
changeset
|
71 req.Context(), |
3cb012d4d9ef
Added search for bottlenecks
Sascha Wilde <wilde@intevation.de>
parents:
744
diff
changeset
|
72 searchBottleneckSQL, |
3cb012d4d9ef
Added search for bottlenecks
Sascha Wilde <wilde@intevation.de>
parents:
744
diff
changeset
|
73 "%"+s.SearchString+"%", |
3cb012d4d9ef
Added search for bottlenecks
Sascha Wilde <wilde@intevation.de>
parents:
744
diff
changeset
|
74 ).Scan(&result) |
751
21dd10f26206
Some refactoring in search endpoint.
Sascha Wilde <wilde@intevation.de>
parents:
750
diff
changeset
|
75 } |
748
3cb012d4d9ef
Added search for bottlenecks
Sascha Wilde <wilde@intevation.de>
parents:
744
diff
changeset
|
76 |
751
21dd10f26206
Some refactoring in search endpoint.
Sascha Wilde <wilde@intevation.de>
parents:
750
diff
changeset
|
77 if err != nil { |
21dd10f26206
Some refactoring in search endpoint.
Sascha Wilde <wilde@intevation.de>
parents:
750
diff
changeset
|
78 return |
21dd10f26206
Some refactoring in search endpoint.
Sascha Wilde <wilde@intevation.de>
parents:
750
diff
changeset
|
79 } |
743
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
80 |
751
21dd10f26206
Some refactoring in search endpoint.
Sascha Wilde <wilde@intevation.de>
parents:
750
diff
changeset
|
81 jr.Result = strings.NewReader(result) |
743
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
82 |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
83 return |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
84 } |