I'm building a C# application that lets the user change their DataSources of all the layers in their MXD. I have it working on FeatureLayers and LYR files but I'm stuck on how to change Raster layers from one sde database to another sde database. Below is all I have so far with Rasters. To change FeatureLayers I used IMapAdmin2 but theure is no option to change Rasters with that interface. Any ideas or additional code? Many thanks
UID uid_ras = new UIDClass();
uid_ras.Value = "{D02371C7-35F7-11D2-B1F2-00C04F8EDEFF}"; //IRasterLayer
IEnumLayer raslayers = pMap.get_Layers(uid_ras, true);
raslayers.Reset();
pRas = raslayers.Next() as IRasterLayer; //get the first layer
while(pRas != null) //Now change each Raster's DataSource to the new Datasource
{
//I'm unsure what to do from here..
}
Answer
Cast the layer to the IDataLayer interface. You can set the IDataLayer.DataSourceName property to reference the data source (defined as a name object) in the other SDE database.
You can either create the name object from scratch or use the existing one, altered to point to a different database.
If you are not familiar with name objects, see the documentation for IName (particularly to the list of classes implementing this interface) along with the Geodatabase library overview (section "Name objects").
Update:
The code snippet below demonstrates the concept. It is meant to work with layers representing raster datasets and may need some tweaking to work with other raster types. But you get the general idea:
private void RedirectRasterDatasetLayerToNewWorkspace(IRasterLayer rasterLayer, IWorkspace newWorkspace)
{
var dataLayer = (IDataLayer) rasterLayer;
// DataSourceName gives us the name object representing the raster dataset
var rasterDatasetName = (IRasterDatasetName)dataLayer.DataSourceName;
var datasetName = (IDatasetName) rasterDatasetName;
// get the IWorkspaceName object for the new workspace
var newWorkspaceName = (IWorkspaceName)((IDataset)newWorkspace).FullName;
// Alter the raster dataset name to reference the same dataset, but in the new workspace.
// Since this datasetName is linkes to the raster layer, it will change the layer's source.
datasetName.WorkspaceName = newWorkspaceName;
}
No comments:
Post a Comment