Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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);

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..
share|improve this question
up vote 4 down vote accepted

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").


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;
share|improve this answer
Hi Petr- I've tried the below code and while it doesn't bust, it doesn't switch the datasource to the other sde Raster. pName is coming from the old sde Raster but the new sde Raster has the same name. Thoughts? IRasterWorkspaceEx pRasDataset = OpenSDE(propertySet) as IRasterWorkspaceEx; IDataLayer newdatalayer = pRasDataset.OpenRasterDataset(pName.NameString) as IDataLayer; pRasDataLayer = newdatalayer; – DaBears Apr 5 '12 at 19:32
Hi, did you really study the conceptual documentation and name object hierarchy carefully? Anyway, see my update with the code snippet, it should shed some light on the matter. – Petr Krebs Apr 5 '12 at 20:42

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.