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);
     }
 }
Exemplo n.º 2
0
 }
 /* get the feature source from the layer */
 $featureResId = new MgResourceIdentifier($layerObj->GetFeatureSourceId());
 $featureGeometryName = $layerObj->GetFeatureGeometryName();
 $queryOptions = new MgFeatureQueryOptions();
 /* the class that is used for this layer will be used to select
    features */
 $class = $layerObj->GetFeatureClassName();
 //only retrieve properties that we actually need
 $mappings = $_SESSION['property_mappings'][$layerObj->GetObjectId()];
 //TODO : seems that property mapping breaks the selection ????
 //could it be that $selection->AddFeatures($layerObj, $featureReader, 0) is
 //the one causing a problem when the properies are limited ?
 if (0 && count($mappings) > 0) {
     foreach ($mappings as $name => $value) {
         $queryOptions->AddFeatureProperty($name);
         //echo "$name $value <br>\n";
     }
     $geomName = $layerObj->GetFeatureGeometryName();
     $queryOptions->AddFeatureProperty($geomName);
 }
 //get the layer filter value if supplied
 $layerReader = $resourceService->GetResourceContent($layerObj->GetLayerDefinition());
 $xmldoc = new DOMDocument();
 $xmldoc->loadXML(ByteReaderToString($layerReader));
 $xpathObj = new domXPath($xmldoc);
 $xpathStr = "/LayerDefinition/VectorLayerDefinition/Filter";
 //TODO: need this for DWF or Grid layers?
 $xpathQuery = $xpathObj->query($xpathStr);
 $layerFilter = '';
 if ($xpathQuery->length > 0) {
Exemplo n.º 3
0
$cmdColl = new MgFeatureCommandCollection();
$cmdColl->Add($cmd);
try {
    $svc->CreateFeatureSource($id, $params);
    $startTime = microtime(true);
    $props = $svc->UpdateFeatures($id, $cmdColl, false);
    $endTime = microtime(true);
    $diffTime = $endTime - $startTime;
    $reader = $props->GetItem(0)->GetValue();
    while ($reader->ReadNext() == true) {
        $key = $reader->GetInt32("KEY");
    }
    $reader->Close();
    // Now select a few of the feature and make sure we get everything back correctly.
    $query = new MgFeatureQueryOptions();
    $query->AddFeatureProperty("NAME");
    $query->AddFeatureProperty("KEY");
    $query->AddFeatureProperty("GEOM");
    $query->SetFilter("");
    $reader = $svc->SelectFeatures($id, "Buffer", $query);
    while ($reader->ReadNext() == true) {
        $geomText = '';
        $agfStream = $reader->GetGeometry("GEOM");
        $geom = $agf->Read($agfStream);
        $geomText = $wkt->Write($geom);
        echo $reader->GetInt32("KEY") . " " . $reader->GetString("NAME") . " " . $geomText . "\n";
    }
    echo "Time for " . $i . " ops was " . $diffTime . " seconds\n";
    echo "Average throughput of " . $i / $diffTime . " features per second\n";
} catch (MgException $exc) {
    echo "First failure on insert... caught...\n";
Exemplo n.º 4
0
 function GetPropertyMinMaxCount()
 {
     $resourceService = $this->site->CreateService(MgServiceType::ResourceService);
     $map = new MgMap();
     $map->Open($resourceService, $this->args['MAPNAME']);
     $layer = $map->GetLayers()->GetItem($this->args['LAYERNAME']);
     $featureService = $this->site->CreateService(MgServiceType::FeatureService);
     $resId = new MgResourceIdentifier($layer->GetFeatureSourceId());
     // Note: Should be able to do this:
     //
     //		$minValue = 0;
     //		$maxValue = 0;
     //		$count = 0;
     //
     //		$aggregateOptions = new MgFeatureAggregateOptions();
     //		$aggregateOptions->AddComputedProperty('min', 'Min(' . $this->args['PROPERTYNAME'] . ')');
     //		$aggregateOptions->AddComputedProperty('max', 'Max(' . $this->args['PROPERTYNAME'] . ')');
     //		$aggregateOptions->AddComputedProperty('count', 'Count(' . $this->args['PROPERTYNAME'] . ')');
     //
     //		$dataReader = $featureService->SelectAggregate($resId, $layer->GetFeatureClassName(), $aggregateOptions);
     //
     //		if ($dataReader->ReadNext())
     //		{
     //			$minValue = $this->GetFeaturePropertyValue($dataReader, 'min');
     //			$maxValue = $this->GetFeaturePropertyValue($dataReader, 'max');
     //			$count = $this->GetFeaturePropertyValue($dataReader, 'count');
     //		}
     //		$dataReader->Close();
     //
     // However due to a bug, Min/Max do not work on string types, so we have to do this:
     unset($minValue);
     unset($maxValue);
     $count = 0;
     $queryOptions = new MgFeatureQueryOptions();
     $queryOptions->AddFeatureProperty($this->args['PROPERTYNAME']);
     $featureReader = $featureService->SelectFeatures($resId, $layer->GetFeatureClassName(), $queryOptions);
     while ($featureReader->ReadNext()) {
         $value = $this->GetFeaturePropertyValue($featureReader, $this->args['PROPERTYNAME']);
         if ($value != null) {
             if (!isset($maxValue) && !isset($minValue)) {
                 $maxValue = $value;
                 $minValue = $value;
             }
             if ($value > $maxValue) {
                 $maxValue = $value;
             }
             if ($value < $minValue) {
                 $minValue = $value;
             }
         }
         $count++;
     }
     $featureReader->Close();
     if (!isset($maxValue) && !isset($minValue)) {
         $maxValue = "";
         $minValue = "";
     }
     return array($minValue, $maxValue, $count);
 }
Exemplo n.º 5
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;
 }
Exemplo n.º 6
0
 function SelectFeatures($paramSet)
 {
     try {
         $repId = null;
         $this->unitTestParamVm->Execute("Select ParamValue from Params WHERE ParamSet={$paramSet} AND ParamName=\"RESOURCEID\"");
         $this->arrayParam["RESOURCEID"] = $this->unitTestParamVm->GetString("ParamValue");
         $this->unitTestParamVm->Execute("Select ParamValue from Params WHERE ParamSet={$paramSet} AND ParamName=\"CLASSNAME\"");
         $this->arrayParam["CLASSNAME"] = $this->unitTestParamVm->GetString("ParamValue");
         //TODO: used the value retrieved from the database to create the MgStringCollection object
         if (SQLITE_ROW == $this->unitTestParamVm->Execute("Select ParamValue from Params WHERE ParamSet={$paramSet} AND ParamName=\"PROPERTIES\"")) {
             $this->arrayParam["PROPERTIES"] = $this->unitTestParamVm->GetString("ParamValue");
         }
         if (SQLITE_ROW == $this->unitTestParamVm->Execute("Select ParamValue from Params WHERE ParamSet={$paramSet} AND ParamName=\"FILTER\"")) {
             $this->arrayParam["FILTER"] = $this->unitTestParamVm->GetString("ParamValue");
         }
         $queryOptions = new MgFeatureQueryOptions();
         //TODO: Set query options
         if (array_key_exists("FILTER", $this->arrayParam)) {
             $queryOptions->SetFilter($this->arrayParam["FILTER"]);
         }
         //PROPERTIES should be stored in the database as comma separated string without spaces
         if (array_key_exists("PROPERTIES", $this->arrayParam)) {
             $stringArray = explode(",", $this->arrayParam["PROPERTIES"]);
             for ($i = 0; $i < count($stringArray); $i++) {
                 $queryOptions->AddFeatureProperty($stringArray[$i]);
             }
         }
         if (array_key_exists("RESOURCEID", $this->arrayParam)) {
             $repId = new MgResourceIdentifier($this->arrayParam["RESOURCEID"]);
         }
         $featureReader = $this->featureSrvc->SelectFeatures($repId, $this->arrayParam['CLASSNAME'], $queryOptions);
         $byteReader = $featureReader->ToXml();
         $featureReader->Close();
         return Utils::GetResponse($byteReader);
     } catch (MgException $e) {
         return new Result(get_class($e), "text/plain");
     } catch (SqliteException $s) {
         return new Result($s->GetExceptionMessage(), "text/plain");
     }
 }
 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;
 }