github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/common/location/location_test.go (about)

     1  // This file is part of the Smart Home
     2  // Program complex distribution https://github.com/e154/smart-home
     3  // Copyright (C) 2023, Filippov Alex
     4  //
     5  // This library is free software: you can redistribute it and/or
     6  // modify it under the terms of the GNU Lesser General Public
     7  // License as published by the Free Software Foundation; either
     8  // version 3 of the License, or (at your option) any later version.
     9  //
    10  // This library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13  // Library General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public
    16  // License along with this library.  If not, see
    17  // <https://www.gnu.org/licenses/>.
    18  
    19  package location
    20  
    21  import (
    22  	"math"
    23  	"testing"
    24  
    25  	"github.com/stretchr/testify/require"
    26  
    27  	m "github.com/e154/smart-home/models"
    28  )
    29  
    30  func TestGetDistanceBetweenPoints(t *testing.T) {
    31  
    32  	point1 := m.Point{
    33  		Lon: 80.113927,
    34  		Lat: 6.131738,
    35  	}
    36  	point2 := m.Point{
    37  		Lon: 80.108525,
    38  		Lat: 6.125416,
    39  	}
    40  
    41  	distance := GetDistanceBetweenPoints(point1, point2)
    42  	require.Equal(t, 0.93, math.Ceil(distance*100)/100)
    43  
    44  }
    45  
    46  func TestCalculateDistanceToPolygon(t *testing.T) {
    47  
    48  	point1 := m.Point{
    49  		Lon: 80.108620,
    50  		Lat: 6.125419,
    51  	}
    52  	polygon := []m.Point{
    53  		{
    54  			Lon: 80.112948,
    55  			Lat: 6.131909,
    56  		},
    57  		{
    58  			Lon: 80.113697,
    59  			Lat: 6.132449,
    60  		},
    61  		{
    62  			Lon: 80.114448,
    63  			Lat: 6.131780,
    64  		},
    65  		{
    66  			Lon: 80.113730,
    67  			Lat: 6.131185,
    68  		},
    69  	}
    70  
    71  	distance := GetDistanceToPolygon(point1, polygon)
    72  	require.Equal(t, 0.86, math.Ceil(distance*100)/100)
    73  
    74  }
    75  
    76  func TestPointInsidePolygon(t *testing.T) {
    77  
    78  	point1 := m.Point{
    79  		Lon: 80.103999,
    80  		Lat: 6.121958,
    81  	}
    82  	polygon := []m.Point{
    83  		{
    84  			Lon: 80.112948,
    85  			Lat: 6.131909,
    86  		},
    87  		{
    88  			Lon: 80.113697,
    89  			Lat: 6.132449,
    90  		},
    91  		{
    92  			Lon: 80.114448,
    93  			Lat: 6.131780,
    94  		},
    95  		{
    96  			Lon: 80.113730,
    97  			Lat: 6.131185,
    98  		},
    99  		{
   100  			Lon: 80.112948,
   101  			Lat: 6.131909,
   102  		},
   103  	}
   104  
   105  	contains := PointInsidePolygon(point1, polygon)
   106  	require.False(t, contains)
   107  
   108  	point1 = m.Point{
   109  		Lon: 77.782887,
   110  		Lat: 14.242355,
   111  	}
   112  
   113  	contains = PointInsidePolygon(point1, polygon)
   114  	require.False(t, contains)
   115  
   116  	point1 = m.Point{
   117  		Lon: 80.113927,
   118  		Lat: 6.131738,
   119  	}
   120  
   121  	contains = PointInsidePolygon(point1, polygon)
   122  	require.True(t, contains)
   123  
   124  	point1 = m.Point{
   125  		Lon: 80.113564,
   126  		Lat: 6.131762,
   127  	}
   128  
   129  	contains = PointInsidePolygon(point1, polygon)
   130  	require.True(t, contains)
   131  
   132  }