Mercurial > gemma
annotate pkg/imports/polygon.go @ 1639:3c9e33b4cea0
fix: make length check explicit
author | Thomas Junk <thomas.junk@intevation.de> |
---|---|
date | Thu, 20 Dec 2018 12:50:20 +0100 |
parents | 20c98c2964f7 |
children |
rev | line source |
---|---|
1017
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
972
diff
changeset
|
1 // This is Free Software under GNU Affero General Public License v >= 3.0 |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
972
diff
changeset
|
2 // without warranty, see README.md and license for details. |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
972
diff
changeset
|
3 // |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
972
diff
changeset
|
4 // SPDX-License-Identifier: AGPL-3.0-or-later |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
972
diff
changeset
|
5 // License-Filename: LICENSES/AGPL-3.0.txt |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
972
diff
changeset
|
6 // |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
972
diff
changeset
|
7 // Copyright (C) 2018 by via donau |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
972
diff
changeset
|
8 // – Österreichische Wasserstraßen-Gesellschaft mbH |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
972
diff
changeset
|
9 // Software engineering by Intevation GmbH |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
972
diff
changeset
|
10 // |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
972
diff
changeset
|
11 // Author(s): |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
972
diff
changeset
|
12 // * Sascha L. Teichmann <sascha.teichmann@intevation.de> |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
972
diff
changeset
|
13 |
972
17a03a84b0e8
Split out polygon code out of sounding result importer source file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
14 package imports |
17a03a84b0e8
Split out polygon code out of sounding result importer source file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
15 |
17a03a84b0e8
Split out polygon code out of sounding result importer source file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
16 import ( |
17a03a84b0e8
Split out polygon code out of sounding result importer source file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
17 "bytes" |
17a03a84b0e8
Split out polygon code out of sounding result importer source file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
18 "encoding/binary" |
17a03a84b0e8
Split out polygon code out of sounding result importer source file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
19 "fmt" |
17a03a84b0e8
Split out polygon code out of sounding result importer source file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
20 "math" |
17a03a84b0e8
Split out polygon code out of sounding result importer source file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
21 |
17a03a84b0e8
Split out polygon code out of sounding result importer source file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
22 shp "github.com/jonas-p/go-shp" |
17a03a84b0e8
Split out polygon code out of sounding result importer source file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
23 ) |
17a03a84b0e8
Split out polygon code out of sounding result importer source file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
24 |
17a03a84b0e8
Split out polygon code out of sounding result importer source file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
25 type ( |
1496
3bf1f0de0763
Un-export point, line string and polygon types in import package as these are only used locally.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1017
diff
changeset
|
26 point struct { |
972
17a03a84b0e8
Split out polygon code out of sounding result importer source file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
27 X float64 |
17a03a84b0e8
Split out polygon code out of sounding result importer source file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
28 Y float64 |
17a03a84b0e8
Split out polygon code out of sounding result importer source file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
29 } |
1496
3bf1f0de0763
Un-export point, line string and polygon types in import package as these are only used locally.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1017
diff
changeset
|
30 lineString []point |
3bf1f0de0763
Un-export point, line string and polygon types in import package as these are only used locally.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1017
diff
changeset
|
31 polygon []lineString |
972
17a03a84b0e8
Split out polygon code out of sounding result importer source file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
32 ) |
17a03a84b0e8
Split out polygon code out of sounding result importer source file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
33 |
17a03a84b0e8
Split out polygon code out of sounding result importer source file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
34 const ( |
17a03a84b0e8
Split out polygon code out of sounding result importer source file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
35 wkbNDR byte = 1 |
17a03a84b0e8
Split out polygon code out of sounding result importer source file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
36 wkbPolygon uint32 = 3 |
17a03a84b0e8
Split out polygon code out of sounding result importer source file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
37 ) |
17a03a84b0e8
Split out polygon code out of sounding result importer source file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
38 |
1496
3bf1f0de0763
Un-export point, line string and polygon types in import package as these are only used locally.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1017
diff
changeset
|
39 func shapeToPolygon(s shp.Shape) (polygon, error) { |
972
17a03a84b0e8
Split out polygon code out of sounding result importer source file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
40 switch p := s.(type) { |
17a03a84b0e8
Split out polygon code out of sounding result importer source file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
41 case *shp.Polygon: |
17a03a84b0e8
Split out polygon code out of sounding result importer source file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
42 return toPolygon(p.NumParts, p.Parts, p.Points), nil |
17a03a84b0e8
Split out polygon code out of sounding result importer source file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
43 case *shp.PolygonZ: |
17a03a84b0e8
Split out polygon code out of sounding result importer source file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
44 return toPolygon(p.NumParts, p.Parts, p.Points), nil |
17a03a84b0e8
Split out polygon code out of sounding result importer source file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
45 case *shp.PolygonM: |
17a03a84b0e8
Split out polygon code out of sounding result importer source file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
46 return toPolygon(p.NumParts, p.Parts, p.Points), nil |
17a03a84b0e8
Split out polygon code out of sounding result importer source file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
47 } |
17a03a84b0e8
Split out polygon code out of sounding result importer source file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
48 return nil, fmt.Errorf("Unsupported shape type %T", s) |
17a03a84b0e8
Split out polygon code out of sounding result importer source file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
49 } |
17a03a84b0e8
Split out polygon code out of sounding result importer source file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
50 |
1496
3bf1f0de0763
Un-export point, line string and polygon types in import package as these are only used locally.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1017
diff
changeset
|
51 func toPolygon(numParts int32, parts []int32, points []shp.Point) polygon { |
3bf1f0de0763
Un-export point, line string and polygon types in import package as these are only used locally.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1017
diff
changeset
|
52 out := make(polygon, numParts) |
1622
a3a3fc630620
Sounding result import: Fixed reading bounding polygon from clipping shape.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1496
diff
changeset
|
53 var pos int32 |
a3a3fc630620
Sounding result import: Fixed reading bounding polygon from clipping shape.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1496
diff
changeset
|
54 |
972
17a03a84b0e8
Split out polygon code out of sounding result importer source file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
55 for i := range out { |
1622
a3a3fc630620
Sounding result import: Fixed reading bounding polygon from clipping shape.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1496
diff
changeset
|
56 var howMany int32 |
a3a3fc630620
Sounding result import: Fixed reading bounding polygon from clipping shape.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1496
diff
changeset
|
57 if i+1 >= len(parts) { |
a3a3fc630620
Sounding result import: Fixed reading bounding polygon from clipping shape.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1496
diff
changeset
|
58 howMany = int32(len(points)) - pos |
a3a3fc630620
Sounding result import: Fixed reading bounding polygon from clipping shape.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1496
diff
changeset
|
59 } else { |
1623
20c98c2964f7
More obvious calculation of part length in clipping polygon segments.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1622
diff
changeset
|
60 howMany = parts[i+1] - parts[i] |
1622
a3a3fc630620
Sounding result import: Fixed reading bounding polygon from clipping shape.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1496
diff
changeset
|
61 } |
a3a3fc630620
Sounding result import: Fixed reading bounding polygon from clipping shape.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1496
diff
changeset
|
62 |
a3a3fc630620
Sounding result import: Fixed reading bounding polygon from clipping shape.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1496
diff
changeset
|
63 line := make(lineString, howMany) |
a3a3fc630620
Sounding result import: Fixed reading bounding polygon from clipping shape.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1496
diff
changeset
|
64 for j := int32(0); j < howMany; j, pos = j+1, pos+1 { |
972
17a03a84b0e8
Split out polygon code out of sounding result importer source file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
65 p := &points[pos] |
1496
3bf1f0de0763
Un-export point, line string and polygon types in import package as these are only used locally.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1017
diff
changeset
|
66 line[j] = point{p.X, p.Y} |
972
17a03a84b0e8
Split out polygon code out of sounding result importer source file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
67 } |
17a03a84b0e8
Split out polygon code out of sounding result importer source file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
68 out[i] = line |
17a03a84b0e8
Split out polygon code out of sounding result importer source file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
69 } |
17a03a84b0e8
Split out polygon code out of sounding result importer source file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
70 return out |
17a03a84b0e8
Split out polygon code out of sounding result importer source file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
71 } |
17a03a84b0e8
Split out polygon code out of sounding result importer source file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
72 |
1496
3bf1f0de0763
Un-export point, line string and polygon types in import package as these are only used locally.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1017
diff
changeset
|
73 func (p polygon) asWKB() []byte { |
972
17a03a84b0e8
Split out polygon code out of sounding result importer source file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
74 if p == nil { |
17a03a84b0e8
Split out polygon code out of sounding result importer source file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
75 return nil |
17a03a84b0e8
Split out polygon code out of sounding result importer source file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
76 } |
17a03a84b0e8
Split out polygon code out of sounding result importer source file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
77 // pre-calculate size to avoid reallocations. |
17a03a84b0e8
Split out polygon code out of sounding result importer source file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
78 size := 1 + 4 + 4 |
17a03a84b0e8
Split out polygon code out of sounding result importer source file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
79 for _, ring := range p { |
17a03a84b0e8
Split out polygon code out of sounding result importer source file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
80 size += 4 + len(ring)*2*8 |
17a03a84b0e8
Split out polygon code out of sounding result importer source file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
81 } |
17a03a84b0e8
Split out polygon code out of sounding result importer source file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
82 |
17a03a84b0e8
Split out polygon code out of sounding result importer source file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
83 buf := bytes.NewBuffer(make([]byte, 0, size)) |
17a03a84b0e8
Split out polygon code out of sounding result importer source file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
84 |
17a03a84b0e8
Split out polygon code out of sounding result importer source file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
85 binary.Write(buf, binary.LittleEndian, wkbNDR) |
17a03a84b0e8
Split out polygon code out of sounding result importer source file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
86 binary.Write(buf, binary.LittleEndian, wkbPolygon) |
17a03a84b0e8
Split out polygon code out of sounding result importer source file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
87 binary.Write(buf, binary.LittleEndian, uint32(len(p))) |
17a03a84b0e8
Split out polygon code out of sounding result importer source file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
88 |
17a03a84b0e8
Split out polygon code out of sounding result importer source file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
89 for _, ring := range p { |
17a03a84b0e8
Split out polygon code out of sounding result importer source file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
90 binary.Write(buf, binary.LittleEndian, uint32(len(ring))) |
17a03a84b0e8
Split out polygon code out of sounding result importer source file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
91 for _, v := range ring { |
17a03a84b0e8
Split out polygon code out of sounding result importer source file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
92 binary.Write(buf, binary.LittleEndian, math.Float64bits(v.X)) |
17a03a84b0e8
Split out polygon code out of sounding result importer source file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
93 binary.Write(buf, binary.LittleEndian, math.Float64bits(v.Y)) |
17a03a84b0e8
Split out polygon code out of sounding result importer source file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
94 } |
17a03a84b0e8
Split out polygon code out of sounding result importer source file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
95 } |
17a03a84b0e8
Split out polygon code out of sounding result importer source file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
96 |
17a03a84b0e8
Split out polygon code out of sounding result importer source file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
97 return buf.Bytes() |
17a03a84b0e8
Split out polygon code out of sounding result importer source file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
98 } |