annotate pkg/models/reproject.go @ 762:01ba06da8f46

Factored repojection of coordinates to own logic as we need it to reproject the results of the cross sections back to WGS84.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Tue, 25 Sep 2018 10:35:17 +0200
parents
children a244b18cb916
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
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
1 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
2
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
3 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
4 "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
5 "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
6 )
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
7
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
8 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
9 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
10 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
11
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
12 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
13 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
14 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
15 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
16 }
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
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 func NewReprojector(
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 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
20 ctx context.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
21 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
22 ) (*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
23 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
24 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
25 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
26 }
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 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
28 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
29 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
30 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
31 }, 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
32 }
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
33
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 (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
35 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
36 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
37 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
38 }
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 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
40 }
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
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 func (r *Reprojector) Reproject(
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 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
44 ctx context.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
45 ) (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
46 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
47 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
48 }