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