I'm having a hard time adding my grid to the map and changing its symbology with an existing lyr file. I have found a great post by Neil Clemmons that explains this, but it doesn't work for me. I tried this code:

Dim map As IMap = pMxDoc.FocusMap

' make new layerfile and open lyr file 

Dim layerFile As ILayerFile = New LayerFileClass
Dim layerFilePath As String = [my layer file path]
Dim layer As ILayer = layerFile.Layer

' cast layer to rasterlayer and add grid with content 

Dim rasterLayer As IRasterLayer = TryCast(layer, IRasterLayer)

' not able to do this casting with a raster
' layerFile.ReplaceContents(CType(myGrid, ILayer))

Dim map As IMap = mxDoc.FocusMap

The layer gets formatted and added to the map but it seems 'empty'

table of content

Layer properties

  • @Hornbydd No, just objectID, value and count fields. It works when I add the lyr file manually and change its datasource. – bvh9000 Sep 2 '15 at 12:35

Okay, I found a partial answer to this question. The reason why the layer showed up empty has to with the way I used a reference to the document. I opened up a new mxdocument from my unit test, but the tool I'm working on is an add-in so I need the correct IApplication reference. I isolated the problem in a fresh solution so I could test it (you can't really run methods that need IApplication from a unit test so it seems, you really need to run the whole thing!)



Finally found the right way to apply a layer's symbology on another layer.

The trick is to use (copy) the renderer from your symbology layer and assign it to the new layer. That way the original lyr file also stays intact.

So, in general, this is what you do:


            ' make a new feature layer and add the new feature class to it'
            Dim pFeatLayer As IFeatureLayer = New FeatureLayer
            pFeatLayer.FeatureClass = vrijboordFc
            pFeatLayer.Name = vrijboordFc.AliasName

            ' add the featureLayer to the map'
            Dim pMap As IMap = mxdoc.FocusMap

            ' get the symbology lyr file'
            Dim symLayer As ILayer = OpenLayerFile(pLayerFilePath)

            ' change the layer symbology'
            ChangeLayerSymbology(pFeatLayer, symLayer)

            ' refresh and zoom to extent'
            Dim activeView As IActiveView = CType(pMap, IActiveView)
            ZoomToDefaultExtent(CType(pMap, IMapDocument), pFeatLayer)

        Catch ex As Exception

            Throw New Exception(String.Format("Layer '{0}' could not be added to the map", vrijboordFc.AliasName), ex.Message)

        End Try

         Public Shared Sub ChangeLayerSymbology(ByVal contentLayer As IGeoFeatureLayer, ByVal symbologyLayer As IGeoFeatureLayer)

        ' Assign renderer from layer file to the layer in ArcMap.'
        ' First store the old renderer so we can re-assign it if an error occurs.'
        Dim oldRenderer As IFeatureRenderer = contentLayer.Renderer

            If contentLayer IsNot Nothing AndAlso symbologyLayer IsNot Nothing Then
                Dim pCopy As IObjectCopy = New ObjectCopy
                contentLayer.Renderer = DirectCast(pCopy.Copy(symbologyLayer.Renderer), IFeatureRenderer)
            End If

        Catch ex As Exception

            ' Restore the old renderer, before you wrap the exception'
            contentLayer.Renderer = oldRenderer

            Dim msg As String = String.Format("Could not change renderer of layer ""{0}"" to renderer of layer ""{1}"" .", contentLayer.Name, symbologyLayer.Name)
            Throw New exception(msg, ex)

        End Try

    End Sub

I think you are on the right way, but you should create the rasterlayer from the dataset as a separate layer first and then change the content of the layerfile. Something like this(don't know VB, casting IRasterLayer to ILayer):

Dim rasterLayer As ESRI.ArcGIS.Carto.IRasterLayer = New RasterLayerClass()

layerFile.ReplaceContents(CType(rasterLayer, ILayer))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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