-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
177 lines (154 loc) · 5.18 KB
/
types.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
package gogeojson
import (
"errors"
)
// TODO List:
// - Add validation to check the type
// - Check all the deepest nested coordinates slices if they hold max a length of 2
// - Check polygon multi and single ring
// - Add validation for max length in not the deepest level of coordinates like for LineString max 2 slices in coordinates
type FeatureType string
const (
FeatureTypePoint FeatureType = "Point"
FeatureTypeLineString FeatureType = "LineString"
FeatureTypePolygon FeatureType = "Polygon"
FeatureTypeMultiPoint FeatureType = "MultiPoint"
FeatureTypeMultiLineString FeatureType = "MultiLineString"
FeatureTypeMultiPolygon FeatureType = "MultiPolygon"
)
var (
ErrCoordinateTooManyValues = errors.New("a slice in the deepest level of coordinates has more then 2 values")
)
// PointField: Holds the value of a Point
type PointField struct {
Type FeatureType `json:"type" bson:"type"`
Coordinates []float64 `json:"coordinates" bson:"coordinates"`
}
// NewPointField: Returns a new PointField with the provided coordinates
func NewPointField(coordinates []float64) *PointField {
return &PointField{
Type: FeatureTypePoint,
Coordinates: coordinates,
}
}
// Validate: Returns an error if the deepest level of PointField coordinates has more then 2 values
func (f *PointField) Validate() error {
if len(f.Coordinates) > 2 {
return ErrCoordinateTooManyValues
}
return nil
}
// LineStringField: Holds the value of a LineString
type LineStringField struct {
Type FeatureType `json:"type" bson:"type"`
Coordinates [][]float64 `json:"coordinates" bson:"coordinates"`
}
// NewLineStringField: Returns a new LineStringField with the provided coordinates
func NewLineStringField(coordinates [][]float64) *LineStringField {
return &LineStringField{
Type: FeatureTypeLineString,
Coordinates: coordinates,
}
}
// Validate: Returns an error if the deepest level of LineStringField coordinates has more then 2 values
func (f *LineStringField) Validate() error {
if len(f.Coordinates) > 2 {
return ErrCoordinateTooManyValues
}
for _, elem := range f.Coordinates {
if len(elem) > 2 {
return ErrCoordinateTooManyValues
}
}
return nil
}
// PolygonField: Holds the value of a Polygon
type PolygonField struct {
Type FeatureType `json:"type" bson:"type"`
Coordinates [][][]float64 `json:"coordinates" bson:"coordinates"`
}
// NewPolygonField: Returns a new PolygonField with the provided coordinates
func NewPolygonField(coordinates [][][]float64) *PolygonField {
return &PolygonField{
Type: FeatureTypePolygon,
Coordinates: coordinates,
}
}
// Validate: Returns an error if the deepest level of PolygonField coordinates has more then 2 values
func (f *PolygonField) Validate() error {
for _, elem := range f.Coordinates {
for _, subElem := range elem {
if len(subElem) > 2 {
return ErrCoordinateTooManyValues
}
}
}
return nil
}
// MultiPointField: Holds the value of a MultiPoint
type MultiPointField struct {
Type FeatureType `json:"type" bson:"type"`
Coordinates [][]float64 `json:"coordinates" bson:"coordinates"`
}
// NewMultiPointField: Returns a new MultiPointField with the provided coordinates
func NewMultiPointField(coordinates [][]float64) *MultiPointField {
return &MultiPointField{
Type: FeatureTypeMultiPoint,
Coordinates: coordinates,
}
}
// Validate: Returns an error if the deepest level of MultiPointField coordinates has more then 2 values
func (f *MultiPointField) Validate() error {
for _, elem := range f.Coordinates {
if len(elem) > 2 {
return ErrCoordinateTooManyValues
}
}
return nil
}
// MultiLineStringField: Holds the value of a MultiLineString
type MultiLineStringField struct {
Type FeatureType `json:"type" bson:"type"`
Coordinates [][][]float64 `json:"coordinates" bson:"coordinates"`
}
// NewMultiLineStringField: Returns a new MultiLineStringField with the provided coordinates
func NewMultiLineStringField(coordinates [][][]float64) *MultiLineStringField {
return &MultiLineStringField{
Type: FeatureTypeMultiLineString,
Coordinates: coordinates,
}
}
// Validate: Returns an error if the deepest level of MultiLineStringField coordinates has more then 2 values
func (f *MultiLineStringField) Validate() error {
for _, elem := range f.Coordinates {
for _, subElem := range elem {
if len(subElem) > 2 {
return ErrCoordinateTooManyValues
}
}
}
return nil
}
// MultiPolygonField: Holds the value of a MultiPolygon
type MultiPolygonField struct {
Type FeatureType `json:"type" bson:"type"`
Coordinates [][][]float64 `json:"coordinates" bson:"coordinates"`
}
// NewMultiPolygonField: Returns a new MultiPolygonField with the provided coordinates
func NewMultiPolygonField(coordinates [][][]float64) *MultiPolygonField {
return &MultiPolygonField{
Type: FeatureTypeMultiPolygon,
Coordinates: coordinates,
}
}
// Validate: Returns an error if the deepest level of MultiPolygonField coordinates has more then 2 values
func (f *MultiPolygonField) Validate() error {
for _, elem := range f.Coordinates {
for _, subElem := range elem {
if len(subElem) > 2 {
return ErrCoordinateTooManyValues
}
}
}
return nil
}