...

Source file src/github.com/golang/geo/s2/projections_test.go

Documentation: github.com/golang/geo/s2

     1  // Copyright 2018 Google Inc. All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package s2
    16  
    17  import (
    18  	"math"
    19  	"testing"
    20  
    21  	"github.com/golang/geo/r2"
    22  	"github.com/golang/geo/r3"
    23  )
    24  
    25  func TestPlateCarreeProjectionInterpolate(t *testing.T) {
    26  	a := r2.Point{1.234, -5.456e-20}
    27  	b := r2.Point{2.1234e-20, 7.456}
    28  	tests := []struct {
    29  		dist float64
    30  		a, b r2.Point
    31  		want r2.Point
    32  	}{
    33  		{
    34  			// Test that coordinates and/or arguments are not accidentally reversed.
    35  			0.25,
    36  			r2.Point{1, 5},
    37  			r2.Point{3, 9},
    38  			r2.Point{1.5, 6},
    39  		},
    40  		{
    41  			// Test extrapolation.
    42  			-2,
    43  			r2.Point{1, 0},
    44  			r2.Point{3, 0},
    45  			r2.Point{-3, 0},
    46  		},
    47  		// Check that interpolation is exact at both endpoints.
    48  		{0, a, b, a},
    49  		{1, a, b, b},
    50  	}
    51  	proj := NewPlateCarreeProjection(180)
    52  
    53  	for _, test := range tests {
    54  		if got := proj.Interpolate(test.dist, test.a, test.b); got != test.want {
    55  			t.Errorf("proj.Interpolate(%v, %v, %v) = %v, want %v", test.dist, test.a, test.b, got, test.want)
    56  		}
    57  	}
    58  }
    59  
    60  func TestPlateCarreeProjectionProjectUnproject(t *testing.T) {
    61  	tests := []struct {
    62  		have Point
    63  		want r2.Point
    64  	}{
    65  		{Point{r3.Vector{1, 0, 0}}, r2.Point{0, 0}},
    66  		{Point{r3.Vector{-1, 0, 0}}, r2.Point{180, 0}},
    67  		{Point{r3.Vector{0, 1, 0}}, r2.Point{90, 0}},
    68  		{Point{r3.Vector{0, -1, 0}}, r2.Point{-90, 0}},
    69  		{Point{r3.Vector{0, 0, 1}}, r2.Point{0, 90}},
    70  		{Point{r3.Vector{0, 0, -1}}, r2.Point{0, -90}},
    71  	}
    72  
    73  	proj := NewPlateCarreeProjection(180)
    74  
    75  	for _, test := range tests {
    76  		if got := proj.Project(test.have); !r2PointsApproxEqual(test.want, got, epsilon) {
    77  			t.Errorf("proj.Project(%v) = %v, want %v", test.have, got, test.want)
    78  		}
    79  		if got := proj.Unproject(test.want); !got.ApproxEqual(test.have) {
    80  			t.Errorf("proj.Unproject(%v) = %v, want %v", test.want, got, test.have)
    81  		}
    82  	}
    83  }
    84  
    85  func TestMercatorProjectionProjectUnproject(t *testing.T) {
    86  	tests := []struct {
    87  		have Point
    88  		want r2.Point
    89  	}{
    90  		{Point{r3.Vector{1, 0, 0}}, r2.Point{0, 0}},
    91  		{Point{r3.Vector{-1, 0, 0}}, r2.Point{180, 0}},
    92  		{Point{r3.Vector{0, 1, 0}}, r2.Point{90, 0}},
    93  		{Point{r3.Vector{0, -1, 0}}, r2.Point{-90, 0}},
    94  		// Test one arbitrary point as a sanity check.
    95  		{PointFromLatLng(LatLng{1, 0}), r2.Point{0, 70.255578967830246}},
    96  	}
    97  
    98  	proj := NewMercatorProjection(180)
    99  
   100  	for _, test := range tests {
   101  		if got := proj.Project(test.have); !r2PointsApproxEqual(test.want, got, epsilon) {
   102  			t.Errorf("proj.Project(%v) = %v, want %v", test.have, got, test.want)
   103  		}
   104  		if got := proj.Unproject(test.want); !got.ApproxEqual(test.have) {
   105  			t.Errorf("proj.Unproject(%v) = %v, want %v", test.want, got, test.have)
   106  		}
   107  	}
   108  
   109  	// two cases have values that should be infinities, so the equality tests
   110  	// need to be inf ready.
   111  	testsInf := []struct {
   112  		have Point
   113  		want r2.Point
   114  	}{
   115  		{Point{r3.Vector{0, 0, 1}}, r2.Point{0, math.Inf(1)}},
   116  		{Point{r3.Vector{0, 0, -1}}, r2.Point{0, math.Inf(-1)}},
   117  	}
   118  	for _, test := range testsInf {
   119  		got := proj.Project(test.have)
   120  		if ((math.IsInf(test.want.X, 1) && !math.IsInf(got.X, 1)) ||
   121  			(math.IsInf(test.want.X, -1) && !math.IsInf(got.X, -1))) ||
   122  			((math.IsInf(test.want.Y, 1) && !math.IsInf(got.Y, 1)) ||
   123  				(math.IsInf(test.want.Y, -1) && !math.IsInf(got.Y, -1))) {
   124  			t.Errorf("proj.Project(%v) = %v, want %v", test.have, got, test.want)
   125  		}
   126  	}
   127  }
   128  

View as plain text