3

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..
}

1 Answer 1

5

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;
    }
2
  • 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
    Commented Apr 5, 2012 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
    Commented Apr 5, 2012 at 20:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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