annotate cmd/octree2contour/store.go @ 726:5af9ab39e715

Renamed a few types to uppercase names to prepare the move to the octree package.
author Sascha L. Teichmann <teichmann@intevation.de>
date Sat, 22 Sep 2018 21:34:12 +0200
parents c0bba602b60e
children 41c8dc61f38f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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 const (
a9783d8f74ed octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
11 wkbNDR byte = 1
a9783d8f74ed octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
12 wkbPointZ uint32 = 1000 + 1
a9783d8f74ed octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
13 wkbLineStringZ uint32 = 1000 + 2
a9783d8f74ed octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
14 wkbMultiLineStringZ uint32 = 1000 + 5
a9783d8f74ed octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
15 )
a9783d8f74ed octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
16
a9783d8f74ed octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
17 type result struct {
a9783d8f74ed octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
18 h float64
726
5af9ab39e715 Renamed a few types to uppercase names to prepare the move to the octree package.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 718
diff changeset
19 lines MultiLineStringZ
694
a9783d8f74ed octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
20 }
a9783d8f74ed octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
21
a9783d8f74ed octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
22 const insertSQL = `
a9783d8f74ed octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
23 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
24 VALUES ($1, ST_Transform(
a9783d8f74ed octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
25 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
26 `
a9783d8f74ed octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
27
a9783d8f74ed octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
28 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
29
a9783d8f74ed octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
30 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
31
a9783d8f74ed octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
32 tx, err := db.Begin()
a9783d8f74ed octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
33 if err != nil {
a9783d8f74ed octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
34 return err
a9783d8f74ed octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
35 }
a9783d8f74ed octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
36 defer tx.Rollback()
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 stmt, err := tx.Prepare(insertSQL)
a9783d8f74ed octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
39 if err != nil {
a9783d8f74ed octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
40 return err
a9783d8f74ed octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
41 }
a9783d8f74ed octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
42
a9783d8f74ed octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
43 for _, r := range all {
a9783d8f74ed octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
44 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
45 return err
a9783d8f74ed octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
46 }
a9783d8f74ed octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
47 }
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 return tx.Commit()
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 }
a9783d8f74ed octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
52
726
5af9ab39e715 Renamed a few types to uppercase names to prepare the move to the octree package.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 718
diff changeset
53 func (mls MultiLineStringZ) asWKB() []byte {
694
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 var buf bytes.Buffer
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 binary.Write(&buf, binary.LittleEndian, wkbNDR)
a9783d8f74ed octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
58 binary.Write(&buf, binary.LittleEndian, wkbMultiLineStringZ)
a9783d8f74ed octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
59 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
60
a9783d8f74ed octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
61 for _, ml := range mls {
a9783d8f74ed octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
62 binary.Write(&buf, binary.LittleEndian, wkbNDR)
a9783d8f74ed octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
63 binary.Write(&buf, binary.LittleEndian, wkbLineStringZ)
a9783d8f74ed octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
64 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
65 for _, p := range ml {
a9783d8f74ed octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
66 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
67 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
68 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
69 }
a9783d8f74ed octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
70 }
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 return buf.Bytes()
a9783d8f74ed octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
73 }