Add options to output polygon in various formats: polygon_geojson=1&polygon_svg=1&polygon_kml=1&polygon_text=1 They can be all used at the same time if prefered. Where format and polygon type are compatible output is as native type e.g. format=json&polygon_geojson=1

This commit is contained in:
Brian Quinion 2012-11-06 00:08:01 +00:00
parent c0d9ba7e0c
commit 753d2ac79f
4 changed files with 117 additions and 25 deletions

View File

@ -49,6 +49,26 @@
$aPlace['address'] = $aPointDetails['address'];
}
if (isset($aResult['asgeojson']))
{
$aPlace['geojson'] = json_decode($aResult['asgeojson']);
}
if (isset($aResult['assvg']))
{
$aPlace['svg'] = $aResult['assvg'];
}
if (isset($aResult['astext']))
{
$aPlace['geotext'] = $aResult['astext'];
}
if (isset($aResult['askml']))
{
$aPlace['geokml'] = $aResult['askml'];
}
$aFilteredPlaces[] = $aPlace;
}

View File

@ -50,7 +50,27 @@
$aPlace['address'] = $aPointDetails['address'];
}
if (isset($aResult['asgeojson']))
{
$aPlace['geojson'] = json_decode($aResult['asgeojson']);
}
if (isset($aResult['assvg']))
{
$aPlace['svg'] = $aResult['assvg'];
}
if (isset($aResult['astext']))
{
$aPlace['geotext'] = $aResult['astext'];
}
if (isset($aResult['askml']))
{
$aPlace['geokml'] = $aResult['askml'];
}
$aFilteredPlaces[] = $aPlace;
}
javascript_renderData($aFilteredPlaces);
javascript_renderData($aFilteredPlaces, array('geojson'));

View File

@ -50,6 +50,27 @@
}
}
if (isset($aResult['asgeojson']))
{
echo ' geojson=\'';
echo $aResult['asgeojson'];
echo '\'';
}
if (isset($aResult['assvg']))
{
echo ' geosvg=\'';
echo $aResult['assvg'];
echo '\'';
}
if (isset($aResult['astext']))
{
echo ' geotext=\'';
echo $aResult['astext'];
echo '\'';
}
if (isset($aResult['zoom']))
{
echo " zoom='".$aResult['zoom']."'";
@ -66,9 +87,20 @@
echo " icon='".htmlspecialchars($aResult['icon'], ENT_QUOTES)."'";
}
if (isset($aResult['address']))
if (isset($aResult['address']) || isset($aResult['askml']))
{
echo ">";
}
if (isset($aResult['askml']))
{
echo "\n";
echo $aResult['askml'];
}
if (isset($aResult['address']))
{
echo "\n";
foreach($aResult['address'] as $sKey => $sValue)
{
$sKey = str_replace(' ','_',$sKey);
@ -76,7 +108,10 @@
echo htmlspecialchars($sValue);
echo "</$sKey>";
}
}
if (isset($aResult['address']) || isset($aResult['askml']))
{
echo "</place>";
}
else

View File

@ -31,7 +31,11 @@
}
// Show / use polygons
$bShowPolygons = isset($_GET['polygon']) && $_GET['polygon'];
$bShowPolygons = (boolean)isset($_GET['polygon']) && $_GET['polygon'];
$bAsGeoJSON = (boolean)isset($_GET['polygon_geojson']) && $_GET['polygon_geojson'];
$bAsKML = (boolean)isset($_GET['polygon_kml']) && $_GET['polygon_kml'];
$bAsSVG = (boolean)isset($_GET['polygon_svg']) && $_GET['polygon_svg'];
$bAsText = (boolean)isset($_GET['polygon_text']) && $_GET['polygon_text'];
// Show address breakdown
$bShowAddressDetails = isset($_GET['addressdetails']) && $_GET['addressdetails'];
@ -1202,8 +1206,13 @@
$sSQL = "select place_id,0 as numfeatures,st_area(geometry) as area,";
$sSQL .= "ST_Y(centroid) as centrelat,ST_X(centroid) as centrelon,";
$sSQL .= "ST_Y(ST_PointN(ST_ExteriorRing(Box2D(geometry)),4)) as minlat,ST_Y(ST_PointN(ST_ExteriorRing(Box2D(geometry)),2)) as maxlat,";
$sSQL .= "ST_X(ST_PointN(ST_ExteriorRing(Box2D(geometry)),1)) as minlon,ST_X(ST_PointN(ST_ExteriorRing(Box2D(geometry)),3)) as maxlon,";
$sSQL .= "ST_AsText(geometry) as outlinestring from placex where place_id = ".$aResult['place_id'].' and st_geometrytype(Box2D(geometry)) = \'ST_Polygon\'';
$sSQL .= "ST_X(ST_PointN(ST_ExteriorRing(Box2D(geometry)),1)) as minlon,ST_X(ST_PointN(ST_ExteriorRing(Box2D(geometry)),3)) as maxlon";
if ($bAsGeoJSON) $sSQL .= ",ST_AsGeoJSON(geometry) as asgeojson";
if ($bAsKML) $sSQL .= ",ST_AsKML(geometry) as askml";
if ($bAsSVG) $sSQL .= ",ST_AsSVG(geometry) as assvg";
if ($bAsText) $sSQL .= ",ST_AsText(geometry) as astext";
if ($bShowPolygons) $sSQL .= ",ST_AsText(geometry) as outlinestring";
$sSQL .= " from placex where place_id = ".$aResult['place_id'].' and st_geometrytype(Box2D(geometry)) = \'ST_Polygon\'';
$aPointPolygon = $oDB->getRow($sSQL);
if (PEAR::IsError($aPointPolygon))
{
@ -1211,33 +1220,41 @@
}
if ($aPointPolygon['place_id'])
{
if ($bAsGeoJSON) $aResult['asgeojson'] = $aPointPolygon['asgeojson'];
if ($bAsKML) $aResult['askml'] = $aPointPolygon['askml'];
if ($bAsSVG) $aResult['assvg'] = $aPointPolygon['assvg'];
if ($bAsText) $aResult['astext'] = $aPointPolygon['astext'];
if ($aPointPolygon['centrelon'] !== null && $aPointPolygon['centrelat'] !== null ) {
$aResult['lat'] = $aPointPolygon['centrelat'];
$aResult['lon'] = $aPointPolygon['centrelon'];
}
// Translate geometary string to point array
if (preg_match('#POLYGON\\(\\(([- 0-9.,]+)#',$aPointPolygon['outlinestring'],$aMatch))
if ($bShowPolygons)
{
preg_match_all('/(-?[0-9.]+) (-?[0-9.]+)/',$aMatch[1],$aPolyPoints,PREG_SET_ORDER);
}
elseif (preg_match('#MULTIPOLYGON\\(\\(\\(([- 0-9.,]+)#',$aPointPolygon['outlinestring'],$aMatch))
{
preg_match_all('/(-?[0-9.]+) (-?[0-9.]+)/',$aMatch[1],$aPolyPoints,PREG_SET_ORDER);
}
elseif (preg_match('#POINT\\((-?[0-9.]+) (-?[0-9.]+)\\)#',$aPointPolygon['outlinestring'],$aMatch))
{
$fRadius = 0.01;
$iSteps = ($fRadius * 40000)^2;
$fStepSize = (2*pi())/$iSteps;
$aPolyPoints = array();
for($f = 0; $f < 2*pi(); $f += $fStepSize)
// Translate geometary string to point array
if (preg_match('#POLYGON\\(\\(([- 0-9.,]+)#',$aPointPolygon['outlinestring'],$aMatch))
{
$aPolyPoints[] = array('',$aMatch[1]+($fRadius*sin($f)),$aMatch[2]+($fRadius*cos($f)));
preg_match_all('/(-?[0-9.]+) (-?[0-9.]+)/',$aMatch[1],$aPolyPoints,PREG_SET_ORDER);
}
elseif (preg_match('#MULTIPOLYGON\\(\\(\\(([- 0-9.,]+)#',$aPointPolygon['outlinestring'],$aMatch))
{
preg_match_all('/(-?[0-9.]+) (-?[0-9.]+)/',$aMatch[1],$aPolyPoints,PREG_SET_ORDER);
}
elseif (preg_match('#POINT\\((-?[0-9.]+) (-?[0-9.]+)\\)#',$aPointPolygon['outlinestring'],$aMatch))
{
$fRadius = 0.01;
$iSteps = ($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)));
}
$aPointPolygon['minlat'] = $aPointPolygon['minlat'] - $fRadius;
$aPointPolygon['maxlat'] = $aPointPolygon['maxlat'] + $fRadius;
$aPointPolygon['minlon'] = $aPointPolygon['minlon'] - $fRadius;
$aPointPolygon['maxlon'] = $aPointPolygon['maxlon'] + $fRadius;
}
$aPointPolygon['minlat'] = $aPointPolygon['minlat'] - $fRadius;
$aPointPolygon['maxlat'] = $aPointPolygon['maxlat'] + $fRadius;
$aPointPolygon['minlon'] = $aPointPolygon['minlon'] - $fRadius;
$aPointPolygon['maxlon'] = $aPointPolygon['maxlon'] + $fRadius;
}
// Output data suitable for display (points and a bounding box)