[mapguide-commits] r6798 - branches/2.4/MgDev/Web/src/schemareport

svn_mapguide at osgeo.org svn_mapguide at osgeo.org
Thu Jun 21 07:59:13 PDT 2012


Author: jng
Date: 2012-06-21 07:59:13 -0700 (Thu, 21 Jun 2012)
New Revision: 6798

Modified:
   branches/2.4/MgDev/Web/src/schemareport/displayschemafunctions.php
   branches/2.4/MgDev/Web/src/schemareport/showclass.php
   branches/2.4/MgDev/Web/src/schemareport/showgeom.php
Log:
More schema report improvements:
 - Refactor out the record counting and MBR calculation into external functions
 - Update showclass.php to use the external counting function. Thus it will now use the Count() SelectAggregate shortcut if available instead of raw-spinning the feature reader to get the total.

Modified: branches/2.4/MgDev/Web/src/schemareport/displayschemafunctions.php
===================================================================
--- branches/2.4/MgDev/Web/src/schemareport/displayschemafunctions.php	2012-06-20 11:13:45 UTC (rev 6797)
+++ branches/2.4/MgDev/Web/src/schemareport/displayschemafunctions.php	2012-06-21 14:59:13 UTC (rev 6798)
@@ -21,6 +21,186 @@
 
 <?php
 
+class MBR 
+{
+    public $coordinateSystem;
+    public $extentGeometry;
+}
+
+function GetFeatureClassMBR($featuresId, $className, $geomProp, $featureSrvc)
+{
+    $extentGeometryAgg = null;
+    $extentGeometrySc = null;
+    $extentByteReader = null;
+    
+    $mbr = new MBR();
+    $geomName = $geomProp->GetName();
+    $spatialContext = $geomProp->GetSpatialContextAssociation();
+
+    // Finds the coordinate system
+    $agfReaderWriter = new MgAgfReaderWriter();
+    $spatialcontextReader = $featureSrvc->GetSpatialContexts($featuresId, false);
+    while ($spatialcontextReader->ReadNext())
+    {
+        if ($spatialcontextReader->GetName() == $spatialContext)
+        {
+            $mbr->coordinateSystem = $spatialcontextReader->GetCoordinateSystemWkt();
+
+            // Finds the extent
+            $extentByteReader = $spatialcontextReader->GetExtent();
+            break;
+        }
+    }
+    $spatialcontextReader->Close();
+    if ($extentByteReader != null)
+    {
+        // Get the extent geometry from the spatial context
+        $extentGeometrySc = $agfReaderWriter->Read($extentByteReader);
+    }
+
+    // Try to get the extents using the selectaggregate as sometimes the spatial context
+    // information is not set
+    $aggregateOptions = new MgFeatureAggregateOptions();
+    $featureProp = 'SPATIALEXTENTS("' . $geomName . '")';
+    $aggregateOptions->AddComputedProperty('EXTENTS', $featureProp);
+
+    try
+    {
+        $dataReader = $featureSrvc->SelectAggregate($featuresId, $className, $aggregateOptions);
+        if($dataReader->ReadNext())
+        {
+            // Get the extents information
+            $byteReader = $dataReader->GetGeometry('EXTENTS');
+            $extentGeometryAgg = $agfReaderWriter->Read($byteReader);
+        }
+        $dataReader->Close();
+    }
+    catch (MgException $e)
+    {
+        if ($extentGeometryAgg == null) 
+        {
+            //We do have one last hope. EXTENT() is an internal MapGuide custom function that's universally supported
+            //as it operates against an underlying select query result. This raw-spins the reader server-side so there
+            //is no server -> web tier transmission overhead involved.
+            try
+            {
+                $aggregateOptions = new MgFeatureAggregateOptions();
+                $aggregateOptions->AddComputedProperty("COMP_EXTENT", "EXTENT(".$geomName.")");
+                
+                $dataReader = $featureSrvc->SelectAggregate($featuresId, $className, $aggregateOptions);
+                if($dataReader->ReadNext())
+                {
+                    // Get the extents information
+                    $byteReader = $dataReader->GetGeometry('COMP_EXTENT');
+                    $extentGeometryAgg = $agfReaderWriter->Read($byteReader);
+                }
+                $dataReader->Close();
+            }
+            catch (MgException $e2) 
+            {
+                
+            }
+        }
+    }
+    
+    $mbr->extentGeometry = null;
+    // Prefer SpatialExtents() of EXTENT() result over spatial context extent
+    if ($extentGeometryAgg != null)
+        $mbr->extentGeometry = $extentGeometryAgg;
+    if ($mbr->extentGeometry == null) { //Stil null? Now try spatial context
+        if ($extentGeometrySc != null)
+            $mbr->extentGeometry = $extentGeometrySc;
+    }
+    return $mbr;
+}
+
+function GetFeatureCount($featuresId, $schemaName, $className, $resourceSrvc, $featureSrvc)
+{
+    //Try the SelectAggregate shortcut. This is faster than raw spinning a feature reader
+    //
+    //NOTE: If MapGuide supported scrollable readers like FDO, we'd have also tried 
+    //that as well.
+    $featureName = $schemaName . ":" . $className;
+    $canCount = false;
+    $gotCount = false;
+    $fsBr = $resourceSrvc->GetResourceContent($featuresId);
+    $fsXml = $fsBr->ToString();
+    $fsDoc = DOMDocument::loadXML($fsXml);
+    $providerNodeList = $fsDoc->getElementsByTagName("Provider");
+    $providerName = $providerNodeList->item(0)->nodeValue;
+    $capsBr = $featureSrvc->GetCapabilities($providerName);
+    $capsXml = $capsBr->ToString();
+    //This should be good enough to find out if Count() is supported
+    $canCount = !(strstr($capsXml, "<Name>Count</Name>") === false);
+    
+    if ($canCount) {
+        $clsDef = $featureSrvc->GetClassDefinition($featuresId, $schemaName, $className);
+        $idProps = $clsDef->GetIdentityProperties();
+        if ($idProps->GetCount() > 0)
+        {
+            $pd = $idProps->GetItem(0);
+            $expr = "COUNT(" .$pd->GetName(). ")";
+            $query = new MgFeatureAggregateOptions();
+            $query->AddComputedProperty("TotalCount", $expr);
+            try 
+            {
+                $dataReader = $featureSrvc->SelectAggregate($featuresId, $featureName, $query);
+                if ($dataReader->ReadNext())
+                {
+                    // When there is no data, the property will be null.
+                    if($dataReader->IsNull("TotalCount"))
+                    {
+                        $totalEntries = 0;
+                        $gotCount = true;
+                    }
+                    else
+                    {
+                        $ptype = $dataReader->GetPropertyType("TotalCount");
+                        switch ($ptype)
+                        {
+                            case MgPropertyType::Int32:
+                                $totalEntries = $dataReader->GetInt32("TotalCount");
+                                $gotCount = true;
+                                break;
+                            case MgPropertyType::Int64:
+                                $totalEntries = $dataReader->GetInt64("TotalCount");
+                                $gotCount = true;
+                                break;
+                        }
+                        $dataReader->Close();
+                    }
+                }
+            }
+            catch (MgException $ex) //Some providers like OGR can lie
+            {
+                $gotCount = false;
+            }
+        }
+    }
+    
+    if ($gotCount == false)
+    {
+        $featureReader = null;
+        try 
+        {
+            $featureReader = $featureSrvc->SelectFeatures($featuresId, $featureName, null);
+        }
+        catch (MgException $ex)
+        {
+            $totalEntries = -1; //Can't Count() or raw spin? Oh dear!
+        }
+        
+        if ($featureReader != null)
+        {
+            while($featureReader->ReadNext())
+                $totalEntries++;
+            $featureReader->Close();
+        }
+    }
+    
+    return $totalEntries;
+}
+
 function GetPropertyName($featureReader, $property, $propertyType)
 {
     if($featureReader->IsNull($property))

Modified: branches/2.4/MgDev/Web/src/schemareport/showclass.php
===================================================================
--- branches/2.4/MgDev/Web/src/schemareport/showclass.php	2012-06-20 11:13:45 UTC (rev 6797)
+++ branches/2.4/MgDev/Web/src/schemareport/showclass.php	2012-06-21 14:59:13 UTC (rev 6798)
@@ -60,6 +60,7 @@
                 $site->Open($userInfo);
 
                 $featureSrvc = $site->CreateService(MgServiceType::FeatureService);
+                $resourceSrvc = $site->CreateService(MgServiceType::ResourceService);
                 $resId = new MgResourceIdentifier($resName);
 
                 $schemaName = substr(strrchr($schemaName, "/"), 1);
@@ -67,12 +68,8 @@
                 $geomName = $classDef->GetDefaultGeometryPropertyName();
 
                 $qualifiedClassName = $schemaName . ":" . $className;
-                $featureReader = $featureSrvc->SelectFeatures($resId, $qualifiedClassName, null);
-
                 // Calculate total number of entries.
-                while($featureReader->ReadNext())
-                  $totalEntries++;
-                $featureReader->Close();
+                $totalEntries = GetFeatureCount($resId, $schemaName, $className, $resourceSrvc, $featureSrvc);
 
                 $currentPage = ceil(($index+$maxEntries)/$maxEntries);
                 $maxPage = ceil($totalEntries/$maxEntries);

Modified: branches/2.4/MgDev/Web/src/schemareport/showgeom.php
===================================================================
--- branches/2.4/MgDev/Web/src/schemareport/showgeom.php	2012-06-20 11:13:45 UTC (rev 6797)
+++ branches/2.4/MgDev/Web/src/schemareport/showgeom.php	2012-06-21 14:59:13 UTC (rev 6798)
@@ -70,11 +70,10 @@
                 $featuresId = new MgResourceIdentifier($resName);
 
                 $schemaName = substr(strrchr($schemaName, "/"), 1);
-                $featureName = $schemaName . ':' . $className;
-
+                
                 $classDef = $featureSrvc->GetClassDefinition($featuresId, $schemaName, $className);
-                $geomProp = $classDef->GetProperties()->GetItem($geomName);
-                $spatialContext = $geomProp->GetSpatialContextAssociation();
+                $clsProps = $classDef->GetProperties();
+                $geomProp = $clsProps->GetItem($geomName);
 
                 //TODO: Worse case scenario is no provider support for Count() or SpatialExtents()
                 //In such cases, we should piggy-back off of the same raw-spun feature reader to compile the count and MBR
@@ -83,87 +82,9 @@
                 //TODO: Have a way to do this purely mgserver-side so we don't have row transmission overhead by doing the counting
                 //on the web tier
 
-                //Try the SelectAggregate shortcut. This is faster than raw spinning a feature reader
-                //
-                //NOTE: If MapGuide supported scrollable readers like FDO, we'd have also tried 
-                //that as well.
-                $canCount = false;
-                $gotCount = false;
-                $fsBr = $resourceSrvc->GetResourceContent($featuresId);
-                $fsXml = $fsBr->ToString();
-                $fsDoc = DOMDocument::loadXML($fsXml);
-                $providerNodeList = $fsDoc->getElementsByTagName("Provider");
-                $providerName = $providerNodeList->item(0)->nodeValue;
-                $capsBr = $featureSrvc->GetCapabilities($providerName);
-                $capsXml = $capsBr->ToString();
-                //This should be good enough to find out if Count() is supported
-                $canCount = !(strstr($capsXml, "<Name>Count</Name>") === false);
+                $totalEntries = GetFeatureCount($featuresId, $schemaName, $className, $resourceSrvc, $featureSrvc);
+                $featureName = $schemaName . ':' . $className;
                 
-                if ($canCount) {
-                    $clsDef = $featureSrvc->GetClassDefinition($featuresId, $schemaName, $className);
-                    $idProps = $clsDef->GetIdentityProperties();
-                    if ($idProps->GetCount() > 0)
-                    {
-                        $pd = $idProps->GetItem(0);
-                        $expr = "COUNT(" .$pd->GetName(). ")";
-                        $query = new MgFeatureAggregateOptions();
-                        $query->AddComputedProperty("TotalCount", $expr);
-                        try 
-                        {
-                            $dataReader = $featureSrvc->SelectAggregate($featuresId, $featureName, $query);
-                            if ($dataReader->ReadNext())
-                            {
-                                // When there is no data, the property will be null.
-                                if($dataReader->IsNull("TotalCount"))
-                                {
-                                    $totalEntries = 0;
-                                    $gotCount = true;
-                                }
-                                else
-                                {
-                                    $ptype = $dataReader->GetPropertyType("TotalCount");
-                                    switch ($ptype)
-                                    {
-                                        case MgPropertyType::Int32:
-                                            $totalEntries = $dataReader->GetInt32("TotalCount");
-                                            $gotCount = true;
-                                            break;
-                                        case MgPropertyType::Int64:
-                                            $totalEntries = $dataReader->GetInt64("TotalCount");
-                                            $gotCount = true;
-                                            break;
-                                    }
-                                    $dataReader->Close();
-                                }
-                            }
-                        }
-                        catch (MgException $ex) //Some providers like OGR can lie
-                        {
-                            $gotCount = false;
-                        }
-                    }
-                }
-                
-                if ($gotCount == false)
-                {
-                    $featureReader = null;
-                    try 
-                    {
-                        $featureReader = $featureSrvc->SelectFeatures($featuresId, $featureName, null);
-                    }
-                    catch (MgException $ex)
-                    {
-                        $totalEntries = -1; //Can't Count() or raw spin? Oh dear!
-                    }
-                    
-                    if ($featureReader != null)
-                    {
-                        while($featureReader->ReadNext())
-                            $totalEntries++;
-                        $featureReader->Close();
-                    }
-                }
-                
                 // Create a layer definition
                 $layerfactory = new LayerDefinitionFactory();
                 $layerDefinition = CreateLayerDef($layerfactory, $resName, $featureName, $geomName, $geomType);
@@ -175,87 +96,12 @@
                 $resId = new MgResourceIdentifier($resName);
                 $resourceSrvc->SetResource($resId, $byteSource->GetReader(), null);
 
-                $extentGeometryAgg = null;
-                $extentGeometrySc = null;
-                $extentByteReader = null;
-
-                // Finds the coordinate system
-                $agfReaderWriter = new MgAgfReaderWriter();
-                $spatialcontextReader = $featureSrvc->GetSpatialContexts($featuresId, false);
-                while ($spatialcontextReader->ReadNext())
-                {
-                    if ($spatialcontextReader->GetName() == $spatialContext)
-                    {
-                        $coordinate = $spatialcontextReader->GetCoordinateSystemWkt();
-
-                        // Finds the extent
-                        $extentByteReader = $spatialcontextReader->GetExtent();
-                        break;
-                    }
-                }
-                $spatialcontextReader->Close();
-                if ($extentByteReader != null)
-                {
-                    // Get the extent geometry from the spatial context
-                    $extentGeometrySc = $agfReaderWriter->Read($extentByteReader);
-                }
-
-                // Try to get the extents using the selectaggregate as sometimes the spatial context
-                // information is not set
-                $aggregateOptions = new MgFeatureAggregateOptions();
-                $featureProp = 'SPATIALEXTENTS("' . $geomName . '")';
-                $aggregateOptions->AddComputedProperty('EXTENTS', $featureProp);
-
-                try
-                {
-                    $dataReader = $featureSrvc->SelectAggregate($featuresId, $className, $aggregateOptions);
-                    if($dataReader->ReadNext())
-                    {
-                        // Get the extents information
-                        $byteReader = $dataReader->GetGeometry('EXTENTS');
-                        $extentGeometryAgg = $agfReaderWriter->Read($byteReader);
-                    }
-                    $dataReader->Close();
-                }
-                catch (MgException $e)
-                {
-                    if ($extentGeometryAgg == null) 
-                    {
-                        //We do have one last hope. EXTENT() is an internal MapGuide custom function that's universally supported
-                        //as it operates against an underlying select query result. This raw-spins the reader server-side so there
-                        //is no server -> web tier transmission overhead involved.
-                        try
-                        {
-                            $aggregateOptions = new MgFeatureAggregateOptions();
-                            $aggregateOptions->AddComputedProperty("COMP_EXTENT", "EXTENT(".$geomName.")");
-                            
-                            $dataReader = $featureSrvc->SelectAggregate($featuresId, $className, $aggregateOptions);
-                            if($dataReader->ReadNext())
-                            {
-                                // Get the extents information
-                                $byteReader = $dataReader->GetGeometry('COMP_EXTENT');
-                                $extentGeometryAgg = $agfReaderWriter->Read($byteReader);
-                            }
-                            $dataReader->Close();
-                        }
-                        catch (MgException $e2) 
-                        {
-                            
-                        }
-                    }
-                }
-                $extentGeometry = null;
-                // Prefer SpatialExtents() of EXTENT() result over spatial context extent
-                if ($extentGeometryAgg != null)
-                    $extentGeometry = $extentGeometryAgg;
-                if ($extentGeometry == null) { //Stil null? Now try spatial context
-                    if ($extentGeometrySc != null)
-                        $extentGeometry = $extentGeometrySc;
-                    else
-                        throw new Exception(ErrorMessages::NullExtent);
-                }
+                $mbr = GetFeatureClassMBR($featuresId, $className, $geomProp, $featureSrvc);
+                if ($mbr->extentGeometry == null)
+                    throw new Exception(ErrorMessages::NullExtent);
+                
                 // Get the coordinates
-                $iterator = $extentGeometry->GetCoordinates();
+                $iterator = $mbr->extentGeometry->GetCoordinates();
                 while($iterator->MoveNext())
                 {
                     $x = $iterator->GetCurrent()->GetX();
@@ -280,7 +126,7 @@
 
                 // Create a map definition
                 $mapfactory = new MapDefinitionFactory();
-                $mapDefinition = CreateMapDef($mapfactory, $className, $resName, $coordinate, $minX, $maxX, $minY, $maxY);
+                $mapDefinition = CreateMapDef($mapfactory, $className, $resName, $mbr->coordinateSystem, $minX, $maxX, $minY, $maxY);
 
                 // Save the map definition to a resource stored in the session repository
                 $byteSource = new MgByteSource($mapDefinition, strlen($mapDefinition));



More information about the mapguide-commits mailing list