mirror of
https://github.com/osm-search/Nominatim.git
synced 2024-11-22 21:28:10 +03:00
move looksLikeLatLonPair into lib.php, basic PHP test suite using phpunit
This commit is contained in:
parent
aae90ea5cb
commit
d6f298f033
2
.gitignore
vendored
2
.gitignore
vendored
@ -1,4 +1,5 @@
|
||||
*.log
|
||||
*.pyc
|
||||
|
||||
nominatim/*.d
|
||||
nominatim/*.o
|
||||
@ -27,3 +28,4 @@ compile
|
||||
data/wiki_import.sql
|
||||
data/wiki_specialphrases.sql
|
||||
data/osmosischange.osc
|
||||
|
||||
|
@ -542,35 +542,9 @@
|
||||
}
|
||||
|
||||
// Do we have anything that looks like a lat/lon pair?
|
||||
if (preg_match('/\\b([NS])[ ]+([0-9]+[0-9.]*)[ ]+([0-9.]+)?[, ]+([EW])[ ]+([0-9]+)[ ]+([0-9]+[0-9.]*)?\\b/', $sQuery, $aData))
|
||||
{
|
||||
$fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2] + $aData[3]/60);
|
||||
$fQueryLon = ($aData[4]=='E'?1:-1) * ($aData[5] + $aData[6]/60);
|
||||
if ($fQueryLat <= 90.1 && $fQueryLat >= -90.1 && $fQueryLon <= 180.1 && $fQueryLon >= -180.1)
|
||||
{
|
||||
$this->setNearPoint(array($fQueryLat, $fQueryLon));
|
||||
$sQuery = trim(str_replace($aData[0], ' ', $sQuery));
|
||||
}
|
||||
}
|
||||
elseif (preg_match('/\\b([0-9]+)[ ]+([0-9]+[0-9.]*)?[ ]+([NS])[, ]+([0-9]+)[ ]+([0-9]+[0-9.]*)?[ ]+([EW])\\b/', $sQuery, $aData))
|
||||
{
|
||||
$fQueryLat = ($aData[3]=='N'?1:-1) * ($aData[1] + $aData[2]/60);
|
||||
$fQueryLon = ($aData[6]=='E'?1:-1) * ($aData[4] + $aData[5]/60);
|
||||
if ($fQueryLat <= 90.1 && $fQueryLat >= -90.1 && $fQueryLon <= 180.1 && $fQueryLon >= -180.1)
|
||||
{
|
||||
$this->setNearPoint(array($fQueryLat, $fQueryLon));
|
||||
$sQuery = trim(str_replace($aData[0], ' ', $sQuery));
|
||||
}
|
||||
}
|
||||
elseif (preg_match('/(\\[|^|\\b)(-?[0-9]+[0-9]*\\.[0-9]+)[, ]+(-?[0-9]+[0-9]*\\.[0-9]+)(\\]|$|\\b)/', $sQuery, $aData))
|
||||
{
|
||||
$fQueryLat = $aData[2];
|
||||
$fQueryLon = $aData[3];
|
||||
if ($fQueryLat <= 90.1 && $fQueryLat >= -90.1 && $fQueryLon <= 180.1 && $fQueryLon >= -180.1)
|
||||
{
|
||||
$this->setNearPoint(array($fQueryLat, $fQueryLon));
|
||||
$sQuery = trim(str_replace($aData[0], ' ', $sQuery));
|
||||
}
|
||||
if ( $aLooksLike = looksLikeLatLonPair($sQuery) ){
|
||||
$this->setNearPoint(array($aLooksLike['lat'], $aLooksLike['lon']));
|
||||
$sQuery = $aLooksLike['query'];
|
||||
}
|
||||
|
||||
$aSearchResults = array();
|
||||
|
90
lib/lib.php
90
lib/lib.php
@ -905,3 +905,93 @@
|
||||
{
|
||||
return "'".$s."'";
|
||||
}
|
||||
|
||||
// returns boolean
|
||||
function validLatLon($fLat,$fLon)
|
||||
{
|
||||
return ($fLat <= 90.1 && $fLat >= -90.1 && $fLon <= 180.1 && $fLon >= -180.1);
|
||||
}
|
||||
|
||||
// Do we have anything that looks like a lat/lon pair?
|
||||
// returns array(lat,lon,query_with_lat_lon_removed)
|
||||
// or null
|
||||
function looksLikeLatLonPair($sQuery)
|
||||
{
|
||||
$sFound = null;
|
||||
$fQueryLat = null;
|
||||
$fQueryLon = null;
|
||||
|
||||
// degrees decimal minutes
|
||||
// N 40 26.767, W 79 58.933
|
||||
// N 40°26.767′, W 79°58.933′
|
||||
// 1 2 3 4 5 6
|
||||
if (preg_match('/\\b([NS])[ ]+([0-9]+[0-9.]*)[° ]+([0-9.]+)?[′\']*[, ]+([EW])[ ]+([0-9]+)[° ]+([0-9]+[0-9.]*)[′\']*?\\b/', $sQuery, $aData))
|
||||
{
|
||||
$sFound = $aData[0];
|
||||
$fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2] + $aData[3]/60);
|
||||
$fQueryLon = ($aData[4]=='E'?1:-1) * ($aData[5] + $aData[6]/60);
|
||||
}
|
||||
// degrees decimal minutes
|
||||
// 40 26.767 N, 79 58.933 W
|
||||
// 40° 26.767′ N 79° 58.933′ W
|
||||
// 1 2 3 4 5 6
|
||||
elseif (preg_match('/\\b([0-9]+)[° ]+([0-9]+[0-9.]*)?[′\']*[ ]+([NS])[, ]+([0-9]+)[° ]+([0-9]+[0-9.]*)?[′\' ]+([EW])\\b/', $sQuery, $aData))
|
||||
{
|
||||
$sFound = $aData[0];
|
||||
$fQueryLat = ($aData[3]=='N'?1:-1) * ($aData[1] + $aData[2]/60);
|
||||
$fQueryLon = ($aData[6]=='E'?1:-1) * ($aData[4] + $aData[5]/60);
|
||||
}
|
||||
// degrees decimal seconds
|
||||
// N 40 26 46 W 79 58 56
|
||||
// N 40° 26′ 46″ W, 79° 58′ 56″
|
||||
// 1 2 3 4 5 6 7 8
|
||||
elseif (preg_match('/\\b([NS])[ ]([0-9]+)[° ]+([0-9]+)[′\' ]+([0-9]+)[″"]*[, ]+([EW])[ ]([0-9]+)[° ]+([0-9]+)[′\' ]+([0-9]+)[″"]*\\b/', $sQuery, $aData))
|
||||
{
|
||||
$sFound = $aData[0];
|
||||
$fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2] + $aData[3]/60 + $aData[4]/3600);
|
||||
$fQueryLon = ($aData[5]=='E'?1:-1) * ($aData[6] + $aData[7]/60 + $aData[8]/3600);
|
||||
}
|
||||
// degrees decimal seconds
|
||||
// 40 26 46 N 79 58 56 W
|
||||
// 40° 26′ 46″ N, 79° 58′ 56″ W
|
||||
// 1 2 3 4 5 6 7 8
|
||||
elseif (preg_match('/\\b([0-9]+)[° ]+([0-9]+)[′\' ]+([0-9]+)[″" ]+([NS])[, ]+([0-9]+)[° ]+([0-9]+)[′\' ]+([0-9]+)[″" ]+([EW])\\b/', $sQuery, $aData))
|
||||
{
|
||||
$sFound = $aData[0];
|
||||
$fQueryLat = ($aData[4]=='N'?1:-1) * ($aData[1] + $aData[2]/60 + $aData[3]/3600);
|
||||
$fQueryLon = ($aData[8]=='E'?1:-1) * ($aData[5] + $aData[6]/60 + $aData[7]/3600);
|
||||
}
|
||||
// degrees decimal
|
||||
// N 40.446° W 79.982°
|
||||
// 1 2 3 4
|
||||
elseif (preg_match('/\\b([NS])[ ]([0-9]+[0-9]*\\.[0-9]+)[°]*[, ]+([EW])[ ]([0-9]+[0-9]*\\.[0-9]+)[°]*\\b/', $sQuery, $aData))
|
||||
{
|
||||
$sFound = $aData[0];
|
||||
$fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2]);
|
||||
$fQueryLon = ($aData[3]=='E'?1:-1) * ($aData[4]);
|
||||
}
|
||||
// degrees decimal
|
||||
// 40.446° N 79.982° W
|
||||
// 1 2 3 4
|
||||
elseif (preg_match('/\\b([0-9]+[0-9]*\\.[0-9]+)[° ]+([NS])[, ]+([0-9]+[0-9]*\\.[0-9]+)[° ]+([EW])\\b/', $sQuery, $aData))
|
||||
{
|
||||
$sFound = $aData[0];
|
||||
$fQueryLat = ($aData[2]=='N'?1:-1) * ($aData[1]);
|
||||
$fQueryLon = ($aData[4]=='E'?1:-1) * ($aData[3]);
|
||||
}
|
||||
// degrees decimal
|
||||
// 12.34, 56.78
|
||||
// [12.456,-78.90]
|
||||
// 1 2 3 4
|
||||
elseif (preg_match('/(\\[|^|\\b)(-?[0-9]+[0-9]*\\.[0-9]+)[, ]+(-?[0-9]+[0-9]*\\.[0-9]+)(\\]|$|\\b)/', $sQuery, $aData))
|
||||
{
|
||||
$sFound = $aData[0];
|
||||
$fQueryLat = $aData[2];
|
||||
$fQueryLon = $aData[3];
|
||||
}
|
||||
|
||||
if (!validLatLon($fQueryLat, $fQueryLon)) return;
|
||||
$sQuery = trim(str_replace($sFound, ' ', $sQuery));
|
||||
|
||||
return array('lat' => $fQueryLat, 'lon' => $fQueryLon, 'query' => $sQuery);
|
||||
}
|
26
phpunit.xml
Normal file
26
phpunit.xml
Normal file
@ -0,0 +1,26 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<phpunit backupGlobals="false"
|
||||
backupStaticAttributes="false"
|
||||
colors="true"
|
||||
convertErrorsToExceptions="true"
|
||||
convertNoticesToExceptions="true"
|
||||
convertWarningsToExceptions="true"
|
||||
processIsolation="false"
|
||||
stopOnFailure="false"
|
||||
syntaxCheck="true"
|
||||
bootstrap="tests-php/bootstrap.php"
|
||||
>
|
||||
<php>
|
||||
</php>
|
||||
<testsuites>
|
||||
<testsuite name="Nominatim PHP Test Suite">
|
||||
<directory>./tests-php/Nominatim</directory>
|
||||
</testsuite>
|
||||
</testsuites>
|
||||
<filter>
|
||||
<whitelist>
|
||||
<directory>./lib/</directory>
|
||||
</whitelist>
|
||||
</filter>
|
||||
|
||||
</phpunit>
|
77
tests-php/Nominatim/NominatimTest.php
Normal file
77
tests-php/Nominatim/NominatimTest.php
Normal file
@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
namespace Nominatim;
|
||||
require 'lib/lib.php';
|
||||
|
||||
|
||||
class NominatimTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
public function test_addQuotes()
|
||||
{
|
||||
// FIXME: not quoting existing quote signs is probably a bug
|
||||
$this->assertSame("'St. John's'", addQuotes("St. John's"));
|
||||
$this->assertSame("''", addQuotes(''));
|
||||
}
|
||||
|
||||
public function test_looksLikeLatLonPair()
|
||||
{
|
||||
// no coordinates expected
|
||||
$this->assertNull(looksLikeLatLonPair(''));
|
||||
$this->assertNull(looksLikeLatLonPair('abc'));
|
||||
$this->assertNull(looksLikeLatLonPair('12 34'));
|
||||
$this->assertNull(looksLikeLatLonPair('200.1 89.9')); // because latitude > 180
|
||||
|
||||
// coordinates expected
|
||||
$this->assertNotNull(looksLikeLatLonPair('0.0 -0.0'));
|
||||
|
||||
$this->assertEquals(
|
||||
array( 'lat' => 12.456, 'lon' => -78.90, 'query' => 'abc def'),
|
||||
looksLikeLatLonPair(' abc 12.456 -78.90 def ')
|
||||
);
|
||||
|
||||
$this->assertEquals(
|
||||
array( 'lat' => 12.456, 'lon' => -78.90, 'query' => ''),
|
||||
looksLikeLatLonPair(' [12.456,-78.90] ')
|
||||
);
|
||||
|
||||
// http://en.wikipedia.org/wiki/Geographic_coordinate_conversion
|
||||
// these all represent the same location
|
||||
$aQueries = array(
|
||||
'40 26.767 N 79 58.933 W',
|
||||
'40° 26.767′ N 79° 58.933′ W',
|
||||
"40° 26.767' N 79° 58.933' W",
|
||||
'N 40 26.767, W 79 58.933',
|
||||
'N 40°26.767′, W 79°58.933′',
|
||||
"N 40°26.767', W 79°58.933'",
|
||||
|
||||
'40 26 46 N 79 58 56 W',
|
||||
'40° 26′ 46″ N 79° 58′ 56″ W',
|
||||
'N 40 26 46 W 79 58 56',
|
||||
'N 40° 26′ 46″, W 79° 58′ 56″',
|
||||
'N 40° 26\' 46", W 79° 58\' 56"',
|
||||
|
||||
'40.446 -79.982',
|
||||
'40.446,-79.982',
|
||||
'40.446° N 79.982° W',
|
||||
'N 40.446° W 79.982°',
|
||||
|
||||
'[40.446 -79.982]',
|
||||
' 40.446 , -79.982 ',
|
||||
);
|
||||
|
||||
|
||||
foreach($aQueries as $sQuery){
|
||||
$aRes = looksLikeLatLonPair($sQuery);
|
||||
$this->assertEquals( 40.446, $aRes['lat'], 'degrees decimal ' . $sQuery, 0.01);
|
||||
$this->assertEquals(-79.982, $aRes['lon'], 'degrees decimal ' . $sQuery, 0.01);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
13
tests-php/README.txt
Normal file
13
tests-php/README.txt
Normal file
@ -0,0 +1,13 @@
|
||||
Basic unit tests of PHP code. Very low coverage. Doesn't cover interaction
|
||||
with the webserver/HTTP or database (yet).
|
||||
|
||||
You need to have
|
||||
https://phpunit.de/manual/4.2/en/
|
||||
installed.
|
||||
|
||||
To execute the test suite run
|
||||
$ phpunit
|
||||
|
||||
It will read phpunit.xml which points to the library, test path, bootstrap
|
||||
strip and set other parameters.
|
||||
|
2
tests-php/bootstrap.php
Normal file
2
tests-php/bootstrap.php
Normal file
@ -0,0 +1,2 @@
|
||||
<?php
|
||||
|
@ -21,7 +21,7 @@ Usage
|
||||
|
||||
* get prerequisites
|
||||
|
||||
[sudo] pip install lettuce nose pytidylib haversine
|
||||
[sudo] pip install lettuce nose pytidylib haversine psycopg2
|
||||
|
||||
* run the tests
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user