comparison 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
comparison
equal deleted inserted replaced
689:614135d69823 694:a9783d8f74ed
1 package main
2
3 import (
4 "bytes"
5 "database/sql"
6 "encoding/binary"
7 "math"
8 )
9
10 type multiLineStringZ [][]vertex
11
12 const (
13 wkbNDR byte = 1
14 wkbPointZ uint32 = 1000 + 1
15 wkbLineStringZ uint32 = 1000 + 2
16 wkbMultiLineStringZ uint32 = 1000 + 5
17 )
18
19 type result struct {
20 h float64
21 lines multiLineStringZ
22 }
23
24 const insertSQL = `
25 INSERT INTO waterway.contour_lines (height, geom)
26 VALUES ($1, ST_Transform(
27 ST_SetSRID(ST_GeomFromWKB($2), $3), 4326)::geography)
28 `
29
30 func store(all []result, epsg uint32) error {
31
32 return run(func(db *sql.DB) error {
33
34 tx, err := db.Begin()
35 if err != nil {
36 return err
37 }
38 defer tx.Rollback()
39
40 stmt, err := tx.Prepare(insertSQL)
41 if err != nil {
42 return err
43 }
44
45 for _, r := range all {
46 if _, err := stmt.Exec(r.h, r.lines.asWKB(), epsg); err != nil {
47 return err
48 }
49 }
50
51 return tx.Commit()
52 })
53 }
54
55 func (mls multiLineStringZ) asWKB() []byte {
56
57 var buf bytes.Buffer
58
59 binary.Write(&buf, binary.LittleEndian, wkbNDR)
60 binary.Write(&buf, binary.LittleEndian, wkbMultiLineStringZ)
61 binary.Write(&buf, binary.LittleEndian, uint32(len(mls)))
62
63 for _, ml := range mls {
64 binary.Write(&buf, binary.LittleEndian, wkbNDR)
65 binary.Write(&buf, binary.LittleEndian, wkbLineStringZ)
66 binary.Write(&buf, binary.LittleEndian, uint32(len(ml)))
67 for _, p := range ml {
68 binary.Write(&buf, binary.LittleEndian, math.Float64bits(p.x))
69 binary.Write(&buf, binary.LittleEndian, math.Float64bits(p.y))
70 binary.Write(&buf, binary.LittleEndian, math.Float64bits(p.z))
71 }
72 }
73
74 return buf.Bytes()
75 }