]> git.openstreetmap.org Git - nominatim.git/blob - tests-php/Nominatim/NominatimTest.php
Merge remote-tracking branch 'origin/master' into osmline
[nominatim.git] / tests-php / Nominatim / NominatimTest.php
1 <?php
2
3 namespace Nominatim;
4 require '../lib/lib.php';
5
6
7 class NominatimTest extends \PHPUnit_Framework_TestCase
8 {
9
10         protected function setUp()
11         {
12         }
13
14
15         public function test_getClassTypesWithImportance()
16         {
17                 $aClasses = getClassTypesWithImportance();
18
19                 $this->assertGreaterThan(
20                         200,
21                         count($aClasses)
22                 );
23
24                 $this->assertEquals(
25                         array(
26                                 'label' => "Country",
27                                 'frequency' => 0,
28                                 'icon' => "poi_boundary_administrative",
29                                 'defzoom' => 6,
30                                 'defdiameter' => 15,
31                                 'importance' => 3
32                         ),
33                         $aClasses['place:country']
34                 );
35         }
36
37
38         public function test_getResultDiameter()
39         {
40                 $aResult = array();
41                 $this->assertEquals(
42                         0.0001,
43                         getResultDiameter($aResult)
44                 );
45
46                 $aResult = array('class' => 'place', 'type' => 'country');
47                 $this->assertEquals(
48                         15,
49                         getResultDiameter($aResult)
50                 );
51
52                 $aResult = array('class' => 'boundary', 'type' => 'administrative', 'admin_level' => 6);
53                 $this->assertEquals(
54                         0.32,
55                         getResultDiameter($aResult)
56                 );
57         }
58
59
60         public function test_addQuotes()
61         {
62                 // FIXME: not quoting existing quote signs is probably a bug
63                 $this->assertSame("'St. John's'", addQuotes("St. John's"));
64                 $this->assertSame("''", addQuotes(''));
65         }
66
67         public function test_looksLikeLatLonPair()
68         {
69                 // no coordinates expected
70                 $this->assertNull(looksLikeLatLonPair(''));
71                 $this->assertNull(looksLikeLatLonPair('abc'));
72                 $this->assertNull(looksLikeLatLonPair('12 34'));
73                 $this->assertNull(looksLikeLatLonPair('200.1 89.9')); // because latitude > 180
74
75                 // coordinates expected
76                 $this->assertNotNull(looksLikeLatLonPair('0.0 -0.0'));
77
78                 $this->assertEquals(
79                                 array( 'lat' => 12.456, 'lon' => -78.90, 'query' => 'abc   def'),
80                                 looksLikeLatLonPair(' abc 12.456 -78.90 def ')
81                         );
82
83                 $this->assertEquals(
84                                 array( 'lat' => 12.456, 'lon' => -78.90, 'query' => ''),
85                                 looksLikeLatLonPair(' [12.456,-78.90] ')
86                         );
87
88                 // http://en.wikipedia.org/wiki/Geographic_coordinate_conversion
89                 // these all represent the same location
90                 $aQueries = array(
91                                         '40 26.767 N 79 58.933 W',
92                                         '40° 26.767′ N 79° 58.933′ W',
93                                         "40° 26.767' N 79° 58.933' W",
94                                         'N 40 26.767, W 79 58.933',
95                                         'N 40°26.767′, W 79°58.933′',
96                                         "N 40°26.767', W 79°58.933'",
97
98                                         '40 26 46 N 79 58 56 W',
99                                         '40° 26′ 46″ N 79° 58′ 56″ W',
100                                         'N 40 26 46 W 79 58 56',
101                                         'N 40° 26′ 46″, W 79° 58′ 56″',
102                                         'N 40° 26\' 46", W 79° 58\' 56"',
103
104                                         '40.446 -79.982',
105                                         '40.446,-79.982',
106                                         '40.446° N 79.982° W',
107                                         'N 40.446° W 79.982°',
108
109                                         '[40.446 -79.982]',
110                                         '       40.446  ,   -79.982     ',
111                 );
112
113
114                 foreach($aQueries as $sQuery){
115                         $aRes = looksLikeLatLonPair($sQuery);
116                         $this->assertEquals( 40.446, $aRes['lat'], 'degrees decimal ' . $sQuery, 0.01);
117                         $this->assertEquals(-79.982, $aRes['lon'], 'degrees decimal ' . $sQuery, 0.01);
118                 }
119
120         }
121
122
123
124         public function test_getWordSets()
125         {
126
127                 // given an array of arrays like
128                 // array( array('a','b'), array('c','d') )
129                 // returns a summary as string: '(a|b),(c|d)'
130                 function serialize_sets($aSets)
131                 {       
132                         $aParts = array();
133                         foreach($aSets as $aSet){
134                                 $aParts[] = '(' . join('|', $aSet) . ')';
135                         }
136                         return join(',', $aParts);
137                 }
138
139                 $this->assertEquals(
140                         array(array('')),
141                         getWordSets(array(),0)
142                 );
143
144                 $this->assertEquals(
145                         '(a)',
146                         serialize_sets( getWordSets(array("a"),0) )
147                 );
148
149                 $this->assertEquals(
150                         '(a b),(a|b)',
151                         serialize_sets( getWordSets(array('a','b'),0) )
152                 );
153
154                 $this->assertEquals(
155                         '(a b c),(a|b c),(a|b|c),(a b|c)',
156                         serialize_sets( getWordSets(array('a','b','c'),0) )
157                 );
158
159                 $this->assertEquals(
160                         '(a b c d),(a|b c d),(a|b|c d),(a|b|c|d),(a|b c|d),(a b|c d),(a b|c|d),(a b c|d)',
161                         serialize_sets( getWordSets(array('a','b','c','d'),0) )
162                 );
163
164
165                 // Inverse
166                 $this->assertEquals(
167                         '(a b c),(c|a b),(c|b|a),(b c|a)',
168                         serialize_sets( getInverseWordSets(array('a','b','c'),0) )
169                 );
170
171
172                 // make sure we don't create too many sets
173                 // 4 words => 8 sets
174                 // 10 words => 511 sets
175                 // 15 words => 12911 sets
176                 // 18 words => 65536 sets
177                 // 20 words => 169766 sets
178                 // 22 words => 401930 sets
179                 // 28 words => 3505699 sets (needs more than 4GB via 'phpunit -d memory_limit=' to run)
180                 $this->assertEquals(
181                         8,
182                         count( getWordSets(array_fill( 0, 4, 'a'),0) )
183                 );
184
185
186                 $this->assertEquals(
187                         65536,
188                         count( getWordSets(array_fill( 0, 18, 'a'),0) )
189                 );
190         }
191
192         }
193
194
195
196
197         public function test_geometryText2Points()
198         {
199                 $fRadius = 1;
200
201                 // invalid value
202                 $this->assertEquals(
203                         NULL,
204                         geometryText2Points('', $fRadius)
205                 );
206
207
208                 // POINT
209                 $aPoints = geometryText2Points('POINT(10 20)', $fRadius);
210                 $this->assertEquals(
211                         101,
212                         count($aPoints)
213                 );
214
215                 $this->assertEquals(
216
217                 array(
218                         ['', 10, 21],
219                         ['', 10.062790519529, 20.998026728428],
220                                 ['', 10.125333233564, 20.992114701314]
221                         ),
222                         array_splice($aPoints, 0,3)
223                 );
224
225                 // POLYGON
226                 $this->assertEquals(
227                         array(
228                                 ['30 10', '30', '10'],
229                                 ['40 40', '40', '40'],
230                                 ['20 40', '20', '40'],
231                                 ['10 20', '10', '20'],
232                                 ['30 10', '30', '10']
233                         ),
234                         geometryText2Points('POLYGON((30 10, 40 40, 20 40, 10 20, 30 10))', $fRadius)
235                 );
236
237                 // MULTIPOLYGON
238                 // only the first polygon is used
239                 $this->assertEquals(
240                         array(
241                                 ['30 20', '30', '20'],
242                                 ['45 40', '45', '40'],
243                                 ['10 40', '10', '40'],
244                                 ['30 20', '30', '20'],
245
246                                 // ['15 5' , '15', '5' ],
247                                 // ['45 10', '45', '10'],
248                                 // ['10 20', '10', '20'],
249                                 // ['5 10' , '5' , '10'],
250                                 // ['15 5' , '15', '5' ]
251                         ),
252                         geometryText2Points('MULTIPOLYGON(((30 20, 45 40, 10 40, 30 20)),((15 5, 40 10, 10 20, 5 10, 15 5)))', $fRadius)
253                 );
254
255         // you might say we're creating a circle
256         public function test_createPointsAroundCenter()
257         {
258                 $aPoints = createPointsAroundCenter(0, 0, 2);
259
260                 $this->assertEquals(
261                         101,
262                         count($aPoints)
263                 );
264                 $this->assertEquals(
265                         array(
266                                 ['', 0, 2],
267                                 ['', 0.12558103905863, 1.9960534568565],
268                                 ['', 0.25066646712861, 1.984229402629]
269                         ),
270                         array_splice($aPoints, 0, 3)
271                 );
272         }
273
274         public function test_geometryText2Points()
275         {
276                 $fRadius = 1;
277                 // invalid value
278                 $this->assertEquals(
279                         NULL,
280                         geometryText2Points('', $fRadius)
281                 );
282
283                 // POINT
284                 $aPoints = geometryText2Points('POINT(10 20)', $fRadius);
285                 $this->assertEquals(
286                         101,
287                         count($aPoints)
288                 );
289                 $this->assertEquals(
290                         array(
291                                 [10, 21],
292                                 [10.062790519529, 20.998026728428],
293                                 [10.125333233564, 20.992114701314]
294                         ),
295                         array_splice($aPoints, 0,3)
296                 );
297
298                 // POLYGON
299                 $this->assertEquals(
300                         array(
301                                 ['30', '10'],
302                                 ['40', '40'],
303                                 ['20', '40'],
304                                 ['10', '20'],
305                                 ['30', '10']
306                         ),
307                         geometryText2Points('POLYGON((30 10, 40 40, 20 40, 10 20, 30 10))', $fRadius)
308                 );
309
310                 // MULTIPOLYGON
311                 $this->assertEquals(
312                         array(
313                                 ['30', '20'], // first polygon only
314                                 ['45', '40'],
315                                 ['10', '40'],
316                                 ['30', '20'],
317                         ),
318                         geometryText2Points('MULTIPOLYGON(((30 20, 45 40, 10 40, 30 20)),((15 5, 40 10, 10 20, 5 10, 15 5)))', $fRadius)
319                 );
320         }
321
322 }