comparison pkg/imports/wkb.go @ 5008:0b97f5301a17

Add a multiLineSlice type to be serialized as WKB in importers.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Wed, 11 Mar 2020 15:54:37 +0100
parents 799e8248de8d
children cf25b23e3eec
comparison
equal deleted inserted replaced
5007:799e8248de8d 5008:0b97f5301a17
23 23
24 "gemma.intevation.de/gemma/pkg/wkb" 24 "gemma.intevation.de/gemma/pkg/wkb"
25 ) 25 )
26 26
27 type ( 27 type (
28 pointSlice []float64 28 pointSlice []float64
29 lineSlice [][]float64 29 lineSlice [][]float64
30 polygonSlice [][][]float64 30 multiLineSlice []lineSlice
31 polygonSlice [][][]float64
31 ) 32 )
32 33
33 func newPointFeature(newProperties func() interface{}) func() (string, interface{}) { 34 func newPointFeature(newProperties func() interface{}) func() (string, interface{}) {
34 return func() (string, interface{}) { return "Point", newProperties() } 35 return func() (string, interface{}) { return "Point", newProperties() }
35 } 36 }
36 37
37 func (ls lineSlice) asWKB() []byte { 38 func (ls lineSlice) toWKB(buf *bytes.Buffer) {
38
39 size := 1 + 4 + 4 + len(ls)*(2*8)
40
41 buf := bytes.NewBuffer(make([]byte, 0, size))
42
43 binary.Write(buf, binary.LittleEndian, wkb.NDR) 39 binary.Write(buf, binary.LittleEndian, wkb.NDR)
44 binary.Write(buf, binary.LittleEndian, wkb.LineString) 40 binary.Write(buf, binary.LittleEndian, wkb.LineString)
45 binary.Write(buf, binary.LittleEndian, uint32(len(ls))) 41 binary.Write(buf, binary.LittleEndian, uint32(len(ls)))
46 42
47 for _, c := range ls { 43 for _, c := range ls {
53 lon = c[1] 49 lon = c[1]
54 } 50 }
55 binary.Write(buf, binary.LittleEndian, math.Float64bits(lat)) 51 binary.Write(buf, binary.LittleEndian, math.Float64bits(lat))
56 binary.Write(buf, binary.LittleEndian, math.Float64bits(lon)) 52 binary.Write(buf, binary.LittleEndian, math.Float64bits(lon))
57 } 53 }
54 }
55
56 func (ls lineSlice) asWKB() []byte {
57
58 size := 1 + 4 + 4 + len(ls)*(2*8)
59
60 buf := bytes.NewBuffer(make([]byte, 0, size))
61 ls.toWKB(buf)
58 62
59 return buf.Bytes() 63 return buf.Bytes()
60 } 64 }
61 65
62 func (ls lineSlice) LinearRingGeom() wkb.LinearRingGeom { 66 func (ls lineSlice) LinearRingGeom() wkb.LinearRingGeom {
64 for i, v := range ls { 68 for i, v := range ls {
65 lr[i].X = v[0] 69 lr[i].X = v[0]
66 lr[i].Y = v[1] 70 lr[i].Y = v[1]
67 } 71 }
68 return lr 72 return lr
73 }
74
75 func (mls multiLineSlice) asWKB() []byte {
76
77 size := 1 + 4 + 4
78
79 for _, ls := range mls {
80 size += 1 + 4 + 4 + len(ls)*(2*8)
81 }
82
83 buf := bytes.NewBuffer(make([]byte, 0, size))
84
85 binary.Write(buf, binary.LittleEndian, wkb.NDR)
86 binary.Write(buf, binary.LittleEndian, wkb.MultiLineString)
87 binary.Write(buf, binary.LittleEndian, uint32(len(mls)))
88
89 for _, ls := range mls {
90 ls.toWKB(buf)
91 }
92
93 return buf.Bytes()
69 } 94 }
70 95
71 func (p pointSlice) asWKB() []byte { 96 func (p pointSlice) asWKB() []byte {
72 97
73 size := 1 + 4 + 2*8 98 size := 1 + 4 + 2*8