-
Notifications
You must be signed in to change notification settings - Fork 186
/
Copy pathtextformat_test.go
399 lines (352 loc) · 11.4 KB
/
textformat_test.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
// Copyright 2017 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package s2
// s2textformat_test contains a collection of functions for converting geometry
// to and from a human-readable format. It is intended for testing and debugging.
// Be aware that the human-readable format is *NOT* designed to preserve the full
// precision of the original object, so it should not be used for data storage.
//
// Most functions here use the same format for inputs, a comma separated set of
// latitude-longitude coordinates in degrees. Functions that expect a different
// input document the values in the function comment.
//
// Examples of the input format:
// "" // no points
// "-20:150" // one point
// "-20:150, 10:-120, 0.123:-170.652" // three points
import (
"bytes"
"fmt"
"io"
"strconv"
"strings"
"github.com/golang/geo/r3"
)
// writePoint formats the point and writes it to the given writer.
//
// If roundtripPrecision is true, the coordinates are formatted using
// enough precision to exactly preserve the floating point values.
func writePoint(w io.Writer, p Point, roundtripPrecision bool) {
ll := LatLngFromPoint(p)
if roundtripPrecision {
fmt.Fprintf(w, "%.17g:%.17g", ll.Lat.Degrees(), ll.Lng.Degrees())
} else {
fmt.Fprintf(w, "%.15g:%.15g", ll.Lat.Degrees(), ll.Lng.Degrees())
}
}
// writePoints formats the given points in debug format and writes them to the given writer.
func writePoints(w io.Writer, pts []Point, roundtripPrecision bool) {
for i, pt := range pts {
if i > 0 {
fmt.Fprint(w, ", ")
}
writePoint(w, pt, roundtripPrecision)
}
}
// parsePoint returns a Point from the given string, or the origin if the
// string was invalid. If more than one value is given, only the first is used.
func parsePoint(s string) Point {
p := parsePoints(s)
if len(p) > 0 {
return p[0]
}
return Point{r3.Vector{X: 0, Y: 0, Z: 0}}
}
// pointToString returns a string representation suitable for reconstruction
// by the parsePoint method.
func pointToString(point Point, roundtripPrecision bool) string {
var buf bytes.Buffer
writePoint(&buf, point, roundtripPrecision)
return buf.String()
}
// parsePoints returns the values in the input string as Points.
func parsePoints(s string) []Point {
lls := parseLatLngs(s)
if len(lls) == 0 {
return nil
}
points := make([]Point, len(lls))
for i, ll := range lls {
points[i] = PointFromLatLng(ll)
}
return points
}
// pointsToString returns a string representation suitable for reconstruction
// by the parsePoints method.
func pointsToString(points []Point, roundtripPrecision bool) string {
var buf bytes.Buffer
writePoints(&buf, points, roundtripPrecision)
return buf.String()
}
// parseLatLngs returns the values in the input string as LatLngs.
func parseLatLngs(s string) []LatLng {
var lls []LatLng
if s == "" {
return lls
}
for _, piece := range strings.Split(s, ",") {
piece = strings.TrimSpace(piece)
// Skip empty strings.
if piece == "" {
continue
}
p := strings.Split(piece, ":")
if len(p) != 2 {
continue
}
lat, err := strconv.ParseFloat(p[0], 64)
if err != nil {
panic(fmt.Sprintf("invalid float in parseLatLngs: %q, err: %v", p[0], err))
}
lng, err := strconv.ParseFloat(p[1], 64)
if err != nil {
panic(fmt.Sprintf("invalid float in parseLatLngs: %q, err: %v", p[1], err))
}
lls = append(lls, LatLngFromDegrees(lat, lng))
}
return lls
}
// makeRect returns the minimal bounding Rect that contains the values in the input string.
func makeRect(s string) Rect {
var rect Rect
lls := parseLatLngs(s)
if len(lls) > 0 {
rect = RectFromLatLng(lls[0])
}
for _, ll := range lls[1:] {
rect = rect.AddPoint(ll)
}
return rect
}
// makeCellUnion returns a CellUnion from the given CellID token strings.
func makeCellUnion(tokens ...string) CellUnion {
var cu CellUnion
for _, t := range tokens {
cu = append(cu, CellIDFromString(t))
}
return cu
}
// makeLoop constructs a Loop from the input string.
// The strings "empty" or "full" create an empty or full loop respectively.
func makeLoop(s string) *Loop {
if s == "full" {
return FullLoop()
}
if s == "empty" {
return EmptyLoop()
}
return LoopFromPoints(parsePoints(s))
}
// makePolygon constructs a polygon from the sequence of loops in the input
// string. Loops are automatically normalized by inverting them if necessary
// so that they enclose at most half of the unit sphere. (Historically this was
// once a requirement of polygon loops. It also hides the problem that if the
// user thinks of the coordinates as X:Y rather than LAT:LNG, it yields a loop
// with the opposite orientation.)
//
// Loops are semicolon separated in the input string with each loop using the
// same format as above.
//
// Examples of the input format:
//
// "10:20, 90:0, 20:30" // one loop
// "10:20, 90:0, 20:30; 5.5:6.5, -90:-180, -15.2:20.3" // two loops
// "" // the empty polygon (consisting of no loops)
// "empty" // the empty polygon (consisting of no loops)
// "full" // the full polygon (consisting of one full loop)
func makePolygon(s string, normalize bool) *Polygon {
var loops []*Loop
// Avoid the case where strings.Split on empty string will still return
// one empty value, where we want no values.
if s == "empty" || s == "" {
return PolygonFromLoops(loops)
}
for _, str := range strings.Split(s, ";") {
// The polygon test strings mostly have a trailing semicolon
// (to make concatenating them for tests easy). The C++
// SplitString doesn't return empty elements where as Go does,
// so we need to check before using it.
if str == "" {
continue
}
loop := makeLoop(strings.TrimSpace(str))
if normalize && !loop.IsFull() {
loop.Normalize()
}
loops = append(loops, loop)
}
return PolygonFromLoops(loops)
}
// makePolyline constructs a Polyline from the given string.
func makePolyline(s string) *Polyline {
p := Polyline(parsePoints(s))
return &p
}
// makeLaxPolyline constructs a LaxPolyline from the given string.
func makeLaxPolyline(s string) *LaxPolyline {
return LaxPolylineFromPoints(parsePoints(s))
}
// laxPolylineToString returns a string representation suitable for reconstruction
// by the makeLaxPolyline method.
func laxPolylineToString(l *LaxPolyline) string {
var buf bytes.Buffer
writePoints(&buf, l.vertices, false) // TODO(rsned): Add rountripPrecision param.
return buf.String()
}
// makeLaxPolygon creates a LaxPolygon from the given debug formatted string.
// Similar to makePolygon, except that loops must be oriented so that the
// interior of the loop is always on the left, and polygons with degeneracies
// are supported. As with makePolygon, "full" denotes the full polygon and "empty"
// is not allowed (instead, simply create a LaxPolygon with no loops).
func makeLaxPolygon(s string) *LaxPolygon {
var points [][]Point
if s == "" {
return LaxPolygonFromPoints(points)
}
for _, l := range strings.Split(s, ";") {
if l == "full" {
points = append(points, []Point{})
} else if l != "empty" {
points = append(points, parsePoints(l))
}
}
return LaxPolygonFromPoints(points)
}
// makeShapeIndex builds a ShapeIndex from the given debug string containing
// the points, polylines, and loops (in the form of a single polygon)
// described by the following format:
//
// point1|point2|... # line1|line2|... # polygon1|polygon2|...
//
// Examples:
//
// 1:2 | 2:3 # # // Two points
// # 0:0, 1:1, 2:2 | 3:3, 4:4 # // Two polylines
// # # 0:0, 0:3, 3:0; 1:1, 2:1, 1:2 // Two nested loops (one polygon)
// 5:5 # 6:6, 7:7 # 0:0, 0:1, 1:0 // One of each
// # # empty // One empty polygon
// # # empty | full // One empty polygon, one full polygon
//
// Loops should be directed so that the region's interior is on the left.
// Loops can be degenerate (they do not need to meet Loop requirements).
//
// Note: Because whitespace is ignored, empty polygons must be specified
// as the string "empty" rather than as the empty string ("").
//
// If roundtripPrecision is true, the coordinates are formatted using
// enough precision to exactly preserve the floating point values.
//
// TODO(rsned): plumb roundtripPrecision parameter through here.
func makeShapeIndex(s string) *ShapeIndex {
fields := strings.Split(s, "#")
if len(fields) != 3 {
panic("shapeIndex debug string must contain 2 '#' characters")
}
index := NewShapeIndex()
var points []Point
for _, p := range strings.Split(fields[0], "|") {
p = strings.TrimSpace(p)
if p == "" {
continue
}
points = append(points, parsePoint(p))
}
if len(points) > 0 {
p := PointVector(points)
index.Add(&p)
}
for _, p := range strings.Split(fields[1], "|") {
p = strings.TrimSpace(p)
if p == "" {
continue
}
if polyline := makeLaxPolyline(p); polyline != nil {
index.Add(polyline)
}
}
for _, p := range strings.Split(fields[2], "|") {
p = strings.TrimSpace(p)
if p == "" {
continue
}
if polygon := makeLaxPolygon(p); polygon != nil {
index.Add(polygon)
}
}
return index
}
// shapeIndexDebugString outputs the contents of this ShapeIndex in debug
// format. The index may contain Shapes of any type. Shapes are reordered
// if necessary so that all point geometry (shapes of dimension 0) are first,
// followed by all polyline geometry, followed by all polygon geometry.
func shapeIndexDebugString(index *ShapeIndex, roundtripPrecision bool) string {
var buf bytes.Buffer
for dim := 0; dim <= 2; dim++ {
if dim > 0 {
buf.WriteByte('#')
}
var count int
// Use shapes ordered by id rather than ranging over the
// index.shapes map to ensure that the ordering of shapes in the
// generated string matches the C++ generated strings.
for i := int32(0); i < index.nextID; i++ {
shape := index.Shape(i)
// Only use shapes that are still in the index and at the
// current geometry level we are outputting.
if shape == nil || shape.Dimension() != dim {
continue
}
if count > 0 {
buf.WriteString(" | ")
} else {
if dim > 0 {
buf.WriteString(" ")
} else {
buf.WriteString("")
}
}
for c := 0; c < shape.NumChains(); c++ {
if c > 0 {
if dim == 2 {
buf.WriteString("; ")
} else {
buf.WriteString(" | ")
}
}
chain := shape.Chain(c)
if chain.Length == 0 {
buf.WriteString("full")
} else {
writePoint(&buf, shape.Edge(chain.Start).V0, roundtripPrecision)
}
limit := chain.Start + chain.Length
if dim != 1 {
limit--
}
for e := chain.Start; e < limit; e++ {
buf.WriteString(", ")
writePoint(&buf, shape.Edge(e).V1, roundtripPrecision)
}
count++
}
}
if dim == 1 || (dim == 0 && count > 0) {
buf.WriteByte(' ')
}
}
return buf.String()
}
// TODO(roberts): Remaining C++ textformat related methods
// make$S2TYPE methods for missing types.
// to debug string for many types