-
Notifications
You must be signed in to change notification settings - Fork 0
/
factory.go
145 lines (127 loc) · 3.12 KB
/
factory.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package geom
import (
"errors"
"runtime"
"github.com/vistarmedia/geom/geos-go"
"github.com/vistarmedia/geom/geos-go/handle"
)
var (
ErrEmptyCoords = errors.New("Empty coordinates")
)
type Coord struct {
X, Y float64
}
func newGeosLinearRing(h *geos.Handle, coords []Coord) (*geos.Geometry, error) {
coordsLen := len(coords)
if coordsLen == 0 {
return nil, ErrEmptyCoords
}
cs := geos.NewCoordSeq(h, uint(coordsLen), 2)
for i, c := range coords {
if err := cs.SetX(h, uint(i), c.X); err != nil {
cs.Destroy(h)
return nil, err
}
if err := cs.SetY(h, uint(i), c.Y); err != nil {
cs.Destroy(h)
return nil, err
}
}
// LinearRing destructor will destroy the coord seq even on error
return cs.LinearRing(h)
}
// For creating geometries
type Factory struct {
hp handle.GeosHandleProvider
}
func NewFactory(hp handle.GeosHandleProvider) Factory {
return Factory{hp}
}
func (f Factory) FromGeos(g *geos.Geometry) *Geometry {
return newGeometry(f.hp, g)
}
func (f Factory) NewEmptyPoint() Point {
h := f.hp.Get()
defer f.hp.Put(h)
return newPoint(newGeometry(f.hp, geos.NewEmptyPoint(h)))
}
func (f Factory) NewEmptyPolygon() Polygon {
h := f.hp.Get()
defer f.hp.Put(h)
return newPolygon(newGeometry(f.hp, geos.NewEmptyPolygon(h)))
}
func (f Factory) NewEmptyMultipolygon() Multipolygon {
h := f.hp.Get()
defer f.hp.Put(h)
mp := geos.NewEmptyGeometryCollection(h, geos.MULTIPOLYGON)
return newMultiPolygon(newGeometry(f.hp, mp))
}
func (f Factory) NewPoint(c Coord) (p Point, err error) {
h := f.hp.Get()
defer f.hp.Put(h)
cs := geos.NewCoordSeq(h, 1, 2)
if err = cs.SetX(h, 0, c.X); err != nil {
cs.Destroy(h)
return
}
if err = cs.SetY(h, 0, c.Y); err != nil {
cs.Destroy(h)
return
}
if point, err := cs.Point(h); err == nil {
p = newPoint(newGeometry(f.hp, point))
}
return
}
func (f Factory) NewLinearRing(coords []Coord) (lr LinearRing, err error) {
h := f.hp.Get()
g, err := newGeosLinearRing(h, coords)
f.hp.Put(h)
if err != nil {
return
}
lr = newLinearRing(newGeometry(f.hp, g))
return
}
func (f Factory) NewPolygon(
shell []Coord, holes ...[]Coord) (p Polygon, err error) {
h := f.hp.Get()
defer f.hp.Put(h)
shellRing, err := newGeosLinearRing(h, shell)
if err != nil {
return
}
var holeRings []*geos.Geometry
for _, hole := range holes {
var holeRing *geos.Geometry
holeRing, err = newGeosLinearRing(h, hole)
if err != nil {
return
}
holeRings = append(holeRings, holeRing)
}
g, err := geos.NewPolygon(h, shellRing, holeRings)
if err != nil {
return
}
p = newPolygon(newGeometry(f.hp, g))
return
}
// Create a MULTIPOLYGON from some POLYGONs. This will clone the passed POLYGONs
// and assume ownership of those clones. Arguments continue to be managed
// independently by GC
func (f Factory) NewMultipolygon(ps ...Polygon) (mp Multipolygon, err error) {
h := f.hp.Get()
defer f.hp.Put(h)
geoms := make([]*geos.Geometry, len(ps))
for i, poly := range ps {
geoms[i] = poly.g.Clone(h)
runtime.KeepAlive(poly)
}
g, err := geos.NewGeometryCollection(h, geos.MULTIPOLYGON, geoms)
if err != nil {
return
}
mp = newMultiPolygon(newGeometry(f.hp, g))
return
}