Example: Setting the Active Selection
 
 

The following example combines the pieces needed to create a selection using the Web API and pass it back to the Viewer where it becomes the active selection for the map. It is an extension of the example shown in Example: Listing Selected Features.

The PHP code in this example creates the selection XML. Following that is a JavaScript function that calls the SetSelectionXML() function with the selection. This function is executed when the page loads.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

  "http://www.w3.org/TR/html4/loose.dtd">

<html>

 

   <head>

      <title>Server-side Selection</title>

      <meta content="text/html; charset=utf-8"

       http-equiv="Content-Type">

      <meta http-equiv="content-script-type"

       content="text/javascript" />

      <meta http-equiv="content-style-type" content="text/css" />

      <link href="style/globalStyles.css" rel="stylesheet"

       type="text/css">

      <link href="style/alphaStyles.css" rel="stylesheet"

       type="text/css">

   </head>

 

   <body class="AppFrame" onLoad="OnPageLoad()">

 

      <h1 class="AppHeading">Example</h1>

 

         <?php

         include 'AppConstants.php';

 

         $mgSessionId = ($_SERVER['REQUEST_METHOD'] == "POST")

            ? $_POST['SESSION']: $_GET['SESSION'];

         $mapName = ($_SERVER['REQUEST_METHOD'] == "POST")

            ? $_POST['MAPNAME']: $_GET['MAPNAME'];

 

         try

         {

 

            //

Initialize the Web Extensions and connect to

            //

the Server using

            //

the Web Extensions session identifier stored

            //

in PHP session state.

 

            MgInitializeWebTier ($configFilePath);

 

            $userInfo = new MgUserInformation($mgSessionId);

            $siteConnection = new MgSiteConnection();

            $siteConnection->Open($userInfo);

 

            $resourceService = $siteConnection->CreateService(

               MgServiceType::ResourceService);

            $featureService = $siteConnection->CreateService(

               MgServiceType::FeatureService);

 

            $map = new MgMap();

            $map->Open($resourceService, $mapName);

 

            //

Get the geometry for the boundaries of District 1

 

            $districtQuery = new MgFeatureQueryOptions();

            $districtQuery->SetFilter("Autogenerated_SDF_ID = 1");

            $districtResId = new MgResourceIdentifier(

"Library://Samples/Sheboygan/Data/VotingDistricts.FeatureSource");

            $featureReader = $featureService->SelectFeatures(

               $districtResId, "VotingDistricts", $districtQuery);

            $featureReader->ReadNext();

            $districtGeometryData = $featureReader->GetGeometry(

               'Data');

 

            //

Convert the AGF binary data to MgGeometry.

 

            $agfReaderWriter = new MgAgfReaderWriter();

            $districtGeometry = $agfReaderWriter->Read(

               $districtGeometryData);

 

            //

Create a filter to select the desired features.

            //

Combine a basic filter and a spatial filter.

 

            $queryOptions = new MgFeatureQueryOptions();

            $queryOptions->SetFilter("RNAME LIKE 'Schmitt%'");

            $queryOptions->SetSpatialFilter('SHPGEOM',

               $districtGeometry,

               MgFeatureSpatialOperations::Inside);

 

            //

Get the features from the feature source,

            //

turn it into a selection, then save as XML.

 

            $featureResId = new MgResourceIdentifier(

"Library://Samples/Sheboygan/Data/Parcels.FeatureSource");

            $featureReader = $featureService->SelectFeatures(

               $featureResId, "Parcels", $queryOptions);

 

            $layer = $map->GetLayers()->GetItem('Parcels');

            $selection = new MgSelection($map);

            $selection->AddFeatures($layer, $featureReader, 0);

            $selectionXml = $selection->ToXml();

 

            echo 'Setting selection...';

         }

         catch (MgException $e)

         {

            echo $e->GetMessage();

            echo $e->GetDetails();

         }

         ?>

 

   </body>

 

   <script language="javascript">

 

<!--

Emit this function and assocate it with the

onLoad

event

-->

<!--

for the page so that it gets executed when this page loads

-->

<!--

in the browser. The function calls the

SetSelectionXML   -->

<!--

method on the Viewer Frame, which updates the current

   -->

<!--

selection on the viewer and the server.

                 -->

 

      function OnPageLoad()

      {

         selectionXml = '<?php echo $selectionXml; ?>';

         parent.parent.SetSelectionXML(selectionXml);

      }

 

   </script>

 

</html>