-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcoordseq.go
210 lines (194 loc) · 4.88 KB
/
coordseq.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package geos
// #include "go-geos.h"
import "C"
// A CoordSeq is a coordinate sequence.
type CoordSeq struct {
context *Context
s *C.struct_GEOSCoordSeq_t
parent *Geom
dimensions int
size int
}
// Clone returns a clone of s.
func (s *CoordSeq) Clone() *CoordSeq {
s.context.Lock()
defer s.context.Unlock()
return s.context.newNonNilCoordSeq(C.GEOSCoordSeq_clone_r(s.context.handle, s.s))
}
// Destroy destroys s and all resources associated with s.
func (s *CoordSeq) Destroy() {
if s == nil || s.context == nil {
return
}
s.context.Lock()
defer s.context.Unlock()
C.GEOSCoordSeq_destroy_r(s.context.handle, s.s)
*s = CoordSeq{} // Clear all references.
}
// Dimensions returns the dimensions of s.
func (s *CoordSeq) Dimensions() int {
return s.dimensions
}
// IsCCW returns if s is counter-clockwise.
func (s *CoordSeq) IsCCW() bool {
s.context.Lock()
defer s.context.Unlock()
var cIsCCW C.char
switch C.GEOSCoordSeq_isCCW_r(s.context.handle, s.s, &cIsCCW) {
case 1:
return cIsCCW != 0
default:
panic(s.context.err)
}
}
// Ordinate returns the idx-th dim coordinate of s.
func (s *CoordSeq) Ordinate(idx, dim int) float64 {
s.context.Lock()
defer s.context.Unlock()
if idx < 0 || s.size <= idx {
panic(errIndexOutOfRange)
}
if dim < 0 || s.dimensions <= dim {
panic(errDimensionOutOfRange)
}
var value float64
if C.GEOSCoordSeq_getOrdinate_r(s.context.handle, s.s, C.uint(idx), C.uint(dim), (*C.double)(&value)) == 0 {
panic(s.context.err)
}
return value
}
// SetOrdinate sets the idx-th dim coordinate of s to val.
func (s *CoordSeq) SetOrdinate(idx, dim int, val float64) {
s.context.Lock()
defer s.context.Unlock()
if idx < 0 || s.size <= idx {
panic(errIndexOutOfRange)
}
if dim < 0 || s.dimensions <= dim {
panic(errDimensionOutOfRange)
}
if C.GEOSCoordSeq_setOrdinate_r(s.context.handle, s.s, C.uint(idx), C.uint(dim), C.double(val)) == 0 {
panic(s.context.err)
}
}
// SetX sets the idx-th X coordinate of s to val.
func (s *CoordSeq) SetX(idx int, val float64) {
s.context.Lock()
defer s.context.Unlock()
if idx < 0 || s.size <= idx {
panic(errIndexOutOfRange)
}
if s.dimensions == 0 {
panic(errDimensionOutOfRange)
}
if C.GEOSCoordSeq_setX_r(s.context.handle, s.s, C.uint(idx), C.double(val)) == 0 {
panic(s.context.err)
}
}
// SetY sets the idx-th Y coordinate of s to val.
func (s *CoordSeq) SetY(idx int, val float64) {
s.context.Lock()
defer s.context.Unlock()
if idx < 0 || s.size <= idx {
panic(errIndexOutOfRange)
}
if s.dimensions < 2 {
panic(errDimensionOutOfRange)
}
if C.GEOSCoordSeq_setY_r(s.context.handle, s.s, C.uint(idx), C.double(val)) == 0 {
panic(s.context.err)
}
}
// SetZ sets the idx-th Z coordinate of s to val.
func (s *CoordSeq) SetZ(idx int, val float64) {
s.context.Lock()
defer s.context.Unlock()
if idx < 0 || s.size <= idx {
panic(errIndexOutOfRange)
}
if s.dimensions < 3 {
panic(errDimensionOutOfRange)
}
if C.GEOSCoordSeq_setZ_r(s.context.handle, s.s, C.uint(idx), C.double(val)) == 0 {
panic(s.context.err)
}
}
// Size returns the size of s.
func (s *CoordSeq) Size() int {
return s.size
}
// ToCoords returns s as a [][]float64.
func (s *CoordSeq) ToCoords() [][]float64 {
s.context.Lock()
defer s.context.Unlock()
if s.size == 0 || s.dimensions == 0 {
return nil
}
flatCoords := make([]float64, s.size*s.dimensions)
var hasZ C.int
if s.dimensions > 2 {
hasZ = 1
}
var hasM C.int
if s.dimensions > 3 {
hasM = 1
}
if C.GEOSCoordSeq_copyToBuffer_r(s.context.handle, s.s, (*C.double)(&flatCoords[0]), hasZ, hasM) == 0 {
panic(s.context.err)
}
coords := make([][]float64, s.size)
j := 0
for i := range s.size {
coords[i] = flatCoords[j : j+s.dimensions : j+s.dimensions]
j += s.dimensions
}
return coords
}
// X returns the idx-th X coordinate of s.
func (s *CoordSeq) X(idx int) float64 {
s.context.Lock()
defer s.context.Unlock()
if idx < 0 || s.size <= idx {
panic(errIndexOutOfRange)
}
if s.dimensions == 0 {
panic(errDimensionOutOfRange)
}
var val float64
if C.GEOSCoordSeq_getX_r(s.context.handle, s.s, C.uint(idx), (*C.double)(&val)) == 0 {
panic(s.context.err)
}
return val
}
// Y returns the idx-th Y coordinate of s.
func (s *CoordSeq) Y(idx int) float64 {
s.context.Lock()
defer s.context.Unlock()
if idx < 0 || s.size <= idx {
panic(errIndexOutOfRange)
}
if s.dimensions < 2 {
panic(errDimensionOutOfRange)
}
var val float64
if C.GEOSCoordSeq_getY_r(s.context.handle, s.s, C.uint(idx), (*C.double)(&val)) == 0 {
panic(s.context.err)
}
return val
}
// Z returns the idx-th Z coordinate of s.
func (s *CoordSeq) Z(idx int) float64 {
s.context.Lock()
defer s.context.Unlock()
if idx < 0 || s.size <= idx {
panic(errIndexOutOfRange)
}
if s.dimensions < 3 {
panic(errDimensionOutOfRange)
}
var val float64
if C.GEOSCoordSeq_getZ_r(s.context.handle, s.s, C.uint(idx), (*C.double)(&val)) == 0 {
panic(s.context.err)
}
return val
}