annotate pkg/models/reproject.go @ 5670:b75d0b303328

Various fixes and improvements of gauges import: - Allow update of erased data (and thereby set erased to false) - Fix source_organization to work with ERDMS2 - Give ISRS of new and updated gauges in summary - Fixed reference of null pointers if revlevels are missing - Fixed reference of null pointer on update errors - Added ISRS to reference_code warning
author Sascha Wilde <wilde@sha-bang.de>
date Fri, 08 Dec 2023 17:29:56 +0100
parents 1222b777f51f
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1017
a244b18cb916 Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 762
diff changeset
1 // This is Free Software under GNU Affero General Public License v >= 3.0
a244b18cb916 Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 762
diff changeset
2 // without warranty, see README.md and license for details.
a244b18cb916 Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 762
diff changeset
3 //
a244b18cb916 Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 762
diff changeset
4 // SPDX-License-Identifier: AGPL-3.0-or-later
a244b18cb916 Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 762
diff changeset
5 // License-Filename: LICENSES/AGPL-3.0.txt
a244b18cb916 Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 762
diff changeset
6 //
a244b18cb916 Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 762
diff changeset
7 // Copyright (C) 2018 by via donau
a244b18cb916 Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 762
diff changeset
8 // – Österreichische Wasserstraßen-Gesellschaft mbH
a244b18cb916 Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 762
diff changeset
9 // Software engineering by Intevation GmbH
a244b18cb916 Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 762
diff changeset
10 //
a244b18cb916 Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 762
diff changeset
11 // Author(s):
a244b18cb916 Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 762
diff changeset
12 // * Sascha L. Teichmann <sascha.teichmann@intevation.de>
a244b18cb916 Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 762
diff changeset
13
762
01ba06da8f46 Factored repojection of coordinates to own logic as we need it to reproject the
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
14 package models
01ba06da8f46 Factored repojection of coordinates to own logic as we need it to reproject the
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
15
01ba06da8f46 Factored repojection of coordinates to own logic as we need it to reproject the
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
16 import (
01ba06da8f46 Factored repojection of coordinates to own logic as we need it to reproject the
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
17 "context"
01ba06da8f46 Factored repojection of coordinates to own logic as we need it to reproject the
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
18 "database/sql"
01ba06da8f46 Factored repojection of coordinates to own logic as we need it to reproject the
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
19 )
01ba06da8f46 Factored repojection of coordinates to own logic as we need it to reproject the
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
20
01ba06da8f46 Factored repojection of coordinates to own logic as we need it to reproject the
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
21 const reprojectSQL = `
01ba06da8f46 Factored repojection of coordinates to own logic as we need it to reproject the
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
22 SELECT ST_X(p), ST_Y(p)
01ba06da8f46 Factored repojection of coordinates to own logic as we need it to reproject the
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
23 FROM ST_Transform(ST_SetSRID(ST_MakePoint($1, $2), $3::integer), $4::integer) AS p`
01ba06da8f46 Factored repojection of coordinates to own logic as we need it to reproject the
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
24
5601
1222b777f51f Made golint finally happy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1328
diff changeset
25 // Reprojector re-projects point data from one coord system
1222b777f51f Made golint finally happy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1328
diff changeset
26 // to another.
762
01ba06da8f46 Factored repojection of coordinates to own logic as we need it to reproject the
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
27 type Reprojector struct {
01ba06da8f46 Factored repojection of coordinates to own logic as we need it to reproject the
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
28 stmt *sql.Stmt
01ba06da8f46 Factored repojection of coordinates to own logic as we need it to reproject the
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
29 FromEPSG uint32
01ba06da8f46 Factored repojection of coordinates to own logic as we need it to reproject the
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
30 ToEPSG uint32
01ba06da8f46 Factored repojection of coordinates to own logic as we need it to reproject the
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
31 }
01ba06da8f46 Factored repojection of coordinates to own logic as we need it to reproject the
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
32
5601
1222b777f51f Made golint finally happy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1328
diff changeset
33 // NewReprojector creates a reprojector.
762
01ba06da8f46 Factored repojection of coordinates to own logic as we need it to reproject the
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
34 func NewReprojector(
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: 1017
diff changeset
35 ctx context.Context,
762
01ba06da8f46 Factored repojection of coordinates to own logic as we need it to reproject the
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
36 conn *sql.Conn,
01ba06da8f46 Factored repojection of coordinates to own logic as we need it to reproject the
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
37 fromEPSG, toEPSG uint32,
01ba06da8f46 Factored repojection of coordinates to own logic as we need it to reproject the
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
38 ) (*Reprojector, error) {
01ba06da8f46 Factored repojection of coordinates to own logic as we need it to reproject the
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
39 stmt, err := conn.PrepareContext(ctx, reprojectSQL)
01ba06da8f46 Factored repojection of coordinates to own logic as we need it to reproject the
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
40 if err != nil {
01ba06da8f46 Factored repojection of coordinates to own logic as we need it to reproject the
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
41 return nil, err
01ba06da8f46 Factored repojection of coordinates to own logic as we need it to reproject the
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
42 }
01ba06da8f46 Factored repojection of coordinates to own logic as we need it to reproject the
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
43 return &Reprojector{
01ba06da8f46 Factored repojection of coordinates to own logic as we need it to reproject the
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
44 stmt: stmt,
01ba06da8f46 Factored repojection of coordinates to own logic as we need it to reproject the
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
45 FromEPSG: fromEPSG,
01ba06da8f46 Factored repojection of coordinates to own logic as we need it to reproject the
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
46 ToEPSG: toEPSG,
01ba06da8f46 Factored repojection of coordinates to own logic as we need it to reproject the
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
47 }, nil
01ba06da8f46 Factored repojection of coordinates to own logic as we need it to reproject the
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
48 }
01ba06da8f46 Factored repojection of coordinates to own logic as we need it to reproject the
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
49
5601
1222b777f51f Made golint finally happy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1328
diff changeset
50 // Close closes the reprojector.
762
01ba06da8f46 Factored repojection of coordinates to own logic as we need it to reproject the
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
51 func (r *Reprojector) Close() error {
01ba06da8f46 Factored repojection of coordinates to own logic as we need it to reproject the
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
52 if s := r.stmt; s != nil {
01ba06da8f46 Factored repojection of coordinates to own logic as we need it to reproject the
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
53 r.stmt = nil
01ba06da8f46 Factored repojection of coordinates to own logic as we need it to reproject the
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
54 return s.Close()
01ba06da8f46 Factored repojection of coordinates to own logic as we need it to reproject the
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
55 }
01ba06da8f46 Factored repojection of coordinates to own logic as we need it to reproject the
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
56 return nil
01ba06da8f46 Factored repojection of coordinates to own logic as we need it to reproject the
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
57 }
01ba06da8f46 Factored repojection of coordinates to own logic as we need it to reproject the
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
58
5601
1222b777f51f Made golint finally happy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1328
diff changeset
59 // Reproject performs the re-projection for a given point.
762
01ba06da8f46 Factored repojection of coordinates to own logic as we need it to reproject the
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
60 func (r *Reprojector) Reproject(
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: 1017
diff changeset
61 ctx context.Context,
762
01ba06da8f46 Factored repojection of coordinates to own logic as we need it to reproject the
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
62 x, y float64,
01ba06da8f46 Factored repojection of coordinates to own logic as we need it to reproject the
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
63 ) (v, w float64, err error) {
01ba06da8f46 Factored repojection of coordinates to own logic as we need it to reproject the
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
64 err = r.stmt.QueryRowContext(ctx, x, y, r.FromEPSG, r.ToEPSG).Scan(&v, &w)
01ba06da8f46 Factored repojection of coordinates to own logic as we need it to reproject the
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
65 return
01ba06da8f46 Factored repojection of coordinates to own logic as we need it to reproject the
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
66 }