public function SelectFeatures($resId, $schemaName, $className, $format)
 {
     //Check for unsupported representations
     $fmt = $this->ValidateRepresentation($format, array("xml", "html", "geojson"));
     $mimeType = $this->GetMimeTypeForFormat($fmt);
     try {
         $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, "SELECTFEATURES", $fmt, $site, $this->userName);
         $featSvc = $siteConn->CreateService(MgServiceType::FeatureService);
         $query = new MgFeatureQueryOptions();
         $filter = $this->GetRequestParameter("filter", "");
         $propList = $this->GetRequestParameter("properties", "");
         $orderby = $this->GetRequestParameter("orderby", "");
         $orderOptions = $this->GetRequestParameter("orderoption", "asc");
         $maxFeatures = $this->GetRequestParameter("maxfeatures", "");
         $transformto = $this->GetRequestParameter("transformto", "");
         $bbox = $this->GetRequestParameter("bbox", "");
         $pageSize = $this->GetRequestParameter("pagesize", -1);
         $pageNo = $this->GetRequestParameter("page", -1);
         //Internal debugging flag
         $chunk = $this->GetBooleanRequestParameter("chunk", true);
         if ($pageNo >= 0 && $pageSize === -1) {
             $this->BadRequest($this->app->localizer->getText("E_MISSING_REQUIRED_PARAMETER", "pagesize"), $mimeType);
         }
         if ($filter !== "") {
             $query->SetFilter($filter);
         }
         $limit = -1;
         if ($maxFeatures !== "") {
             $limit = intval($maxFeatures);
         }
         if ($propList !== "") {
             $propNames = explode(",", $propList);
             //If you have a comma in your property names, it's your own fault :)
             foreach ($propNames as $propName) {
                 $query->AddFeatureProperty($propName);
             }
         }
         if ($orderby !== "") {
             $orderPropNames = explode(",", $orderby);
             //If you have a comma in your property names, it's your own fault :)
             $orderProps = new MgStringCollection();
             foreach ($orderPropNames as $propName) {
                 $orderProps->Add($propName);
             }
             $orderOpt = MgOrderingOption::Ascending;
             if (strtolower($orderOptions) === "desc") {
                 $orderOpt = MgOrderingOption::Descending;
             }
             $query->SetOrderingFilter($orderProps, $orderOpt);
         }
         $transform = null;
         if ($transformto !== "") {
             $transform = MgUtils::GetTransform($featSvc, $resId, $schemaName, $className, $transformto);
         }
         if ($bbox !== "") {
             $parts = explode(",", $bbox);
             if (count($parts) == 4) {
                 $wktRw = new MgWktReaderWriter();
                 $geom = $wktRw->Read(MgUtils::MakeWktPolygon($parts[0], $parts[1], $parts[2], $parts[3]));
                 //Transform the bbox if we have the flag indicating so
                 $bboxIsTargetCs = $this->GetBooleanRequestParameter("bboxistargetcs", false);
                 if ($bboxIsTargetCs) {
                     //Because it has been declared the bbox is in target coordiantes, we have to transform that bbox back to the
                     //source, which means we need an inverse transform
                     $invTx = MgUtils::GetTransform($featSvc, $resId, $schemaName, $className, $transformto, true);
                     $geom = $geom->Transform($invTx);
                 }
                 $clsDef = $featSvc->GetClassDefinition($resId, $schemaName, $className);
                 $query->SetSpatialFilter($clsDef->GetDefaultGeometryPropertyName(), $geom, MgFeatureSpatialOperations::EnvelopeIntersects);
             }
         }
         $reader = $featSvc->SelectFeatures($resId, "{$schemaName}:{$className}", $query);
         $owriter = null;
         if ($chunk === "0") {
             $owriter = new MgSlimChunkWriter($this->app);
         } else {
             $owriter = new MgHttpChunkWriter();
         }
         if ($pageSize > 0) {
             if ($pageNo < 1) {
                 $pageNo = 1;
             }
             $pageReader = new MgPaginatedFeatureReader($reader, $pageSize, $pageNo, $limit);
             $result = new MgReaderChunkedResult($featSvc, $pageReader, $limit, $owriter, $this->app->localizer);
         } else {
             $result = new MgReaderChunkedResult($featSvc, $reader, $limit, $owriter, $this->app->localizer);
         }
         $result->CheckAndSetDownloadHeaders($this->app, $format);
         if ($transform != null) {
             $result->SetTransform($transform);
         }
         if ($fmt === "html") {
             $result->SetHtmlParams($this->app);
         }
         $result->Output($format);
     } catch (MgException $ex) {
         $this->OnException($ex, $mimeType);
     }
 }
Example #2
0
 public function GetSelectedFeatures($sessionId, $mapName, $layerName, $format)
 {
     $fmt = $this->ValidateRepresentation($format, array("xml", "geojson", "html"));
     $propList = $this->GetRequestParameter("properties", "");
     $pageSize = $this->GetRequestParameter("pagesize", -1);
     $pageNo = $this->GetRequestParameter("page", -1);
     $orientation = $this->GetRequestParameter("orientation", "h");
     //Internal debugging flag
     $chunk = $this->GetBooleanRequestParameter("chunk", true);
     try {
         $this->EnsureAuthenticationForSite($sessionId);
         $siteConn = new MgSiteConnection();
         $siteConn->Open($this->userInfo);
         $resSvc = $siteConn->CreateService(MgServiceType::ResourceService);
         $featSvc = $siteConn->CreateService(MgServiceType::FeatureService);
         $map = new MgMap($siteConn);
         $map->Open($mapName);
         $selection = new MgSelection($map);
         $selection->Open($resSvc, $mapName);
         $layers = $selection->GetLayers();
         if ($layers != null) {
             $lidx = -1;
             $layerCount = $layers->GetCount();
             for ($i = 0; $i < $layerCount; $i++) {
                 $currentlayer = $layers->GetItem($i);
                 if ($currentlayer->GetName() == $layerName) {
                     $lidx = $i;
                     break;
                 }
             }
             if ($lidx < 0) {
                 $this->NotFound($this->app->localizer->getText("E_LAYER_NOT_IN_SELECTION", $layerName), $this->GetMimeTypeForFormat($fmt));
             } else {
                 $layer = $layers->GetItem($lidx);
                 $bMapped = $this->GetBooleanRequestParameter("mappedonly", "0") == "1";
                 $transformto = $this->GetRequestParameter("transformto", "");
                 $transform = null;
                 if ($transformto !== "") {
                     $resId = new MgResourceIdentifier($layer->GetFeatureSourceId());
                     $tokens = explode(":", $layer->GetFeatureClassName());
                     $transform = MgUtils::GetTransform($featSvc, $resId, $tokens[0], $tokens[1], $transformto);
                 }
                 $owriter = null;
                 if ($chunk === "0") {
                     $owriter = new MgSlimChunkWriter($this->app);
                 } else {
                     $owriter = new MgHttpChunkWriter();
                 }
                 //NOTE: This does not do a query to ascertain a total, this is already a pre-computed property of the selection set.
                 $total = $selection->GetSelectedFeaturesCount($layer, $layer->GetFeatureClassName());
                 if (strlen($propList) > 0) {
                     $tokens = explode(",", $propList);
                     $propNames = new MgStringCollection();
                     foreach ($tokens as $propName) {
                         $propNames->Add($propName);
                     }
                     $reader = $selection->GetSelectedFeatures($layer, $layer->GetFeatureClassName(), $propNames);
                 } else {
                     $reader = $selection->GetSelectedFeatures($layer, $layer->GetFeatureClassName(), $bMapped);
                 }
                 if ($pageSize > 0) {
                     $pageReader = new MgPaginatedFeatureReader($reader, $pageSize, $pageNo, $total);
                     $result = new MgReaderChunkedResult($featSvc, $pageReader, -1, $owriter, $this->app->localizer);
                 } else {
                     $result = new MgReaderChunkedResult($featSvc, $reader, -1, $owriter, $this->app->localizer);
                 }
                 $result->CheckAndSetDownloadHeaders($this->app, $format);
                 if ($transform != null) {
                     $result->SetTransform($transform);
                 }
                 if ($fmt === "html") {
                     $result->SetAttributeDisplayOrientation($orientation);
                     $result->SetHtmlParams($this->app);
                 }
                 $result->Output($format);
             }
         } else {
             $owriter = new MgHttpChunkWriter();
             $reader = new MgNullFeatureReader();
             $result = new MgReaderChunkedResult($featSvc, $reader, -1, $owriter, $this->app->localizer);
             if ($fmt === "html") {
                 $result->SetAttributeDisplayOrientation($orientation);
                 $result->SetHtmlParams($this->app);
             }
             $result->Output($format);
         }
     } catch (MgException $ex) {
         $this->OnException($ex, $this->GetMimeTypeForFormat($format));
     }
 }