github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/geo/geomfn/linear_reference_test.go (about)

     1  // Copyright 2020 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package geomfn
    12  
    13  import (
    14  	"fmt"
    15  	"testing"
    16  
    17  	"github.com/cockroachdb/cockroach/pkg/geo"
    18  	"github.com/stretchr/testify/require"
    19  )
    20  
    21  func TestLineInterpolatePoints(t *testing.T) {
    22  	var testCasesForLineInterpolate = []struct {
    23  		wkb                       string
    24  		errMsg                    string
    25  		fraction                  float64
    26  		expectedWKTForRepeatTrue  string
    27  		expectedWKTForRepeatFalse string
    28  	}{
    29  		{
    30  			wkb:      "LINESTRING (0 0, 1 1)",
    31  			errMsg:   "fraction -0.200000 should be within [0 1] range",
    32  			fraction: -0.2,
    33  		},
    34  		{
    35  			wkb:      "LINESTRING (0 0, 1 1, 2 5)",
    36  			errMsg:   "fraction 1.500000 should be within [0 1] range",
    37  			fraction: 1.5,
    38  		},
    39  		{
    40  			wkb:      "MULTILINESTRING ((0 0, 1 1, 2 5), (0 0, 1 1))",
    41  			errMsg:   "geometry MultiLineString should be LineString",
    42  			fraction: 0.3,
    43  		},
    44  		{
    45  			wkb:      "POINT (0 0)",
    46  			errMsg:   "geometry Point should be LineString",
    47  			fraction: 0.3,
    48  		},
    49  		{
    50  			wkb:      "POLYGON((-1.0 0.0, 0.0 0.0, 0.0 1.0, -1.0 1.0, -1.0 0.0))",
    51  			errMsg:   "geometry Polygon should be LineString",
    52  			fraction: 0.3,
    53  		},
    54  		{
    55  			wkb:                       "LINESTRING (0 0, 1 1, 2 5)",
    56  			fraction:                  0.51,
    57  			expectedWKTForRepeatTrue:  "POINT (1.3419313865603413 2.367725546241365)",
    58  			expectedWKTForRepeatFalse: "POINT (1.3419313865603413 2.367725546241365)",
    59  		},
    60  		{
    61  			wkb:                       "LINESTRING (0 0, 1 1, 2 5)",
    62  			fraction:                  0.5,
    63  			expectedWKTForRepeatTrue:  "MULTIPOINT (1.3285014148574912 2.3140056594299647, 2 5)",
    64  			expectedWKTForRepeatFalse: "POINT (1.3285014148574912 2.3140056594299647)",
    65  		},
    66  		{
    67  			wkb:                       "LINESTRING (0 0, 1 1, 2 5)",
    68  			fraction:                  0.2,
    69  			expectedWKTForRepeatTrue:  "MULTIPOINT (0.78309518948453 0.78309518948453, 1.1942016978289893 1.7768067913159575, 1.462801131885993 2.851204527543972, 1.7314005659429965 3.925602263771986, 2 5)",
    70  			expectedWKTForRepeatFalse: "POINT (0.78309518948453 0.78309518948453)",
    71  		},
    72  		{
    73  			wkb:                       "LINESTRING (0 0, 1 1, 2 5)",
    74  			fraction:                  0,
    75  			expectedWKTForRepeatTrue:  "POINT (0 0)",
    76  			expectedWKTForRepeatFalse: "POINT (0 0)",
    77  		},
    78  		{
    79  			wkb:                       "LINESTRING (0 0, 1 1, 2 5)",
    80  			fraction:                  1,
    81  			expectedWKTForRepeatTrue:  "POINT (2 5)",
    82  			expectedWKTForRepeatFalse: "POINT (2 5)",
    83  		},
    84  	}
    85  	for _, test := range testCasesForLineInterpolate {
    86  		for _, repeat := range []bool{false, true} {
    87  			t.Run(fmt.Sprintf("%s for fraction %f where repeat is %t", test.wkb, test.fraction, repeat),
    88  				func(t *testing.T) {
    89  					geometry, err := geo.ParseGeometry(test.wkb)
    90  					require.NoError(t, err)
    91  					interpolatedPoint, err := LineInterpolatePoints(geometry, test.fraction, repeat)
    92  					if test.errMsg == "" {
    93  						require.NoError(t, err)
    94  						expectedWKTForRepeat := test.expectedWKTForRepeatFalse
    95  						if repeat {
    96  							expectedWKTForRepeat = test.expectedWKTForRepeatTrue
    97  						}
    98  						expectedInterpolatedPoint, err := geo.ParseGeometry(expectedWKTForRepeat)
    99  						require.NoError(t, err)
   100  						require.Equal(t, expectedInterpolatedPoint, interpolatedPoint)
   101  					} else {
   102  						require.EqualError(t, err, test.errMsg)
   103  					}
   104  				})
   105  		}
   106  	}
   107  }