Select to view content in your preferred language

C#: What to use to get find out number of fields in raster layer

1735
3
09-08-2016 04:53 PM
jameljoseph
Emerging Contributor

I am playing around with a raster and I want to find out if the fieldcount property can be found in any of the Raster interfaces using C#?  I have been unable to find it when searching IRasterLayer, IRaster.....

Thank you

0 Kudos
3 Replies
jameljoseph
Emerging Contributor

Hey Freddie.  

I have tried them now and now I am having an error.

 If I use IRasterLayer then I cannot use IRaster2 or IRasterBand.  If I remove IRasterLayer then I get an error on IRaster2 or IRasterBand saying I cannot convert type string to the IRaster2 or IRasterBand.

I know I get the same value for map.get_Layer(i).name and cboDEM.text using messageboxes when I click the right file so it goes inside the loop.

if (map.get_Layer(i).Name == cboDEM.Text)
{

     IRasterLayer pRasLayer = map.get_Layer(i) as IRasterLayer;
     IRaster2 pRaster = map.get_Layer(i).Name;

0 Kudos
FreddieGibson
Honored Contributor

Hi Jamel,

I was able to get this to work by fetching the layer from the TOC, casting it to IRasterLayer, grabbing the IRaster from IRasterLayer.Raster, casting it to ESRI.ArcGIS.DataSourcesRaster.IRaster2 and then fetching the fields by iterating through the IRaster2.AttributeTable.Fields enumerator. Below is a small snippet from a combobox example I wrote for another post that I modified for this approach.

protected override void OnSelChange(int cookie)
{
    if (cookie < 0)
        return;

    var name = this.items.Where(s => s.Cookie == cookie).FirstOrDefault().Caption;
    var layer = LoopThroughLayersOfSpecificUID(ArcMap.Document.FocusMap, "{D02371C7-35F7-11D2-B1F2-00C04F8EDEFF}", name) as ESRI.ArcGIS.Carto.IRasterLayer;
    var raster = layer.Raster as ESRI.ArcGIS.DataSourcesRaster.IRaster2;

    var fields = new List<string>();

    for (int i = 0; i < raster.AttributeTable.Fields.FieldCount; i++)
        fields.Add(raster.AttributeTable.Fields.Field[i].Name);

    var cbx = ESRI.ArcGIS.Desktop.AddIns.AddIn.FromID<FieldsCombo>(ThisAddIn.IDs.FieldsCombo.ToUID().Value.ToString());
    cbx.UpdateItems(fields.ToArray());

    base.OnSelChange(cookie);
}

// Loop Through Layers of Specific UID Snippet (ArcObjects .NET 10.4 SDK)
// http://desktop.arcgis.com/en/arcobjects/latest/net/webframe.htm#68527c4d-e69d-42f2-9f9e-81652dd4c329...
public string[] LoopThroughLayersOfSpecificUID(ESRI.ArcGIS.Carto.IMap map, System.String layerCLSID)
{
    if (map == null || layerCLSID == null)
        return null;

    List<string> names = new List<string>();

    ESRI.ArcGIS.esriSystem.IUID uid = new ESRI.ArcGIS.esriSystem.UIDClass { Value = layerCLSID };
    try
    {
        ESRI.ArcGIS.Carto.IEnumLayer enumLayer = map.get_Layers(((ESRI.ArcGIS.esriSystem.UID)(uid)), true); // Explicit Cast 
        enumLayer.Reset();
        ESRI.ArcGIS.Carto.ILayer layer;
        while ((layer = enumLayer.Next()) != null)
            names.Add(layer.Name);
    }
    catch (System.Exception ex)
    {
        System.Diagnostics.Debug.WriteLine(ex.Message);
    }

    return names.ToArray();
}‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos