-
Notifications
You must be signed in to change notification settings - Fork 9
/
geom_test.go
549 lines (523 loc) · 24 KB
/
geom_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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
package geos_test
import (
"math"
"runtime"
"strconv"
"testing"
"github.com/alecthomas/assert/v2"
"github.com/twpayne/go-geos"
)
func TestGeometryMethods(t *testing.T) {
for _, tc := range []struct {
name string
wkt string
expectedBounds *geos.Box2D
expectedEmpty bool
expectedEnvelopeWKT string
expectedNumCoordinates int
expectedNumGeometries int
expectedSRID int
expectedType string
expectedTypeID geos.TypeID
expectedArea float64
expectedLength float64
expectedValidWkt string
}{
{
name: "point",
wkt: "POINT (0.0000000000000000 0.0000000000000000)",
expectedBounds: &geos.Box2D{MinX: 0, MinY: 0, MaxX: 0, MaxY: 0},
expectedEmpty: false,
expectedEnvelopeWKT: "POINT (0 0)",
expectedNumCoordinates: 1,
expectedNumGeometries: 1,
expectedSRID: 0,
expectedType: "Point",
expectedTypeID: geos.TypeIDPoint,
expectedArea: 0,
expectedLength: 0,
expectedValidWkt: "POINT (0.0000000000000000 0.0000000000000000)",
},
{
name: "point_empty",
wkt: "POINT EMPTY",
expectedBounds: &geos.Box2D{MinX: math.Inf(1), MinY: math.Inf(1), MaxX: math.Inf(-1), MaxY: math.Inf(-1)},
expectedEmpty: true,
expectedEnvelopeWKT: "POINT EMPTY",
expectedNumGeometries: 1,
expectedSRID: 0,
expectedType: "Point",
expectedTypeID: geos.TypeIDPoint,
expectedArea: 0,
expectedLength: 0,
expectedValidWkt: "POINT EMPTY",
},
{
name: "linestring",
wkt: "LINESTRING (0.0000000000000000 0.0000000000000000, 1.0000000000000000 1.0000000000000000)",
expectedBounds: &geos.Box2D{MinX: 0, MinY: 0, MaxX: 1, MaxY: 1},
expectedEmpty: false,
expectedEnvelopeWKT: "POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0))",
expectedNumCoordinates: 2,
expectedNumGeometries: 1,
expectedSRID: 0,
expectedType: "LineString",
expectedTypeID: geos.TypeIDLineString,
expectedArea: 0,
expectedLength: math.Sqrt(2),
expectedValidWkt: "LINESTRING (0.0000000000000000 0.0000000000000000, 1.0000000000000000 1.0000000000000000)",
},
{
name: "linestring_empty",
wkt: "LINESTRING EMPTY",
expectedBounds: &geos.Box2D{MinX: math.Inf(1), MinY: math.Inf(1), MaxX: math.Inf(-1), MaxY: math.Inf(-1)},
expectedEmpty: true,
expectedEnvelopeWKT: "POLYGON EMPTY",
expectedNumGeometries: 1,
expectedSRID: 0,
expectedType: "LineString",
expectedTypeID: geos.TypeIDLineString,
expectedArea: 0,
expectedLength: 0,
expectedValidWkt: "LINESTRING EMPTY",
},
{
name: "polygon",
wkt: "POLYGON ((0 0, 1 0, 1 1, 0 0))",
expectedBounds: &geos.Box2D{MinX: 0, MinY: 0, MaxX: 1, MaxY: 1},
expectedEmpty: false,
expectedEnvelopeWKT: "POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0))",
expectedNumCoordinates: 4,
expectedNumGeometries: 1,
expectedSRID: 0,
expectedType: "Polygon",
expectedTypeID: geos.TypeIDPolygon,
expectedArea: 0.5,
expectedLength: math.Sqrt(2) + 2,
expectedValidWkt: "POLYGON ((0 0, 1 0, 1 1, 0 0))",
},
{
name: "polygon_empty",
wkt: "POLYGON EMPTY",
expectedBounds: &geos.Box2D{MinX: math.Inf(1), MinY: math.Inf(1), MaxX: math.Inf(-1), MaxY: math.Inf(-1)},
expectedEmpty: true,
expectedEnvelopeWKT: "POLYGON EMPTY",
expectedNumGeometries: 1,
expectedSRID: 0,
expectedType: "Polygon",
expectedTypeID: geos.TypeIDPolygon,
expectedArea: 0,
expectedLength: 0,
expectedValidWkt: "POLYGON EMPTY",
},
} {
t.Run(tc.name, func(t *testing.T) {
c := geos.NewContext()
g := mustNewGeomFromWKT(t, c, tc.wkt)
v := mustNewGeomFromWKT(t, c, tc.expectedValidWkt)
assert.Equal(t, tc.expectedBounds, g.Bounds())
assert.Equal(t, tc.expectedEmpty, g.IsEmpty())
expectedEnvelope := mustNewGeomFromWKT(t, c, tc.expectedEnvelopeWKT)
assert.True(t, expectedEnvelope.Equals(g.Envelope()))
assert.Equal(t, tc.expectedNumCoordinates, g.NumCoordinates())
assert.Equal(t, tc.expectedNumGeometries, g.NumGeometries())
assert.True(t, g.IsSimple())
assert.Equal(t, tc.expectedSRID, g.SRID())
assert.Equal(t, tc.expectedType, g.Type())
assert.Equal(t, tc.expectedTypeID, g.TypeID())
assert.True(t, g.Clone().Equals(g))
assert.Equal(t, !g.IsEmpty(), g.Contains(g))
//nolint:gocritic
assert.True(t, g.Equals(g))
assert.True(t, g.Geometry(0).Equals(g))
assert.Equal(t, !g.IsEmpty(), g.Intersects(g))
assert.True(t, g.IsValid())
assert.Equal(t, "Valid Geometry", g.IsValidReason())
g.SetSRID(4326)
assert.Equal(t, 4326, g.SRID())
assert.Equal(t, tc.expectedArea, g.Area())
assert.Equal(t, tc.expectedLength, g.Length())
assert.Equal(t, v, g.MakeValidWithParams(geos.MakeValidLinework, geos.MakeValidDiscardCollapsed))
assert.Equal(t, v, g.MakeValidWithParams(geos.MakeValidStructure, geos.MakeValidDiscardCollapsed))
assert.Equal(t, v, g.MakeValidWithParams(geos.MakeValidLinework, geos.MakeValidKeepCollapsed))
// assert.Equal(t, v, g.MakeValidWithParams(geos.MakeValidStructure, geos.MakeValidKeepCollapsed))
})
}
}
func TestGeomMethods(t *testing.T) {
defer runtime.GC() // Exercise finalizers.
c := geos.NewContext()
unitSquare := mustNewGeomFromWKT(t, c, "POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0))")
northSouthLine := mustNewGeomFromWKT(t, c, "LINESTRING (0.5 0, 0.5 1)")
eastWestLine := mustNewGeomFromWKT(t, c, "LINESTRING (0 0.5, 1 0.5)")
northWestSquare := mustNewGeomFromWKT(t, c, "POLYGON ((0 0.5, 0 1, 0.5 1, 0.5 0.5, 0 0.5))")
southEastSquare := mustNewGeomFromWKT(t, c, "POLYGON ((0.5 0, 0.5 0.5, 1 0.5, 1 0, 0.5 0))")
middleSquare := mustNewGeomFromWKT(t, c, "POLYGON ((0.25 0.25, 0.25 0.75, 0.75 0.75, 0.75 0.25, 0.25 0.25))")
assert.True(t, unitSquare.Equals(unitSquare.ConvexHull()))
assert.True(t, unitSquare.Contains(middleSquare))
assert.False(t, unitSquare.Contains(mustNewGeomFromWKT(t, c, "POINT (-0.5 -0.5)")))
assert.False(t, unitSquare.CoveredBy(middleSquare))
assert.True(t, middleSquare.CoveredBy(unitSquare))
assert.True(t, unitSquare.Covers(middleSquare))
assert.False(t, middleSquare.Covers(unitSquare))
assert.True(t, northSouthLine.Crosses(eastWestLine))
assert.False(t, northSouthLine.Crosses(mustNewGeomFromWKT(t, c, "LINESTRING (0 0, 0 1)")))
assert.False(t, northSouthLine.Disjoint(eastWestLine))
assert.True(t, southEastSquare.Disjoint(mustNewGeomFromWKT(t, c, "LINESTRING (0 0, 0 1)")))
assert.Equal(t, unitSquare.Distance(unitSquare), 0.)
assert.Equal(t, unitSquare.Distance(mustNewGeomFromWKT(t, c, "POLYGON ((2 0, 3 0, 3 1, 2 1, 2 0))")), 1.)
assert.Equal(t, unitSquare.DistanceIndexed(mustNewGeomFromWKT(t, c, "POLYGON ((2 0, 3 0, 3 1, 2 1, 2 0))")), 1.)
assert.True(t, unitSquare.DistanceWithin(mustNewGeomFromWKT(t, c, "POINT (2 2)"), 2))
assert.False(t, unitSquare.DistanceWithin(mustNewGeomFromWKT(t, c, "POINT (2 2)"), 1))
assert.True(t, middleSquare.Equals(unitSquare.Intersection(middleSquare)))
assert.True(t, unitSquare.EqualsExact(unitSquare, 0.125))
assert.Equal(t, unitSquare.FrechetDistance(unitSquare), 0.)
assert.Equal(t, mustNewGeomFromWKT(t, c, "LINESTRING (0 1, 0 0)").FrechetDistance(mustNewGeomFromWKT(t, c, "LINESTRING (0 0, 0 1)")), 1.)
assert.Equal(t, unitSquare.FrechetDistance(mustNewGeomFromWKT(t, c, "LINESTRING (0 0, 0 1)")), 1.)
assert.Equal(t, unitSquare.FrechetDistanceDensify(unitSquare, 0.1), 0.)
assert.Equal(t, unitSquare.HausdorffDistance(unitSquare), 0.)
assert.Equal(t, unitSquare.HausdorffDistance(mustNewGeomFromWKT(t, c, "LINESTRING (0 0, 0 1)")), 1.)
assert.Equal(t, unitSquare.HausdorffDistanceDensify(mustNewGeomFromWKT(t, c, "LINESTRING (0 0, 0 1)"), 0.01), 1.)
assert.Equal(t, eastWestLine.ProjectNormalized(mustNewGeomFromWKT(t, c, "Point (0.5 0.5)")), 0.5)
assert.Equal(t, eastWestLine.Project(mustNewGeomFromWKT(t, c, "Point (0.5 0.5)")), 0.5)
assert.True(t, northSouthLine.Intersects(eastWestLine))
assert.False(t, southEastSquare.Intersects(mustNewGeomFromWKT(t, c, "LINESTRING (0 0, 0 1)")))
assert.Equal(t, [][]float64{{1, 1}, {2, 2}}, unitSquare.NearestPoints(mustNewGeomFromWKT(t, c, "POLYGON ((2 2, 3 2, 3 3, 2 3, 2 2))")))
assert.Equal(t, nil, unitSquare.NearestPoints(mustNewGeomFromWKT(t, c, "GEOMETRYCOLLECTION EMPTY")))
assert.True(t, middleSquare.Overlaps(southEastSquare))
assert.False(t, northWestSquare.Overlaps(southEastSquare))
assert.True(t, eastWestLine.Touches(southEastSquare))
assert.False(t, southEastSquare.Touches(mustNewGeomFromWKT(t, c, "LINESTRING (0 0, 0 1)")))
assert.True(t, middleSquare.Within(unitSquare))
assert.False(t, unitSquare.Within(middleSquare))
assert.Equal(t, 1.0, northSouthLine.Buffer(0.5, 4).MinimumWidth().Length())
assert.Equal(t, 3, northSouthLine.Densify(0.5).NumPoints())
}
func TestPointMethods(t *testing.T) {
for _, tc := range []struct {
name string
wkt string
expectedCoordSeqCoords [][]float64
expectedX float64
expectedY float64
}{
{
name: "point",
wkt: "POINT (1 2)",
expectedCoordSeqCoords: [][]float64{{1, 2}},
expectedX: 1,
expectedY: 2,
},
} {
t.Run(tc.name, func(t *testing.T) {
defer runtime.GC() // Exercise finalizers.
c := geos.NewContext()
g := mustNewGeomFromWKT(t, c, tc.wkt)
assert.Equal(t, tc.expectedCoordSeqCoords, g.CoordSeq().ToCoords())
assert.Equal(t, tc.expectedX, g.X())
assert.Equal(t, tc.expectedY, g.Y())
})
}
}
func TestLineStringMethods(t *testing.T) {
for _, tc := range []struct {
name string
wkt string
expectedClosed bool
expectedCoordSeqCoords [][]float64
expectedPointWKTs []string
expectedRing bool
}{
{
name: "linestring",
wkt: "LINESTRING (0 1, 2 3)",
expectedClosed: false,
expectedCoordSeqCoords: [][]float64{{0, 1}, {2, 3}},
expectedPointWKTs: []string{
"POINT (0 1)",
"POINT (2 3)",
},
expectedRing: false,
},
{
name: "linearring",
wkt: "LINEARRING (0 0, 1 0, 1 1, 0 0)",
expectedClosed: true,
expectedCoordSeqCoords: [][]float64{{0, 0}, {1, 0}, {1, 1}, {0, 0}},
expectedPointWKTs: []string{
"POINT (0 0)",
"POINT (1 0)",
"POINT (1 1)",
"POINT (0 0)",
},
expectedRing: true,
},
} {
t.Run(tc.name, func(t *testing.T) {
defer runtime.GC() // Exercise finalizers.
c := geos.NewContext()
g := mustNewGeomFromWKT(t, c, tc.wkt)
assert.Equal(t, tc.expectedClosed, g.IsClosed())
assert.Equal(t, len(tc.expectedPointWKTs), g.NumPoints())
for i, expectedPointWKT := range tc.expectedPointWKTs {
expectedPoint := mustNewGeomFromWKT(t, c, expectedPointWKT)
assert.True(t, expectedPoint.Equals(g.Point(i)))
}
assert.Equal(t, tc.expectedRing, g.IsRing())
assert.Equal(t, tc.expectedCoordSeqCoords, g.CoordSeq().ToCoords())
})
}
}
func TestPolygonMethods(t *testing.T) {
defer runtime.GC() // Exercise finalizers.
c := geos.NewContext()
polygon := mustNewGeomFromWKT(t, c, "POLYGON ((0 0, 3 0, 3 3, 0 3, 0 0), (1 1, 1 2, 2 2, 2 1, 1 1))")
assert.Equal(t, nil, polygon.CoordSeq())
assert.Equal(t, 1, polygon.NumInteriorRings())
exteriorRing := polygon.ExteriorRing()
expectedExteriorRing := mustNewGeomFromWKT(t, c, "LINEARRING (0 0, 3 0, 3 3, 0 3, 0 0)")
assert.True(t, expectedExteriorRing.Equals(exteriorRing))
assert.NotEqual(t, nil, exteriorRing.CoordSeq())
interiorRing := polygon.InteriorRing(0)
expectedInteriorRing := mustNewGeomFromWKT(t, c, "LINEARRING (1 1, 1 2, 2 2, 2 1, 1 1)")
assert.True(t, expectedInteriorRing.Equals(interiorRing))
assert.NotEqual(t, nil, interiorRing.CoordSeq())
}
func TestGeometryPanics(t *testing.T) {
defer runtime.GC() // Exercise finalizers.
c := geos.NewContext()
assert.Panics(t, func() { c.NewEmptyLineString().Point(-1) })
assert.Panics(t, func() { c.NewEmptyLineString().Point(0) })
assert.NotPanics(t, func() { c.NewEmptyPolygon().ExteriorRing() })
assert.Panics(t, func() { c.NewEmptyPolygon().InteriorRing(-1) })
assert.Panics(t, func() { c.NewEmptyPolygon().InteriorRing(0) })
assert.NotPanics(t, func() {
g := geos.NewEmptyPoint()
g.Destroy()
g.Destroy()
})
}
func TestBinaryMethods(t *testing.T) {
defer runtime.GC() // Exercise finalizers.
c := geos.NewContext()
multiPoint1 := mustNewGeomFromWKT(t, c, "MULTIPOINT (0 0,1 1)")
multiPoint2 := mustNewGeomFromWKT(t, c, "MULTIPOINT (1 1,2 2)")
difference := multiPoint1.Difference(multiPoint2)
assert.True(t, mustNewGeomFromWKT(t, c, "POINT (0 0)").Equals(difference))
}
func TestGeomInterpolate(t *testing.T) {
defer runtime.GC() // Exercise finalizers.
c := geos.NewContext()
lineString := mustNewGeomFromWKT(t, c, "LINESTRING (0 0,1 0)")
assert.True(t, mustNewGeomFromWKT(t, c, "POINT (0.5 0)").Equals(lineString.Interpolate(0.5)))
point := mustNewGeomFromWKT(t, c, "POINT (0 0)")
assert.Equal(t, nil, point.Interpolate(0.5))
}
func TestGeomPolygonizeFull(t *testing.T) {
for _, tc := range []struct {
name string
wkt string
expectedWKT string
expectedCutsWKT string
expectedDanglesWKT string
expectedInvalidRingsWKT string
}{
{
name: "empty",
wkt: "GEOMETRYCOLLECTION EMPTY",
expectedWKT: "GEOMETRYCOLLECTION EMPTY",
expectedCutsWKT: "GEOMETRYCOLLECTION EMPTY",
expectedDanglesWKT: "GEOMETRYCOLLECTION EMPTY",
expectedInvalidRingsWKT: "GEOMETRYCOLLECTION EMPTY",
},
{
name: "simple",
wkt: "MULTILINESTRING ((0 0,1 0,1 1),(1 1,0 1,0 0))",
expectedWKT: "GEOMETRYCOLLECTION (POLYGON ((0 0,1 0,1 1,0 1,0 0)))",
expectedCutsWKT: "GEOMETRYCOLLECTION EMPTY",
expectedDanglesWKT: "GEOMETRYCOLLECTION EMPTY",
expectedInvalidRingsWKT: "GEOMETRYCOLLECTION EMPTY",
},
{
name: "dangle",
wkt: "MULTILINESTRING ((0 0,1 0,1 1),(1 1,0 1,0 0),(0 0,0 -1))",
expectedWKT: "GEOMETRYCOLLECTION (POLYGON ((0 0,1 0,1 1,0 1,0 0)))",
expectedCutsWKT: "GEOMETRYCOLLECTION EMPTY",
expectedDanglesWKT: "GEOMETRYCOLLECTION (LINESTRING (0 0,0 -1))",
expectedInvalidRingsWKT: "GEOMETRYCOLLECTION EMPTY",
},
} {
t.Run(tc.name, func(t *testing.T) {
c := geos.NewContext()
g := mustNewGeomFromWKT(t, c, tc.wkt)
actual, cuts, dangles, invalidRings := g.PolygonizeFull()
assert.Equal(t, mustNewGeomFromWKT(t, c, tc.expectedWKT), actual)
assert.Equal(t, mustNewGeomFromWKT(t, c, tc.expectedCutsWKT), cuts)
assert.Equal(t, mustNewGeomFromWKT(t, c, tc.expectedDanglesWKT), dangles)
assert.Equal(t, mustNewGeomFromWKT(t, c, tc.expectedInvalidRingsWKT), invalidRings)
})
}
}
func TestNewGeomFromGeoJSON(t *testing.T) {
for i, tc := range []struct {
geoJSON string
expectedWKT string
}{
{
geoJSON: `{"type":"Point","coordinates":[1,2]}`,
expectedWKT: "POINT (1 2)",
},
{
geoJSON: `{"type":"LineString","coordinates":[[1,2],[3,4]]}`,
expectedWKT: "LINESTRING (1 2, 3 4)",
},
} {
t.Run(strconv.Itoa(i), func(t *testing.T) {
defer runtime.GC() // Exercise finalizers.
context := geos.NewContext()
actualGeom, err := context.NewGeomFromGeoJSON(tc.geoJSON)
assert.NoError(t, err)
assert.True(t, mustNewGeomFromWKT(t, context, tc.expectedWKT).Equals(actualGeom))
})
}
}
func TestNewGeomFromGeoJSONError(t *testing.T) {
_, err := geos.NewContext().NewGeomFromGeoJSON(`{"type":`)
assert.Error(t, err)
}
func TestGeomNearestPointsAliasing(t *testing.T) {
c := geos.NewContext()
geom1 := mustNewGeomFromWKT(t, c, "POINT (0 1)")
geom2 := mustNewGeomFromWKT(t, c, "POINT (2 3)")
points := geom1.NearestPoints(geom2)
points[0] = append(points[0], 4)
assert.Equal(t, []float64{2, 3}, points[1])
}
func TestGeomToJSON(t *testing.T) {
geom := mustNewGeomFromWKT(t, geos.NewContext(), "POINT (1 2)")
assert.Equal(t, `{"type":"Point","coordinates":[1.0,2.0]}`, geom.ToGeoJSON(-1))
}
func TestWKBError(t *testing.T) {
_, err := geos.NewContext().NewGeomFromWKT("POINT (0 0")
assert.Error(t, err)
assert.Equal(t, err.Error(), "ParseException: Expected word but encountered end of stream")
}
func TestWKTError(t *testing.T) {
_, err := geos.NewContext().NewGeomFromWKB(nil)
assert.Error(t, err)
assert.Equal(t, err.Error(), "ParseException: Unexpected EOF parsing WKB")
}
func TestWKXRoundTrip(t *testing.T) {
for _, tc := range []struct {
name string
wkt string
wktPre3_12 string
}{
{
name: "point",
wkt: "POINT (0 0)",
wktPre3_12: "POINT (0.0000000000000000 0.0000000000000000)",
},
{
name: "line_string",
wkt: "LINESTRING (0 0, 1 0)",
wktPre3_12: "LINESTRING (0.0000000000000000 0.0000000000000000, 1.0000000000000000 0.0000000000000000)",
},
} {
t.Run(tc.name, func(t *testing.T) {
defer runtime.GC() // Exercise finalizers.
c := geos.NewContext()
wkt := tc.wkt
if geos.VersionCompare(3, 12, 0) < 0 {
wkt = tc.wktPre3_12
}
g := mustNewGeomFromWKT(t, c, wkt)
assert.Equal(t, wkt, g.ToWKT())
newG, err := c.NewGeomFromWKB(g.ToWKB())
assert.NoError(t, err)
assert.Equal(t, wkt, newG.ToWKT())
})
}
}
func TestEWKBWithSRIDRoundTrip(t *testing.T) {
c := geos.NewContext()
for _, tc := range []struct {
name string
geom *geos.Geom
}{
{
name: "point",
geom: mustNewGeomFromWKT(t, c, "POINT (0 0)").SetSRID(4326),
},
} {
t.Run(tc.name, func(t *testing.T) {
newG, err := c.NewGeomFromWKB(tc.geom.ToEWKBWithSRID())
assert.NoError(t, err)
assert.True(t, newG.Equals(tc.geom))
assert.Equal(t, tc.geom.SRID(), newG.SRID())
})
}
}
func TestGeomRelate(t *testing.T) {
c := geos.NewContext()
g1 := mustNewGeomFromWKT(t, c, "POINT (0 0)")
g2 := mustNewGeomFromWKT(t, c, "LINESTRING (0 0,1 0)")
assert.Equal(t, "F0FFFF102", g1.Relate(g2))
}
func TestSetPrecision(t *testing.T) {
g1 := mustNewGeomFromWKT(t, geos.NewContext(), "POINT (1 2)")
g2 := g1.SetPrecision(1, geos.PrecisionRulePointwise)
assert.Equal(t, 0., g1.Precision())
assert.Equal(t, 1., g2.Precision())
}
func TestUserData(t *testing.T) {
g := mustNewGeomFromWKT(t, geos.NewContext(), "POINT (0 0)")
assert.Equal(t, uintptr(0), g.UserData())
assert.Equal(t, g, g.SetUserData(1))
assert.Equal(t, uintptr(1), g.UserData())
}
func TestMakeValid(t *testing.T) {
for _, tc := range []struct {
name string
wkt string
expectedWktLineworkDiscard string
expectedWktStructureDiscard string
expectedWktLineworkKeep string
expectedWktStructureKeep string
}{
{
name: "LINESTRING",
wkt: `LINESTRING(0 0, 0 0)`,
expectedWktLineworkDiscard: `POINT (0.0 0.0)`,
expectedWktStructureDiscard: `LINESTRING EMPTY`,
expectedWktLineworkKeep: `POINT (0.0 0.0)`,
expectedWktStructureKeep: `POINT (0.0 0.0)`,
},
{
name: "MULTIPOLYGON",
wkt: `MULTIPOLYGON(((91 50,79 22,51 10,23 22,11 50,23 78,51 90,79 78,91 50)),
((91 100,79 72,51 60,23 72,11 100,23 128,51 140,79 128,91 100)),
((91 150,79 122,51 110,23 122,11 150,23 178,51 190,79 178,91 150)),
((141 50,129 22,101 10,73 22,61 50,73 78,101 90,129 78,141 50)),
((141 100,129 72,101 60,73 72,61 100,73 128,101 140,129 128,141 100)),
((141 150,129 122,101 110,73 122,61 150,73 178,101 190,129 178,141 150)))`,
expectedWktLineworkDiscard: `MultiPolygon (((51 110, 68.5 117.5, 61 100, 68.5 82.5, 51 90, 23 78, 21.7142857142857153 75, 11 100, 21.7142857142857153 125, 23 122, 51 110)),((51 190, 76 179.28571428571427759, 73 178, 61 150, 68.5 132.5, 51 140, 23 128, 21.7142857142857153 125, 11 150, 23 178, 51 190)),((141 150, 130.28571428571427759 125, 129 128, 101 140, 83.5 132.5, 91 150, 79 178, 76 179.28571428571427759, 101 190, 129 178, 141 150)),((129 78, 101 90, 83.5 82.5, 91 100, 83.5 117.5, 101 110, 129 122, 130.28571428571427759 125, 141 100, 130.28571428571427759 75, 129 78)),((101 10, 76 20.7142857142857153, 79 22, 91 50, 83.5 67.5, 101 60, 129 72, 130.28571428571427759 75, 141 50, 129 22, 101 10)),((11 50, 21.7142857142857153 75, 23 72, 51 60, 68.5 67.5, 61 50, 73 22, 76 20.7142857142857153, 51 10, 23 22, 11 50)),((83.5 82.5, 80.2857142857142918 75, 79 78, 76 79.2857142857142918, 83.5 82.5)),((83.5 67.5, 76 70.7142857142857082, 79 72, 80.2857142857142918 75, 83.5 67.5)),((68.5 67.5, 71.7142857142857082 75, 73 72, 76 70.7142857142857082, 68.5 67.5)),((68.5 82.5, 76 79.2857142857142918, 73 78, 71.7142857142857082 75, 68.5 82.5)),((83.5 132.5, 80.2857142857142918 125, 79 128, 76 129.28571428571427759, 83.5 132.5)),((83.5 117.5, 76 120.7142857142857082, 79 122, 80.2857142857142918 125, 83.5 117.5)),((68.5 117.5, 71.7142857142857082 125, 73 122, 76 120.7142857142857082, 68.5 117.5)),((68.5 132.5, 76 129.28571428571427759, 73 128, 71.7142857142857082 125, 68.5 132.5)))`,
expectedWktStructureDiscard: `Polygon ((23 22, 11 50, 21.7142857142857153 75, 11 100, 21.7142857142857153 125, 11 150, 23 178, 51 190, 76 179.28571428571427759, 101 190, 129 178, 141 150, 130.28571428571427759 125, 141 100, 130.28571428571427759 75, 141 50, 129 22, 101 10, 76 20.7142857142857153, 51 10, 23 22))`,
expectedWktLineworkKeep: `MultiPolygon (((51 110, 68.5 117.5, 61 100, 68.5 82.5, 51 90, 23 78, 21.7142857142857153 75, 11 100, 21.7142857142857153 125, 23 122, 51 110)),((51 190, 76 179.28571428571427759, 73 178, 61 150, 68.5 132.5, 51 140, 23 128, 21.7142857142857153 125, 11 150, 23 178, 51 190)),((141 150, 130.28571428571427759 125, 129 128, 101 140, 83.5 132.5, 91 150, 79 178, 76 179.28571428571427759, 101 190, 129 178, 141 150)),((129 78, 101 90, 83.5 82.5, 91 100, 83.5 117.5, 101 110, 129 122, 130.28571428571427759 125, 141 100, 130.28571428571427759 75, 129 78)),((101 10, 76 20.7142857142857153, 79 22, 91 50, 83.5 67.5, 101 60, 129 72, 130.28571428571427759 75, 141 50, 129 22, 101 10)),((11 50, 21.7142857142857153 75, 23 72, 51 60, 68.5 67.5, 61 50, 73 22, 76 20.7142857142857153, 51 10, 23 22, 11 50)),((83.5 82.5, 80.2857142857142918 75, 79 78, 76 79.2857142857142918, 83.5 82.5)),((83.5 67.5, 76 70.7142857142857082, 79 72, 80.2857142857142918 75, 83.5 67.5)),((68.5 67.5, 71.7142857142857082 75, 73 72, 76 70.7142857142857082, 68.5 67.5)),((68.5 82.5, 76 79.2857142857142918, 73 78, 71.7142857142857082 75, 68.5 82.5)),((83.5 132.5, 80.2857142857142918 125, 79 128, 76 129.28571428571427759, 83.5 132.5)),((83.5 117.5, 76 120.7142857142857082, 79 122, 80.2857142857142918 125, 83.5 117.5)),((68.5 117.5, 71.7142857142857082 125, 73 122, 76 120.7142857142857082, 68.5 117.5)),((68.5 132.5, 76 129.28571428571427759, 73 128, 71.7142857142857082 125, 68.5 132.5)))`,
expectedWktStructureKeep: `Polygon ((23 22, 11 50, 21.7142857142857153 75, 11 100, 21.7142857142857153 125, 11 150, 23 178, 51 190, 76 179.28571428571427759, 101 190, 129 178, 141 150, 130.28571428571427759 125, 141 100, 130.28571428571427759 75, 141 50, 129 22, 101 10, 76 20.7142857142857153, 51 10, 23 22))`,
},
} {
t.Run(tc.name, func(t *testing.T) {
c := geos.NewContext()
g := newInvalidGeomFromWKT(t, c, tc.wkt)
v1 := mustNewGeomFromWKT(t, c, tc.expectedWktLineworkDiscard)
v2 := mustNewGeomFromWKT(t, c, tc.expectedWktStructureDiscard)
v3 := mustNewGeomFromWKT(t, c, tc.expectedWktLineworkKeep)
v4 := mustNewGeomFromWKT(t, c, tc.expectedWktStructureKeep)
assert.Equal(t, v1, g.MakeValidWithParams(geos.MakeValidLinework, geos.MakeValidDiscardCollapsed))
assert.Equal(t, v2, g.MakeValidWithParams(geos.MakeValidStructure, geos.MakeValidDiscardCollapsed))
assert.Equal(t, v3, g.MakeValidWithParams(geos.MakeValidLinework, geos.MakeValidKeepCollapsed))
assert.Equal(t, v4, g.MakeValidWithParams(geos.MakeValidStructure, geos.MakeValidKeepCollapsed))
})
}
}