function CopyLayerToSession($map, $resourceService, $libraryLayerId) { //create the session MgResourceIdentifier try { $destinationLayerId = ToggleRepository($libraryLayerId); $destinationLayerId->SetName($destinationLayerId->GetName() . '-edit'); //copy content from library layer $contentReader = $resourceService->GetResourceContent($libraryLayerId); $layerContent = $contentReader->ToString(); //set the feature source this layer references -- must be in the session //the feature source need not exist before calling this function $libraryLayer = new MgLayer($libraryLayerId, $resourceService); $libraryFeatureSourceId = new MgResourceIdentifier($libraryLayer->GetFeatureSourceId()); $destinationFeatureSourceId = ToggleRepository($libraryFeatureSourceId); //replace the featuresourceid and write xml to the session copy $layerContent = str_replace($libraryFeatureSourceId->ToString(), $destinationFeatureSourceId->ToString(), $layerContent); $byteSource = new MgByteSource($layerContent, strlen($layerContent)); $resourceService->SetResource($destinationLayerId, $byteSource->GetReader(), null); //instantiate the new layer from its MgResourceIdentifier $layer = new MgLayer($destinationLayerId, $resourceService); //add the edit layer to the session map and set it to redraw but not be visible in the legend $layerCollection = $map->GetLayers(); $originalLayer = FindLayer($layerCollection, $libraryLayer->GetLayerDefinition()->ToString()); $nPosition = $layerCollection->IndexOf($originalLayer); $layerCollection->Insert($nPosition, $layer); $layer->SetDisplayInLegend(false); $layer->SetSelectable(true); $layer->ForceRefresh(); //OR //remove the library layer from the session map and replace it with the //session copy //ReplaceLayer($map, $libraryLayer, $layer); //save the session map $map->Save($resourceService); } catch (MgException $e) { echo "CopyLayerToSession threw exception\n"; echo "ERROR: " . $e->GetExceptionMessage() . "n"; echo $e->GetDetails() . "n"; echo $e->GetStackTrace() . "n"; } return $layer; }