Mercurial > gemma
annotate cmd/octree2contour/store.go @ 694:a9783d8f74ed octree
octree: Store contour lines into postgres/postgis.
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Thu, 20 Sep 2018 15:50:07 +0200 |
parents | |
children | c0bba602b60e |
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 "bytes" |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
5 "database/sql" |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
6 "encoding/binary" |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
7 "math" |
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 multiLineStringZ [][]vertex |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
11 |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
12 const ( |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
13 wkbNDR byte = 1 |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
14 wkbPointZ uint32 = 1000 + 1 |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
15 wkbLineStringZ uint32 = 1000 + 2 |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
16 wkbMultiLineStringZ uint32 = 1000 + 5 |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
17 ) |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
18 |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
19 type result struct { |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
20 h float64 |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
21 lines multiLineStringZ |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
22 } |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
23 |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
24 const insertSQL = ` |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
25 INSERT INTO waterway.contour_lines (height, geom) |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
26 VALUES ($1, ST_Transform( |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
27 ST_SetSRID(ST_GeomFromWKB($2), $3), 4326)::geography) |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
28 ` |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
29 |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
30 func store(all []result, epsg uint32) error { |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
31 |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
32 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
|
33 |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
34 tx, err := db.Begin() |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
35 if err != nil { |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
36 return err |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
37 } |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
38 defer tx.Rollback() |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
39 |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
40 stmt, err := tx.Prepare(insertSQL) |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
41 if err != nil { |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
42 return err |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
43 } |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
44 |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
45 for _, r := range all { |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
46 if _, err := stmt.Exec(r.h, r.lines.asWKB(), epsg); err != nil { |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
47 return err |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
48 } |
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 |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
51 return tx.Commit() |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
52 }) |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
53 } |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
54 |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
55 func (mls multiLineStringZ) asWKB() []byte { |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
56 |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
57 var buf bytes.Buffer |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
58 |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
59 binary.Write(&buf, binary.LittleEndian, wkbNDR) |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
60 binary.Write(&buf, binary.LittleEndian, wkbMultiLineStringZ) |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
61 binary.Write(&buf, binary.LittleEndian, uint32(len(mls))) |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
62 |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
63 for _, ml := range mls { |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
64 binary.Write(&buf, binary.LittleEndian, wkbNDR) |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
65 binary.Write(&buf, binary.LittleEndian, wkbLineStringZ) |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
66 binary.Write(&buf, binary.LittleEndian, uint32(len(ml))) |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
67 for _, p := range ml { |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
68 binary.Write(&buf, binary.LittleEndian, math.Float64bits(p.x)) |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
69 binary.Write(&buf, binary.LittleEndian, math.Float64bits(p.y)) |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
70 binary.Write(&buf, binary.LittleEndian, math.Float64bits(p.z)) |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
71 } |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
72 } |
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 return buf.Bytes() |
a9783d8f74ed
octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
75 } |