Mercurial > gemma
changeset 964:1e2dce348cfb
Serialize boundary polygon of sounding result as WKB.
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Wed, 17 Oct 2018 15:29:58 +0200 |
parents | e0825645f00e |
children | ac2e51d5bc40 |
files | pkg/imports/sr.go |
diffstat | 1 files changed, 30 insertions(+), 3 deletions(-) [+] |
line wrap: on
line diff
--- a/pkg/imports/sr.go Wed Oct 17 15:22:38 2018 +0200 +++ b/pkg/imports/sr.go Wed Oct 17 15:29:58 2018 +0200 @@ -3,13 +3,16 @@ import ( "archive/zip" "bufio" + "bytes" "context" "database/sql" + "encoding/binary" "encoding/json" "errors" "fmt" "io" "log" + "math" "os" "path" "strconv" @@ -47,6 +50,11 @@ ) const ( + wkbNDR byte = 1 + wkbPolygon uint32 = 3 +) + +const ( insertPointsSQL = ` INSERT INTO waterway.sounding_results ( bottleneck_id, @@ -320,9 +328,28 @@ } func (p Polygon) AsWBK() []byte { - if len(p) == 0 { + if p == nil { return nil } - // TODO: Implement me! - return nil + // pre-calculate size to avoid reallocations. + size := 1 + 4 + 4 + for _, ring := range p { + size += 4 + len(ring)*2*8 + } + + buf := bytes.NewBuffer(make([]byte, 0, size)) + + binary.Write(buf, binary.LittleEndian, wkbNDR) + binary.Write(buf, binary.LittleEndian, wkbPolygon) + binary.Write(buf, binary.LittleEndian, uint32(len(p))) + + for _, ring := range p { + binary.Write(buf, binary.LittleEndian, uint32(len(ring))) + for _, v := range ring { + binary.Write(buf, binary.LittleEndian, math.Float64bits(v.X)) + binary.Write(buf, binary.LittleEndian, math.Float64bits(v.Y)) + } + } + + return buf.Bytes() }