-
Notifications
You must be signed in to change notification settings - Fork 0
/
multi_polygon.go
106 lines (87 loc) · 2.26 KB
/
multi_polygon.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package ewkb
import (
"database/sql/driver"
"errors"
"fmt"
"github.com/kcasctiv/go-ewkb/geo"
)
// MultiPolygon presents MultiPolygon geometry object
type MultiPolygon struct {
header
mp geo.MultiPolygon
}
// NewMultiPolygon returns new MultiPolygon,
// created from geometry base and coords data
func NewMultiPolygon(b Base, mp geo.MultiPolygon) MultiPolygon {
return MultiPolygon{
header: header{
byteOrder: b.ByteOrder(),
wkbType: getFlags(
b.HasZ(),
b.HasM(),
b.HasSRID(),
) | MultiPolygonType,
srid: b.SRID(),
},
mp: mp,
}
}
// Polygon returns polygon with specified index
func (p *MultiPolygon) Polygon(idx int) geo.Polygon { return p.mp.Polygon(idx) }
// Len returns count of polygons
func (p *MultiPolygon) Len() int { return p.mp.Len() }
// String returns WKT/EWKT geometry representation
func (p *MultiPolygon) String() string {
var s string
if p.HasSRID() {
s = fmt.Sprintf("SRID=%d;", p.srid)
}
s += "MULTIPOLYGON"
if !p.HasZ() && p.HasM() {
s += "M"
}
if p.Len() == 0 {
s += " EMPTY"
return s
}
s += "("
if p.Len() > 0 {
for idx := 0; idx < p.Len(); idx++ {
s += printPolygon(p.Polygon(idx), p.HasZ(), p.HasM()) + ","
}
s = s[:len(s)-1]
}
return s + ")"
}
// Scan implements sql.Scanner interface
func (p *MultiPolygon) Scan(src interface{}) error {
return scanGeometry(src, p)
}
// Value implements sql driver.Valuer interface
func (p *MultiPolygon) Value() (driver.Value, error) {
return p.String(), nil
}
// UnmarshalBinary implements encoding.BinaryUnmarshaler interface
func (p *MultiPolygon) UnmarshalBinary(data []byte) error {
h, byteOrder, offset := readHeader(data)
if h.Type() != MultiPolygonType {
return errors.New("not expected geometry type")
}
p.header = h
var err error
p.mp, _, err = readMultiPolygon(
data[offset:],
byteOrder,
getReadPointFunc(h.wkbType),
)
return err
}
// MarshalBinary implements encoding.BinaryMarshaler interface
func (p *MultiPolygon) MarshalBinary() ([]byte, error) {
size := headerSize(p.HasSRID()) + multiPolygonSize(p, p.HasZ(), p.HasM())
b := make([]byte, size)
byteOrder := getBinaryByteOrder(p.ByteOrder())
offset := writeHeader(p, p.Type(), byteOrder, p.HasSRID(), b)
writeMultiPolygon(p, byteOrder, p.HasZ(), p.HasM(), b[offset:])
return b, nil
}