[mapguide-users] Filter applied to data...

Paul Spencer pspencer at dmsolutions.ca
Tue Oct 10 13:38:07 EDT 2006


The API of MgLayer doesn't include a mechanism for modifying the  
filter applied to the layer at runtime.  This means that you will  
need to use the resource service to get the content of the layer,  
modify the XML directly and use the resource service to put the new  
layer definition back into the repository.

Here is some sample code that you can start with.  Note that this is  
not tested in any way and very probably contains syntax errors etc.   
Please spend some time trying to get this to work BEFORE emailing to  
say it doesn't work ;)

   <?php
   $installDir = "C:/Program Files/MapGuideOpenSource/";
   $extensionDir = $installDir . "WebServerExtensions/www/";
   $viewDir = $extensionDir."mapviewerphp/";

   include $viewDir . "common.php";
   include $viewDir . "constants.php";

   MgInitializeWebTier($extensionDir. "webconfig.ini");

   $username = "Anonymous";
   $password = "";

   $layerName = "Parcels";

   $siteConnection = new MgSiteConnection();

   if (isset($_REQUEST['SessionId'])) {
       $sessionId = $_REQUEST['SessionId'];
       $user = new MgUserInformation($sessionId);
       $siteConnection->Open($user);
   } else {
       $user = new MgUserInformation($username, $password);
       $siteConnection->Open($user);
       $site = $siteConnection->GetSite();
       $sessionId = $site->CreateSession();
       $user->SetMgSessionId($sessionId);
       //subsequent calls to this script must pass SessionId
       echo "<SessionId>$sessionId</SessionId>";
   }

   $resourceService = $siteConnection->CreateService 
(MgServiceType::ResourceService);

   $map = new MgMap();

   if (!isset($_REQUEST['mapName'])) {
       $mapName = "Library://Samples/Sheboygan/Maps/ 
Sheboygan.MapDefintion";
       $mapResourceId = new MgResourceIdentifier($mapName);
       $map->Create($resourceService, $mapResourceId, $mapName);
       //subsequent calls to this script must pass MapName=
       echo "<MapName>".$map->GetName()."</MapName>";
       $mapSessionId = new MgResourceIdentifier("Session:" .  
$sessionId . "//" . $map->GetName() . "." . MgResourceType::Map);
       $map->Save($resourceService, $mapSessionId);
   } else {
       $map->Open($resourceService, $_REQUEST['mapName']);
   }

   if (isset($_REQUEST['Layer'])) {
       $layers = $map->GetLayers();
       $layer = $layers->GetItem($_REQUEST['Layer']);

       $layerId = $layer->GetLayerDefinition();
       //copy layer to session
       if (substr($layerId, 0, 7) != 'Session') {
           //could also use CopyResource?
           $byteReader = $resourceService->GetResourceContent($layer- 
 >GetLayerDefinition());
           $layerContent = $byteReader->ToString();

           $layerSessionId = new MgResourceIdentifier("Session:". 
$sessionId."//".$layer->GetName().".".MgResourceType::LayerDefinition);

           $byteSource = new MgByteSource($layerContent, strlen 
($layerContent));
           $resourceService->SetResource($layerSessionId, $byteSource);
       }
   }

   if (isset($layer) && isset($_REQUEST['Filter'])) {
       $byteReader = $resourceService->GetResourceContent($layer- 
 >GetLayerDefinition());
       $layerContent = $byteReader->ToString();

       //change the filter
       $domDocument = DOMDocument::loadXML($layerContent);

       $xpath = new DOMXPath($domDocument);
       $query = '//VectorLayerDefinition/Filter';
       // Get a list of all the <VectorLayerDefinition><Filter>  
elements in
       // the XML (there should be one).
       $nodes = $xpath->query($query);
       // Find the correct node and change it
       foreach ($nodes as $node )
       {
           $node->nodeValue = $_REQUEST['Filter'];
       }

       // Put the new layer definition back into the repository
       $layerContent = $domDocument->saveXML();
       $byteSource = new MgByteSource($layerContent, strlen 
($layerContent));
       $resourceService->SetResource($layer->GetLayerDefinition(),  
$byteSource);

       //make sure MG knows that the layer has changed
       $layer->ForceRefresh();
       echo "<p>Filter Set</p>";
   }
   //save the map in the session
   $map->Save($resourceService);
   ?>



On 10-Oct-06, at 7:50 AM, Willem Schwarte wrote:

> Ok, but  now I need to change this in my code when e.g. someone  
> presses
> a button or something...
>
> Willem
>
>
> -----Oorspronkelijk bericht-----
> Van: Paul Spencer [mailto:pspencer at dmsolutions.ca]
> Verzonden: dinsdag 10 oktober 2006 13:46
> Aan: users at mapguide.osgeo.org
> Onderwerp: Re: [mapguide-users] Filter applied to data...
> Urgentie: Hoog
>
> You can set a layer-wide filter using Studio (or Web Studio) and
> editing the layer settings.  The filter is below the layer property
> mappings next to the URL and Tooltip settings.  You set the filter by
> building an expression.  And yes, it works just like an SQL where
> clause.
>
> Cheers
>
> Paul
>
> On 10-Oct-06, at 6:47 AM, Willem Schwarte wrote:
>
>> Hi,
>>
>>
>>
>>
>>
>> I am assuming that "Filter applied to data" is the same
>> functionality as the SQL WHERE CLAUSE in mapguide 6.5.
>>
>>
>>
>> In mapguide6.5 I could very easily set this where-clause to only
>> show certain elements of a map. (through layer->database Setup-
>>> setWhereClause('area > 100')  )
>>
>>
>>
>> I need to do the same now, but don't really know where to start
>> looking. In the documentation I can find setFilter, but this is all
>> about selecting data in a datareader.
>>
>>
>>
>>
>>
>> Could somebody help me out here please?
>>
>>
>>
>> Thx
>>
>>
>>
>>
>
> +-----------------------------------------------------------------+
> |Paul Spencer                          pspencer at dmsolutions.ca    |
> +-----------------------------------------------------------------+
> |Chief Technology Officer                                         |
> |DM Solutions Group Inc                http://www.dmsolutions.ca/ |
> +-----------------------------------------------------------------+
>
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe at mapguide.osgeo.org
> For additional commands, e-mail: users-help at mapguide.osgeo.org
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe at mapguide.osgeo.org
> For additional commands, e-mail: users-help at mapguide.osgeo.org
>

+-----------------------------------------------------------------+
|Paul Spencer                          pspencer at dmsolutions.ca    |
+-----------------------------------------------------------------+
|Chief Technology Officer                                         |
|DM Solutions Group Inc                http://www.dmsolutions.ca/ |
+-----------------------------------------------------------------+








More information about the Mapguide_users mailing list