Merge pull request #380 from mtmail/geometryText2Points

Geometry text2 points
This commit is contained in:
Sarah Hoffmann 2016-02-29 20:53:49 +01:00
commit cb3af09ebd
3 changed files with 113 additions and 29 deletions

View File

@ -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,34 +1690,16 @@
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))
{
$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)));
}
}
}
$aPolyPoints[] = geometryText2Points($aPointPolygon['astext'],$fRadius);
// Output data suitable for display (points and a bounding box)
if ($this->bIncludePolygonAsPoints && isset($aPolyPoints))
{
$aResult['aPolyPoints'] = array();
foreach($aPolyPoints as $aPoint)
// Output data suitable for display (points and a bounding box)
if (isset($aPolyPoints))
{
$aResult['aPolyPoints'][] = array($aPoint[1], $aPoint[2]);
$aResult['aPolyPoints'] = array();
foreach($aPolyPoints as $aPoint)
{
$aResult['aPolyPoints'][] = array($aPoint[1], $aPoint[2]);
}
}
}

View File

@ -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;
}

View File

@ -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)
);
}