Example #1
0
     if ($spatialContext != null && $spatialContext->ReadNext() != null) {
         $srsLayerWkt = $spatialContext->GetCoordinateSystemWkt();
         /* skip this layer if the srs is empty */
     }
     if ($srsLayerWkt == null) {
         $srsLayerWkt = $srsDefMap;
     }
     /* create a coordinate system from the layer's SRS wkt */
     $srsLayer = $srsFactory->Create($srsLayerWkt);
     $verMajor = subStr(GetSiteVersion(), 0, 1);
     if ($verMajor == '1') {
         $srsXform = new MgCoordinateSystemTransform($srsMap, $srsLayer);
     } else {
         $srsXform = $srsFactory->GetTransform($srsMap, $srsLayer);
     }
     $wktRW = new MgWktReaderWriter();
     $geom = $wktRW->Read($spatialFilter, $srsXform);
     $queryOptions->SetSpatialFilter($featureGeometryName, $geom, $variant);
 }
 /* select the features */
 try {
     $featureReader = $featureService->SelectFeatures($featureResId, $class, $queryOptions);
 } catch (MgException $e) {
     echo "ERROR2: " . $e->GetExceptionMessage() . "\n";
     echo $e->GetDetails() . "\n";
     echo $e->GetStackTrace() . "\n";
 }
 $layerName = $layerObj->GetName();
 array_push($properties->layers, $layerName);
 if ($bExtendSelection) {
     /* possibly toggle features in the map */
Example #2
0
function MakeLine($name, $x0, $y0, $x1, $y1)
{
    $propertyCollection = new MgPropertyCollection();
    $nameProperty = new MgStringProperty("NAME", $name);
    $propertyCollection->Add($nameProperty);
    $wktReaderWriter = new MgWktReaderWriter();
    $agfReaderWriter = new MgAgfReaderWriter();
    $geometry = $wktReaderWriter->Read("LINESTRING XY ({$x0} {$y0}, {$x1} {$y1})");
    $geometryByteReader = $agfReaderWriter->Write($geometry);
    $geometryProperty = new MgGeometryProperty("SHPGEOM", $geometryByteReader);
    $propertyCollection->Add($geometryProperty);
    return $propertyCollection;
}
 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 #4
0
 function InsertCommand($key)
 {
     $props = new MgPropertyCollection();
     $keyProp = new MgStringProperty("SKEY", $key);
     $props->Add($keyProp);
     $nameProp = new MgStringProperty("NAME", "StringKey" . $key);
     $props->Add($nameProp);
     $wkt = new MgWktReaderWriter();
     $agf = new MgAgfReaderWriter();
     $geom = $wkt->Read("POLYGON ((20 20, 20 100, 120 100, 140 20, 20 20))");
     $geomReader = $agf->Write($geom);
     $geomProp = new MgGeometryProperty("GEOM", $geomReader);
     $props->Add($geomProp);
     $cmd = new MgInsertFeatures("StringKey", $props);
     return $cmd;
 }
Example #5
0
$params = new MgCreateSdfParams("ArbitraryXY", $wkt, $schema);
echo "Created sdfparams\n";
$layerName = 'Library://TrevorWekel/NewSdf.FeatureSource';
$id = new MgResourceIdentifier($layerName);
try {
    $svc->CreateFeatureSource($id, $params);
} catch (MgException $exc) {
    echo $exc->GetExceptionMessage() . "\n";
    echo $exc->GetDetails() . "\n";
    return;
}
echo "Created featuresource\n";
// We need to add some data to the sdf before using it.  The spatial context
// reader must have an extent.
$batchProp = new MgBatchPropertyCollection();
$wkt = new MgWktReaderWriter();
$agf = new MgAgfReaderWriter();
$fact = new MgGeometryFactory();
echo "Created wkt/agf\n";
$count = 100;
$i = 0;
for ($i = 1; $i <= $count; $i++) {
    $bufferProps = new MgPropertyCollection();
    $nameProp = new MgStringProperty("NAME", "NewName_" . $i);
    $bufferProps->Add($nameProp);
    $x = 120 + $i / $count;
    $y = 100 + $i / $count;
    //$wktText = "POLYGON ((20 20, 20 100, {$x} {$y}, 140 20, 20 20))";
    //$geom = $wkt->Read($wktText);
    $coord = $fact->CreateCoordinateXY($x, $y);
    $geom = $fact->CreatePoint($coord);
Example #6
0
 protected function OutputMgPropertyCollection($props, $mimeType = MgMimeType::Xml)
 {
     $content = "<?xml version=\"1.0\" encoding=\"utf-8\"?><PropertyCollection />";
     $count = $props->GetCount();
     $agfRw = null;
     $wktRw = null;
     $this->app->response->header("Content-Type", $mimeType);
     if ($count > 0) {
         $content = "<PropertyCollection>";
         for ($i = 0; $i < $count; $i++) {
             $prop = $props->GetItem($i);
             $name = $prop->GetName();
             $type = null;
             $propType = $prop->GetPropertyType();
             switch ($propType) {
                 case MgPropertyType::Boolean:
                     $type = "boolean";
                     break;
                 case MgPropertyType::Byte:
                     $type = "byte";
                     break;
                 case MgPropertyType::DateTime:
                     $type = "datetime";
                     break;
                 case MgPropertyType::Decimal:
                 case MgPropertyType::Double:
                     $type = "double";
                     break;
                 case MgPropertyType::Geometry:
                     $type = "geometry";
                     break;
                 case MgPropertyType::Int16:
                     $type = "int16";
                     break;
                 case MgPropertyType::Int32:
                     $type = "int32";
                     break;
                 case MgPropertyType::Int64:
                     $type = "int64";
                     break;
                 case MgPropertyType::Single:
                     $type = "single";
                     break;
                 case MgPropertyType::String:
                     $type = "string";
                     break;
             }
             if ($prop->IsNull()) {
                 $content .= "<Property><Name>{$name}</Name><Type>{$type}</Type></Property>";
             } else {
                 $value = "";
                 if ($propType === MgPropertyType::DateTime) {
                     $dt = $prop->GetValue();
                     $value = $dt->ToString();
                 } else {
                     if ($propType === MgPropertyType::Geometry) {
                         if ($wktRw == null) {
                             $wktRw = new MgWktReaderWriter();
                         }
                         if ($agfRw == null) {
                             $agfRw = new MgAgfReaderWriter();
                         }
                         try {
                             $agf = $prop->GetValue();
                             $geom = $agfRw->Read($agf);
                             if ($geom != null) {
                                 $value = $wktRw->Write($geom);
                             }
                         } catch (MgException $ex) {
                             $value = "";
                         }
                     } else {
                         $value = $prop->GetValue();
                     }
                 }
                 $content .= "<Property><Name>{$name}</Name><Type>{$type}</Type><Value>{$value}</Value></Property>";
             }
         }
         $content .= "</PropertyCollection>";
     }
     if ($mimeType === MgMimeType::Json) {
         $content = MgUtils::Xml2Json($content);
     }
     $this->app->response->header("Content-Type", $mimeType);
     $this->app->response->setBody($content);
 }
Example #7
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;
 }
 private function OutputXml($schemas)
 {
     $read = 0;
     $agfRw = new MgAgfReaderWriter();
     $wktRw = new MgWktReaderWriter();
     $this->writer->SetHeader("Content-Type", MgMimeType::Xml);
     $this->writer->StartChunking();
     $output = "<?xml version=\"1.0\" encoding=\"utf-8\"?><FeatureSet>";
     if (!$this->IsEmpty($schemas)) {
         $classXml = $this->featSvc->SchemaToXml($schemas);
         $classXml = substr($classXml, strpos($classXml, "<xs:schema"));
         $output .= $classXml;
     }
     $hasMoreFeatures = $this->reader->ReadNext();
     $writeXmlFooter = false;
     if ($hasMoreFeatures) {
         $output .= "<Features>";
         $this->writer->WriteChunk($output);
         $output = "";
         $writeXmlFooter = true;
     }
     $propCount = $this->reader->GetPropertyCount();
     while ($hasMoreFeatures) {
         $read++;
         if ($this->limit > 0 && $read > $this->limit) {
             break;
         }
         $output = "<Feature>";
         for ($i = 0; $i < $propCount; $i++) {
             $name = $this->reader->GetPropertyName($i);
             $propType = $this->reader->GetPropertyType($i);
             $output .= "<Property><Name>{$name}</Name>";
             if (!$this->reader->IsNull($i)) {
                 $output .= "<Value>";
                 switch ($propType) {
                     case MgPropertyType::Boolean:
                         //NOTE: It appears PHP booleans are not string-able
                         $output .= $this->reader->GetBoolean($i) ? "true" : "false";
                         break;
                     case MgPropertyType::Byte:
                         $output .= $this->reader->GetByte($i);
                         break;
                     case MgPropertyType::DateTime:
                         $dt = $this->reader->GetDateTime($i);
                         $output .= $dt->ToString();
                         break;
                     case MgPropertyType::Decimal:
                     case MgPropertyType::Double:
                         $output .= $this->reader->GetDouble($i);
                         break;
                     case MgPropertyType::Geometry:
                         try {
                             $agf = $this->reader->GetGeometry($i);
                             $geom = $this->transform != null ? $agfRw->Read($agf, $this->transform) : $agfRw->Read($agf);
                             $output .= $wktRw->Write($geom);
                         } catch (MgException $ex) {
                         }
                         break;
                     case MgPropertyType::Int16:
                         $output .= $this->reader->GetInt16($i);
                         break;
                     case MgPropertyType::Int32:
                         $output .= $this->reader->GetInt32($i);
                         break;
                     case MgPropertyType::Int64:
                         $output .= $this->reader->GetInt64($i);
                         break;
                     case MgPropertyType::Single:
                         $output .= $this->reader->GetSingle($i);
                         break;
                     case MgPropertyType::String:
                         $output .= MgUtils::EscapeXmlChars($this->reader->GetString($i));
                         break;
                 }
                 $output .= "</Value>";
             }
             $output .= "</Property>";
         }
         $output .= "</Feature>";
         $this->writer->WriteChunk($output);
         $output = "";
         $hasMoreFeatures = $this->reader->ReadNext();
     }
     if ($writeXmlFooter) {
         $output .= "</Features>";
     }
     $output .= "</FeatureSet>";
     $this->writer->WriteChunk($output);
     $this->writer->EndChunking();
     $this->reader->Close();
 }
Example #9
0
     $measure = null;
 }
 // create a SRS transformer if necessary.
 if ($layerSrsWkt != $srsDefMap) {
     if ($verMajor == '1') {
         $srsXform = new MgCoordinateSystemTransform($layerCs, $srsMap);
     } else {
         $srsXform = $srsFactory->GetTransform($layerCs, $srsMap);
     }
 } else {
     $srsXform = null;
 }
 while ($featureReader->ReadNext()) {
     $oGeomAgf = $featureReader->GetGeometry($geomPropName);
     $oGeom = $agfRW->Read($oGeomAgf);
     $wktReaderWriter = new MgWktReaderWriter();
     $agfTextPoint = $wktReaderWriter->Write($oGeom);
     //echo "<!-- wkt: ".$agfTextPoint." -->\n";
     if (!$merge) {
         /* use measure to accomodate differences in SRS */
         $oNewGeom = $oGeom->Buffer($dist, $measure);
         $geomProp = new MgGeometryProperty("GEOM", $agfRW->Write($oNewGeom));
         $oPropertyColl = new MgPropertyCollection();
         $oPropertyColl->Add($geomProp);
         $oCommandsColl->Add(new MgInsertFeatures($schemaName . ':' . $layerName, $oPropertyColl));
     } else {
         if ($srsXform == null) {
             $oNewGeom = $oGeom;
         } else {
             $oNewGeom = $oGeom->Transform($srsXform);
         }
 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;
 }
Example #11
0
 public function QueryMapFeatures($sessionId, $mapName)
 {
     //TODO: Append only works in featurefilter mode. Add append support for geometry-based selections
     $layerNames = $this->app->request->params("layernames");
     $geometry = $this->app->request->params("geometry");
     $maxFeatures = $this->app->request->params("maxfeatures");
     $selVariant = $this->app->request->params("selectionvariant");
     $selColor = $this->app->request->params("selectioncolor");
     $selFormat = $this->app->request->params("selectionformat");
     $persist = $this->app->request->params("persist");
     $reqData = $this->app->request->params("requestdata");
     $featFilter = $this->app->request->params("featurefilter");
     $bSelectionXml = $this->app->request->params("selectionxml");
     $bAppend = $this->app->request->params("append");
     $layerAttFilter = $this->app->request->params("layerattributefilter");
     $format = $this->app->request->params("format");
     //Convert or coerce to defaults
     if ($format == null) {
         $format = "xml";
     } else {
         $format = strtolower($format);
     }
     if ($maxFeatures == null) {
         $maxFeatures = -1;
     } else {
         $maxFeatures = intval($maxFeatures);
     }
     if ($selFormat == null) {
         $selFormat = "PNG";
     } else {
         $selFormat = strtoupper($selFormat);
     }
     if ($layerAttFilter == null) {
         $layerAttFilter = 3;
     } else {
         $layerAttFilter = intval($layerAttFilter);
     }
     if ($persist == null) {
         $persist = true;
     } else {
         $persist = $persist == "1" || $persist == "true";
     }
     if ($bSelectionXml == null) {
         $bSelectionXml = true;
     } else {
         $bSelectionXml = $bSelectionXml == "1" || $bSelectionXml == "true";
     }
     if ($bAppend == null) {
         $bAppend = true;
     } else {
         $bAppend = $bAppend == "1" || $bAppend == "true";
     }
     if ($reqData == null) {
         $reqData = 0;
     } else {
         $reqData = intval($reqData);
     }
     if ($selColor == null) {
         $selColor = "0x0000FFFF";
     }
     try {
         $this->EnsureAuthenticationForSite($sessionId);
         $siteConn = new MgSiteConnection();
         $siteConn->Open($this->userInfo);
         $admin = new MgServerAdmin();
         $admin->Open($this->userInfo);
         $version = explode(".", $admin->GetSiteVersion());
         $bCanUseNative = false;
         //If appending, we can't use the native operation as that operation does not support appending
         if (intval($version[0]) > 2) {
             //3.0 or greater
             $bCanUseNative = !$bAppend;
         } else {
             if (intval($version[0]) == 2 && intval($version[1]) >= 6) {
                 //2.6 or greater
                 $bCanUseNative = !$bAppend;
             }
         }
         //$this->app->log->debug("APPEND: $bAppend");
         //$this->app->log->debug("FILTER (Before): $featFilter");
         if (!$bSelectionXml) {
             //Append only works in the absence of geometry
             if ($geometry == null && $featFilter != null) {
                 $featFilter = $this->TranslateToSelectionXml($siteConn, $mapName, $featFilter, $bAppend);
                 //$this->app->log->debug("FeatFilter: $featFilter");
             }
         } else {
             //Append only works in the absence of geometry
             if ($geometry == null && $bAppend) {
                 $featFilter = $this->AppendSelectionXml($siteConn, $mapName, $featFilter);
             }
         }
         //$this->app->log->debug("GEOMETRY: $geometry");
         //$this->app->log->debug("FILTER: $featFilter");
         //$this->app->log->debug("Can use native: $bCanUseNative");
         if ($bCanUseNative) {
             $req = new MgHttpRequest("");
             $param = $req->GetRequestParam();
             $param->AddParameter("OPERATION", "QUERYMAPFEATURES");
             $param->AddParameter("VERSION", "2.6.0");
             $param->AddParameter("SESSION", $sessionId);
             $param->AddParameter("MAPNAME", $mapName);
             $param->AddParameter("GEOMETRY", $geometry);
             $param->AddParameter("SELECTIONVARIANT", $selVariant);
             $param->AddParameter("MAXFEATURES", $maxFeatures);
             $param->AddParameter("LAYERNAMES", $layerNames);
             $param->AddParameter("PERSIST", $persist ? "1" : "0");
             $param->AddParameter("LAYERATTRIBUTEFILTER", $layerAttFilter);
             if ($featFilter != null) {
                 $param->AddParameter("FEATUREFILTER", $featFilter);
             }
             $param->AddParameter("REQUESTDATA", $reqData);
             $param->AddParameter("SELECTIONCOLOR", $selColor);
             $param->AddParameter("SELECTIONFORMAT", $selFormat);
             if ($format === "json") {
                 $param->AddParameter("FORMAT", MgMimeType::Json);
             } else {
                 $param->AddParameter("FORMAT", MgMimeType::Xml);
             }
             $this->ExecuteHttpRequest($req);
         } else {
             //Shim the response
             $resSvc = $siteConn->CreateService(MgServiceType::ResourceService);
             $renderSvc = $siteConn->CreateService(MgServiceType::RenderingService);
             $layersToQuery = null;
             if ($layerNames != null) {
                 $layersToQuery = new MgStringCollection();
                 $names = explode(",", $layerNames);
                 foreach ($names as $name) {
                     $layersToQuery->Add($name);
                 }
             }
             $variant = 0;
             if ($selVariant === "TOUCHES") {
                 $variant = MgFeatureSpatialOperations::Touches;
             } else {
                 if ($selVariant === "INTERSECTS") {
                     $variant = MgFeatureSpatialOperations::Intersects;
                 } else {
                     if ($selVariant === "WITHIN") {
                         $variant = MgFeatureSpatialOperations::Within;
                     } else {
                         if ($selVariant === "ENVELOPEINTERSECTS") {
                             $variant = MgFeatureSpatialOperations::EnvelopeIntersects;
                         }
                     }
                 }
             }
             $map = new MgMap($siteConn);
             $map->Open($mapName);
             $selection = new MgSelection($map);
             $wktRw = new MgWktReaderWriter();
             $selectGeom = null;
             if ($geometry != null) {
                 $selectGeom = $wktRw->Read($geometry);
             }
             $featInfo = $renderSvc->QueryFeatures($map, $layersToQuery, $selectGeom, $variant, $featFilter, $maxFeatures, $layerAttFilter);
             $bHasNewSelection = false;
             if ($persist) {
                 $sel = $featInfo->GetSelection();
                 if ($sel != null) {
                     $selXml = $sel->ToXml();
                     //$this->app->log->debug("Query selection:\n$selXml");
                     if ($bAppend) {
                         $selOrig = new MgSelection($map);
                         $selOrig->Open($resSvc, $mapName);
                         $selAppend = new MgSelection($map, $selXml);
                         self::MergeSelections($selOrig, $selAppend);
                         $selNewXml = $selOrig->ToXml();
                         //$this->app->log->debug("Appended selection:\n$selNewXml");
                         $selection->FromXml($selNewXml);
                     } else {
                         $selection->FromXml($selXml);
                     }
                     $bHasNewSelection = true;
                 }
                 $selection->Save($resSvc, $mapName);
             }
             // Render an image of this selection if requested
             $inlineSelectionImg = null;
             if (($reqData & self::REQUEST_INLINE_SELECTION) == self::REQUEST_INLINE_SELECTION && $bHasNewSelection) {
                 $color = new MgColor($selColor);
                 $renderOpts = new MgRenderingOptions($selFormat, self::RenderSelection | self::KeepSelection, $color);
                 $inlineSelectionImg = $renderSvc->RenderDynamicOverlay($map, $selection, $renderOpts);
             }
             // Collect any attributes of selected features
             $bRequestAttributes = ($reqData & self::REQUEST_ATTRIBUTES) == self::REQUEST_ATTRIBUTES;
             $xml = $this->CollectQueryMapFeaturesResult($resSvc, $reqData, $featInfo, $selection, $bRequestAttributes, $inlineSelectionImg);
             $bs = new MgByteSource($xml, strlen($xml));
             $bs->SetMimeType(MgMimeType::Xml);
             $br = $bs->GetReader();
             if ($format == "json") {
                 $this->OutputXmlByteReaderAsJson($br);
             } else {
                 $this->OutputByteReader($br);
             }
         }
     } catch (MgException $ex) {
         $this->OnException($ex, $this->GetMimeTypeForFormat($format));
     }
 }
function MakePoint($name, $x, $y)
{
    $propertyCollection = new MgPropertyCollection();
    $nameProperty = new MgStringProperty("NAME", $name);
    $propertyCollection->Add($nameProperty);
    $wktReaderWriter = new MgWktReaderWriter();
    $agfReaderWriter = new MgAgfReaderWriter();
    $geometry = $wktReaderWriter->Read("POINT XY ({$x} {$y})");
    $geometryByteReader = $agfReaderWriter->Write($geometry);
    $geometryProperty = new MgGeometryProperty("GEOM", $geometryByteReader);
    $propertyCollection->Add($geometryProperty);
    return $propertyCollection;
}