Mercurial > gemma
annotate pkg/models/reproject.go @ 5520:05db984d3db1
Improve performance of bottleneck area calculation
Avoid buffer calculations by replacing them with simple distance comparisons
and calculate the boundary of the result geometry only once per iteration.
In some edge cases with very large numbers of iterations, this reduced
the runtime of a bottleneck import by a factor of more than twenty.
author | Tom Gottfried <tom@intevation.de> |
---|---|
date | Thu, 21 Oct 2021 19:50:39 +0200 |
parents | d753ce6cf588 |
children | 1222b777f51f |
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 |
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 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
|
26 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
|
27 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
|
28 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
|
29 } |
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 |
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 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
|
32 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
|
33 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
|
34 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
|
35 ) (*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
|
36 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
|
37 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
|
38 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
|
39 } |
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 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
|
41 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
|
42 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
|
43 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
|
44 }, 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
|
45 } |
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 |
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 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
|
48 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
|
49 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
|
50 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
|
51 } |
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 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
|
53 } |
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 |
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 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
|
56 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
|
57 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
|
58 ) (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
|
59 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
|
60 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
|
61 } |