annotate pkg/models/sr.go @ 2961:5bc941d9ec43

Fix checking of depth reference for sounding result Depth references have to match any depth reference associated with the bottlenecks reference gauge. However, these cannot necessarily be expected to be also in depth_references.
author Tom Gottfried <tom@intevation.de>
date Mon, 08 Apr 2019 16:27:37 +0200
parents df6383831ad3
children ec6163c6687d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1224
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
1 // This is Free Software under GNU Affero General Public License v >= 3.0
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
2 // without warranty, see README.md and license for details.
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
3 //
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
4 // SPDX-License-Identifier: AGPL-3.0-or-later
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
5 // License-Filename: LICENSES/AGPL-3.0.txt
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
6 //
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
7 // Copyright (C) 2018 by via donau
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
8 // – Österreichische Wasserstraßen-Gesellschaft mbH
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
9 // Software engineering by Intevation GmbH
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
10 //
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
11 // Author(s):
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
12 // * Sascha L. Teichmann <sascha.teichmann@intevation.de>
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
13 // * Bernhard E. Reiter <bernhard.reiter@intevation.de>
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
14
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
15 package models
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
16
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
17 import (
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
18 "context"
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
19 "database/sql"
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
20 "encoding/json"
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
21 "errors"
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
22 "fmt"
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
23 "io"
2345
df6383831ad3 Sounding result import: Be more verbose if sounding result already exists for a bottleneck/date.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1900
diff changeset
24
df6383831ad3 Sounding result import: Be more verbose if sounding result already exists for a bottleneck/date.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1900
diff changeset
25 "gemma.intevation.de/gemma/pkg/common"
1224
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
26 )
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
27
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
28 type (
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
29 SoundingResultMeta struct {
1900
6a67cd819e93 To prepare stretch import made some model data types re-usable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1572
diff changeset
30 Date Date `json:"date"`
6a67cd819e93 To prepare stretch import made some model data types re-usable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1572
diff changeset
31 Bottleneck string `json:"bottleneck"`
6a67cd819e93 To prepare stretch import made some model data types re-usable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1572
diff changeset
32 EPSG uint `json:"epsg"`
6a67cd819e93 To prepare stretch import made some model data types re-usable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1572
diff changeset
33 DepthReference string `json:"depth-reference"`
1224
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
34 }
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
35 )
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
36
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
37 const (
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
38 checkDepthReferenceSQL = `
2961
5bc941d9ec43 Fix checking of depth reference for sounding result
Tom Gottfried <tom@intevation.de>
parents: 2345
diff changeset
39 SELECT EXISTS(SELECT 1
5bc941d9ec43 Fix checking of depth reference for sounding result
Tom Gottfried <tom@intevation.de>
parents: 2345
diff changeset
40 FROM waterway.bottlenecks bn
5bc941d9ec43 Fix checking of depth reference for sounding result
Tom Gottfried <tom@intevation.de>
parents: 2345
diff changeset
41 JOIN waterway.gauges g ON g.location = bn.fk_g_fid
5bc941d9ec43 Fix checking of depth reference for sounding result
Tom Gottfried <tom@intevation.de>
parents: 2345
diff changeset
42 JOIN waterway.gauges_reference_water_levels rl ON rl.gauge_id = g.location
5bc941d9ec43 Fix checking of depth reference for sounding result
Tom Gottfried <tom@intevation.de>
parents: 2345
diff changeset
43 WHERE bn.objnam = $1
5bc941d9ec43 Fix checking of depth reference for sounding result
Tom Gottfried <tom@intevation.de>
parents: 2345
diff changeset
44 AND rl.depth_reference = $2)`
1224
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
45
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
46 checkBottleneckSQL = `
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
47 SELECT true FROM waterway.bottlenecks WHERE objnam = $1`
1297
1c0c9190fcf2 Import sounding result: If uploading a zip with a meta.json which has
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1224
diff changeset
48
1c0c9190fcf2 Import sounding result: If uploading a zip with a meta.json which has
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1224
diff changeset
49 checkBottleneckDateUniqueSQL = `
1c0c9190fcf2 Import sounding result: If uploading a zip with a meta.json which has
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1224
diff changeset
50 SELECT true FROM waterway.sounding_results sr JOIN
1572
056a86b24be2 Made bottleneck primary key an int. Attention: This may break something!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1373
diff changeset
51 waterway.bottlenecks bn ON sr.bottleneck_id = bn.id
1297
1c0c9190fcf2 Import sounding result: If uploading a zip with a meta.json which has
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1224
diff changeset
52 WHERE bn.objnam = $1 AND sr.date_info = $2`
1224
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
53 )
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
54
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
55 func (m *SoundingResultMeta) Decode(r io.Reader) error {
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
56 err := json.NewDecoder(r).Decode(m)
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
57 if err == nil && m.EPSG == 0 {
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
58 m.EPSG = WGS84
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
59 }
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
60 return err
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
61 }
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
62
1328
d753ce6cf588 To make golint happier made context.Context to be the first argument in all calls.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1297
diff changeset
63 func (m *SoundingResultMeta) Validate(ctx context.Context, conn *sql.Conn) []error {
1224
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
64
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
65 var errs []error
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
66
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
67 var b bool
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
68 err := conn.QueryRowContext(ctx,
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
69 checkBottleneckSQL,
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
70 m.Bottleneck).Scan(&b)
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
71 switch {
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
72 case err == sql.ErrNoRows:
1330
785f14264426 Follow the rules for error messages to make golint happy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1328
diff changeset
73 errs = append(errs, fmt.Errorf("unknown bottleneck '%s'", m.Bottleneck))
1224
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
74 case err != nil:
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
75 errs = append(errs, err)
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
76 case !b:
1331
275ead19b9fd More on following the rules for error messages to make golint happy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1330
diff changeset
77 errs = append(errs, errors.New("unexpected bottleneck"))
1224
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
78 }
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
79
1297
1c0c9190fcf2 Import sounding result: If uploading a zip with a meta.json which has
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1224
diff changeset
80 err = conn.QueryRowContext(ctx,
2961
5bc941d9ec43 Fix checking of depth reference for sounding result
Tom Gottfried <tom@intevation.de>
parents: 2345
diff changeset
81 checkDepthReferenceSQL,
5bc941d9ec43 Fix checking of depth reference for sounding result
Tom Gottfried <tom@intevation.de>
parents: 2345
diff changeset
82 m.Bottleneck,
5bc941d9ec43 Fix checking of depth reference for sounding result
Tom Gottfried <tom@intevation.de>
parents: 2345
diff changeset
83 m.DepthReference).Scan(&b)
5bc941d9ec43 Fix checking of depth reference for sounding result
Tom Gottfried <tom@intevation.de>
parents: 2345
diff changeset
84 switch {
5bc941d9ec43 Fix checking of depth reference for sounding result
Tom Gottfried <tom@intevation.de>
parents: 2345
diff changeset
85 case !b:
5bc941d9ec43 Fix checking of depth reference for sounding result
Tom Gottfried <tom@intevation.de>
parents: 2345
diff changeset
86 errs = append(errs,
5bc941d9ec43 Fix checking of depth reference for sounding result
Tom Gottfried <tom@intevation.de>
parents: 2345
diff changeset
87 fmt.Errorf("unknown depth reference '%s'", m.DepthReference))
5bc941d9ec43 Fix checking of depth reference for sounding result
Tom Gottfried <tom@intevation.de>
parents: 2345
diff changeset
88 case err != nil:
5bc941d9ec43 Fix checking of depth reference for sounding result
Tom Gottfried <tom@intevation.de>
parents: 2345
diff changeset
89 errs = append(errs, err)
5bc941d9ec43 Fix checking of depth reference for sounding result
Tom Gottfried <tom@intevation.de>
parents: 2345
diff changeset
90 }
5bc941d9ec43 Fix checking of depth reference for sounding result
Tom Gottfried <tom@intevation.de>
parents: 2345
diff changeset
91
5bc941d9ec43 Fix checking of depth reference for sounding result
Tom Gottfried <tom@intevation.de>
parents: 2345
diff changeset
92 err = conn.QueryRowContext(ctx,
1297
1c0c9190fcf2 Import sounding result: If uploading a zip with a meta.json which has
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1224
diff changeset
93 checkBottleneckDateUniqueSQL,
1c0c9190fcf2 Import sounding result: If uploading a zip with a meta.json which has
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1224
diff changeset
94 m.Bottleneck, m.Date.Time).Scan(&b)
1c0c9190fcf2 Import sounding result: If uploading a zip with a meta.json which has
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1224
diff changeset
95 switch {
1c0c9190fcf2 Import sounding result: If uploading a zip with a meta.json which has
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1224
diff changeset
96 case err == sql.ErrNoRows: // good! -> unique.
1c0c9190fcf2 Import sounding result: If uploading a zip with a meta.json which has
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1224
diff changeset
97 case err != nil:
1c0c9190fcf2 Import sounding result: If uploading a zip with a meta.json which has
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1224
diff changeset
98 errs = append(errs, err)
1c0c9190fcf2 Import sounding result: If uploading a zip with a meta.json which has
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1224
diff changeset
99 case b:
1c0c9190fcf2 Import sounding result: If uploading a zip with a meta.json which has
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1224
diff changeset
100 errs = append(errs,
2345
df6383831ad3 Sounding result import: Be more verbose if sounding result already exists for a bottleneck/date.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1900
diff changeset
101 fmt.Errorf("sounding result for date %s already exists at bottleneck '%s'",
df6383831ad3 Sounding result import: Be more verbose if sounding result already exists for a bottleneck/date.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1900
diff changeset
102 m.Date.Time.Format(common.DateFormat),
df6383831ad3 Sounding result import: Be more verbose if sounding result already exists for a bottleneck/date.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1900
diff changeset
103 m.Bottleneck))
1297
1c0c9190fcf2 Import sounding result: If uploading a zip with a meta.json which has
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1224
diff changeset
104 }
1c0c9190fcf2 Import sounding result: If uploading a zip with a meta.json which has
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1224
diff changeset
105
1224
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
106 return errs
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
107 }