Select to view content in your preferred language

Get GroupLayer Name

4338
20
07-29-2013 10:00 AM
LeoDonahue
Deactivated User
ArcObjects Java 10.0 sp5

If an IGroupLayer is composed of IFeatureLayers, the following code will print the IGroupLayer name.

If I pass a name that I *think* is an IGroupLayer, my method crashes with null pointer.  So, if you have a map document with a mosaic with sub layers: Boundary, Footprint and Image, is that not an IGroupLayer?  I've also tried ICompositeLayer also, with the same null pointer result.

If I pass the name of my mosaic layer to the following method, I get a null pointer.
If pass the name of any other group layer, it will print the parent group name, in this example "Transportation".

    public IGroupLayer getGroupLayerName(com.esri.arcgis.carto.Map map, AGSConnection agsconn){
        ILayer layer = null;
        
        try {
            
            UID id = (UID) agsconn.getContext().createObject(UID.getClsid());
            id.setValue(com.esri.arcgis.carto.IGroupLayer.class);
            
            // getLayers(UID, true) uses recursion to the get the layer name in a GroupLayer
            IEnumLayer enumLayer = map.getLayers(id, true);
            enumLayer.reset();
            layer = enumLayer.next();
            
            while(!(layer == null)){
                if(layer.getName().equalsIgnoreCase("Transportation")){
                    break;
                } else {
                    layer = enumLayer.next();
                }
            }
        } catch (AutomationException ae) {
            ae.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            System.out.println(layer.getName());
        } catch (AutomationException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        return (IGroupLayer) layer;
    }


This method can find the layer named "Image" located within the Mosaic layer, but I really wanted the name of the mosaic group layer.

    public IRasterLayer getRasterLayerName(com.esri.arcgis.carto.Map map, AGSConnection agsconn){
        ILayer layer = null;
        
        try {
            
            UID id = (UID) agsconn.getContext().createObject(UID.getClsid());
            id.setValue(com.esri.arcgis.carto.IRasterLayer.class);
            
            // getLayers(UID, true) uses recursion to the get the layer name in a GroupLayer
            IEnumLayer enumLayer = map.getLayers(id, true);
            enumLayer.reset();
            layer = enumLayer.next();
            
            while(!(layer == null)){
                if(layer.getName().equalsIgnoreCase("Image")){
                    break;
                } else {
                    layer = enumLayer.next();
                }
            }
        } catch (AutomationException ae) {
            ae.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return (IRasterLayer) layer;
    }
0 Kudos
20 Replies
T__WayneWhitley
Honored Contributor
Leo,

Very interesting, frustrating too I'm sure.

If I may question this, this line raises suspicion:
com.esri.arcgis.interop.Dispatch.c

Granted, ILayer is not behaving...but is the underlying problem with the Dispatch class, perhaps because you're communicating with ArcGIS Server?

Anyway, I'm out of my league here - still though, thank you for the painful stretch, lol!  Challenging problems keep us more active.


Maybe your exception is similar to this cause? --

Automation exception when getting layer
http://forums.esri.com/Thread.asp?c=158&f=1696&t=190489

http://forums.arcgis.com/threads/38272-Problem-to-connect-to-ArcGIS-server-from-a-java-program


-Wayne
0 Kudos