public function SelectAggregates($resId, $schemaName, $className, $type, $format)
 {
     //Check for unsupported representations
     $fmt = $this->ValidateRepresentation($format, array("xml", "json"));
     $mimeType = $this->GetMimeTypeForFormat($fmt);
     try {
         $aggType = $this->ValidateValueInDomain($type, array("count", "bbox", "distinctvalues"), $this->GetMimeTypeForFormat($format));
         $distinctPropName = $this->GetRequestParameter("property", "");
         if ($aggType === "distinctvalues" && $distinctPropName === "") {
             $this->BadRequest($this->app->localizer->getText("E_MISSING_REQUIRED_PARAMETER", "property"), $this->GetMimeTypeForFormat($format));
         }
         $sessionId = "";
         if ($resId->GetRepositoryType() == MgRepositoryType::Session) {
             $sessionId = $resId->GetRepositoryName();
         }
         $this->EnsureAuthenticationForSite($sessionId, true, $mimeType);
         $siteConn = new MgSiteConnection();
         $siteConn->Open($this->userInfo);
         $site = $siteConn->GetSite();
         $this->VerifyWhitelist($resId->ToString(), $mimeType, "SELECTAGGREGATES", $fmt, $site, $this->userName);
         $resSvc = $siteConn->CreateService(MgServiceType::ResourceService);
         $featSvc = $siteConn->CreateService(MgServiceType::FeatureService);
         $query = new MgFeatureAggregateOptions();
         $capsXml = MgUtils::GetProviderCapabilties($featSvc, $resSvc, $resId);
         $supportsDistinct = !(strstr($capsXml, "<SupportsSelectDistinct>true</SupportsSelectDistinct>") === false);
         $supportsCount = !(strstr($capsXml, "<Name>Count</Name>") === false);
         $supportsSpatialExtents = !(strstr($capsXml, "<Name>SpatialExtents</Name>") === false);
         switch ($type) {
             case "count":
                 $count = MgUtils::GetFeatureCount($featSvc, $resId, $schemaName, $className, $supportsCount);
                 $output = "<?xml version=\"1.0\" encoding=\"utf-8\"?><AggregateResult>";
                 $output .= "<Type>count</Type>";
                 $output .= "<Total>{$count}</Total>";
                 $output .= "</AggregateResult>";
                 $bs = new MgByteSource($output, strlen($output));
                 $bs->SetMimeType(MgMimeType::Xml);
                 $br = $bs->GetReader();
                 if ($fmt === "json") {
                     $this->OutputXmlByteReaderAsJson($br);
                 } else {
                     $this->OutputByteReader($br);
                 }
                 break;
             case "bbox":
                 $geomName = $this->app->request->get("property");
                 $txTo = $this->app->request->get("transformto");
                 $bounds = MgUtils::GetFeatureClassMBR($this->app, $featSvc, $resId, $schemaName, $className, $geomName, $txTo);
                 $iterator = $bounds->extentGeometry->GetCoordinates();
                 $csCode = $bounds->csCode;
                 $csWkt = $bounds->coordinateSystem;
                 $epsg = $bounds->epsg;
                 $firstTime = true;
                 $minX = null;
                 $minY = null;
                 $maxX = null;
                 $maxY = null;
                 while ($iterator->MoveNext()) {
                     $x = $iterator->GetCurrent()->GetX();
                     $y = $iterator->GetCurrent()->GetY();
                     if ($firstTime) {
                         $maxX = $x;
                         $minX = $x;
                         $maxY = $y;
                         $minY = $y;
                         $firstTime = false;
                     }
                     if ($maxX < $x) {
                         $maxX = $x;
                     }
                     if ($minX > $x || $minX == 0) {
                         $minX = $x;
                     }
                     if ($maxY < $y) {
                         $maxY = $y;
                     }
                     if ($minY > $y || $minY == 0) {
                         $minY = $y;
                     }
                 }
                 $output = "<?xml version=\"1.0\" encoding=\"utf-8\"?><AggregateResult>";
                 $output .= "<Type>bbox</Type>";
                 $output .= "<BoundingBox>";
                 $output .= "<CoordinateSystem>";
                 $output .= "<Code>{$csCode}</Code><EPSG>{$epsg}</EPSG>";
                 $output .= "</CoordinateSystem>";
                 $output .= "<LowerLeft><X>{$minX}</X><Y>{$minY}</Y></LowerLeft>";
                 $output .= "<UpperRight><X>{$maxX}</X><Y>{$maxY}</Y></UpperRight>";
                 $output .= "</BoundingBox>";
                 $output .= "</AggregateResult>";
                 $bs = new MgByteSource($output, strlen($output));
                 $bs->SetMimeType(MgMimeType::Xml);
                 $br = $bs->GetReader();
                 if ($fmt === "json") {
                     $this->OutputXmlByteReaderAsJson($br);
                 } else {
                     $this->OutputByteReader($br);
                 }
                 break;
             case "distinctvalues":
                 $values = MgUtils::GetDistinctValues($featSvc, $resId, $schemaName, $className, $distinctPropName);
                 $output = "<?xml version=\"1.0\" encoding=\"utf-8\"?><AggregateResult>";
                 $output .= "<Type>distinctvalues</Type>";
                 $output .= "<ValueList>";
                 foreach ($values as $val) {
                     $output .= "<Value>" . MgUtils::EscapeXmlChars($val) . "</Value>";
                 }
                 $output .= "</ValueList>";
                 $output .= "</AggregateResult>";
                 $bs = new MgByteSource($output, strlen($output));
                 $bs->SetMimeType(MgMimeType::Xml);
                 $br = $bs->GetReader();
                 if ($fmt === "json") {
                     $this->OutputXmlByteReaderAsJson($br);
                 } else {
                     $this->OutputByteReader($br);
                 }
                 break;
         }
     } catch (MgException $ex) {
         $this->OnException($ex, $mimeType);
     }
 }