From: Marc Tobias Metten Date: Wed, 2 Mar 2016 14:07:22 +0000 (+0100) Subject: Merge branch 'master' of https://github.com/mtmail/Nominatim X-Git-Tag: v3.0.0~201^2~3 X-Git-Url: https://git.openstreetmap.org./nominatim.git/commitdiff_plain/131b06fec3d82ec3538147efa9de401dad26cc2c?hp=27406c8daa514a6b03480972f393d32b709cb315 Merge branch 'master' of https://github.com/mtmail/Nominatim --- diff --git a/lib/Geocode.php b/lib/Geocode.php index ba1842c3..a6ff279c 100644 --- a/lib/Geocode.php +++ b/lib/Geocode.php @@ -862,9 +862,23 @@ { // Start with a blank search $aSearches = array( - array('iSearchRank' => 0, 'iNamePhrase' => -1, 'sCountryCode' => false, 'aName'=>array(), 'aAddress'=>array(), 'aFullNameAddress'=>array(), - 'aNameNonSearch'=>array(), 'aAddressNonSearch'=>array(), - 'sOperator'=>'', 'aFeatureName' => array(), 'sClass'=>'', 'sType'=>'', 'sHouseNumber'=>'', 'fLat'=>'', 'fLon'=>'', 'fRadius'=>'') + array('iSearchRank' => 0, + 'iNamePhrase' => -1, + 'sCountryCode' => false, + 'aName' => array(), + 'aAddress' => array(), + 'aFullNameAddress' => array(), + 'aNameNonSearch' => array(), + 'aAddressNonSearch' => array(), + 'sOperator' => '', + 'aFeatureName' => array(), + 'sClass' => '', + 'sType' => '', + 'sHouseNumber' => '', + 'fLat' => '', + 'fLon' => '', + 'fRadius' => '' + ) ); // Do we have a radius search? @@ -1676,37 +1690,19 @@ if ($this->bIncludePolygonAsPoints) { - // Translate geometry string to point array - if (preg_match('#POLYGON\\(\\(([- 0-9.,]+)#',$aPointPolygon['astext'],$aMatch)) - { - preg_match_all('/(-?[0-9.]+) (-?[0-9.]+)/',$aMatch[1],$aPolyPoints,PREG_SET_ORDER); - } - elseif (preg_match('#MULTIPOLYGON\\(\\(\\(([- 0-9.,]+)#',$aPointPolygon['astext'],$aMatch)) - { - preg_match_all('/(-?[0-9.]+) (-?[0-9.]+)/',$aMatch[1],$aPolyPoints,PREG_SET_ORDER); - } - elseif (preg_match('#POINT\\((-?[0-9.]+) (-?[0-9.]+)\\)#',$aPointPolygon['astext'],$aMatch)) + $aPolyPoints[] = geometryText2Points($aPointPolygon['astext'],$fRadius); + + // Output data suitable for display (points and a bounding box) + if (isset($aPolyPoints)) { - $iSteps = max(8, min(100, ($fRadius * 40000)^2)); - $fStepSize = (2*pi())/$iSteps; - $aPolyPoints = array(); - for($f = 0; $f < 2*pi(); $f += $fStepSize) + $aResult['aPolyPoints'] = array(); + foreach($aPolyPoints as $aPoint) { - $aPolyPoints[] = array('',$aMatch[1]+($fRadius*sin($f)),$aMatch[2]+($fRadius*cos($f))); + $aResult['aPolyPoints'][] = array($aPoint[1], $aPoint[2]); } } } - // Output data suitable for display (points and a bounding box) - if ($this->bIncludePolygonAsPoints && isset($aPolyPoints)) - { - $aResult['aPolyPoints'] = array(); - foreach($aPolyPoints as $aPoint) - { - $aResult['aPolyPoints'][] = array($aPoint[1], $aPoint[2]); - } - } - if (abs($aPointPolygon['minlat'] - $aPointPolygon['maxlat']) < 0.0000001) { $aPointPolygon['minlat'] = $aPointPolygon['minlat'] - $fRadius; diff --git a/lib/lib.php b/lib/lib.php index 2134e1a4..e81d85be 100644 --- a/lib/lib.php +++ b/lib/lib.php @@ -1022,3 +1022,29 @@ return array('lat' => $fQueryLat, 'lon' => $fQueryLon, 'query' => $sQuery); } + + + + function geometryText2Points($geometry_as_text,$fRadius) + { + $aPolyPoints = NULL; + if (preg_match('#POLYGON\\(\\(([- 0-9.,]+)#',$geometry_as_text,$aMatch)) + { + preg_match_all('/(-?[0-9.]+) (-?[0-9.]+)/',$aMatch[1],$aPolyPoints,PREG_SET_ORDER); + } + elseif (preg_match('#MULTIPOLYGON\\(\\(\\(([- 0-9.,]+)#',$geometry_as_text,$aMatch)) + { + preg_match_all('/(-?[0-9.]+) (-?[0-9.]+)/',$aMatch[1],$aPolyPoints,PREG_SET_ORDER); + } + elseif (preg_match('#POINT\\((-?[0-9.]+) (-?[0-9.]+)\\)#',$geometry_as_text,$aMatch)) + { + $iSteps = max(8, min(100, ($fRadius * 40000)^2)); + $fStepSize = (2*pi())/$iSteps; + $aPolyPoints = array(); + for($f = 0; $f < 2*pi(); $f += $fStepSize) + { + $aPolyPoints[] = array('',$aMatch[1]+($fRadius*sin($f)),$aMatch[2]+($fRadius*cos($f))); + } + } + return $aPolyPoints; + } diff --git a/tests-php/Nominatim/NominatimTest.php b/tests-php/Nominatim/NominatimTest.php index 4c5638b1..129fa012 100644 --- a/tests-php/Nominatim/NominatimTest.php +++ b/tests-php/Nominatim/NominatimTest.php @@ -143,6 +143,68 @@ class NominatimTest extends \PHPUnit_Framework_TestCase count( getWordSets(array_fill( 0, 18, 'a'),0) ) ); + } + + + + + public function test_geometryText2Points() + { + $fRadius = 1; + + // invalid value + $this->assertEquals( + NULL, + geometryText2Points('', $fRadius) + ); + + + // POINT + $aPoints = geometryText2Points('POINT(10 20)', $fRadius); + $this->assertEquals( + 101, + count($aPoints) + ); + + $this->assertEquals( + + array( + ['', 10, 21], + ['', 10.062790519529, 20.998026728428], + ['', 10.125333233564, 20.992114701314] + ), + array_splice($aPoints, 0,3) + ); + + // POLYGON + $this->assertEquals( + array( + ['30 10', '30', '10'], + ['40 40', '40', '40'], + ['20 40', '20', '40'], + ['10 20', '10', '20'], + ['30 10', '30', '10'] + ), + geometryText2Points('POLYGON((30 10, 40 40, 20 40, 10 20, 30 10))', $fRadius) + ); + + // MULTIPOLYGON + // only the first polygon is used + $this->assertEquals( + array( + ['30 20', '30', '20'], + ['45 40', '45', '40'], + ['10 40', '10', '40'], + ['30 20', '30', '20'], + + // ['15 5' , '15', '5' ], + // ['45 10', '45', '10'], + // ['10 20', '10', '20'], + // ['5 10' , '5' , '10'], + // ['15 5' , '15', '5' ] + ), + geometryText2Points('MULTIPOLYGON(((30 20, 45 40, 10 40, 30 20)),((15 5, 40 10, 10 20, 5 10, 15 5)))', $fRadius) + ); }