]> git.openstreetmap.org Git - nominatim.git/blob - test/php/Nominatim/LibTest.php
clean up intermediate tables earlier with --drop
[nominatim.git] / test / php / Nominatim / LibTest.php
1 <?php
2
3 namespace Nominatim;
4
5 require_once(CONST_BasePath.'/lib/lib.php');
6 require_once(CONST_BasePath.'/lib/ClassTypes.php');
7
8 class LibTest extends \PHPUnit\Framework\TestCase
9 {
10
11     public function testAddQuotes()
12     {
13         // FIXME: not quoting existing quote signs is probably a bug
14         $this->assertSame("'St. John's'", addQuotes("St. John's"));
15         $this->assertSame("''", addQuotes(''));
16     }
17
18
19     public function testCreatePointsAroundCenter()
20     {
21         // you might say we're creating a circle
22         $aPoints = createPointsAroundCenter(0, 0, 2);
23
24         $this->assertEquals(
25             101,
26             count($aPoints)
27         );
28         $this->assertEquals(
29             array(
30              array('', 0, 2),
31              array('', 0.12558103905863, 1.9960534568565),
32              array('', 0.25066646712861, 1.984229402629)
33             ),
34             array_splice($aPoints, 0, 3)
35         );
36     }
37
38
39     public function testGeometryText2Points()
40     {
41         $fRadius = 1;
42         // invalid value
43         $this->assertEquals(
44             null,
45             geometryText2Points('', $fRadius)
46         );
47
48         // POINT
49         $aPoints = geometryText2Points('POINT(10 20)', $fRadius);
50         $this->assertEquals(
51             101,
52             count($aPoints)
53         );
54         $this->assertEquals(
55             array(
56              array(10, 21),
57              array(10.062790519529, 20.998026728428),
58              array(10.125333233564, 20.992114701314)
59             ),
60             array_splice($aPoints, 0, 3)
61         );
62
63         // POLYGON
64         $this->assertEquals(
65             array(
66              array('30', '10'),
67              array('40', '40'),
68              array('20', '40'),
69              array('10', '20'),
70              array('30', '10')
71             ),
72             geometryText2Points('POLYGON((30 10, 40 40, 20 40, 10 20, 30 10))', $fRadius)
73         );
74
75         // MULTIPOLYGON
76         $this->assertEquals(
77             array(
78              array('30', '20'), // first polygon only
79              array('45', '40'),
80              array('10', '40'),
81              array('30', '20'),
82             ),
83             geometryText2Points('MULTIPOLYGON(((30 20, 45 40, 10 40, 30 20)),((15 5, 40 10, 10 20, 5 10, 15 5)))', $fRadius)
84         );
85     }
86
87     public function testParseLatLon()
88     {
89         // no coordinates expected
90         $this->assertFalse(parseLatLon(''));
91         $this->assertFalse(parseLatLon('abc'));
92         $this->assertFalse(parseLatLon('12 34'));
93
94         // coordinates expected
95         $this->assertNotNull(parseLatLon('0.0 -0.0'));
96
97         $aRes = parseLatLon(' abc 12.456 -78.90 def ');
98         $this->assertEquals($aRes[1], 12.456);
99         $this->assertEquals($aRes[2], -78.90);
100         $this->assertEquals($aRes[0], ' 12.456 -78.90 ');
101
102         $aRes = parseLatLon(' [12.456,-78.90] ');
103         $this->assertEquals($aRes[1], 12.456);
104         $this->assertEquals($aRes[2], -78.90);
105         $this->assertEquals($aRes[0], ' [12.456,-78.90] ');
106
107         $aRes = parseLatLon(' -12.456,-78.90 ');
108         $this->assertEquals($aRes[1], -12.456);
109         $this->assertEquals($aRes[2], -78.90);
110         $this->assertEquals($aRes[0], ' -12.456,-78.90 ');
111
112         // http://en.wikipedia.org/wiki/Geographic_coordinate_conversion
113         // these all represent the same location
114         $aQueries = array(
115                      '40 26.767 N 79 58.933 W',
116                      '40° 26.767′ N 79° 58.933′ W',
117                      "40° 26.767' N 79° 58.933' W",
118                      'N 40 26.767, W 79 58.933',
119                      'N 40°26.767′, W 79°58.933′',
120                      "N 40°26.767', W 79°58.933'",
121  
122                      '40 26 46 N 79 58 56 W',
123                      '40° 26′ 46″ N 79° 58′ 56″ W',
124                      '40° 26′ 46.00″ N 79° 58′ 56.00″ W',
125                      '40°26′46″N 79°58′56″W',
126                      'N 40 26 46 W 79 58 56',
127                      'N 40° 26′ 46″, W 79° 58′ 56″',
128                      'N 40° 26\' 46", W 79° 58\' 56"',
129  
130                      '40.446 -79.982',
131                      '40.446,-79.982',
132                      '40.446° N 79.982° W',
133                      'N 40.446° W 79.982°',
134  
135                      '[40.446 -79.982]',
136                      '       40.446  ,   -79.982     ',
137                     );
138
139
140         foreach ($aQueries as $sQuery) {
141             $aRes = parseLatLon($sQuery);
142             $this->assertEquals(40.446, $aRes[1], 'degrees decimal ' . $sQuery, 0.01);
143             $this->assertEquals(-79.982, $aRes[2], 'degrees decimal ' . $sQuery, 0.01);
144             $this->assertEquals($sQuery, $aRes[0]);
145         }
146     }
147
148     private function closestHouseNumberEvenOddOther($startnumber, $endnumber, $fraction, $aExpected)
149     {
150         foreach (array('even', 'odd', 'other') as $itype) {
151             $this->assertEquals(
152                 $aExpected[$itype],
153                 closestHouseNumber(array(
154                                     'startnumber' => $startnumber,
155                                     'endnumber' => $endnumber,
156                                     'fraction' => $fraction,
157                                     'interpolationtype' => $itype
158                                    )),
159                 "$startnumber => $endnumber, $fraction, $itype"
160             );
161         }
162     }
163
164     public function testClosestHouseNumber()
165     {
166         $this->closestHouseNumberEvenOddOther(50, 100, 0.5, array('even' => 76, 'odd' => 75, 'other' => 75));
167         // upper bound
168         $this->closestHouseNumberEvenOddOther(50, 100, 1.5, array('even' => 100, 'odd' => 100, 'other' => 100));
169         // lower bound
170         $this->closestHouseNumberEvenOddOther(50, 100, -0.5, array('even' => 50, 'odd' => 50, 'other' => 50));
171         // fraction 0
172         $this->closestHouseNumberEvenOddOther(50, 100, 0, array('even' => 50, 'odd' => 51, 'other' => 50));
173         // start == end
174         $this->closestHouseNumberEvenOddOther(50, 50, 0.5, array('even' => 50, 'odd' => 50, 'other' => 50));
175     }
176
177     public function testGetSearchRankLabel()
178     {
179         $this->assertEquals('unknown', getSearchRankLabel(null));
180         $this->assertEquals('continent', getSearchRankLabel(0));
181         $this->assertEquals('continent', getSearchRankLabel(1));
182         $this->assertEquals('other: 30', getSearchRankLabel(30));
183     }
184 }