Getting raster layer path in ArcGIS Pro using C#

339
1
Jump to solution
11-30-2021 12:42 AM
osmansheikh
New Contributor

I am creating an ArcGIS Pro Extension. I need to know the layer's path. To get the layer path, code developed so far:

var pointLayer = MapView.Active.Map.Layers.OfType<FeatureLayer>().FirstOrDefault();
var rasterLayer = MapView.Active.Map.Layers.OfType<RasterLayer>().FirstOrDefault();
var pointLayerPath = await QueuedTask.Run(() => pointLayer.GetPath());
var rasterLayerPath = await QueuedTask.Run(() => rasterLayer.GetPath());

pointLayerPath is assigned the correct path but rasterLayerPath is assigned a null value.
The result is the same for this code also.
MapView.Active.Map.GetLayersAsFlattenedList().OfType<RasterLayer>().FirstOrDefault();

How can I get the RasterLayer path?

0 Kudos
1 Solution

Accepted Solutions
GintautasKmieliauskas
Regular Contributor II

Hi,

I use the following code to get raster layer path:

            return QueuedTask.Run<string>(() =>
            {
                try
                {
                    string fullSpec = string.Empty;
                    CIMDataConnection dataConnection = pLayer.GetDataConnection();
                    if (dataConnection is CIMStandardDataConnection)
                    {
                        CIMStandardDataConnection dataSConnection = dataConnection as CIMStandardDataConnection;

                        string sConnection = dataSConnection.WorkspaceConnectionString;

                        var wFactory = dataSConnection.WorkspaceFactory;
                        if (wFactory == WorkspaceFactory.Raster)
                        {
                            string sWorkspaceName = sConnection.Split('=')[1];

                            string sTable = dataSConnection.Dataset;

                            fullSpec = Path.Combine(sWorkspaceName, sTable);
                        }
                    }

                    return fullSpec;
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine(ex.ToString(), System.Reflection.Assembly.GetExecutingAssembly().FullName);
                    return string.Empty;
                }
            });

It works for file based rasters and Esri GRIDs. I don't check how it works with sde stored rasters

View solution in original post

1 Reply
GintautasKmieliauskas
Regular Contributor II

Hi,

I use the following code to get raster layer path:

            return QueuedTask.Run<string>(() =>
            {
                try
                {
                    string fullSpec = string.Empty;
                    CIMDataConnection dataConnection = pLayer.GetDataConnection();
                    if (dataConnection is CIMStandardDataConnection)
                    {
                        CIMStandardDataConnection dataSConnection = dataConnection as CIMStandardDataConnection;

                        string sConnection = dataSConnection.WorkspaceConnectionString;

                        var wFactory = dataSConnection.WorkspaceFactory;
                        if (wFactory == WorkspaceFactory.Raster)
                        {
                            string sWorkspaceName = sConnection.Split('=')[1];

                            string sTable = dataSConnection.Dataset;

                            fullSpec = Path.Combine(sWorkspaceName, sTable);
                        }
                    }

                    return fullSpec;
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine(ex.ToString(), System.Reflection.Assembly.GetExecutingAssembly().FullName);
                    return string.Empty;
                }
            });

It works for file based rasters and Esri GRIDs. I don't check how it works with sde stored rasters