annotate pkg/imports/wkb.go @ 5670:b75d0b303328

Various fixes and improvements of gauges import: - Allow update of erased data (and thereby set erased to false) - Fix source_organization to work with ERDMS2 - Give ISRS of new and updated gauges in summary - Fixed reference of null pointers if revlevels are missing - Fixed reference of null pointer on update errors - Added ISRS to reference_code warning
author Sascha Wilde <wilde@sha-bang.de>
date Fri, 08 Dec 2023 17:29:56 +0100
parents 41a67619c170
children 6270951dda28
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1785
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
1 // This is Free Software under GNU Affero General Public License v >= 3.0
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
2 // without warranty, see README.md and license for details.
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
3 //
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
4 // SPDX-License-Identifier: AGPL-3.0-or-later
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
5 // License-Filename: LICENSES/AGPL-3.0.txt
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
6 //
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
7 // Copyright (C) 2018 by via donau
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
8 // – Österreichische Wasserstraßen-Gesellschaft mbH
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
9 // Software engineering by Intevation GmbH
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
10 //
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
11 // Author(s):
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
12 // * Sascha L. Teichmann <sascha.teichmann@intevation.de>
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
13
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
14 package imports
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
15
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
16 import (
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
17 "bytes"
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
18 "encoding/binary"
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
19 "fmt"
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
20 "math"
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
21
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
22 shp "github.com/jonas-p/go-shp"
2471
63475c8e710e Moved WKB constants to own package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1860
diff changeset
23
63475c8e710e Moved WKB constants to own package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1860
diff changeset
24 "gemma.intevation.de/gemma/pkg/wkb"
1785
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
25 )
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
26
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
27 type (
5270
41a67619c170 Added multi geometry support for some imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5016
diff changeset
28 pointSlice []float64
41a67619c170 Added multi geometry support for some imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5016
diff changeset
29 lineSlice [][]float64
41a67619c170 Added multi geometry support for some imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5016
diff changeset
30 multiLineSlice []lineSlice
41a67619c170 Added multi geometry support for some imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5016
diff changeset
31 polygonSlice [][][]float64
41a67619c170 Added multi geometry support for some imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5016
diff changeset
32 multiPolygonSlice []polygonSlice
1785
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
33 )
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
34
5007
799e8248de8d First step to move geometry creation for WFS features to loader.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4977
diff changeset
35 func newPointFeature(newProperties func() interface{}) func() (string, interface{}) {
799e8248de8d First step to move geometry creation for WFS features to loader.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4977
diff changeset
36 return func() (string, interface{}) { return "Point", newProperties() }
4977
4bf6cde2d996 Generalize WFS point import job to be able to import other geometry types, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4852
diff changeset
37 }
4bf6cde2d996 Generalize WFS point import job to be able to import other geometry types, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4852
diff changeset
38
5016
cf25b23e3eec Keep historic data of waterway axis
Tom Gottfried <tom@intevation.de>
parents: 5008
diff changeset
39 func newMultiLineFeature(
cf25b23e3eec Keep historic data of waterway axis
Tom Gottfried <tom@intevation.de>
parents: 5008
diff changeset
40 newProperties func() interface{},
cf25b23e3eec Keep historic data of waterway axis
Tom Gottfried <tom@intevation.de>
parents: 5008
diff changeset
41 ) func() (string, interface{}) {
cf25b23e3eec Keep historic data of waterway axis
Tom Gottfried <tom@intevation.de>
parents: 5008
diff changeset
42 return func() (string, interface{}) {
cf25b23e3eec Keep historic data of waterway axis
Tom Gottfried <tom@intevation.de>
parents: 5008
diff changeset
43 return "MultiLineString", newProperties()
cf25b23e3eec Keep historic data of waterway axis
Tom Gottfried <tom@intevation.de>
parents: 5008
diff changeset
44 }
cf25b23e3eec Keep historic data of waterway axis
Tom Gottfried <tom@intevation.de>
parents: 5008
diff changeset
45 }
cf25b23e3eec Keep historic data of waterway axis
Tom Gottfried <tom@intevation.de>
parents: 5008
diff changeset
46
5008
0b97f5301a17 Add a multiLineSlice type to be serialized as WKB in importers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5007
diff changeset
47 func (ls lineSlice) toWKB(buf *bytes.Buffer) {
2471
63475c8e710e Moved WKB constants to own package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1860
diff changeset
48 binary.Write(buf, binary.LittleEndian, wkb.NDR)
63475c8e710e Moved WKB constants to own package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1860
diff changeset
49 binary.Write(buf, binary.LittleEndian, wkb.LineString)
4852
046a07a33b19 Fixed the golint issues of the imports package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4313
diff changeset
50 binary.Write(buf, binary.LittleEndian, uint32(len(ls)))
1785
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
51
4852
046a07a33b19 Fixed the golint issues of the imports package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4313
diff changeset
52 for _, c := range ls {
1785
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
53 var lat, lon float64
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
54 if len(c) > 0 {
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
55 lat = c[0]
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
56 }
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
57 if len(c) > 1 {
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
58 lon = c[1]
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
59 }
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
60 binary.Write(buf, binary.LittleEndian, math.Float64bits(lat))
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
61 binary.Write(buf, binary.LittleEndian, math.Float64bits(lon))
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
62 }
5008
0b97f5301a17 Add a multiLineSlice type to be serialized as WKB in importers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5007
diff changeset
63 }
0b97f5301a17 Add a multiLineSlice type to be serialized as WKB in importers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5007
diff changeset
64
0b97f5301a17 Add a multiLineSlice type to be serialized as WKB in importers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5007
diff changeset
65 func (ls lineSlice) asWKB() []byte {
0b97f5301a17 Add a multiLineSlice type to be serialized as WKB in importers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5007
diff changeset
66
0b97f5301a17 Add a multiLineSlice type to be serialized as WKB in importers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5007
diff changeset
67 size := 1 + 4 + 4 + len(ls)*(2*8)
0b97f5301a17 Add a multiLineSlice type to be serialized as WKB in importers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5007
diff changeset
68
0b97f5301a17 Add a multiLineSlice type to be serialized as WKB in importers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5007
diff changeset
69 buf := bytes.NewBuffer(make([]byte, 0, size))
0b97f5301a17 Add a multiLineSlice type to be serialized as WKB in importers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5007
diff changeset
70 ls.toWKB(buf)
1785
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
71
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
72 return buf.Bytes()
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
73 }
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
74
4313
5da02dcc51f6 shape upload stretch import: Started to decode geometries and attributes from uploaded shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4177
diff changeset
75 func (ls lineSlice) LinearRingGeom() wkb.LinearRingGeom {
5da02dcc51f6 shape upload stretch import: Started to decode geometries and attributes from uploaded shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4177
diff changeset
76 lr := make(wkb.LinearRingGeom, len(ls))
5da02dcc51f6 shape upload stretch import: Started to decode geometries and attributes from uploaded shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4177
diff changeset
77 for i, v := range ls {
5da02dcc51f6 shape upload stretch import: Started to decode geometries and attributes from uploaded shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4177
diff changeset
78 lr[i].X = v[0]
5da02dcc51f6 shape upload stretch import: Started to decode geometries and attributes from uploaded shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4177
diff changeset
79 lr[i].Y = v[1]
5da02dcc51f6 shape upload stretch import: Started to decode geometries and attributes from uploaded shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4177
diff changeset
80 }
5da02dcc51f6 shape upload stretch import: Started to decode geometries and attributes from uploaded shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4177
diff changeset
81 return lr
5da02dcc51f6 shape upload stretch import: Started to decode geometries and attributes from uploaded shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4177
diff changeset
82 }
5da02dcc51f6 shape upload stretch import: Started to decode geometries and attributes from uploaded shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4177
diff changeset
83
5008
0b97f5301a17 Add a multiLineSlice type to be serialized as WKB in importers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5007
diff changeset
84 func (mls multiLineSlice) asWKB() []byte {
0b97f5301a17 Add a multiLineSlice type to be serialized as WKB in importers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5007
diff changeset
85
0b97f5301a17 Add a multiLineSlice type to be serialized as WKB in importers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5007
diff changeset
86 size := 1 + 4 + 4
0b97f5301a17 Add a multiLineSlice type to be serialized as WKB in importers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5007
diff changeset
87
0b97f5301a17 Add a multiLineSlice type to be serialized as WKB in importers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5007
diff changeset
88 for _, ls := range mls {
0b97f5301a17 Add a multiLineSlice type to be serialized as WKB in importers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5007
diff changeset
89 size += 1 + 4 + 4 + len(ls)*(2*8)
0b97f5301a17 Add a multiLineSlice type to be serialized as WKB in importers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5007
diff changeset
90 }
0b97f5301a17 Add a multiLineSlice type to be serialized as WKB in importers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5007
diff changeset
91
0b97f5301a17 Add a multiLineSlice type to be serialized as WKB in importers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5007
diff changeset
92 buf := bytes.NewBuffer(make([]byte, 0, size))
0b97f5301a17 Add a multiLineSlice type to be serialized as WKB in importers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5007
diff changeset
93
0b97f5301a17 Add a multiLineSlice type to be serialized as WKB in importers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5007
diff changeset
94 binary.Write(buf, binary.LittleEndian, wkb.NDR)
0b97f5301a17 Add a multiLineSlice type to be serialized as WKB in importers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5007
diff changeset
95 binary.Write(buf, binary.LittleEndian, wkb.MultiLineString)
0b97f5301a17 Add a multiLineSlice type to be serialized as WKB in importers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5007
diff changeset
96 binary.Write(buf, binary.LittleEndian, uint32(len(mls)))
0b97f5301a17 Add a multiLineSlice type to be serialized as WKB in importers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5007
diff changeset
97
0b97f5301a17 Add a multiLineSlice type to be serialized as WKB in importers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5007
diff changeset
98 for _, ls := range mls {
0b97f5301a17 Add a multiLineSlice type to be serialized as WKB in importers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5007
diff changeset
99 ls.toWKB(buf)
0b97f5301a17 Add a multiLineSlice type to be serialized as WKB in importers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5007
diff changeset
100 }
0b97f5301a17 Add a multiLineSlice type to be serialized as WKB in importers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5007
diff changeset
101
0b97f5301a17 Add a multiLineSlice type to be serialized as WKB in importers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5007
diff changeset
102 return buf.Bytes()
0b97f5301a17 Add a multiLineSlice type to be serialized as WKB in importers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5007
diff changeset
103 }
0b97f5301a17 Add a multiLineSlice type to be serialized as WKB in importers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5007
diff changeset
104
1860
f54ac71db1ac Importer: Added point geoemtry to wkb datatypes.
Raimund Renkert <raimund.renkert@intevation.de>
parents: 1786
diff changeset
105 func (p pointSlice) asWKB() []byte {
f54ac71db1ac Importer: Added point geoemtry to wkb datatypes.
Raimund Renkert <raimund.renkert@intevation.de>
parents: 1786
diff changeset
106
f54ac71db1ac Importer: Added point geoemtry to wkb datatypes.
Raimund Renkert <raimund.renkert@intevation.de>
parents: 1786
diff changeset
107 size := 1 + 4 + 2*8
f54ac71db1ac Importer: Added point geoemtry to wkb datatypes.
Raimund Renkert <raimund.renkert@intevation.de>
parents: 1786
diff changeset
108
f54ac71db1ac Importer: Added point geoemtry to wkb datatypes.
Raimund Renkert <raimund.renkert@intevation.de>
parents: 1786
diff changeset
109 buf := bytes.NewBuffer(make([]byte, 0, size))
f54ac71db1ac Importer: Added point geoemtry to wkb datatypes.
Raimund Renkert <raimund.renkert@intevation.de>
parents: 1786
diff changeset
110
2471
63475c8e710e Moved WKB constants to own package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1860
diff changeset
111 binary.Write(buf, binary.LittleEndian, wkb.NDR)
63475c8e710e Moved WKB constants to own package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1860
diff changeset
112 binary.Write(buf, binary.LittleEndian, wkb.Point)
1860
f54ac71db1ac Importer: Added point geoemtry to wkb datatypes.
Raimund Renkert <raimund.renkert@intevation.de>
parents: 1786
diff changeset
113
f54ac71db1ac Importer: Added point geoemtry to wkb datatypes.
Raimund Renkert <raimund.renkert@intevation.de>
parents: 1786
diff changeset
114 var lat, lon float64
f54ac71db1ac Importer: Added point geoemtry to wkb datatypes.
Raimund Renkert <raimund.renkert@intevation.de>
parents: 1786
diff changeset
115 if len(p) > 0 {
f54ac71db1ac Importer: Added point geoemtry to wkb datatypes.
Raimund Renkert <raimund.renkert@intevation.de>
parents: 1786
diff changeset
116 lat = p[0]
f54ac71db1ac Importer: Added point geoemtry to wkb datatypes.
Raimund Renkert <raimund.renkert@intevation.de>
parents: 1786
diff changeset
117 }
f54ac71db1ac Importer: Added point geoemtry to wkb datatypes.
Raimund Renkert <raimund.renkert@intevation.de>
parents: 1786
diff changeset
118 if len(p) > 1 {
f54ac71db1ac Importer: Added point geoemtry to wkb datatypes.
Raimund Renkert <raimund.renkert@intevation.de>
parents: 1786
diff changeset
119 lon = p[1]
f54ac71db1ac Importer: Added point geoemtry to wkb datatypes.
Raimund Renkert <raimund.renkert@intevation.de>
parents: 1786
diff changeset
120 }
f54ac71db1ac Importer: Added point geoemtry to wkb datatypes.
Raimund Renkert <raimund.renkert@intevation.de>
parents: 1786
diff changeset
121 binary.Write(buf, binary.LittleEndian, math.Float64bits(lat))
f54ac71db1ac Importer: Added point geoemtry to wkb datatypes.
Raimund Renkert <raimund.renkert@intevation.de>
parents: 1786
diff changeset
122 binary.Write(buf, binary.LittleEndian, math.Float64bits(lon))
f54ac71db1ac Importer: Added point geoemtry to wkb datatypes.
Raimund Renkert <raimund.renkert@intevation.de>
parents: 1786
diff changeset
123
f54ac71db1ac Importer: Added point geoemtry to wkb datatypes.
Raimund Renkert <raimund.renkert@intevation.de>
parents: 1786
diff changeset
124 return buf.Bytes()
f54ac71db1ac Importer: Added point geoemtry to wkb datatypes.
Raimund Renkert <raimund.renkert@intevation.de>
parents: 1786
diff changeset
125 }
f54ac71db1ac Importer: Added point geoemtry to wkb datatypes.
Raimund Renkert <raimund.renkert@intevation.de>
parents: 1786
diff changeset
126
4852
046a07a33b19 Fixed the golint issues of the imports package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4313
diff changeset
127 func (ps polygonSlice) asWKB() []byte {
046a07a33b19 Fixed the golint issues of the imports package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4313
diff changeset
128 if ps == nil {
1785
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
129 return nil
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
130 }
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
131 // pre-calculate size to avoid reallocations.
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
132 size := 1 + 4 + 4
4852
046a07a33b19 Fixed the golint issues of the imports package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4313
diff changeset
133 for _, ring := range ps {
1785
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
134 size += 4 + len(ring)*2*8
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
135 }
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
136
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
137 buf := bytes.NewBuffer(make([]byte, 0, size))
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
138
2471
63475c8e710e Moved WKB constants to own package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1860
diff changeset
139 binary.Write(buf, binary.LittleEndian, wkb.NDR)
63475c8e710e Moved WKB constants to own package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1860
diff changeset
140 binary.Write(buf, binary.LittleEndian, wkb.Polygon)
4852
046a07a33b19 Fixed the golint issues of the imports package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4313
diff changeset
141 binary.Write(buf, binary.LittleEndian, uint32(len(ps)))
1785
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
142
4852
046a07a33b19 Fixed the golint issues of the imports package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4313
diff changeset
143 for _, ring := range ps {
1785
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
144 binary.Write(buf, binary.LittleEndian, uint32(len(ring)))
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
145 for _, v := range ring {
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
146 var lat, lon float64
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
147 if len(v) > 0 {
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
148 lat = v[0]
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
149 }
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
150 if len(v) > 1 {
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
151 lon = v[1]
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
152 }
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
153 binary.Write(buf, binary.LittleEndian, math.Float64bits(lat))
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
154 binary.Write(buf, binary.LittleEndian, math.Float64bits(lon))
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
155 }
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
156 }
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
157
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
158 return buf.Bytes()
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
159 }
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
160
1786
09349ca27dd7 Imports: Removed duplicated code path to store WKB polygons into database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1785
diff changeset
161 func shapeToPolygon(s shp.Shape) (polygonSlice, error) {
1785
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
162 switch p := s.(type) {
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
163 case *shp.Polygon:
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
164 return toPolygon(p.NumParts, p.Parts, p.Points), nil
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
165 case *shp.PolygonZ:
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
166 return toPolygon(p.NumParts, p.Parts, p.Points), nil
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
167 case *shp.PolygonM:
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
168 return toPolygon(p.NumParts, p.Parts, p.Points), nil
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
169 }
4177
8b75ac5e243e Made 'staticcheck' happy with pgxutils package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2471
diff changeset
170 return nil, fmt.Errorf("unsupported shape type %T", s)
1785
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
171 }
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
172
1786
09349ca27dd7 Imports: Removed duplicated code path to store WKB polygons into database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1785
diff changeset
173 func toPolygon(numParts int32, parts []int32, points []shp.Point) polygonSlice {
09349ca27dd7 Imports: Removed duplicated code path to store WKB polygons into database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1785
diff changeset
174 out := make(polygonSlice, numParts)
1785
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
175 var pos int32
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
176
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
177 for i := range out {
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
178 var howMany int32
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
179 if i+1 >= len(parts) {
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
180 howMany = int32(len(points)) - pos
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
181 } else {
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
182 howMany = parts[i+1] - parts[i]
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
183 }
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
184
1786
09349ca27dd7 Imports: Removed duplicated code path to store WKB polygons into database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1785
diff changeset
185 line := make([][]float64, howMany)
09349ca27dd7 Imports: Removed duplicated code path to store WKB polygons into database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1785
diff changeset
186 vertices := make([]float64, 2*howMany)
1785
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
187 for j := int32(0); j < howMany; j, pos = j+1, pos+1 {
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
188 p := &points[pos]
1786
09349ca27dd7 Imports: Removed duplicated code path to store WKB polygons into database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1785
diff changeset
189 vertex := vertices[j*2 : j*2+2]
09349ca27dd7 Imports: Removed duplicated code path to store WKB polygons into database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1785
diff changeset
190 vertex[0], vertex[1] = p.X, p.Y
09349ca27dd7 Imports: Removed duplicated code path to store WKB polygons into database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1785
diff changeset
191 line[j] = vertex
1785
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
192 }
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
193 out[i] = line
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
194 }
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
195 return out
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
196 }
4313
5da02dcc51f6 shape upload stretch import: Started to decode geometries and attributes from uploaded shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4177
diff changeset
197
5da02dcc51f6 shape upload stretch import: Started to decode geometries and attributes from uploaded shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4177
diff changeset
198 func (ps polygonSlice) MultiPolygonGeom() wkb.MultiPolygonGeom {
5da02dcc51f6 shape upload stretch import: Started to decode geometries and attributes from uploaded shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4177
diff changeset
199
5da02dcc51f6 shape upload stretch import: Started to decode geometries and attributes from uploaded shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4177
diff changeset
200 var mp wkb.MultiPolygonGeom
5da02dcc51f6 shape upload stretch import: Started to decode geometries and attributes from uploaded shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4177
diff changeset
201 var curr wkb.PolygonGeom
5da02dcc51f6 shape upload stretch import: Started to decode geometries and attributes from uploaded shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4177
diff changeset
202
5da02dcc51f6 shape upload stretch import: Started to decode geometries and attributes from uploaded shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4177
diff changeset
203 for _, r := range ps {
5da02dcc51f6 shape upload stretch import: Started to decode geometries and attributes from uploaded shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4177
diff changeset
204 lr := lineSlice(r).LinearRingGeom()
5da02dcc51f6 shape upload stretch import: Started to decode geometries and attributes from uploaded shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4177
diff changeset
205 // A counter clockwise ring starts a new polygon.
5da02dcc51f6 shape upload stretch import: Started to decode geometries and attributes from uploaded shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4177
diff changeset
206 if lr.CCW() {
5da02dcc51f6 shape upload stretch import: Started to decode geometries and attributes from uploaded shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4177
diff changeset
207 if len(curr) > 0 {
5da02dcc51f6 shape upload stretch import: Started to decode geometries and attributes from uploaded shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4177
diff changeset
208 mp = append(mp, curr)
5da02dcc51f6 shape upload stretch import: Started to decode geometries and attributes from uploaded shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4177
diff changeset
209 curr = wkb.PolygonGeom{}
5da02dcc51f6 shape upload stretch import: Started to decode geometries and attributes from uploaded shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4177
diff changeset
210 }
5da02dcc51f6 shape upload stretch import: Started to decode geometries and attributes from uploaded shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4177
diff changeset
211 }
5da02dcc51f6 shape upload stretch import: Started to decode geometries and attributes from uploaded shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4177
diff changeset
212 curr = append(curr, lr)
5da02dcc51f6 shape upload stretch import: Started to decode geometries and attributes from uploaded shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4177
diff changeset
213 }
5da02dcc51f6 shape upload stretch import: Started to decode geometries and attributes from uploaded shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4177
diff changeset
214
5da02dcc51f6 shape upload stretch import: Started to decode geometries and attributes from uploaded shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4177
diff changeset
215 if len(curr) > 0 {
5da02dcc51f6 shape upload stretch import: Started to decode geometries and attributes from uploaded shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4177
diff changeset
216 mp = append(mp, curr)
5da02dcc51f6 shape upload stretch import: Started to decode geometries and attributes from uploaded shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4177
diff changeset
217 }
5da02dcc51f6 shape upload stretch import: Started to decode geometries and attributes from uploaded shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4177
diff changeset
218
5da02dcc51f6 shape upload stretch import: Started to decode geometries and attributes from uploaded shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4177
diff changeset
219 return mp
5da02dcc51f6 shape upload stretch import: Started to decode geometries and attributes from uploaded shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4177
diff changeset
220 }