-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwkt.go
155 lines (132 loc) · 3.2 KB
/
wkt.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
package revisor
import (
"fmt"
"strings"
"github.com/IvanZagoskin/wkt/geometry"
"github.com/IvanZagoskin/wkt/parser"
)
type Geometry string
const (
GeometryAny Geometry = ""
GeometryPoint Geometry = "point"
GeometryMultiPoint Geometry = "multipoint"
GeometryLineString Geometry = "linestring"
GeometryMultiLineString Geometry = "multilinestring"
GeometryPolygon Geometry = "polygon"
GeometryMultiPolygon Geometry = "multipolygon"
GeometryCircularString Geometry = "circularstring"
)
var coords = map[string]geometry.CoordinateType{
"": geometry.XY,
"z": geometry.XYZ,
"m": geometry.XYM,
"zm": geometry.XYZM,
}
func errCoordMismatch(want string, got geometry.CoordinateType) error {
var gotName string
for k, t := range coords {
if t == got {
gotName = k
break
}
}
if want == "" {
return fmt.Errorf(
"unexpected coordinate type %q where none was expected",
gotName)
}
if got == geometry.XY {
return fmt.Errorf(
"missing coordinate type where %q was expected",
want)
}
return fmt.Errorf(
"unexpected coordinate type %q where %q was expected",
gotName, want)
}
func validateWKT(spec string, value string) error {
parser := parser.New()
geo, err := parser.ParseWKT(strings.NewReader(value))
if err != nil {
return fmt.Errorf("failed to parse: %w", err)
}
g, coord, _ := strings.Cut(spec, "-")
ct, ok := coords[coord]
if !ok {
return fmt.Errorf("unknown coordinate type %q", coord)
}
switch Geometry(g) {
case GeometryAny:
case GeometryPoint:
p, ok := geo.(*geometry.Point)
if !ok {
return fmt.Errorf("geometry is not a point")
}
if p.Type != ct {
return errCoordMismatch(coord, p.Type)
}
case GeometryLineString:
l, ok := geo.(*geometry.LineString)
if !ok {
return fmt.Errorf("geometry is not a linestring")
}
if l.Type != ct {
return errCoordMismatch(coord, l.Type)
}
case GeometryPolygon:
p, ok := geo.(*geometry.Polygon)
if !ok {
return fmt.Errorf("geometry is not a polygon")
}
for _, l := range p.LineStrings {
if l.Type != ct {
return errCoordMismatch(coord, l.Type)
}
}
case GeometryMultiPoint:
mp, ok := geo.(*geometry.MultiPoint)
if !ok {
return fmt.Errorf("geometry is not a multipoint")
}
for _, p := range mp.Points {
if p.Type != ct {
return errCoordMismatch(coord, p.Type)
}
}
case GeometryMultiLineString:
ml, ok := geo.(*geometry.MultiLineString)
if !ok {
return fmt.Errorf("geometry is not a multilinestring")
}
for _, l := range ml.Lines {
if l.Type != ct {
return errCoordMismatch(coord, l.Type)
}
}
case GeometryMultiPolygon:
mp, ok := geo.(*geometry.MultiPolygon)
if !ok {
return fmt.Errorf("geometry is not a multipolygon")
}
for _, p := range mp.Polygons {
for _, l := range p.LineStrings {
if l.Type != ct {
return errCoordMismatch(coord, l.Type)
}
}
}
case GeometryCircularString:
cs, ok := geo.(*geometry.CircularString)
if !ok {
return fmt.Errorf("geometry is not a circular string")
}
for _, l := range cs.Points {
if l.Type != ct {
return errCoordMismatch(coord, l.Type)
}
}
default:
return fmt.Errorf("unknown geometry type %q", g)
}
return nil
}