Mercurial > gemma
annotate pkg/wkb/data.go @ 5736:55892008ec96 default tip
Fixed a bunch of corner cases in WG import.
author | Sascha Wilde <wilde@sha-bang.de> |
---|---|
date | Wed, 29 May 2024 19:02:42 +0200 |
parents | 2dd155cc95ec |
children |
rev | line source |
---|---|
4296
95786a675d70
WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
1 // This is Free Software under GNU Affero General Public License v >= 3.0 |
95786a675d70
WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
2 // without warranty, see README.md and license for details. |
95786a675d70
WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
3 // |
95786a675d70
WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
4 // SPDX-License-Identifier: AGPL-3.0-or-later |
95786a675d70
WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
5 // License-Filename: LICENSES/AGPL-3.0.txt |
95786a675d70
WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
6 // |
95786a675d70
WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
7 // Copyright (C) 2019 by via donau |
95786a675d70
WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
8 // – Österreichische Wasserstraßen-Gesellschaft mbH |
95786a675d70
WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
9 // Software engineering by Intevation GmbH |
95786a675d70
WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
10 // |
95786a675d70
WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
11 // Author(s): |
95786a675d70
WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
12 // * Sascha L. Teichmann <sascha.teichmann@intevation.de> |
95786a675d70
WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
13 |
95786a675d70
WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
14 package wkb |
95786a675d70
WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
15 |
95786a675d70
WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
16 import ( |
95786a675d70
WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
17 "bytes" |
95786a675d70
WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
18 "encoding/binary" |
95786a675d70
WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
19 "fmt" |
4300
8b730ee6f17b
Completed parsing multi polygons from WKB.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4296
diff
changeset
|
20 "math" |
4296
95786a675d70
WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
21 ) |
95786a675d70
WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
22 |
95786a675d70
WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
23 type ( |
5711
2dd155cc95ec
Fix all revive issue (w/o machine generated stuff).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5601
diff
changeset
|
24 // PointGeom is a 2D point. |
4296
95786a675d70
WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
25 PointGeom struct { |
95786a675d70
WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
26 X float64 |
95786a675d70
WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
27 Y float64 |
95786a675d70
WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
28 } |
5601
1222b777f51f
Made golint finally happy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4667
diff
changeset
|
29 // LinearRingGeom is a ring of points. |
1222b777f51f
Made golint finally happy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4667
diff
changeset
|
30 LinearRingGeom []PointGeom |
1222b777f51f
Made golint finally happy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4667
diff
changeset
|
31 // PolygonGeom is a polygon. |
1222b777f51f
Made golint finally happy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4667
diff
changeset
|
32 PolygonGeom []LinearRingGeom |
1222b777f51f
Made golint finally happy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4667
diff
changeset
|
33 // MultiPolygonGeom is a multi polygon. |
4296
95786a675d70
WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
34 MultiPolygonGeom []PolygonGeom |
95786a675d70
WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
35 ) |
95786a675d70
WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
36 |
5601
1222b777f51f
Made golint finally happy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4667
diff
changeset
|
37 // AsWKB serializes a multi polygon as WKB. |
4314
c3b5cf2f200a
shape upload stretch import: Converted multi polygon to WKB.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4313
diff
changeset
|
38 func (mpg MultiPolygonGeom) AsWKB() []byte { |
c3b5cf2f200a
shape upload stretch import: Converted multi polygon to WKB.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4313
diff
changeset
|
39 |
4331
98497ac4af3c
Multi polygon: Fixed creation of WKB representation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4316
diff
changeset
|
40 size := 1 + 4 + 4 |
4314
c3b5cf2f200a
shape upload stretch import: Converted multi polygon to WKB.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4313
diff
changeset
|
41 for _, pg := range mpg { |
4331
98497ac4af3c
Multi polygon: Fixed creation of WKB representation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4316
diff
changeset
|
42 size += 1 + 4 + 4 |
4314
c3b5cf2f200a
shape upload stretch import: Converted multi polygon to WKB.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4313
diff
changeset
|
43 for _, r := range pg { |
4331
98497ac4af3c
Multi polygon: Fixed creation of WKB representation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4316
diff
changeset
|
44 size += 4 + 2*8*len(r) |
4314
c3b5cf2f200a
shape upload stretch import: Converted multi polygon to WKB.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4313
diff
changeset
|
45 } |
c3b5cf2f200a
shape upload stretch import: Converted multi polygon to WKB.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4313
diff
changeset
|
46 } |
c3b5cf2f200a
shape upload stretch import: Converted multi polygon to WKB.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4313
diff
changeset
|
47 |
c3b5cf2f200a
shape upload stretch import: Converted multi polygon to WKB.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4313
diff
changeset
|
48 buf := bytes.NewBuffer(make([]byte, 0, size)) |
c3b5cf2f200a
shape upload stretch import: Converted multi polygon to WKB.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4313
diff
changeset
|
49 |
c3b5cf2f200a
shape upload stretch import: Converted multi polygon to WKB.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4313
diff
changeset
|
50 binary.Write(buf, binary.LittleEndian, NDR) |
c3b5cf2f200a
shape upload stretch import: Converted multi polygon to WKB.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4313
diff
changeset
|
51 binary.Write(buf, binary.LittleEndian, MultiPolygon) |
c3b5cf2f200a
shape upload stretch import: Converted multi polygon to WKB.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4313
diff
changeset
|
52 binary.Write(buf, binary.LittleEndian, uint32(len(mpg))) |
c3b5cf2f200a
shape upload stretch import: Converted multi polygon to WKB.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4313
diff
changeset
|
53 |
c3b5cf2f200a
shape upload stretch import: Converted multi polygon to WKB.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4313
diff
changeset
|
54 for _, pg := range mpg { |
c3b5cf2f200a
shape upload stretch import: Converted multi polygon to WKB.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4313
diff
changeset
|
55 binary.Write(buf, binary.LittleEndian, NDR) |
4331
98497ac4af3c
Multi polygon: Fixed creation of WKB representation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4316
diff
changeset
|
56 binary.Write(buf, binary.LittleEndian, Polygon) |
4316
3d6a2c6b436c
shape upload stretch import: Store features in database. Still broken [WIP].
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4314
diff
changeset
|
57 binary.Write(buf, binary.LittleEndian, uint32(len(pg))) |
4314
c3b5cf2f200a
shape upload stretch import: Converted multi polygon to WKB.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4313
diff
changeset
|
58 |
c3b5cf2f200a
shape upload stretch import: Converted multi polygon to WKB.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4313
diff
changeset
|
59 for _, r := range pg { |
c3b5cf2f200a
shape upload stretch import: Converted multi polygon to WKB.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4313
diff
changeset
|
60 binary.Write(buf, binary.LittleEndian, uint32(len(r))) |
c3b5cf2f200a
shape upload stretch import: Converted multi polygon to WKB.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4313
diff
changeset
|
61 for _, p := range r { |
c3b5cf2f200a
shape upload stretch import: Converted multi polygon to WKB.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4313
diff
changeset
|
62 x := math.Float64bits(p.X) |
c3b5cf2f200a
shape upload stretch import: Converted multi polygon to WKB.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4313
diff
changeset
|
63 y := math.Float64bits(p.Y) |
c3b5cf2f200a
shape upload stretch import: Converted multi polygon to WKB.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4313
diff
changeset
|
64 binary.Write(buf, binary.LittleEndian, x) |
c3b5cf2f200a
shape upload stretch import: Converted multi polygon to WKB.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4313
diff
changeset
|
65 binary.Write(buf, binary.LittleEndian, y) |
c3b5cf2f200a
shape upload stretch import: Converted multi polygon to WKB.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4313
diff
changeset
|
66 } |
c3b5cf2f200a
shape upload stretch import: Converted multi polygon to WKB.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4313
diff
changeset
|
67 } |
c3b5cf2f200a
shape upload stretch import: Converted multi polygon to WKB.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4313
diff
changeset
|
68 } |
c3b5cf2f200a
shape upload stretch import: Converted multi polygon to WKB.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4313
diff
changeset
|
69 |
c3b5cf2f200a
shape upload stretch import: Converted multi polygon to WKB.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4313
diff
changeset
|
70 return buf.Bytes() |
c3b5cf2f200a
shape upload stretch import: Converted multi polygon to WKB.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4313
diff
changeset
|
71 } |
c3b5cf2f200a
shape upload stretch import: Converted multi polygon to WKB.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4313
diff
changeset
|
72 |
5601
1222b777f51f
Made golint finally happy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4667
diff
changeset
|
73 // FromWKB deserializes a multi polygon from WKB. |
4296
95786a675d70
WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
74 func (mpg *MultiPolygonGeom) FromWKB(data []byte) error { |
95786a675d70
WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
75 r := bytes.NewReader(data) |
95786a675d70
WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
76 |
95786a675d70
WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
77 var order binary.ByteOrder |
95786a675d70
WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
78 |
4300
8b730ee6f17b
Completed parsing multi polygons from WKB.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4296
diff
changeset
|
79 switch endian, err := r.ReadByte(); { |
4296
95786a675d70
WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
80 case err != nil: |
95786a675d70
WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
81 return err |
95786a675d70
WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
82 case endian == NDR: |
95786a675d70
WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
83 order = binary.LittleEndian |
95786a675d70
WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
84 case endian == XDR: |
95786a675d70
WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
85 order = binary.BigEndian |
95786a675d70
WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
86 default: |
95786a675d70
WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
87 return fmt.Errorf("unknown byte order %x", endian) |
95786a675d70
WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
88 } |
95786a675d70
WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
89 |
95786a675d70
WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
90 var geomType uint32 |
95786a675d70
WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
91 |
4300
8b730ee6f17b
Completed parsing multi polygons from WKB.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4296
diff
changeset
|
92 switch err := binary.Read(r, order, &geomType); { |
4296
95786a675d70
WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
93 case err != nil: |
95786a675d70
WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
94 return err |
95786a675d70
WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
95 case geomType != MultiPolygon: |
95786a675d70
WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
96 return fmt.Errorf("unknown geometry type %x", geomType) |
95786a675d70
WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
97 } |
95786a675d70
WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
98 |
4331
98497ac4af3c
Multi polygon: Fixed creation of WKB representation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4316
diff
changeset
|
99 var numPolygons uint32 |
4296
95786a675d70
WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
100 if err := binary.Read(r, order, &numPolygons); err != nil { |
95786a675d70
WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
101 return err |
95786a675d70
WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
102 } |
95786a675d70
WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
103 |
95786a675d70
WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
104 polygons := make([]PolygonGeom, numPolygons) |
95786a675d70
WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
105 |
4300
8b730ee6f17b
Completed parsing multi polygons from WKB.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4296
diff
changeset
|
106 for i := range polygons { |
8b730ee6f17b
Completed parsing multi polygons from WKB.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4296
diff
changeset
|
107 switch endian, err := r.ReadByte(); { |
8b730ee6f17b
Completed parsing multi polygons from WKB.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4296
diff
changeset
|
108 case err != nil: |
8b730ee6f17b
Completed parsing multi polygons from WKB.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4296
diff
changeset
|
109 return err |
8b730ee6f17b
Completed parsing multi polygons from WKB.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4296
diff
changeset
|
110 case endian == NDR: |
8b730ee6f17b
Completed parsing multi polygons from WKB.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4296
diff
changeset
|
111 order = binary.LittleEndian |
8b730ee6f17b
Completed parsing multi polygons from WKB.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4296
diff
changeset
|
112 case endian == XDR: |
8b730ee6f17b
Completed parsing multi polygons from WKB.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4296
diff
changeset
|
113 order = binary.BigEndian |
8b730ee6f17b
Completed parsing multi polygons from WKB.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4296
diff
changeset
|
114 default: |
8b730ee6f17b
Completed parsing multi polygons from WKB.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4296
diff
changeset
|
115 return fmt.Errorf("unknown byte order %x", endian) |
8b730ee6f17b
Completed parsing multi polygons from WKB.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4296
diff
changeset
|
116 } |
8b730ee6f17b
Completed parsing multi polygons from WKB.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4296
diff
changeset
|
117 |
8b730ee6f17b
Completed parsing multi polygons from WKB.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4296
diff
changeset
|
118 switch err := binary.Read(r, order, &geomType); { |
8b730ee6f17b
Completed parsing multi polygons from WKB.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4296
diff
changeset
|
119 case err != nil: |
8b730ee6f17b
Completed parsing multi polygons from WKB.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4296
diff
changeset
|
120 return err |
8b730ee6f17b
Completed parsing multi polygons from WKB.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4296
diff
changeset
|
121 case geomType != Polygon: |
8b730ee6f17b
Completed parsing multi polygons from WKB.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4296
diff
changeset
|
122 return fmt.Errorf("unknown geometry type %x", geomType) |
8b730ee6f17b
Completed parsing multi polygons from WKB.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4296
diff
changeset
|
123 } |
8b730ee6f17b
Completed parsing multi polygons from WKB.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4296
diff
changeset
|
124 |
8b730ee6f17b
Completed parsing multi polygons from WKB.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4296
diff
changeset
|
125 var numRings uint32 |
8b730ee6f17b
Completed parsing multi polygons from WKB.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4296
diff
changeset
|
126 if err := binary.Read(r, order, &numRings); err != nil { |
8b730ee6f17b
Completed parsing multi polygons from WKB.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4296
diff
changeset
|
127 return err |
8b730ee6f17b
Completed parsing multi polygons from WKB.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4296
diff
changeset
|
128 } |
8b730ee6f17b
Completed parsing multi polygons from WKB.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4296
diff
changeset
|
129 |
8b730ee6f17b
Completed parsing multi polygons from WKB.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4296
diff
changeset
|
130 rings := make([]LinearRingGeom, numRings) |
8b730ee6f17b
Completed parsing multi polygons from WKB.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4296
diff
changeset
|
131 |
8b730ee6f17b
Completed parsing multi polygons from WKB.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4296
diff
changeset
|
132 for j := range rings { |
8b730ee6f17b
Completed parsing multi polygons from WKB.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4296
diff
changeset
|
133 var numPoints uint32 |
8b730ee6f17b
Completed parsing multi polygons from WKB.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4296
diff
changeset
|
134 if err := binary.Read(r, order, &numPoints); err != nil { |
8b730ee6f17b
Completed parsing multi polygons from WKB.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4296
diff
changeset
|
135 return err |
8b730ee6f17b
Completed parsing multi polygons from WKB.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4296
diff
changeset
|
136 } |
8b730ee6f17b
Completed parsing multi polygons from WKB.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4296
diff
changeset
|
137 points := make([]PointGeom, numPoints) |
8b730ee6f17b
Completed parsing multi polygons from WKB.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4296
diff
changeset
|
138 |
8b730ee6f17b
Completed parsing multi polygons from WKB.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4296
diff
changeset
|
139 for k := range points { |
4301
b333d48ae555
Tightend the parsing of x/y data of WKB multi polyons a bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4300
diff
changeset
|
140 var x, y uint64 |
4300
8b730ee6f17b
Completed parsing multi polygons from WKB.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4296
diff
changeset
|
141 if err := binary.Read(r, order, &x); err != nil { |
8b730ee6f17b
Completed parsing multi polygons from WKB.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4296
diff
changeset
|
142 return err |
8b730ee6f17b
Completed parsing multi polygons from WKB.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4296
diff
changeset
|
143 } |
8b730ee6f17b
Completed parsing multi polygons from WKB.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4296
diff
changeset
|
144 if err := binary.Read(r, order, &y); err != nil { |
8b730ee6f17b
Completed parsing multi polygons from WKB.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4296
diff
changeset
|
145 return err |
8b730ee6f17b
Completed parsing multi polygons from WKB.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4296
diff
changeset
|
146 } |
8b730ee6f17b
Completed parsing multi polygons from WKB.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4296
diff
changeset
|
147 points[k] = PointGeom{ |
8b730ee6f17b
Completed parsing multi polygons from WKB.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4296
diff
changeset
|
148 X: math.Float64frombits(x), |
8b730ee6f17b
Completed parsing multi polygons from WKB.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4296
diff
changeset
|
149 Y: math.Float64frombits(y), |
8b730ee6f17b
Completed parsing multi polygons from WKB.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4296
diff
changeset
|
150 } |
8b730ee6f17b
Completed parsing multi polygons from WKB.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4296
diff
changeset
|
151 } |
8b730ee6f17b
Completed parsing multi polygons from WKB.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4296
diff
changeset
|
152 rings[j] = points |
8b730ee6f17b
Completed parsing multi polygons from WKB.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4296
diff
changeset
|
153 } |
8b730ee6f17b
Completed parsing multi polygons from WKB.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4296
diff
changeset
|
154 polygons[i] = rings |
8b730ee6f17b
Completed parsing multi polygons from WKB.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4296
diff
changeset
|
155 } |
8b730ee6f17b
Completed parsing multi polygons from WKB.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4296
diff
changeset
|
156 |
4296
95786a675d70
WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
157 *mpg = polygons |
95786a675d70
WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
158 return nil |
95786a675d70
WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
159 } |
4313
5da02dcc51f6
shape upload stretch import: Started to decode geometries and attributes from uploaded shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4310
diff
changeset
|
160 |
5601
1222b777f51f
Made golint finally happy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4667
diff
changeset
|
161 // CCW tells if the ring is oriented counter clock wise. |
4313
5da02dcc51f6
shape upload stretch import: Started to decode geometries and attributes from uploaded shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4310
diff
changeset
|
162 func (lr LinearRingGeom) CCW() bool { |
5da02dcc51f6
shape upload stretch import: Started to decode geometries and attributes from uploaded shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4310
diff
changeset
|
163 var sum float64 |
5da02dcc51f6
shape upload stretch import: Started to decode geometries and attributes from uploaded shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4310
diff
changeset
|
164 for i, v1 := range lr { |
5da02dcc51f6
shape upload stretch import: Started to decode geometries and attributes from uploaded shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4310
diff
changeset
|
165 v2 := lr[(i+1)%len(lr)] |
5da02dcc51f6
shape upload stretch import: Started to decode geometries and attributes from uploaded shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4310
diff
changeset
|
166 sum += (v2.X - v1.X) * (v2.Y + v1.Y) |
5da02dcc51f6
shape upload stretch import: Started to decode geometries and attributes from uploaded shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4310
diff
changeset
|
167 } |
5da02dcc51f6
shape upload stretch import: Started to decode geometries and attributes from uploaded shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4310
diff
changeset
|
168 return sum > 0 |
5da02dcc51f6
shape upload stretch import: Started to decode geometries and attributes from uploaded shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4310
diff
changeset
|
169 } |
4667
0ddb308fed37
Reintroduced the ST_MakeValid calls when inserting the iso geometries into the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4331
diff
changeset
|
170 |
5601
1222b777f51f
Made golint finally happy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4667
diff
changeset
|
171 // Reverse changes the orientation of the ring. |
4667
0ddb308fed37
Reintroduced the ST_MakeValid calls when inserting the iso geometries into the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4331
diff
changeset
|
172 func (lr LinearRingGeom) Reverse() { |
0ddb308fed37
Reintroduced the ST_MakeValid calls when inserting the iso geometries into the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4331
diff
changeset
|
173 for i, j := 0, len(lr)-1; i < j; i, j = i+1, j-1 { |
0ddb308fed37
Reintroduced the ST_MakeValid calls when inserting the iso geometries into the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4331
diff
changeset
|
174 lr[i], lr[j] = lr[j], lr[i] |
0ddb308fed37
Reintroduced the ST_MakeValid calls when inserting the iso geometries into the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4331
diff
changeset
|
175 } |
0ddb308fed37
Reintroduced the ST_MakeValid calls when inserting the iso geometries into the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4331
diff
changeset
|
176 } |