[mapguide-users] Dynamically Modify SDF Source at runtime problem

James Card James.Card at calcad.com
Thu Mar 6 18:56:09 EST 2008


On Thu, 06 Mar 2008 15:27:01 -0800, Bob Cassarly <bob.cassarly at famis.com>  
wrote:

> I am a MapGuide 6.5 Dynamic Authoring Toolkit developer trying to  
> rewrite some of my DAT code.
>
> What I want to do is modify the source SDF of a layer from  
> FLOOR-01_Space.SDF to FLOOR-02_Space.SDF for the current session only (I  
> do not want to update the MapGuide repository).  I know I am probably  
> missing something very simple but all I have not been able to find a way  
> to do this.

The way I solved this problem was to update the FeatureSource definiton(s)  
first and save them to the session repository. Then, because the  
LayerDefinition points to the FeatureSource I created a new  
LayerDefinition that had the name of the newly-created FeatureSource and  
save that in the session repository. The same process for the  
MapDefinition and the LayoutDefinition: update to reflect the new path and  
save to the session repository.

I saved copies of all my resources as XML files and read them from disk. A  
better method would be to retrieve the definitions from the Library, make  
the necessary modifications, and then save to the Session repository. Here  
is code from one of our applications (with extra layers trimmed out):

   // Initialize a PHP session and register a variable to hold the
   // Server session id, then initialize the Web Extensions,
   // and connect to the Server, and create a session.

   try
   {
     session_start();
     session_register('mgSessionId');

     MgInitializeWebTier ($configFilePath);

     $userInfo = new MgUserInformation("MyUser", "MyPassword");

     $site = new MgSite();
     $site->Open($userInfo);
     $HTTP_SESSION_VARS['mgSessionId'] = $site->CreateSession();
     $siteConnection = new MgSiteConnection();
     $siteConnection->Open($userInfo);
     $resourceService =  
$siteConnection->CreateService(MgServiceType::ResourceService);
     $mapDefinition =  
"Session:".$HTTP_SESSION_VARS['mgSessionId']."//Project_1.MapDefinition";
     $webLayout =  
"Session:".$HTTP_SESSION_VARS['mgSessionId']."//Project_1.WebLayout";

     // configure this to point to the Feature Source that contains all  
Rooms
     $roomsFSId = new  
MgResourceIdentifier('Library://Project_1/Data/Project_Room_Polys.FeatureSource');
     // this is the feature class of the feature source
     $roomsFeatureClass = 'Default:Project_Room_PolysJoin';

     // Modify the FeatureSources to point to the appropriate SDF file and  
save
     // them to the session repository, then update all the objects that  
depend
     // on them to look for their data in the session repository instead of  
the
     // library. Unfortunately, that means ALL the layers, maps, and  
layouts.

     // First the FeatureSources  
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

     // Load the Rooms layer FeatureSource template into a PHP DOM object  
and
     // modify its file-name node so we're displaying the right building  
and floor.
     $doc = DOMDocument::load('Project_Room_Polys.FeatureSource.xml');
     $FileNode = $doc->getElementsByTagName('Value')->item(0);
     $SdfPath = $FileNode->nodeValue;
     $FileNode->nodeValue =  
dirname($SdfPath).'\\'.$BuildingID.'_'.$Floor.'_Rooms.sdf';
     // Get the updated FeatureSource definition from the DOM object and  
save it
     // to the repository using the ResourceService object.
     $FeatSrcDefinition = $doc->saveXML();
     $byteSource = new MgByteSource($FeatSrcDefinition,  
strlen($FeatSrcDefinition));
     $byteSource->SetMimeType(MgMimeType::Xml);
     $tempResourceID = new  
MgResourceIdentifier("Session:".$HTTP_SESSION_VARS['mgSessionId']."//Project_Room_Polys.FeatureSource");
     $resourceService->SetResource($tempResourceID,  
$byteSource->GetReader(), null);

     // Next, the LayerDefinitions  
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

     // Load the Rooms Layer Definition template into a PHP DOM object and
     // modify its Resource node to read from our modified FeatureSources in
     // the session repository
     $doc = DOMDocument::load('Rooms.LayerDefinition.xml');
     $ResourceNode = $doc->getElementsByTagName('ResourceId')->item(0);
     $ResourceNode->nodeValue =  
'Session:'.$HTTP_SESSION_VARS["mgSessionId"].'//Project_Room_Polys.FeatureSource';
     // Get the updated Layer definition from the DOM object and save it to
     // the repository using the ResourceService object.
     $LayerDefinition = $doc->saveXML();
     $byteSource = new MgByteSource($LayerDefinition,  
strlen($LayerDefinition));
     $byteSource->SetMimeType(MgMimeType::Xml);
     $tempResourceID = new  
MgResourceIdentifier("Session:".$HTTP_SESSION_VARS['mgSessionId']."//Rooms.LayerDefinition");
     $resourceService->SetResource($tempResourceID,  
$byteSource->GetReader(), null);

     // Then the MapDefinition  
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

     // Load the Project_1 Map definition template into a PHP DOM object and
     // modify its Extents nodes.
     $doc = DOMDocument::load('Project_1.MapDefinition.xml');
     $ResourceNodes = $doc->getElementsByTagName('ResourceId');
     foreach ($ResourceNodes as $ResourceNode)
     {
       $ResourceNode->nodeValue =  
str_replace('Library://Project_1/Layers/',"Session:".$HTTP_SESSION_VARS['mgSessionId']."//",$ResourceNode->nodeValue);
     }
     if ($gotBBox)
     {
       $MinXNode            = $doc->getElementsByTagName('MinX')->item(0);
       $MinXNode->nodeValue = $BldgMinX;
       $MinYNode            = $doc->getElementsByTagName('MinY')->item(0);
       $MinYNode->nodeValue = $BldgMinY;
       $MaxXNode            = $doc->getElementsByTagName('MaxX')->item(0);
       $MaxXNode->nodeValue = $BldgMaxX;
       $MaxYNode            = $doc->getElementsByTagName('MaxY')->item(0);
       $MaxYNode->nodeValue = $BldgMaxY;
     } else {
       echo '<pre class="error">Unable to retrieve a valid bounding box for
                                the map. Please report this to the system
                                administrator.</pre>';
     }
     // Get the updated Map definition from the DOM object and save it to
     // the repository using the ResourceService object.
     $MapDefinition = $doc->saveXML();
     $byteSource = new MgByteSource($MapDefinition, strlen($MapDefinition));
     $byteSource->SetMimeType(MgMimeType::Xml);
     $tempResourceID = new  
MgResourceIdentifier("Session:".$HTTP_SESSION_VARS['mgSessionId']."//Project_1.MapDefinition");
     $resourceService->SetResource($tempResourceID,  
$byteSource->GetReader(), null);

     // Finally, the WebLayout  
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

     $doc = DOMDocument::load('Project_1.WebLayout.xml');
     $ResourceNode = $doc->getElementsByTagName('ResourceId')->item(0);
     $ResourceNode->nodeValue =  
'Session:'.$HTTP_SESSION_VARS["mgSessionId"].'//Project_1.MapDefinition';
     // Get the updated Layout definition from the DOM object and save it
     // to the repository using the ResourceService object.
     $LayerDefinition = $doc->saveXML();
     $byteSource = new MgByteSource($LayerDefinition,  
strlen($LayerDefinition));
     $byteSource->SetMimeType(MgMimeType::Xml);
     $tempResourceID = new  
MgResourceIdentifier("Session:".$HTTP_SESSION_VARS['mgSessionId']."//Project_1.WebLayout");
     $resourceService->SetResource($tempResourceID,  
$byteSource->GetReader(), null);

   }
   catch (MgException $e)
   {
       echo '<pre class="error MgException">'.$e->GetMessage().'</pre>';
       echo '<pre class="error MgException">'.$e->GetDetails().'</pre>';
   }


-- 
James Card
California CAD Solutions, Inc.
209 578-5580 Voice
209 521-6493 FAX


More information about the mapguide-users mailing list