/
type_multi_point.go
276 lines (244 loc) · 8.04 KB
/
type_multi_point.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package geom
import (
"bytes"
"database/sql/driver"
"io"
"unsafe"
)
// MultiPoint is a 0-dimensional geometry that is a collection of points. Its
// zero value is the empty MultiPoint (i.e. a collection of zero points) with
// 2D coordinates type. It is immutable after creation.
type MultiPoint struct {
seq Sequence
empty BitSet
}
// NewMultiPointFromPoints creates a MultiPoint from a list of Points. The
// coordinate type of the MultiPoint is the lowest common coordinates type of
// its Points.
func NewMultiPointFromPoints(pts []Point, opts ...ConstructorOption) MultiPoint {
if len(pts) == 0 {
return MultiPoint{}
}
ctype := DimXYZM
for _, p := range pts {
ctype &= p.CoordinatesType()
}
var empty BitSet
floats := make([]float64, 0, len(pts)*ctype.Dimension())
for i, pt := range pts {
c, ok := pt.Coordinates()
if !ok {
empty.Set(i, true)
}
floats = append(floats, c.X, c.Y)
if ctype.Is3D() {
floats = append(floats, c.Z)
}
if ctype.IsMeasured() {
floats = append(floats, c.M)
}
}
seq := NewSequence(floats, ctype)
return NewMultiPointWithEmptyMask(seq, empty, opts...)
}
// NewMultiPoint creates a new MultiPoint from a sequence of Coordinates.
func NewMultiPoint(seq Sequence, opts ...ConstructorOption) MultiPoint {
return MultiPoint{seq, BitSet{}}
}
// NewMultiPointWithEmptyMask creates a new MultiPoint from a sequence of
// coordinates. If there are any positions set in the BitSet, then these are
// used to indicate that the corresponding point in the sequence is an empty
// point.
func NewMultiPointWithEmptyMask(seq Sequence, empty BitSet, opts ...ConstructorOption) MultiPoint {
return MultiPoint{
seq,
empty.Clone(), // clone so that the caller doesn't have access to the interal empty set
}
}
// Type return type string for MultiPoint
func (m MultiPoint) Type() string {
return multiPointType
}
// AsGeometry converts this MultiPoint into a Geometry.
func (m MultiPoint) AsGeometry() Geometry {
return Geometry{multiPointTag, unsafe.Pointer(&m)}
}
// NumPoints gives the number of element points making up the MultiPoint.
func (m MultiPoint) NumPoints() int {
return m.seq.Length()
}
// PointN gives the nth (zero indexed) Point.
func (m MultiPoint) PointN(n int) Point {
if m.empty.Get(n) {
return NewEmptyPoint(m.CoordinatesType())
}
c := m.seq.Get(n)
return NewPoint(c)
}
// AsText returns the WKT (Well Known Text) representation of this geometry.
func (m MultiPoint) AsText() string {
return string(m.AppendWKT(nil))
}
// AppendWKT appends the WKT (Well Known Text) representation of this geometry
// to the input byte slice.
func (m MultiPoint) AppendWKT(dst []byte) []byte {
dst = appendWKTHeader(dst, "MULTIPOINT", m.CoordinatesType())
if m.NumPoints() == 0 {
return appendWKTEmpty(dst)
}
return appendWKTSequence(dst, m.seq, true, m.empty)
}
// IsSimple returns true if this geometry contains no anomalous geometry
// points, such as self intersection or self tangency. MultiPoints are simple
// if and only if no two of its points have equal XY coordinates.
func (m MultiPoint) IsSimple() bool {
seen := make(map[XY]bool)
for i := 0; i < m.NumPoints(); i++ {
xy, ok := m.PointN(i).XY()
if !ok {
continue
}
if seen[xy] {
return false
}
seen[xy] = true
}
return true
}
// Intersection calculates the of this geometry and another, i.e. the portion
// of the two geometries that are shared. It is not implemented for all
// geometry pairs, and returns an error for those cases.
func (m MultiPoint) Intersection(g Geometry) (Geometry, error) {
return intersection(m.AsGeometry(), g)
}
// Intersects return true if and only if this geometry intersects with the
// other, i.e. they shared at least one common point.
func (m MultiPoint) Intersects(g Geometry) bool {
return hasIntersection(m.AsGeometry(), g)
}
// IsEmpty return true if and only if this MultiPoint doesn't contain any
// Points, or only contains empty Points.
func (m MultiPoint) IsEmpty() bool {
for i := 0; i < m.NumPoints(); i++ {
if !m.empty.Get(i) {
return false
}
}
return true
}
// Envelope returns the Envelope that most tightly surrounds the geometry. If
// the geometry is empty, then false is returned.
func (m MultiPoint) Envelope() (Envelope, bool) {
var has bool
var env Envelope
for i := 0; i < m.NumPoints(); i++ {
xy, ok := m.PointN(i).XY()
if !ok {
continue
}
if has {
env = env.ExtendToIncludePoint(xy)
} else {
env = NewEnvelope(xy)
has = true
}
}
return env, has
}
// Boundary returns the spatial boundary for this MultiPoint, which is always
// the empty set. This is represented by the empty GeometryCollection.
func (m MultiPoint) Boundary() GeometryCollection {
return GeometryCollection{}
}
// Value implements the database/sql/driver.Valuer interface by returning the
// WKB (Well Known Binary) representation of this Geometry.
func (m MultiPoint) Value() (driver.Value, error) {
var buf bytes.Buffer
err := m.AsBinary(&buf)
return buf.Bytes(), err
}
// AsBinary writes the WKB (Well Known Binary) representation of the geometry
// to the writer.
func (m MultiPoint) AsBinary(w io.Writer) error {
marsh := newWKBMarshaller(w)
marsh.writeByteOrder()
marsh.writeGeomType(wkbGeomTypeMultiPoint, m.CoordinatesType())
n := m.NumPoints()
marsh.writeCount(n)
for i := 0; i < n; i++ {
pt := m.PointN(i)
marsh.setErr(pt.AsBinary(w))
}
return marsh.err
}
// ConvexHull returns the geometry representing the smallest convex geometry
// that contains this geometry.
func (m MultiPoint) ConvexHull() Geometry {
return convexHull(m.AsGeometry())
}
// MarshalJSON implements the encoding/json.Marshaller interface by encoding
// this geometry as a GeoJSON geometry object.
func (m MultiPoint) MarshalJSON() ([]byte, error) {
var dst []byte
dst = append(dst, `{"type":"MultiPoint","coordinates":`...)
dst = appendGeoJSONSequence(dst, m.seq, m.empty)
dst = append(dst, '}')
return dst, nil
}
// Coordinates returns the coordinates of the points represented by the
// MultiPoint. If a point has its corresponding bit set to true in the BitSet,
// then that point is empty.
func (m MultiPoint) Coordinates() (seq Sequence, empty BitSet) {
// TODO: If we had a read-only BitSet, then we could avoid the clone here.
return m.seq, m.empty.Clone()
}
// TransformXY transforms this MultiPoint into another MultiPoint according to fn.
func (m MultiPoint) TransformXY(fn func(XY) XY, opts ...ConstructorOption) (MultiPoint, error) {
transformed := transformSequence(m.seq, fn)
return NewMultiPointWithEmptyMask(transformed, m.empty, opts...), nil
}
// EqualsExact checks if this MultiPoint is exactly equal to another MultiPoint.
func (m MultiPoint) EqualsExact(other Geometry, opts ...EqualsExactOption) bool {
return other.IsMultiPoint() &&
multiPointExactEqual(m, other.AsMultiPoint(), opts)
}
// IsValid checks if this MultiPoint is valid. However, there is no way to indicate
// whether or not MultiPoint is valid, so this function always returns true
func (m MultiPoint) IsValid() bool {
return true
}
// Centroid gives the centroid of the coordinates of the MultiPoint.
func (m MultiPoint) Centroid() Point {
var sum XY
var n int
for i := 0; i < m.NumPoints(); i++ {
xy, ok := m.PointN(i).XY()
if ok {
sum = sum.Add(xy)
n++
}
}
if n == 0 {
return NewEmptyPoint(DimXY)
}
return NewPointFromXY(sum.Scale(1 / float64(n)))
}
// Reverse in the case of MultiPoint outputs each component point in their
// original order.
func (m MultiPoint) Reverse() MultiPoint {
return m
}
// CoordinatesType returns the CoordinatesType used to represent points making
// up the geometry.
func (m MultiPoint) CoordinatesType() CoordinatesType {
return m.seq.CoordinatesType()
}
// ForceCoordinatesType returns a new MultiPoint with a different CoordinatesType. If a
// dimension is added, then new values are populated with 0.
func (m MultiPoint) ForceCoordinatesType(newCType CoordinatesType) MultiPoint {
return MultiPoint{m.seq.ForceCoordinatesType(newCType), m.empty}
}
// Force2D returns a copy of the MultiPoint with Z and M values removed.
func (m MultiPoint) Force2D() MultiPoint {
return m.ForceCoordinatesType(DimXY)
}