Mercurial > gemma
annotate cmd/octree2contour/store.go @ 920:862cc5ba4d1d
Merge contour lines
Lines have been stored as multilinestrings comprised of adjacent
2-vertex linestrings. Merging these to longer linestrings will
allow further processing such as simplification.
author | Tom Gottfried <tom@intevation.de> |
---|---|
date | Thu, 04 Oct 2018 17:46:59 +0200 |
parents | 271561dce2e6 |
children | 52cb0b82b490 |
rev | line source |
---|---|
694
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
1 package main |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
2 |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
3 import ( |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
4 "database/sql" |
732
39b5cf9a6b8f
Store sounding results contour lines into right table.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
727
diff
changeset
|
5 "time" |
694
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
6 |
727
41c8dc61f38f
Moved octree loading stuff to octree package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
726
diff
changeset
|
7 "gemma.intevation.de/gemma/pkg/octree" |
694
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
8 ) |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
9 |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
10 type result struct { |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
11 h float64 |
727
41c8dc61f38f
Moved octree loading stuff to octree package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
726
diff
changeset
|
12 lines octree.MultiLineStringZ |
694
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
13 } |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
14 |
732
39b5cf9a6b8f
Store sounding results contour lines into right table.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
727
diff
changeset
|
15 const ( |
39b5cf9a6b8f
Store sounding results contour lines into right table.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
727
diff
changeset
|
16 deleteSQL = ` |
735
83a9f04f1759
Spell table names correctly.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
732
diff
changeset
|
17 DELETE FROM waterway.sounding_results_contour_lines |
732
39b5cf9a6b8f
Store sounding results contour lines into right table.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
727
diff
changeset
|
18 WHERE sounding_result_id IN (SELECT id |
39b5cf9a6b8f
Store sounding results contour lines into right table.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
727
diff
changeset
|
19 FROM waterway.sounding_results |
39b5cf9a6b8f
Store sounding results contour lines into right table.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
727
diff
changeset
|
20 WHERE bottleneck_id = $1 AND date_info = $2) |
694
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
21 ` |
732
39b5cf9a6b8f
Store sounding results contour lines into right table.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
727
diff
changeset
|
22 insertSQL = ` |
735
83a9f04f1759
Spell table names correctly.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
732
diff
changeset
|
23 INSERT INTO waterway.sounding_results_contour_lines |
736
34ea9d5df653
Fixed problem with unique constraint for countour lines.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
735
diff
changeset
|
24 (sounding_result_id, height, lines) |
732
39b5cf9a6b8f
Store sounding results contour lines into right table.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
727
diff
changeset
|
25 SELECT |
742
fb9faf2c4f60
Clip contour lines against bounding area of sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
736
diff
changeset
|
26 sr.id, |
732
39b5cf9a6b8f
Store sounding results contour lines into right table.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
727
diff
changeset
|
27 $1, |
742
fb9faf2c4f60
Clip contour lines against bounding area of sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
736
diff
changeset
|
28 ST_Transform( |
920 | 29 ST_Multi( |
30 ST_CollectionExtract( | |
919
271561dce2e6
Store contour lines in 2D
Tom Gottfried <tom@intevation.de>
parents:
742
diff
changeset
|
31 ST_Intersection( |
271561dce2e6
Store contour lines in 2D
Tom Gottfried <tom@intevation.de>
parents:
742
diff
changeset
|
32 ST_Transform(sr.area::geometry, $3::integer), |
920 | 33 ST_LineMerge(ST_Force2D(ST_GeomFromWKB($2, $3::integer))) |
34 ), | |
35 2 | |
36 ) | |
919
271561dce2e6
Store contour lines in 2D
Tom Gottfried <tom@intevation.de>
parents:
742
diff
changeset
|
37 ), |
271561dce2e6
Store contour lines in 2D
Tom Gottfried <tom@intevation.de>
parents:
742
diff
changeset
|
38 4326 |
271561dce2e6
Store contour lines in 2D
Tom Gottfried <tom@intevation.de>
parents:
742
diff
changeset
|
39 ) |
742
fb9faf2c4f60
Clip contour lines against bounding area of sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
736
diff
changeset
|
40 FROM waterway.sounding_results sr |
732
39b5cf9a6b8f
Store sounding results contour lines into right table.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
727
diff
changeset
|
41 WHERE bottleneck_id = $4 AND date_info = $5 |
39b5cf9a6b8f
Store sounding results contour lines into right table.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
727
diff
changeset
|
42 ` |
39b5cf9a6b8f
Store sounding results contour lines into right table.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
727
diff
changeset
|
43 ) |
694
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
44 |
732
39b5cf9a6b8f
Store sounding results contour lines into right table.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
727
diff
changeset
|
45 func store( |
39b5cf9a6b8f
Store sounding results contour lines into right table.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
727
diff
changeset
|
46 all []result, epsg uint32, |
39b5cf9a6b8f
Store sounding results contour lines into right table.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
727
diff
changeset
|
47 bottleneck string, date time.Time, |
39b5cf9a6b8f
Store sounding results contour lines into right table.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
727
diff
changeset
|
48 ) error { |
694
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
49 |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
50 return run(func(db *sql.DB) error { |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
51 |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
52 tx, err := db.Begin() |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
53 if err != nil { |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
54 return err |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
55 } |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
56 defer tx.Rollback() |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
57 |
732
39b5cf9a6b8f
Store sounding results contour lines into right table.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
727
diff
changeset
|
58 if _, err := tx.Exec(deleteSQL, bottleneck, date); err != nil { |
39b5cf9a6b8f
Store sounding results contour lines into right table.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
727
diff
changeset
|
59 return err |
39b5cf9a6b8f
Store sounding results contour lines into right table.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
727
diff
changeset
|
60 } |
39b5cf9a6b8f
Store sounding results contour lines into right table.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
727
diff
changeset
|
61 |
694
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
62 stmt, err := tx.Prepare(insertSQL) |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
63 if err != nil { |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
64 return err |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
65 } |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
66 |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
67 for _, r := range all { |
732
39b5cf9a6b8f
Store sounding results contour lines into right table.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
727
diff
changeset
|
68 if _, err := stmt.Exec( |
39b5cf9a6b8f
Store sounding results contour lines into right table.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
727
diff
changeset
|
69 r.h, r.lines.AsWKB(), epsg, |
39b5cf9a6b8f
Store sounding results contour lines into right table.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
727
diff
changeset
|
70 bottleneck, date, |
39b5cf9a6b8f
Store sounding results contour lines into right table.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
727
diff
changeset
|
71 ); err != nil { |
694
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
72 return err |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
73 } |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
74 } |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
75 |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
76 return tx.Commit() |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
77 }) |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
78 } |