]> git.openstreetmap.org Git - nominatim.git/blob - test/php/Nominatim/NearPointTest.php
fix style
[nominatim.git] / test / php / Nominatim / NearPointTest.php
1 <?php
2
3 namespace Nominatim;
4
5 require '../../lib/NearPoint.php';
6
7 class NearPointTest extends \PHPUnit_Framework_TestCase
8 {
9
10
11     protected function setUp()
12     {
13     }
14
15     public function testExtractFromQuery()
16     {
17         // no coordinates expected
18         $this->assertFalse(NearPoint::extractFromQuery(''));
19         $this->assertFalse(NearPoint::extractFromQuery('abc'));
20         $this->assertFalse(NearPoint::extractFromQuery('12 34'));
21         $this->assertFalse(NearPoint::extractFromQuery('200.1 89.9')); // because latitude > 180
22
23         // coordinates expected
24         $this->assertNotNull(NearPoint::extractFromQuery('0.0 -0.0'));
25
26         $aRes = NearPoint::extractFromQuery(' abc 12.456 -78.90 def ');
27         $this->assertEquals($aRes['pt']->lat(), 12.456);
28         $this->assertEquals($aRes['pt']->lon(), -78.90);
29         $this->assertEquals($aRes['pt']->radius(), 0.1);
30         $this->assertEquals($aRes['query'], 'abc   def');
31
32         $aRes = NearPoint::extractFromQuery(' [12.456,-78.90] ');
33         $this->assertEquals($aRes['pt']->lat(), 12.456);
34         $this->assertEquals($aRes['pt']->lon(), -78.90);
35         $this->assertEquals($aRes['pt']->radius(), 0.1);
36         $this->assertEquals($aRes['query'], '');
37
38         // http://en.wikipedia.org/wiki/Geographic_coordinate_conversion
39         // these all represent the same location
40         $aQueries = array(
41                      '40 26.767 N 79 58.933 W',
42                      '40° 26.767′ N 79° 58.933′ W',
43                      "40° 26.767' N 79° 58.933' W",
44                      'N 40 26.767, W 79 58.933',
45                      'N 40°26.767′, W 79°58.933′',
46                      "N 40°26.767', W 79°58.933'",
47  
48                      '40 26 46 N 79 58 56 W',
49                      '40° 26′ 46″ N 79° 58′ 56″ W',
50                      'N 40 26 46 W 79 58 56',
51                      'N 40° 26′ 46″, W 79° 58′ 56″',
52                      'N 40° 26\' 46", W 79° 58\' 56"',
53  
54                      '40.446 -79.982',
55                      '40.446,-79.982',
56                      '40.446° N 79.982° W',
57                      'N 40.446° W 79.982°',
58  
59                      '[40.446 -79.982]',
60                      '       40.446  ,   -79.982     ',
61                     );
62
63
64         foreach ($aQueries as $sQuery) {
65             $aRes = NearPoint::extractFromQuery($sQuery);
66             $this->assertEquals(40.446, $aRes['pt']->lat(), 'degrees decimal ' . $sQuery, 0.01);
67             $this->assertEquals(-79.982, $aRes['pt']->lon(), 'degrees decimal ' . $sQuery, 0.01);
68         }
69     }
70
71     public function testWithinSQL()
72     {
73         $np = new NearPoint(0.1, 23, 1);
74
75         $this->assertEquals(
76             'ST_DWithin(foo, ST_SetSRID(ST_Point(23,0.1),4326), 1.000000)',
77             $np->withinSQL('foo')
78         );
79     }
80
81     public function testDistanceSQL()
82     {
83         $np = new NearPoint(0.1, 23, 1);
84
85         $this->assertEquals(
86             'ST_Distance(ST_SetSRID(ST_Point(23,0.1),4326), foo)',
87             $np->distanceSQL('foo')
88         );
89     }
90 }