Spatial Reference of ILayer C#

3335
3
04-17-2012 11:41 AM
EnriqueVicent
New Contributor
Hi everybody!

I want to Know how can I put the Spatial Reference of the layers of a Map in a message box.

     protected override void OnClick()
        {
            IActiveView activeView = ArcMap.Document.ActiveView;
            IMap focusMap = ArcMap.Document.FocusMap;
            ILayer layer;
            IEnumLayer layermap;
            layermap = focusMap.get_Layers(null, true);             

            layer = layermap.Next();

            while (layer != null)
            {
                MessageBox.Show("The spatial reference of the map is: " + focusMap.SpatialReference.Name +
                                 "the spatial reference of the layer is :" + layer.SpatialReference.Name);
    
                layer = layermap.Next();
            }

        }



The error appears when I define the property of the layer in the Message Box.

The property or indexer 'ESRI.ArcGIS.Carto.ILayer.SpatialReference' cannot be used in this context because it lacks the get accessor

I saw a lot of material but I don´t find how can I solve it.


Thanks
0 Kudos
3 Replies
KenBuja
MVP Esteemed Contributor
If you look at the help about ILayer::SpatialReference, you'll see that lets you set the property but not get it (i.e. you can't get information about it directly). There's a snippet called "Get Spatial Reference from Dataset" that will return the spatial reference. You'll be able to get its name from that.
0 Kudos
EnriqueVicent
New Contributor
Thank you very much Kenbunja! but I can´t find the way how can do it.

I´ll try it!

Thanks again
0 Kudos
KenBuja
MVP Esteemed Contributor
After you add that snippet to your code, use something similar to this

IDataset pDataset = default(IDataset);
ICompositeLayer pCompositeLayer = default(ICompositeLayer);
ISpatialReference pSR = default(ISpatialReference);

if (layer is ICompositeLayer) {
    pCompositeLayer = layer;
    for (l = 0; l <= pCompositeLayer.Count - 1; l++) {
        pDataset = pCompositeLayer.Layer(l);
        pSR = GetSpatialReferenceFromDataset(pDataset);
        if (pSR != null)
            MessageBox.Show("the spatial reference of the layer is :" + pSR.Name);
    }
} else {
    pDataset = layer;
    pSR = GetSpatialReferenceFromDataset(pDataset);
    if (pSR != null)
        MessageBox.Show("the spatial reference of the layer is :" + pSR.Name);
}
0 Kudos