function add_layer_definition_to_map($layerDefinition, $layerName, $layerLegendLabel, $sessionId, $resourceService, &$map) { global $schemaDirectory; // Validate the XML. $domDocument = new DOMDocument(); $domDocument->loadXML($layerDefinition); if (!$domDocument->schemaValidate($schemaDirectory . "LayerDefinition-1.3.0.xsd")) { echo "ERROR: The new XML document is invalid.<BR>\n."; return NULL; } // Save the new layer definition to the session repository $byteSource = new MgByteSource($layerDefinition, strlen($layerDefinition)); $byteSource->SetMimeType(MgMimeType::Xml); $resourceID = new MgResourceIdentifier("Session:{$sessionId}//{$layerName}.LayerDefinition"); $resourceService->SetResource($resourceID, $byteSource->GetReader(), null); $newLayer = add_layer_resource_to_map($resourceID, $resourceService, $layerName, $layerLegendLabel, $map); return $newLayer; }
function SetResourceData($paramSet) { try { $arrayParam = array(); $repId = null; if (SQLITE_ROW == $this->unitTestParamVm->Execute("Select ParamValue from Params WHERE ParamSet={$paramSet} AND ParamName=\"RESOURCEID\"")) { $arrayParam["RESOURCEID"] = $this->unitTestParamVm->GetString("ParamValue"); } if (array_key_exists("RESOURCEID", $arrayParam)) { $repId = new MgResourceIdentifier($arrayParam["RESOURCEID"]); } $this->unitTestParamVm->Execute("Select ParamValue from Params WHERE ParamSet={$paramSet} AND ParamName=\"DATANAME\""); $arrayParam["DATANAME"] = $this->unitTestParamVm->GetString("ParamValue"); $this->unitTestParamVm->Execute("Select ParamValue from Params WHERE ParamSet={$paramSet} AND ParamName=\"DATATYPE\""); $arrayParam["DATATYPE"] = $this->unitTestParamVm->GetString("ParamValue"); $this->unitTestParamVm->Execute("Select ParamValue from Params WHERE ParamSet={$paramSet} AND ParamName=\"DATA\""); $arrayParam["DATA"] = $this->unitTestParamVm->GetString("ParamValue"); $arrayParam["DATA"] = Utils::GetPath($arrayParam["DATA"]); $extension = self::GetExtension($arrayParam["DATANAME"]); $mimeType = self::GetMimeType($extension); $dataSource = new MgByteSource($arrayParam["DATA"]); $dataSource->SetMimeType($mimeType); $dataReader = $dataSource->GetReader(); $byteReader = $this->resourceSrvc->SetResourceData($repId, $arrayParam["DATANAME"], $arrayParam["DATATYPE"], $dataReader); $result = Utils::GetResponse($byteReader); return $result; } catch (MgException $e) { return new Result(get_class($e), "text/plain"); } catch (SqliteException $s) { return new Result($s->GetExceptionMessage(), "text/plain"); } }
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); } }
private function DescribeRuntimeMapXml($mapDefinition, $map, $sessionId, $mapName, $iconFormat, $iconWidth, $iconHeight, $reqFeatures, $iconsPerScaleRange, $resSvc, $mappingSvc) { //TODO: Caching opportunity here $admin = new MgServerAdmin(); $admin->Open($this->userInfo); $siteVersion = $admin->GetSiteVersion(); $xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; $xml .= "<RuntimeMap xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"RuntimeMap-2.6.0.xsd\">\n"; // ---------------------- Site Version --------------------------- // $xml .= "<SiteVersion>{$siteVersion}</SiteVersion>\n"; // ---------------------- Session ID --------------------------- // $xml .= "<SessionId>{$sessionId}</SessionId>\n"; // ---------------------- Map Name --------------------------- // $xml .= "<Name>{$mapName}</Name>\n"; // ---------------------- Map Definition --------------------------- // $xml .= "<MapDefinition>{$mapDefinition}</MapDefinition>\n"; // ---------------------- Background Color --------------------------- // $bgColor = $map->GetBackgroundColor(); $xml .= "<BackgroundColor>{$bgColor}</BackgroundColor>\n"; // ---------------------- Display DPI --------------------------- // $dpi = $map->GetDisplayDpi(); $xml .= "<DisplayDpi>{$dpi}</DisplayDpi>"; // ---------------------- Icon MIME Type --------------------------- // if (($reqFeatures & self::REQUEST_LAYER_ICONS) == self::REQUEST_LAYER_ICONS) { switch ($iconFormat) { case "JPG": $xml .= "<IconMimeType>" . MgMimeType::Jpeg . "</IconMimeType>\n"; break; case "GIF": $xml .= "<IconMimeType>" . MgMimeType::Gif . "</IconMimeType>\n"; break; case "PNG8": $xml .= "<IconMimeType>" . MgMimeType::Png . "</IconMimeType>\n"; break; default: $xml .= "<IconMimeType>" . MgMimeType::Png . "</IconMimeType>\n"; break; } } // ---------------------- Coordinate System --------------------------- // $csFactory = new MgCoordinateSystemFactory(); $metersPerUnit = 1.0; $wkt = $map->GetMapSRS(); $csCode = ""; $epsg = ""; try { $cs = $csFactory->Create($wkt); $metersPerUnit = $cs->ConvertCoordinateSystemUnitsToMeters(1.0); $epsg = $cs->GetEpsgCode(); $csCode = $cs->GetCsCode(); } catch (MgException $ex) { } $xml .= "<CoordinateSystem>\n<Wkt>{$wkt}</Wkt>\n<MentorCode>{$csCode}</MentorCode>\n<EpsgCode>{$epsg}</EpsgCode>\n<MetersPerUnit>{$metersPerUnit}</MetersPerUnit>\n</CoordinateSystem>"; // ---------------------- Map Extents--------------------------- // $extents = $map->GetMapExtent(); $ll = $extents->GetLowerLeftCoordinate(); $ur = $extents->GetUpperRightCoordinate(); $minX = $ll->GetX(); $minY = $ll->GetY(); $maxX = $ur->GetX(); $maxY = $ur->GetY(); $xml .= "<Extents>\n<LowerLeftCoordinate><X>{$minX}</X><Y>{$minY}</Y></LowerLeftCoordinate>\n<UpperRightCoordinate><X>{$maxX}</X><Y>{$maxY}</Y></UpperRightCoordinate></Extents>\n"; $layerDefinitionMap = array(); // ---------------------- Optional things if requested --------------------------- // if (($reqFeatures & self::REQUEST_LAYER_STRUCTURE) == self::REQUEST_LAYER_STRUCTURE) { $layers = $map->GetLayers(); $layerCount = $layers->GetCount(); //Build our LayerDefinition map for code below that requires it if (($reqFeatures & self::REQUEST_LAYER_ICONS) == self::REQUEST_LAYER_ICONS) { $layerIds = new MgStringCollection(); for ($i = 0; $i < $layerCount; $i++) { $layer = $layers->GetItem($i); $ldfId = $layer->GetLayerDefinition(); $layerIds->Add($ldfId->ToString()); } $layerContents = $resSvc->GetResourceContents($layerIds, null); $layerIdCount = $layerIds->GetCount(); for ($i = 0; $i < $layerIdCount; $i++) { $ldfId = $layerIds->GetItem($i); $content = $layerContents->GetItem($i); $layerDefinitionMap[$ldfId] = $content; } } // ----------- Some pre-processing before we do groups/layers ------------- // $groups = $map->GetLayerGroups(); $groupCount = $groups->GetCount(); for ($i = 0; $i < $groupCount; $i++) { $group = $groups->GetItem($i); $parent = $group->GetGroup(); $xml .= self::CreateGroupItem($group, $parent); } $doc = new DOMDocument(); for ($i = 0; $i < $layerCount; $i++) { $layer = $layers->GetItem($i); $parent = $layer->GetGroup(); $ldf = $layer->GetLayerDefinition(); $layerId = $ldf->ToString(); $layerDoc = null; if (array_key_exists($layerId, $layerDefinitionMap)) { $doc->loadXML($layerDefinitionMap[$layerId]); $layerDoc = $doc; } $xml .= self::CreateLayerItem($reqFeatures, $iconsPerScaleRange, $iconFormat, $iconWidth, $iconHeight, $layer, $parent, $layerDoc, $mappingSvc); } } else { //Base Layer Groups need to be outputted regardless, otherwise a client application doesn't have enough information to build GETTILEIMAGE requests $groups = $map->GetLayerGroups(); $groupCount = $groups->GetCount(); for ($i = 0; $i < $groupCount; $i++) { $group = $groups->GetItem($i); if ($group->GetLayerGroupType() != MgLayerGroupType::BaseMap) { continue; } $parent = $group->GetGroup(); $xml .= self::CreateGroupItem($group, $parent); } } // ------------------------ Finite Display Scales (if any) ------------------------- // $fsCount = $map->GetFiniteDisplayScaleCount(); if ($fsCount > 0) { for ($i = 0; $i < $fsCount; $i++) { $xml .= "<FiniteDisplayScale>"; $xml .= $map->GetFiniteDisplayScaleAt($i); $xml .= "</FiniteDisplayScale>"; } } $xml .= "</RuntimeMap>"; $bs = new MgByteSource($xml, strlen($xml)); $bs->SetMimeType(MgMimeType::Xml); $br = $bs->GetReader(); return $br; }
} // Create a map definition $mapfactory = new MapDefinitionFactory(); $mapDefinition = CreateMapDef($mapfactory, $className, $resName, $mbr->coordinateSystem, $minX, $maxX, $minY, $maxY); // Save the map definition to a resource stored in the session repository $byteSource = new MgByteSource($mapDefinition, strlen($mapDefinition)); $byteSource->SetMimeType(MgMimeType::Xml); $resName = 'Session:' . $sessionId . '//' . $className . '.MapDefinition'; $resId = new MgResourceIdentifier($resName); $resourceSrvc->SetResource($resId, $byteSource->GetReader(), null); // Create a web layout $webfactory = new WebLayoutFactory(); $webLayout = CreateWebLay($webfactory, $resName, $useBasicViewer); // Save the web layout to a resource stored in the session repository $byteSource = new MgByteSource($webLayout, strlen($webLayout)); $byteSource->SetMimeType(MgMimeType::Xml); if ($useBasicViewer) { $resName = 'Session:' . $sessionId . '//' . $className . '.WebLayout'; $viewerRequest = '../mapviewerajax/?SESSION=' . $sessionId . '&WEBLAYOUT=' . $resName; } else { $resName = 'Session:' . $sessionId . '//' . $className . '.ApplicationDefinition'; $viewerRequest = '../fusion/templates/mapguide/preview/indexNoLegend.html?SESSION=' . $sessionId . '&APPLICATIONDEFINITION=' . $resName; } $resId = new MgResourceIdentifier($resName); $resourceSrvc->SetResource($resId, $byteSource->GetReader(), null); } catch (MgSessionExpiredException $s) { $validSession = 0; echo ErrorMessages::SessionExpired; } catch (MgException $mge) { $validSession = 0; echo $mge->GetExceptionMessage();
function CreateParcelMarkerLayer($resourceService, $parcelMarkerDataResId, $sessionId) { // Load the ParcelMarker layer definition template into // a PHP DOM object, find the "ResourceId" element, and // modify its content to reference the temporary // feature source. $doc = DOMDocument::load('parcelmarker.xml'); $featureSourceNode = $doc->getElementsByTagName('ResourceId')->item(0); $featureSourceNode->nodeValue = $parcelMarkerDataResId->ToString(); // Get the updated layer definition from the DOM object // and save it to the session repository using the // ResourceService object. $layerDefinition = $doc->saveXML(); $byteSource = new MgByteSource($layerDefinition, strlen($layerDefinition)); $byteSource->SetMimeType(MgMimeType::Xml); $tempLayerResId = new MgResourceIdentifier("Session:" . $sessionId . "//ParcelMarker.LayerDefinition"); $resourceService->SetResource($tempLayerResId, $byteSource->GetReader(), null); // Create an MgLayer object based on the new layer definition // and return it to the caller. $parcelMarkerLayer = new MgLayer($tempLayerResId, $resourceService); $parcelMarkerLayer->SetName("ParcelMarker"); $parcelMarkerLayer->SetLegendLabel("ParcelMarker"); $parcelMarkerLayer->SetDisplayInLegend(true); $parcelMarkerLayer->SetSelectable(false); return $parcelMarkerLayer; }
public function UpdateMapLayersAndGroups($sessionId, $mapName, $format) { //Check for unsupported representations $fmt = $this->ValidateRepresentation($format, array("xml", "json")); try { $this->EnsureAuthenticationForSite($sessionId); $siteConn = new MgSiteConnection(); $siteConn->Open($this->userInfo); $resSvc = $siteConn->CreateService(MgServiceType::ResourceService); $map = new MgMap($siteConn); $map->Open($mapName); if ($fmt == "json") { $body = $this->app->request->getBody(); $json = json_decode($body); if ($json == NULL) { throw new Exception($this->app->localizer->getText("E_MALFORMED_JSON_BODY")); } } else { $body = $this->app->request->getBody(); $jsonStr = MgUtils::Xml2Json($body); $json = json_decode($jsonStr); } if (!isset($json->UpdateMap)) { throw new Exception($this->app->localizer->getText("E_MALFORMED_JSON_BODY")); } /* Expected structure /UpdateMap /Operation [1...n] /Type - [AddLayer|UpdateLayer|RemoveLayer|AddGroup|UpdateGroup|RemoveGroup] /Name /ResourceId /SetLegendLabel /SetDisplayInLegend /SetExpandInLegend /SetVisible /SetSelectable /InsertAt */ $layers = $map->GetLayers(); $groups = $map->GetLayerGroups(); $um = $json->UpdateMap; $updateStats = new stdClass(); $updateStats->AddedLayers = 0; $updateStats->UpdatedLayers = 0; $updateStats->RemovedLayers = 0; $updateStats->AddedGroups = 0; $updateStats->UpdatedGroups = 0; $updateStats->RemovedGroups = 0; $this->app->log->debug("Operations found: " . count($um->Operation)); for ($i = 0; $i < count($um->Operation); $i++) { $op = $um->Operation[$i]; switch ($op->Type) { case "AddLayer": $resId = new MgResourceIdentifier($op->ResourceId); $layer = new MgLayer($resId, $resSvc); $layer->SetName($op->Name); self::ApplyCommonLayerProperties($layer, $op, $groups); if (isset($op->InsertAt)) { $layers->Insert(intval($op->InsertAt), $layer); } else { $layers->Add($layer); } $this->app->log->debug("Add Layer: " . $op->Name); $updateStats->AddedLayers++; break; case "UpdateLayer": $layer = $layers->GetItem($op->Name); if (self::ApplyCommonLayerProperties($layer, $op, $groups)) { $this->app->log->debug("Updated Layer: " . $op->Name); $updateStats->UpdatedLayers++; } break; case "RemoveLayer": $layer = $layers->GetItem($op->Name); if ($layers->Remove($layer)) { $this->app->log->debug("Removed Layer: " . $op->Name); $updateStats->RemovedLayers++; } break; case "AddGroup": $group = new MgLayerGroup($op->Name); self::ApplyCommonGroupProperties($group, $op, $groups); if (isset($op->InsertAt)) { $groups->Insert(intval($op->InsertAt), $group); } else { $groups->Add($group); } $this->app->log->debug("Add Group: " . $op->Name); $updateStats->AddedGroups++; break; case "UpdateGroup": $gidx = $groups->IndexOf($op->Name); if ($gidx < 0) { if ($op->AddIfNotExists) { $group = new MgLayerGroup($op->Name); self::ApplyCommonGroupProperties($group, $op, $groups); if (isset($op->InsertAt)) { $groups->Insert(intval($op->InsertAt), $group); } else { $groups->Add($group); } $this->app->log->debug("Add Group: " . $op->Name); $updateStats->AddedGroups++; } else { throw new Exception($this->app->localizer->getText("E_GROUP_NOT_FOUND", $op->Name)); } } else { $group = $groups->GetItem($gidx); if (self::ApplyCommonGroupProperties($group, $op, $groups)) { $this->app->log->debug("Updated Group: " . $op->Name); $updateStats->UpdatedGroups++; } } break; case "RemoveGroup": $group = $groups->GetItem($op->Name); if ($groups->Remove($group)) { $this->app->log->debug("Removed Group: " . $op->Name); $updateStats->RemovedGroups++; } break; } } if ($updateStats->AddedLayers > 0 || $updateStats->UpdatedLayers > 0 || $updateStats->RemovedLayers > 0 || $updateStats->AddedGroups > 0 || $updateStats->UpdatedGroups > 0 || $updateStats->RemovedGroups > 0) { $map->Save(); } $response = "<UpdateMapResult>"; $response .= "<AddedLayers>"; $response .= $updateStats->AddedLayers; $response .= "</AddedLayers>"; $response .= "<UpdatedLayers>"; $response .= $updateStats->UpdatedLayers; $response .= "</UpdatedLayers>"; $response .= "<RemovedLayers>"; $response .= $updateStats->RemovedLayers; $response .= "</RemovedLayers>"; $response .= "<AddedGroups>"; $response .= $updateStats->AddedGroups; $response .= "</AddedGroups>"; $response .= "<UpdatedGroups>"; $response .= $updateStats->UpdatedGroups; $response .= "</UpdatedGroups>"; $response .= "<RemovedGroups>"; $response .= $updateStats->RemovedGroups; $response .= "</RemovedGroups>"; $response .= "</UpdateMapResult>"; $bs = new MgByteSource($response, strlen($response)); $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 SetProps() { global $site; global $userInfo; global $errInvalidWFSFile; // Get WFS reader $serverAdmin = new MgServerAdmin(); $serverAdmin->Open($userInfo); $wfsReader = $serverAdmin->GetDocument('Wfs:OgcWfsService.config'); // Set WFS metadata $wfsData = ""; $chunk = ""; do { $chunkSize = $wfsReader->Read($chunk, 4096); $wfsData = $wfsData . $chunk; } while ($chunkSize != 0); $keywordsStr = ""; foreach ($this->keywords as $keyword) { $keywordsStr = $keywordsStr . '<item>' . $keyword . '</item>'; } $this->serviceMetadata[WFS_KEYWORDS_ITEM] = $keywordsStr; foreach ($this->serviceMetadata as $serviceItem => $serviceVal) { $itemPos = strpos($wfsData, $serviceItem); if ($itemPos === false) { throw new Exception($errInvalidWFSFile); } $valStartPos = strpos($wfsData, '>', $itemPos); $valEndPos = strpos($wfsData, '</Define>', $itemPos); if ($valStartPos === false || $valEndPos === false || $valStartPos >= $valEndPos) { throw new Exception($errInvalidWFSFile); } $wfsData = substr_replace($wfsData, $serviceVal, $valStartPos + 1, $valEndPos - $valStartPos - 1); } // Save wfs config $wfsByteSource = new MgByteSource($wfsData, strlen($wfsData)); $wfsByteSource->SetMimeType($wfsReader->GetMimeType()); $serverAdmin->SetDocument('Wfs:OgcWfsService.config', $wfsByteSource->GetReader()); $serverAdmin->Close(); }