]> git.openstreetmap.org Git - nominatim.git/blob - test/php/Nominatim/NearPointTest.php
NearPoint::extractFromQuery - greedy-match optional quote sign
[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         $aRes = NearPoint::extractFromQuery(' -12.456,-78.90 ');
39         $this->assertEquals($aRes['pt']->lat(), -12.456);
40         $this->assertEquals($aRes['pt']->lon(), -78.90);
41
42         // http://en.wikipedia.org/wiki/Geographic_coordinate_conversion
43         // these all represent the same location
44         $aQueries = array(
45                      '40 26.767 N 79 58.933 W',
46                      '40° 26.767′ N 79° 58.933′ W',
47                      "40° 26.767' N 79° 58.933' W",
48                      'N 40 26.767, W 79 58.933',
49                      'N 40°26.767′, W 79°58.933′',
50                      "N 40°26.767', W 79°58.933'",
51  
52                      '40 26 46 N 79 58 56 W',
53                      '40° 26′ 46″ N 79° 58′ 56″ W',
54                      'N 40 26 46 W 79 58 56',
55                      'N 40° 26′ 46″, W 79° 58′ 56″',
56                      'N 40° 26\' 46", W 79° 58\' 56"',
57  
58                      '40.446 -79.982',
59                      '40.446,-79.982',
60                      '40.446° N 79.982° W',
61                      'N 40.446° W 79.982°',
62  
63                      '[40.446 -79.982]',
64                      '       40.446  ,   -79.982     ',
65                     );
66
67
68         foreach ($aQueries as $sQuery) {
69             $aRes = NearPoint::extractFromQuery($sQuery);
70             $this->assertEquals(40.446, $aRes['pt']->lat(), 'degrees decimal ' . $sQuery, 0.01);
71             $this->assertEquals(-79.982, $aRes['pt']->lon(), 'degrees decimal ' . $sQuery, 0.01);
72             $this->assertEquals('', $aRes['query']);
73         }
74     }
75
76     public function testWithinSQL()
77     {
78         $np = new NearPoint(0.1, 23, 1);
79
80         $this->assertEquals(
81             'ST_DWithin(foo, ST_SetSRID(ST_Point(23,0.1),4326), 1.000000)',
82             $np->withinSQL('foo')
83         );
84     }
85
86     public function testDistanceSQL()
87     {
88         $np = new NearPoint(0.1, 23, 1);
89
90         $this->assertEquals(
91             'ST_Distance(ST_SetSRID(ST_Point(23,0.1),4326), foo)',
92             $np->distanceSQL('foo')
93         );
94     }
95 }