[mapguide-commits] r5584 - in trunk/MgDev/Doc/samples/phpsamples: . analyzing_features common working_with_feature_data

svn_mapguide at osgeo.org svn_mapguide at osgeo.org
Thu Mar 3 08:59:18 EST 2011


Author: jng
Date: 2011-03-03 05:59:18 -0800 (Thu, 03 Mar 2011)
New Revision: 5584

Modified:
   trunk/MgDev/Doc/samples/phpsamples/analyzing_features/createbuffer.php
   trunk/MgDev/Doc/samples/phpsamples/analyzing_features/selectfeaturesinbuffer.php
   trunk/MgDev/Doc/samples/phpsamples/analyzing_features/task_pane.php
   trunk/MgDev/Doc/samples/phpsamples/common/common.php
   trunk/MgDev/Doc/samples/phpsamples/main.php
   trunk/MgDev/Doc/samples/phpsamples/working_with_feature_data/task_pane.php
Log:
#1614: Update devguide buffer samples to reflect simplified usage patterns enabled by convenience APIs provided by RFC9 and RFC33. For purposes of comparing the old way of doing things and the new way of doing things, the existing way of doing things have been commented out with the new way of doing things following the commented code fragment afterwards.

Modified: trunk/MgDev/Doc/samples/phpsamples/analyzing_features/createbuffer.php
===================================================================
--- trunk/MgDev/Doc/samples/phpsamples/analyzing_features/createbuffer.php	2011-03-02 16:23:17 UTC (rev 5583)
+++ trunk/MgDev/Doc/samples/phpsamples/analyzing_features/createbuffer.php	2011-03-03 13:59:18 UTC (rev 5584)
@@ -16,6 +16,12 @@
 //  License along with this library; if not, write to the Free Software
 //  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 -->
+<!--
+This sample has been updated to reflect simplified usage patterns enabled by convenience APIs
+introduced in MapGuide OS 2.0 by allowing you to query and insert features directly from 
+the MgLayer objects themselves and the ability to get a MgFeatureReader directly from the MgSelection
+object.
+-->
   <head>
     <title>Viewer Sample Application - Create Buffer</title>
     <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
@@ -58,8 +64,8 @@
       $featureService = $siteConnection->CreateService(MgServiceType::FeatureService);
       $queryOptions = new MgFeatureQueryOptions();
 
-      $map = new MgMap();
-      $map->Open($resourceService, $mapName);
+      $map = new MgMap($siteConnection);
+      $map->Open($mapName);
 
       // Check for selection data passed via HTTP POST
 
@@ -85,13 +91,15 @@
         $wktReaderWriter = new MgWktReaderWriter();
         $coordinateSystemFactory = new MgCoordinateSystemFactory();
         $srs = $coordinateSystemFactory->Create($mapWktSrs);
-        $srsMeasure = new MgCoordinateSystemMeasure($srs);
+        $srsMeasure = $srs->GetMeasure();
 
         // Check for a buffer layer. If it exists, delete
         // the current features.
         // If it does not exist, create a feature source and
         // a layer to hold the buffer.
 
+        /*
+        // Old way, pre MapGuide OS 2.0. Kept here for reference
         try
         {
           $bufferLayer = $map->GetLayers()->GetItem('Buffer');
@@ -112,7 +120,28 @@
           $bufferLayer = CreateBufferLayer($resourceService, $bufferFeatureResId, $sessionId);
           $map->GetLayers()->Insert(0, $bufferLayer);
         }
+        */
 
+        // New way, post MapGuide 2.0
+        $layerIndex = $map->GetLayers()->IndexOf('Buffer');
+        if ($layerIndex < 0)
+        {
+            // The layer does not exist and must be created.
+
+            $bufferFeatureResId = new MgResourceIdentifier("Session:" . $sessionId . "//Buffer.FeatureSource");
+            CreateBufferFeatureSource($featureService, $mapWktSrs, $bufferFeatureResId);
+            $bufferLayer = CreateBufferLayer($resourceService, $bufferFeatureResId, $sessionId);
+            $map->GetLayers()->Insert(0, $bufferLayer);
+        }
+        else
+        {
+            $bufferLayer = $map->GetLayers()->GetItem($layerIndex);
+            $commands = new MgFeatureCommandCollection();
+            $commands->Add(new MgDeleteFeatures('BufferClass', "ID like '%'"));
+            
+            $bufferLayer->UpdateFeatures($commands);
+        }
+        
         for ($i = 0; $i < $selectedLayers->GetCount(); $i++)
         {
           // Only check selected features in the Parcels layer.
@@ -121,7 +150,8 @@
 
           if ($layer->GetName() == 'Parcels')
           {
-
+            // Old way, pre MapGuide OS 2.0. Kept here for reference
+            /*
             // Create a filter containing the IDs of the selected features on this layer
 
             $layerClassName = $layer->GetFeatureClassName();
@@ -131,13 +161,13 @@
 
             $layerFeatureId = $layer->GetFeatureSourceId();
             $layerFeatureResource = new MgResourceIdentifier($layerFeatureId);
-
+        
             // Apply the filter to the feature resource for the selected layer. This returns
             // an MgFeatureReader of all the selected features.
 
             $queryOptions->SetFilter($selectionString);
             $featureReader = $featureService->SelectFeatures($layerFeatureResource, $layerClassName, $queryOptions);
-
+            
             // Process each item in the MgFeatureReader. Get the
             // geometries from all the selected features and
             // merge them into a single geometry.
@@ -175,12 +205,65 @@
             }
 
             $results = $featureService->UpdateFeatures($bufferFeatureResId, $commands, false);
-
+            
             $bufferLayer->SetVisible(true);
             $bufferLayer->ForceRefresh();
             $bufferLayer->SetDisplayInLegend(true);
             $map->Save($resourceService);
+            */
+            
+            // New way, post MapGuide 2.0
+            
+            // Get the selected features from the MgSelection object
+            $featureReader = $selection->GetSelectedFeatures($layer, $layer->GetFeatureClassName(), false);
+            
+            // Process each item in the MgFeatureReader. Get the
+            // geometries from all the selected features and
+            // merge them into a single geometry.
 
+            $inputGeometries = new MgGeometryCollection();
+            while ($featureReader->ReadNext())
+            {
+              $featureGeometryData = $featureReader->GetGeometry('SHPGEOM');
+              $featureGeometry = $agfReaderWriter->Read($featureGeometryData);
+
+              $inputGeometries->Add($featureGeometry);
+            }
+
+            $geometryFactory = new MgGeometryFactory();
+            $mergedGeometries = $geometryFactory->CreateMultiGeometry($inputGeometries);
+
+            // Add buffer features to the temporary feature source.
+            // Create multiple concentric buffers to show area.
+            // If the stylization for the layer draws the features
+            // partially transparent, the concentric rings will be
+            // progressively darker towards the center.
+            // The stylization is set in the layer template file, which
+            // is used in function CreateBufferLayer().
+
+            $commands = new MgFeatureCommandCollection();
+            for ($bufferRing = 0; $bufferRing < $bufferRingCount; $bufferRing++)
+            {
+              $bufferDist = $srs->ConvertMetersToCoordinateSystemUnits($bufferRingSize * ($bufferRing + 1));
+              $bufferGeometry = $mergedGeometries->Buffer($bufferDist, $srsMeasure);
+
+              $properties = new MgPropertyCollection();
+              $properties->Add(new MgGeometryProperty('BufferGeometry', $agfReaderWriter->Write($bufferGeometry)));
+
+              $commands->Add(new MgInsertFeatures('BufferClass', $properties));
+            }
+
+            // Old way, pre MapGuide OS 2.0
+            //$featureService->UpdateFeatures($bufferFeatureResId, $commands, false);
+            
+            // New way, post MapGuide OS 2.0
+            $bufferLayer->UpdateFeatures($commands);
+            
+            $bufferLayer->SetVisible(true);
+            $bufferLayer->ForceRefresh();
+            $bufferLayer->SetDisplayInLegend(true);
+            $map->Save();
+            
           }
         }
       }

Modified: trunk/MgDev/Doc/samples/phpsamples/analyzing_features/selectfeaturesinbuffer.php
===================================================================
--- trunk/MgDev/Doc/samples/phpsamples/analyzing_features/selectfeaturesinbuffer.php	2011-03-02 16:23:17 UTC (rev 5583)
+++ trunk/MgDev/Doc/samples/phpsamples/analyzing_features/selectfeaturesinbuffer.php	2011-03-03 13:59:18 UTC (rev 5584)
@@ -15,6 +15,13 @@
 //  License along with this library; if not, write to the Free Software
 //  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 -->
+
+<!--
+This sample has been updated to reflect simplified usage patterns enabled by convenience APIs
+introduced in MapGuide OS 2.0 by allowing you to query and insert features directly from 
+the MgLayer objects themselves and the ability to get a MgFeatureReader directly from the MgSelection
+object.
+-->
 <html>
 
   <head>
@@ -58,8 +65,8 @@
       $featureService = $siteConnection->CreateService(MgServiceType::FeatureService);
       $queryOptions = new MgFeatureQueryOptions();
 
-      $map = new MgMap();
-      $map->Open($resourceService, $mapName);
+      $map = new MgMap($siteConnection);
+      $map->Open($mapName);
 
       // Check for selection data passed via HTTP POST
 
@@ -84,13 +91,15 @@
         $wktReaderWriter = new MgWktReaderWriter();
         $coordinateSystemFactory = new MgCoordinateSystemFactory();
         $srs = $coordinateSystemFactory->Create($mapWktSrs);
-        $srsMeasure = new MgCoordinateSystemMeasure($srs);
+        $srsMeasure = $srs->GetMeasure();
 
         // Check for a buffer layer. If it exists, delete
         // the current features.
         // If it does not exist, create a feature source and
         // a layer to hold the buffer.
 
+        /*
+        // Old way, pre MapGuide OS 2.0. Kept here for reference
         try
         {
           $bufferLayer = $map->GetLayers()->GetItem('Buffer');
@@ -111,12 +120,35 @@
           $bufferLayer = CreateBufferLayer($resourceService, $bufferFeatureResId, $sessionId);
           $map->GetLayers()->Insert(0, $bufferLayer);
         }
+        */
+        
+        // This is how things can be done now
+        $layerIndex = $map->GetLayers()->IndexOf('Buffer');
+        if ($layerIndex < 0)
+        {
+            // The layer does not exist and must be created.
 
+            $bufferFeatureResId = new MgResourceIdentifier("Session:" . $sessionId . "//Buffer.FeatureSource");
+            CreateBufferFeatureSource($featureService, $mapWktSrs, $bufferFeatureResId);
+            $bufferLayer = CreateBufferLayer($resourceService, $bufferFeatureResId, $sessionId);
+            $map->GetLayers()->Insert(0, $bufferLayer);
+        }
+        else
+        {
+            $bufferLayer = $map->GetLayers()->GetItem($layerIndex);
+            $commands = new MgFeatureCommandCollection();
+            $commands->Add(new MgDeleteFeatures('BufferClass', "ID like '%'"));
+            
+            $bufferLayer->UpdateFeatures($commands);
+        }
+
         // Check for a parcel marker layer. If it exists, delete
         // the current features.
         // If it does not exist, create a feature source and
         // a layer to hold the parcel markers.
 
+        /*
+        // Old way, pre MapGuide OS 2.0. Kept here for reference
         try
         {
           $parcelMarkerLayer = $map->GetLayers()->GetItem('ParcelMarker');
@@ -137,6 +169,25 @@
           $parcelMarkerLayer = CreateParcelMarkerLayer($resourceService, $parcelFeatureResId, $sessionId);
           $map->GetLayers()->Insert(0, $parcelMarkerLayer);
         }
+        */
+        
+        // New way, post MapGuide 2.0
+        $layerIndex = $map->GetLayers()->IndexOf('ParcelMarker');
+        if ($layerIndex < 0)
+        {
+            $parcelFeatureResId = new MgResourceIdentifier("Session:" . $sessionId . "//ParcelMarker.FeatureSource");
+            CreateParcelMarkerFeatureSource($featureService, $mapWktSrs, $parcelFeatureResId);
+            $parcelMarkerLayer = CreateParcelMarkerLayer($resourceService, $parcelFeatureResId, $sessionId);
+            $map->GetLayers()->Insert(0, $parcelMarkerLayer);
+        }
+        else
+        {
+            $parcelMarkerLayer = $map->GetLayers()->GetItem($layerIndex);
+            $commands = new MgFeatureCommandCollection();
+            $commands->Add(new MgDeleteFeatures('ParcelMarkerClass', "ID like '%'"));
+            
+            $parcelMarkerLayer->UpdateFeatures($commands);
+        }
 
         // Check each layer in the selection.
 
@@ -151,6 +202,9 @@
 
             echo 'Marking all parcels inside the buffer that are of type "MFG"';
 
+            /*
+            // Old way, pre MapGuide OS 2.0. Kept here for reference
+            
             // Create a filter containing the IDs of the selected features on this layer
 
             $layerClassName = $layer->GetFeatureClassName();
@@ -166,7 +220,13 @@
 
             $queryOptions->SetFilter($selectionString);
             $featureReader = $featureService->SelectFeatures($layerFeatureResource, $layerClassName, $queryOptions);
-
+            */
+            
+            // New way, post MapGuide 2.0
+            
+            $featureReader = $selection->GetSelectedFeatures($layer, $layer->GetFeatureClassName(), false);
+            
+            
             // Process each item in the MgFeatureReader. Get the
             // geometries from all the selected features and
             // merge them into a single geometry.
@@ -196,16 +256,22 @@
             $queryOptions->SetFilter("RTYPE = 'MFG'");
             $queryOptions->SetSpatialFilter('SHPGEOM', $bufferGeometry, MgFeatureSpatialOperations::Inside);
 
+            /*
+            // Old way, pre MapGuide OS 2.0. Kept here for reference
+            $featureResId = new MgResourceIdentifier("Library://Samples/Sheboygan/Data/Parcels.FeatureSource");
+            $featureReader = $featureService->SelectFeatures($featureResId, "Parcels", $queryOptions);
+            */
+            
+            // New way, post MapGuide OS 2.0
+            $featureReader = $layer->SelectFeatures($queryOptions);
+            
             // Get the features from the feature source,
             // determine the centroid of each selected feature, and
             // add a point to the ParcelMarker layer to mark the
             // centroid.
             // Collect all the points into an MgFeatureCommandCollection,
             // so they can all be added in one operation.
-
-            $featureResId = new MgResourceIdentifier("Library://Samples/Sheboygan/Data/Parcels.FeatureSource");
-            $featureReader = $featureService->SelectFeatures($featureResId, "Parcels", $queryOptions);
-
+            
             $parcelMarkerCommands = new MgFeatureCommandCollection();
             while ($featureReader->ReadNext())
             {
@@ -227,7 +293,11 @@
 
             if ($parcelMarkerCommands->GetCount() > 0)
             {
-              $featureService->UpdateFeatures($parcelFeatureResId, $parcelMarkerCommands, false);
+                // Old way, pre MapGuide OS 2.0. Kept here for reference
+                //$featureService->UpdateFeatures($parcelFeatureResId, $parcelMarkerCommands, false);
+                
+                // New way, post MapGuide OS 2.0
+                $parcelMarkerLayer->UpdateFeatures($parcelMarkerCommands);
             }
             else
             {
@@ -242,7 +312,11 @@
             $commands = new MgFeatureCommandCollection();
             $commands->Add(new MgInsertFeatures('BufferClass', $properties));
 
-            $featureService->UpdateFeatures($bufferFeatureResId, $commands, false);
+            // Old way, pre MapGuide OS 2.0
+            //$featureService->UpdateFeatures($bufferFeatureResId, $commands, false);
+            
+            // New way, post MapGuide OS 2.0
+            $bufferLayer->UpdateFeatures($commands);
 
             // Ensure that the buffer layer is visible and in the legend.
 
@@ -252,7 +326,7 @@
             $parcelMarkerLayer->SetVisible(true);
             $parcelMarkerLayer->ForceRefresh();
 
-            $map->Save($resourceService);
+            $map->Save();
 
           }
         }

Modified: trunk/MgDev/Doc/samples/phpsamples/analyzing_features/task_pane.php
===================================================================
--- trunk/MgDev/Doc/samples/phpsamples/analyzing_features/task_pane.php	2011-03-02 16:23:17 UTC (rev 5583)
+++ trunk/MgDev/Doc/samples/phpsamples/analyzing_features/task_pane.php	2011-03-03 13:59:18 UTC (rev 5584)
@@ -54,7 +54,7 @@
     ?>
     <ul>
       <li>
-        <a href="#" onClick="submitBufferRequest('/mapguide/phpsamples/analyzing_features/createbuffer.php'); return false;">
+        <a href="#" onClick="submitBufferRequest('../phpsamples/analyzing_features/createbuffer.php'); return false;">
         Create buffer</a>
         <br/>Create a buffer around a selected parcel.
         <br/>
@@ -67,7 +67,7 @@
       </li>
 
       <li>
-        <a href="#" onClick="submitBufferRequest('/mapguide/phpsamples/analyzing_features/selectfeaturesinbuffer.php'); return false;">
+        <a href="#" onClick="submitBufferRequest('../phpsamples/analyzing_features/selectfeaturesinbuffer.php'); return false;">
         Find features in buffer</a>
         <br/>Create a buffer around a selected parcel, then mark parcels inside the buffer that are
         of type "MFG".

Modified: trunk/MgDev/Doc/samples/phpsamples/common/common.php
===================================================================
--- trunk/MgDev/Doc/samples/phpsamples/common/common.php	2011-03-02 16:23:17 UTC (rev 5583)
+++ trunk/MgDev/Doc/samples/phpsamples/common/common.php	2011-03-03 13:59:18 UTC (rev 5584)
@@ -19,9 +19,9 @@
 // -----------------------------------------------------------------------------------
 // Use the following for Windows installations
 // -----------------------------------------------------------------------------------
-$webExtensionsDirectory = 'C:\Program Files\MapGuideOpenSource\WebServerExtensions\\';
+$webExtensionsDirectory = 'C:\Program Files\OSGeo\MapGuide\Web\\';
   
-$MapGuideServerDirectory = 'C:\Program Files\MapGuideOpenSource\Server\\';
+$MapGuideServerDirectory = 'C:\Program Files\OSGeo\MapGuide\Server\\';
   
 $viewerFilesDirectory = $webExtensionsDirectory . 'www\viewerfiles\\';
   

Modified: trunk/MgDev/Doc/samples/phpsamples/main.php
===================================================================
--- trunk/MgDev/Doc/samples/phpsamples/main.php	2011-03-02 16:23:17 UTC (rev 5583)
+++ trunk/MgDev/Doc/samples/phpsamples/main.php	2011-03-03 13:59:18 UTC (rev 5584)
@@ -40,7 +40,7 @@
 
     // Define some constants
     $webLayout     = "Library://Samples/Layouts/PHPSamples.WebLayout";
-    $title         = "PHP Samples";
+    $title         = "MapGuide Developer's Guide PHP Samples";
 }
 catch (MgException $e)
 {
@@ -62,9 +62,6 @@
 
   <frameset rows="110,*" frameborder="NO" border="0" framespacing="0">
     <frame src="common/Title.php?TitleText=<?= $title ?>" name="TitleFrame" scrolling="NO" noresize />
-    <frame
-    src="/mapguide/mapviewerajax/?
-    SESSION=<?= $sessionId ?>&
-    WEBLAYOUT=<?= $webLayout ?>" name="ViewerFrame" />
+    <frame src="/mapguide/mapviewerajax/?SESSION=<?= $sessionId ?>&WEBLAYOUT=<?= $webLayout ?>" name="ViewerFrame" />
   </frameset>
 </html>

Modified: trunk/MgDev/Doc/samples/phpsamples/working_with_feature_data/task_pane.php
===================================================================
--- trunk/MgDev/Doc/samples/phpsamples/working_with_feature_data/task_pane.php	2011-03-02 16:23:17 UTC (rev 5583)
+++ trunk/MgDev/Doc/samples/phpsamples/working_with_feature_data/task_pane.php	2011-03-03 13:59:18 UTC (rev 5584)
@@ -32,7 +32,7 @@
       params = new Array("SESSION", parent.parent.mapFrame.GetSessionId(), 
         "MAPNAME", parent.parent.mapFrame.GetMapName(),
         "SELECTION", xmlSel);
-      pageUrl = "/mapguide/phpsamples/working_with_feature_data/listselection.php";
+      pageUrl = "../phpsamples/working_with_feature_data/listselection.php";
       parent.parent.formFrame.Submit(pageUrl, params, "taskPaneFrame");
     }
     </script>



More information about the mapguide-commits mailing list