-
Notifications
You must be signed in to change notification settings - Fork 0
/
geometry_collection.go
109 lines (90 loc) · 2.44 KB
/
geometry_collection.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
107
108
109
package ewkb
import (
"database/sql/driver"
"errors"
"fmt"
"strings"
)
// GeometryCollection presents collection of geometry objects
type GeometryCollection struct {
header
geoms []Geometry
}
// NewGeometryCollection returns new GeometryCollection,
// created from geometry base and coords data
func NewGeometryCollection(b Base, geoms []Geometry) GeometryCollection {
return GeometryCollection{
header: header{
byteOrder: b.ByteOrder(),
wkbType: getFlags(
b.HasZ(),
b.HasM(),
b.HasSRID(),
) | CollectionType,
srid: b.SRID(),
},
geoms: geoms,
}
}
// Geometry returns geometry with specified index
func (c *GeometryCollection) Geometry(idx int) Geometry { return c.geoms[idx] }
// Len returns length of collection (count of geometry objects)
func (c *GeometryCollection) Len() int { return len(c.geoms) }
// String returns WKT/EWKT geometry representation
func (c *GeometryCollection) String() string {
var s string
if c.HasSRID() {
s = fmt.Sprintf("SRID=%d;", c.srid)
}
s += "GEOMETRYCOLLECTION"
if !c.HasZ() && c.HasM() {
s += "M"
}
if c.Len() == 0 {
s += " EMPTY"
return s
}
s += "("
for idx := 0; idx < c.Len(); idx++ {
gs := c.Geometry(idx).String()
if c.Geometry(idx).HasSRID() {
splitted := strings.Split(gs, ";")
if len(splitted) > 1 {
gs = splitted[1]
}
}
s += gs + ","
}
return s[:len(s)-1] + ")"
}
// Scan implements sql.Scanner interface
func (c *GeometryCollection) Scan(src interface{}) error {
return scanGeometry(src, c)
}
// Value implements sql driver.Valuer interface
func (c *GeometryCollection) Value() (driver.Value, error) {
return c.String(), nil
}
// UnmarshalBinary implements encoding.BinaryUnmarshaler interface
func (c *GeometryCollection) UnmarshalBinary(data []byte) error {
h, byteOrder, offset := readHeader(data)
if h.Type() != CollectionType {
return errors.New("not expected geometry type")
}
geoms, _, err := readCollection(data[offset:], byteOrder)
if err != nil {
return err
}
c.header = h
c.geoms = geoms
return nil
}
// MarshalBinary implements encoding.BinaryMarshaler interface
func (c *GeometryCollection) MarshalBinary() ([]byte, error) {
size := headerSize(c.HasSRID()) + collectionSize(c.geoms, c.HasZ(), c.HasM())
b := make([]byte, size)
byteOrder := getBinaryByteOrder(c.ByteOrder())
offset := writeHeader(c, c.Type(), byteOrder, c.HasSRID(), b)
writeCollection(c.geoms, byteOrder, c.HasZ(), c.HasM(), b[offset:])
return b, nil
}