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