GetLayers();
$n = 0;
$bLayers = array();
for ($i = 0; $i < $layerCollection->GetCount(); $i++)
{
$layer = $layerCollection->GetItem($i);
$layerType = $layer->GetLayerType();
if (MgLayerType::BaseMap == $layerType)
{
$bLayers[$n++] = $layer;
}
}
for($j = 0; $j < count($bLayers); $j++)
{
$layerCollection->Remove($bLayers[$j]);
}
}
function CheckMapExist()
{
global $mapResourceId;
$mapNotExist = true;
if(isset($mapResourceId))
{
try
{
global $userInfo;
global $mapResources;
$mapResourcesXml = "";
// Enumerates all maps in the library
$resourceID = new MgResourceIdentifier("Library://");
//connect to the site and get a resource service instance
$siteConn = new MgSiteConnection();
$siteConn->Open($userInfo);
$resourceService = $siteConn->CreateService(MgServiceType::ResourceService);
$byteReader = $resourceService->EnumerateResources($resourceID, -1, "MapDefinition");
$chunk = "";
do
{
$chunkSize = $byteReader->Read($chunk, 4096);
$mapResourcesXml = $mapResourcesXml . $chunk;
} while ($chunkSize != 0);
$resourceList = new DOMDocument();
$resourceList->loadXML($mapResourcesXml);
$resourceIdNodeList = $resourceList->documentElement->getElementsByTagName("ResourceId");
for ($i = 0; $i < $resourceIdNodeList->length; $i++)
{
$mapResourceID = $resourceIdNodeList->item($i)->nodeValue;
if(trim($mapResourceId) == trim($mapResourceID))
{
$mapNotExist = false;
break;
}
}
}
catch (Exception $exc)
{
//true again
}
}
if($mapNotExist)
{
echo "mapNotExist";
exit(0);
}
}
//get the background color of the given map resource id
function GetBackGroundColor($resourceID)
{
$bgc;
try
{
//site and userInfo are saved in the session
global $userInfo;
$mapResourceContent = "";
//connect to the site and get a resource service instance
$siteConn = new MgSiteConnection();
$siteConn->Open($userInfo);
$resourceService = $siteConn->CreateService(MgServiceType::ResourceService);
//get the map resource content from the server
$mgResourceID = new MgResourceIdentifier($resourceID);
$byteReader = $resourceService->GetResourceContent($mgResourceID);
//read the content into a string
$chunk = "";
do
{
$chunkSize = $byteReader->Read($chunk, 4096);
$mapResourceContent = $mapResourceContent . $chunk;
} while ($chunkSize != 0);
//parse the xml data use DOMDocument
$resourceContent = new DOMDocument();
$resourceContent->loadXML($mapResourceContent);
//get all the elements with the element name "BackgroundColor"
$backgroundColor = $resourceContent->documentElement->getElementsByTagName("BackgroundColor");
//if there's no "BackgroundColor" node
if(0 == $backgroundColor->length)
{
$bgc = new MgColor(255, 255, 255, 255);
}
else
{
//the background color is saved as hex format string
$tempBackgroundColor = $backgroundColor->item(0)->nodeValue;
$red = hexdec(substr($tempBackgroundColor, 0,2));
$green = hexdec(substr($tempBackgroundColor, 2,2));
$blue = hexdec(substr($tempBackgroundColor, 4,2));
$alpha = hexdec(substr($tempBackgroundColor, 6,2));
$bgc = new MgColor($red, $green, $blue, $alpha);
}
}
catch (Exception $exc)
{
$errorMsg = $exc->getMessage();
$bgc = new MgColor(255, 255, 255, 255);
}
return $bgc;
}
function GetProfilingResults()
{
//profiling map parameters
global $mapResourceId;
global $mapProfileResult;
global $scale;
global $centerPoint;
global $clientWidth;
global $clientHeigth;
list($x, $y) = explode("*", $centerPoint);
$x = trim($x);
$y = trim($y);
$geometryFactory = new MgGeometryFactory();
//[centerPoint]
$coordNewCenter = $geometryFactory->CreateCoordinateXY($x,$y);
//[backgroundColor]
$bgc = GetBackGroundColor($mapResourceId);
//[imageFormat]
$imageFormat = "PNG";
//userInfo are saved in the session
global $userInfo;
//connect to the site and get a resource service instance
//create profiling service too
$siteConn = new MgSiteConnection();
$siteConn->Open($userInfo);
$profilingService = $siteConn->CreateService(MgServiceType::ProfilingService);
//the profiling result is saved as xml file, the file name is unique
//the format is like this "YYYYMMDDHHMMSS10"
$newXmlFileId = date("YmdHis") . rand(10, 99);
$resourceID = new MgResourceIdentifier($mapResourceId);
// Get a runtime map from a map definition
$map = new MgMap($siteConn);
$map->Create($resourceID, $newXmlFileId);
//The result will not show base layers
RemoveBaseLayers($map);
//get the profiling map result
$byteReader = $profilingService->ProfileRenderMap($map, NULL, $coordNewCenter, $scale, $clientWidth, $clientHeigth, $bgc, $imageFormat, false);
//read the content into a string
$profilingResourceContent = "";
$chunk = "";
do
{
$chunkSize = $byteReader->Read($chunk, 4096);
$profilingResourceContent = $profilingResourceContent . $chunk;
} while ($chunkSize != 0);
//save the file on the server
$newXmlFileName = "profilingmapxml/".$newXmlFileId.".xml";
$fp = fopen($newXmlFileName, "w");
fwrite($fp, $profilingResourceContent);
fclose($fp);
//put the file name in the session
$_SESSION["ProfilingResultFile"] = $newXmlFileName;
//read the result into the DOM
$resultSource = new DOMDocument();
$resultSource->load($newXmlFileName);
$mapProfileResult->ReadFromXML($resultSource);
$mapProfileResult->GetBaseLayerCount();
}
CheckMapExist();
GetProfilingResults();
$displayManager->mapProfileResult = $mapProfileResult;
$recentSettings = new RecentSettings();
$recentSettings->SaveRecentSettings($mapResourceId,$centerPoint,$scale);
//set cookie
//save the recent setting in the cookie, so next time the user open
//the page will restore the last time setting
//it will expire in 1 month
setcookie("c_mapResourceId", $mapResourceId, time()+60*60*24*30);
setcookie("c_centerPoint", $centerPoint, time()+60*60*24*30);
setcookie("c_scale", $scale, time()+60*60*24*30);
}
catch ( MgException $e )
{
CheckForFatalMgException( $e );
$errorMsg = $e->GetExceptionMessage();
}
catch ( Exception $e )
{
$errorMsg = $e->getMessage();
}
?>