]> git.openstreetmap.org Git - nominatim.git/blob - test/php/Nominatim/NominatimTest.php
ParameterParser: getFloat with empty string value throws exception
[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 testCreatePointsAroundCenter()
70     {
71         // you might say we're creating a circle
72         $aPoints = createPointsAroundCenter(0, 0, 2);
73
74         $this->assertEquals(
75             101,
76             count($aPoints)
77         );
78         $this->assertEquals(
79             array(
80              ['', 0, 2],
81              ['', 0.12558103905863, 1.9960534568565],
82              ['', 0.25066646712861, 1.984229402629]
83             ),
84             array_splice($aPoints, 0, 3)
85         );
86     }
87
88
89     public function testGeometryText2Points()
90     {
91         $fRadius = 1;
92         // invalid value
93         $this->assertEquals(
94             null,
95             geometryText2Points('', $fRadius)
96         );
97
98         // POINT
99         $aPoints = geometryText2Points('POINT(10 20)', $fRadius);
100         $this->assertEquals(
101             101,
102             count($aPoints)
103         );
104         $this->assertEquals(
105             array(
106              [10, 21],
107              [10.062790519529, 20.998026728428],
108              [10.125333233564, 20.992114701314]
109             ),
110             array_splice($aPoints, 0, 3)
111         );
112
113         // POLYGON
114         $this->assertEquals(
115             array(
116              ['30', '10'],
117              ['40', '40'],
118              ['20', '40'],
119              ['10', '20'],
120              ['30', '10']
121             ),
122             geometryText2Points('POLYGON((30 10, 40 40, 20 40, 10 20, 30 10))', $fRadius)
123         );
124
125         // MULTIPOLYGON
126         $this->assertEquals(
127             array(
128              ['30', '20'], // first polygon only
129              ['45', '40'],
130              ['10', '40'],
131              ['30', '20'],
132             ),
133             geometryText2Points('MULTIPOLYGON(((30 20, 45 40, 10 40, 30 20)),((15 5, 40 10, 10 20, 5 10, 15 5)))', $fRadius)
134         );
135     }
136
137     public function testParseLatLon()
138     {
139         // no coordinates expected
140         $this->assertFalse(parseLatLon(''));
141         $this->assertFalse(parseLatLon('abc'));
142         $this->assertFalse(parseLatLon('12 34'));
143
144         // coordinates expected
145         $this->assertNotNull(parseLatLon('0.0 -0.0'));
146
147         $aRes = parseLatLon(' abc 12.456 -78.90 def ');
148         $this->assertEquals($aRes[1], 12.456);
149         $this->assertEquals($aRes[2], -78.90);
150         $this->assertEquals($aRes[0], ' 12.456 -78.90 ');
151
152         $aRes = parseLatLon(' [12.456,-78.90] ');
153         $this->assertEquals($aRes[1], 12.456);
154         $this->assertEquals($aRes[2], -78.90);
155         $this->assertEquals($aRes[0], ' [12.456,-78.90] ');
156
157         $aRes = parseLatLon(' -12.456,-78.90 ');
158         $this->assertEquals($aRes[1], -12.456);
159         $this->assertEquals($aRes[2], -78.90);
160         $this->assertEquals($aRes[0], ' -12.456,-78.90 ');
161
162         // http://en.wikipedia.org/wiki/Geographic_coordinate_conversion
163         // these all represent the same location
164         $aQueries = array(
165                      '40 26.767 N 79 58.933 W',
166                      '40° 26.767′ N 79° 58.933′ W',
167                      "40° 26.767' N 79° 58.933' W",
168                      'N 40 26.767, W 79 58.933',
169                      'N 40°26.767′, W 79°58.933′',
170                      "N 40°26.767', W 79°58.933'",
171  
172                      '40 26 46 N 79 58 56 W',
173                      '40° 26′ 46″ N 79° 58′ 56″ W',
174                      '40° 26′ 46.00″ N 79° 58′ 56.00″ W',
175                      '40°26′46″N 79°58′56″W',
176                      'N 40 26 46 W 79 58 56',
177                      'N 40° 26′ 46″, W 79° 58′ 56″',
178                      'N 40° 26\' 46", W 79° 58\' 56"',
179  
180                      '40.446 -79.982',
181                      '40.446,-79.982',
182                      '40.446° N 79.982° W',
183                      'N 40.446° W 79.982°',
184  
185                      '[40.446 -79.982]',
186                      '       40.446  ,   -79.982     ',
187                     );
188
189
190         foreach ($aQueries as $sQuery) {
191             $aRes = parseLatLon($sQuery);
192             $this->assertEquals(40.446, $aRes[1], 'degrees decimal ' . $sQuery, 0.01);
193             $this->assertEquals(-79.982, $aRes[2], 'degrees decimal ' . $sQuery, 0.01);
194             $this->assertEquals($sQuery, $aRes[0]);
195         }
196     }
197
198     private function closestHouseNumberEvenOddOther($startnumber, $endnumber, $fraction, $aExpected)
199     {
200         foreach (['even', 'odd', 'other'] as $itype) {
201             $this->assertEquals(
202                 $aExpected[$itype],
203                 closestHouseNumber([
204                                     'startnumber' => $startnumber,
205                                     'endnumber' => $endnumber,
206                                     'fraction' => $fraction,
207                                     'interpolationtype' => $itype
208                                    ]),
209                 "$startnumber => $endnumber, $fraction, $itype"
210             );
211         }
212     }
213
214     public function testClosestHouseNumber()
215     {
216         $this->closestHouseNumberEvenOddOther(50, 100, 0.5, ['even' => 76, 'odd' => 75, 'other' => 75]);
217         // upper bound
218         $this->closestHouseNumberEvenOddOther(50, 100, 1.5, ['even' => 100, 'odd' => 100, 'other' => 100]);
219         // lower bound
220         $this->closestHouseNumberEvenOddOther(50, 100, -0.5, ['even' => 50, 'odd' => 50, 'other' => 50]);
221         // fraction 0
222         $this->closestHouseNumberEvenOddOther(50, 100, 0, ['even' => 50, 'odd' => 51, 'other' => 50]);
223         // start == end
224         $this->closestHouseNumberEvenOddOther(50, 50, 0.5, ['even' => 50, 'odd' => 50, 'other' => 50]);
225     }
226 }