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