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