示例#1
0
 /**
  * Queries the configured feature source and returns a MgReader based on the current GET query parameters and adapter configuration
  */
 protected function CreateQueryOptions($single)
 {
     $query = new MgFeatureQueryOptions();
     $this->EnsureQualifiedClassName();
     $tokens = explode(":", $this->className);
     $clsDef = null;
     $clsDef = $this->featSvc->GetClassDefinition($this->featureSourceId, $tokens[0], $tokens[1]);
     if ($single === true) {
         if ($this->featureId == null) {
             throw new Exception($this->app->localizer->getText("E_NO_FEATURE_ID_SET"));
         }
         $idType = MgPropertyType::String;
         if ($this->featureIdProp == null) {
             $idProps = $clsDef->GetIdentityProperties();
             if ($idProps->GetCount() == 0) {
                 throw new Exception($this->app->localizer->getText("E_CANNOT_QUERY_NO_ID_PROPS", $this->className, $this->featureSourceId->ToString()));
             } else {
                 if ($idProps->GetCount() > 1) {
                     throw new Exception($this->app->localizer->getText("E_CANNOT_QUERY_MULTIPLE_ID_PROPS", $this->className, $this->featureSourceId->ToString()));
                 } else {
                     $idProp = $idProps->GetItem(0);
                     $this->featureIdProp = $idProp->GetName();
                     $idType = $idProp->GetDataType();
                 }
             }
         } else {
             $props = $clsDef->GetProperties();
             $iidx = $props->IndexOf($this->featureIdProp);
             if ($iidx >= 0) {
                 $propDef = $props->GetItem($iidx);
                 if ($propDef->GetPropertyType() != MgFeaturePropertyType::DataProperty) {
                     throw new Exception($this->app->localizer->getText("E_ID_PROP_NOT_DATA", $this->featureIdProp));
                 }
             } else {
                 throw new Exception($this->app->localizer->getText("E_ID_PROP_NOT_FOUND", $this->featureIdProp));
             }
         }
         if ($idType == MgPropertyType::String) {
             $query->SetFilter($this->featureIdProp . " = '" . $this->featureId . "'");
         } else {
             $query->SetFilter($this->featureIdProp . " = " . $this->featureId);
         }
     } else {
         $flt = $this->app->request->get("filter");
         if ($flt != null) {
             $query->SetFilter($flt);
         }
         $bbox = $this->app->request->get("bbox");
         if ($bbox != null) {
             $parts = explode(",", $bbox);
             if (count($parts) == 4) {
                 $wktRw = new MgWktReaderWriter();
                 $geom = $wktRw->Read(MgUtils::MakeWktPolygon($parts[0], $parts[1], $parts[2], $parts[3]));
                 $query->SetSpatialFilter($clsDef->GetDefaultGeometryPropertyName(), $geom, MgFeatureSpatialOperations::EnvelopeIntersects);
             }
         }
     }
     if (isset($this->app->ComputedProperties)) {
         $compProps = $this->app->ComputedProperties;
         foreach ($compProps as $alias => $expression) {
             $this->computedPropertyList[$alias] = $expression;
         }
     }
     $bAppliedComputedProperties = false;
     if (count($this->computedPropertyList) > 0) {
         foreach ($this->computedPropertyList as $alias => $expression) {
             $query->AddComputedProperty($alias, $expression);
             $bAppliedComputedProperties = true;
         }
     }
     //If computed properties were applied, add all properties from the class definition if no
     //explicit property list supplied
     if ($bAppliedComputedProperties && count($this->propertyList) == 0) {
         $clsProps = $clsDef->GetProperties();
         for ($i = 0; $i < $clsProps->GetCount(); $i++) {
             $propDef = $clsProps->GetItem($i);
             $query->AddFeatureProperty($propDef->GetName());
         }
     } else {
         if (count($this->propertyList) > 0) {
             foreach ($this->propertyList as $propName) {
                 $query->AddFeatureProperty($propName);
             }
         }
     }
     $orderby = $this->app->request->get("orderby");
     $orderOptions = $this->app->request->get("orderoption");
     if ($orderby != null) {
         if ($orderOptions == null) {
             $orderOptions = "asc";
         }
         $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);
     }
     return $query;
 }
 public function SelectLayerFeatures($ldfId, $format)
 {
     //Check for unsupported representations
     $fmt = $this->ValidateRepresentation($format, array("xml", "geojson", "html", "czml"));
     $mimeType = $this->GetMimeTypeForFormat($fmt);
     try {
         $sessionId = "";
         if ($ldfId->GetRepositoryType() == MgRepositoryType::Session) {
             $sessionId = $ldfId->GetRepositoryName();
         }
         $this->EnsureAuthenticationForSite($sessionId, true, $mimeType);
         $siteConn = new MgSiteConnection();
         $siteConn->Open($this->userInfo);
         $featSvc = $siteConn->CreateService(MgServiceType::FeatureService);
         $query = new MgFeatureQueryOptions();
         $propList = $this->GetRequestParameter("properties", "");
         $filter = $this->GetRequestParameter("filter", "");
         $orderby = $this->GetRequestParameter("orderby", "");
         $orderOptions = $this->GetRequestParameter("orderoption", "");
         $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);
         } else {
             //The way that CZML output is done means we cannot support pagination
             if ($pageNo >= 0 && $pageSize > 0 && $fmt === "czml") {
                 $this->BadRequest($this->app->localizer->getText("E_CZML_PAGINATION_NOT_SUPPORTED"), $mimeType);
             }
         }
         $limit = -1;
         if ($maxFeatures !== "") {
             $limit = intval($maxFeatures);
         }
         //Load the Layer Definition document and extract the relevant bits of information
         //we're interested in
         $resSvc = $siteConn->CreateService(MgServiceType::ResourceService);
         $ldfContent = $resSvc->GetResourceContent($ldfId);
         $doc = new DOMDocument();
         $doc->loadXML($ldfContent->ToString());
         $vl = $doc->getElementsByTagName("VectorLayerDefinition");
         if ($vl->length == 1) {
             $vlNode = $vl->item(0);
             $fsId = $vlNode->getElementsByTagName("ResourceId");
             $fc = $vlNode->getElementsByTagName("FeatureName");
             $hlink = $vlNode->getElementsByTagName("Url");
             $tt = $vlNode->getElementsByTagName("ToolTip");
             $flt = $vlNode->getElementsByTagName("Filter");
             $elev = $vlNode->getElementsByTagName("ElevationSettings");
             if ($fsId->length == 1) {
                 $fsId = new MgResourceIdentifier($fsId->item(0)->nodeValue);
                 $site = $siteConn->GetSite();
                 $this->VerifyWhitelist($fsId->ToString(), $this->GetMimeTypeForFormat($fmt), "SELECTFEATURES", $fmt, $site, $this->userName);
                 if ($fc->length == 1) {
                     //Add hyperlink, tooltip and elevation as special computed properties
                     if ($hlink->length == 1 && strlen($hlink->item(0)->nodeValue) > 0) {
                         $query->AddComputedProperty(MgRestConstants::PROP_HYPERLINK, $hlink->item(0)->nodeValue);
                     }
                     if ($tt->length == 1 && strlen($tt->item(0)->nodeValue) > 0) {
                         $query->AddComputedProperty(MgRestConstants::PROP_TOOLTIP, $tt->item(0)->nodeValue);
                     }
                     if ($elev->length == 1) {
                         $elevNode = $elev->item(0);
                         $zoff = $elevNode->getElementsByTagName("ZOffset");
                         $zofftype = $elevNode->getElementsByTagName("ZOffsetType");
                         $zext = $elevNode->getElementsByTagName("ZExtrusion");
                         $unit = $elevNode->getElementsByTagName("Unit");
                         if ($zoff->length == 1 && strlen($zoff->item(0)->nodeValue) > 0) {
                             $query->AddComputedProperty(MgRestConstants::PROP_Z_OFFSET, $zoff->item(0)->nodeValue);
                         } else {
                             $query->AddComputedProperty(MgRestConstants::PROP_Z_OFFSET, "0");
                         }
                         if ($zofftype->length == 1 && strlen($zofftype->item(0)->nodeValue) > 0) {
                             $query->AddComputedProperty(MgRestConstants::PROP_Z_OFFSET_TYPE, "'" . $zofftype->item(0)->nodeValue . "'");
                         } else {
                             $query->AddComputedProperty(MgRestConstants::PROP_Z_OFFSET_TYPE, "'RelativeToGround'");
                         }
                         if ($zext->length == 1 && strlen($zext->item(0)->nodeValue) > 0) {
                             $query->AddComputedProperty(MgRestConstants::PROP_Z_EXTRUSION, $zext->item(0)->nodeValue);
                         } else {
                             $query->AddComputedProperty(MgRestConstants::PROP_Z_EXTRUSION, "0");
                         }
                         if ($unit->length == 1 && strlen($unit->item(0)->nodeValue) > 0) {
                             $query->AddComputedProperty(MgRestConstants::PROP_Z_UNITS, "'" . $unit->item(0)->nodeValue . "'");
                         } else {
                             $query->AddComputedProperty(MgRestConstants::PROP_Z_UNITS, "'Meters'");
                         }
                     }
                     $baseFilter = "";
                     //Set filter from layer if defined
                     if ($flt->length == 1 && strlen($flt->item(0)->nodeValue) > 0) {
                         if ($filter !== "") {
                             //logical AND with the layer's filter to combine them
                             $baseFilter = "(" . $flt->item(0)->nodeValue . ") AND (" . $filter . ")";
                             $query->SetFilter($baseFilter);
                         } else {
                             $baseFilter = $flt->item(0)->nodeValue;
                             $query->SetFilter($baseFilter);
                         }
                     } else {
                         if ($filter !== "") {
                             $baseFilter = $filter;
                             $query->SetFilter($baseFilter);
                         }
                     }
                     $tokens = explode(":", $fc->item(0)->nodeValue);
                     $schemaName = $tokens[0];
                     $className = $tokens[1];
                     $clsDef = NULL;
                     //Unless an explicit property list has been specified, we're explicitly adding all properties
                     //from the class definition
                     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);
                         }
                     } else {
                         if ($clsDef == NULL) {
                             $clsDef = $featSvc->GetClassDefinition($fsId, $schemaName, $className);
                         }
                         $clsProps = $clsDef->GetProperties();
                         for ($i = 0; $i < $clsProps->GetCount(); $i++) {
                             $propDef = $clsProps->GetItem($i);
                             $query->AddFeatureProperty($propDef->GetName());
                         }
                     }
                     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);
                     }
                     //We must require features as LL84 for CZML output
                     if ($fmt == "czml") {
                         $transformto = "LL84";
                     }
                     $transform = null;
                     if ($transformto !== "") {
                         $transform = MgUtils::GetTransform($featSvc, $fsId, $schemaName, $className, $transformto);
                     }
                     if ($bbox !== "") {
                         $parts = explode(",", $bbox);
                         if (count($parts) == 4) {
                             $wktRw = new MgWktReaderWriter();
                             if ($clsDef == NULL) {
                                 $clsDef = $featSvc->GetClassDefinition($fsId, $schemaName, $className);
                             }
                             $geom = $wktRw->Read(MgUtils::MakeWktPolygon($parts[0], $parts[1], $parts[2], $parts[3]));
                             //Transform bbox to target cs if flag specified
                             $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, $fsId, $schemaName, $className, $transformto, true);
                                 $geom = $geom->Transform($invTx);
                             }
                             $query->SetSpatialFilter($clsDef->GetDefaultGeometryPropertyName(), $geom, MgFeatureSpatialOperations::EnvelopeIntersects);
                         }
                     }
                     //Ensure valid page number if specified
                     if ($pageSize > 0) {
                         if ($pageNo < 1) {
                             $pageNo = 1;
                         }
                     }
                     $owriter = null;
                     if ($chunk === "0") {
                         $owriter = new MgSlimChunkWriter($this->app);
                     } else {
                         $owriter = new MgHttpChunkWriter();
                     }
                     if ($fmt == "czml") {
                         $result = new MgCzmlResult($featSvc, $fsId, "{$schemaName}:{$className}", $query, $limit, $baseFilter, $vlNode, $owriter);
                         $result->CheckAndSetDownloadHeaders($this->app, $format);
                         if ($transform != null) {
                             $result->SetTransform($transform);
                         }
                         $result->Output($format);
                     } else {
                         $reader = $featSvc->SelectFeatures($fsId, "{$schemaName}:{$className}", $query);
                         if ($pageSize > 0) {
                             $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);
                     }
                 } else {
                     throw new Exception($this->app->localizer->getText("E_LAYER_HAS_INVALID_FEATURE_CLASS", $ldfId->ToString()));
                 }
             } else {
                 throw new Exception($this->app->localizer->getText("E_LAYER_HAS_INVALID_FEATURE_SOURCE", $ldfId->ToString()));
             }
         }
     } catch (MgException $ex) {
         $this->OnException($ex, $mimeType);
     }
 }
 private function PutVectorTileXYZ($map, $groupName, $siteConn, $metersPerUnit, $csFactory, $path, $boundsMinx, $boundsMinY, $boundsMaxX, $boundsMaxY, $layerNames)
 {
     $wktRw = new MgWktReaderWriter();
     $agfRw = new MgAgfReaderWriter();
     $resSvc = $siteConn->CreateService(MgServiceType::ResourceService);
     $featSvc = $siteConn->CreateService(MgServiceType::FeatureService);
     $mapCsWkt = $map->GetMapSRS();
     $layers = $map->GetLayers();
     $groups = $map->GetLayerGroups();
     $baseGroup = $groups->GetItem($groupName);
     $layerCount = $layers->GetCount();
     $firstFeature = true;
     $scale = self::GetScaleFromBounds($map, self::XYZ_TILE_WIDTH, self::XYZ_TILE_HEIGHT, $metersPerUnit, $boundsMinx, $boundsMinY, $boundsMaxX, $boundsMaxY);
     $fp = fopen($path, "w");
     fwrite($fp, '{ "type": "FeatureCollection", "features": [');
     for ($i = 0; $i < $layerCount; $i++) {
         $layer = $layers->GetItem($i);
         $parentGroup = $layer->GetGroup();
         if ($parentGroup != null && $parentGroup->GetObjectId() == $baseGroup->GetObjectId()) {
             if (!self::IsLayerVisibleAtScale($layer, $resSvc, $scale)) {
                 continue;
             }
             //If list of layer names specified, skip if this layer is not in that list
             if ($layerNames != null) {
                 $bFound = false;
                 foreach ($layerNames as $layerName) {
                     if ($layer->GetName() == $layerName) {
                         $bFound = true;
                         break;
                     }
                 }
                 if (!$bFound) {
                     continue;
                 }
             }
             $wktPoly = MgUtils::MakeWktPolygon($boundsMinx, $boundsMinY, $boundsMaxX, $boundsMaxY);
             $geom = $wktRw->Read($wktPoly);
             $clsDef = $layer->GetClassDefinition();
             $clsProps = $clsDef->GetProperties();
             $idProps = $clsDef->GetIdentityProperties();
             $idName = NULL;
             if ($idProps->GetCount() == 1) {
                 $idp = $idProps->GetItem(0);
                 $idName = $idp->GetName();
             }
             $fsId = new MgResourceIdentifier($layer->GetFeatureSourceId());
             //Set up forward and inverse transforms. Inverse for transforming map bounding box
             //Forward for transforming source geometries to map space
             $xform = self::GetTransform($featSvc, $fsId, $clsDef, $mapCsWkt, $csFactory);
             $query = new MgFeatureQueryOptions();
             $geomName = $layer->GetFeatureGeometryName();
             if ($xform != null) {
                 $sourceCs = $xform->GetSource();
                 $targetCs = $xform->GetTarget();
                 $invXform = $csFactory->GetTransform($targetCs, $sourceCs);
                 $txgeom = $geom->Transform($invXform);
                 $query->SetSpatialFilter($geomName, $txgeom, MgFeatureSpatialOperations::EnvelopeIntersects);
             } else {
                 $query->SetSpatialFilter($geomName, $geom, MgFeatureSpatialOperations::EnvelopeIntersects);
             }
             for ($p = 0; $p < $clsProps->GetCount(); $p++) {
                 $propDef = $clsProps->GetItem($p);
                 $query->AddFeatureProperty($propDef->GetName());
             }
             //If we're rendering a vector tile for a single layer, we don't need these special attributes
             if ($layerNames == NULL || count($layerNames) > 1) {
                 $query->AddComputedProperty("_displayIndex", $layerCount - $i);
                 $query->AddComputedProperty("_layer", "'" . $layer->GetName() . "'");
                 $query->AddComputedProperty("_selectable", $layer->GetSelectable() ? "true" : "false");
             }
             $reader = $layer->SelectFeatures($query);
             $read = 0;
             while ($reader->ReadNext()) {
                 $read++;
                 if (!$reader->IsNull($geomName)) {
                     if (!$firstFeature) {
                         fwrite($fp, ",");
                     }
                     try {
                         $output = MgGeoJsonWriter::FeatureToGeoJson($reader, $agfRw, $xform, $idName);
                         fwrite($fp, $output);
                         $firstFeature = false;
                     } catch (MgException $ex) {
                     }
                 }
             }
             $reader->Close();
         }
     }
     fwrite($fp, ']}');
     fclose($fp);
     return $path;
 }