Question

Is there a way in arcobjects to get a unique id for a layer? If you do a search by layer name there could be possible duplicates.

If there isn't a property is there a way to generate an id? I tried using the GetHash() but that didn't stay consistent.

Was it helpful?

Solution

It isn't pretty, but in the past I've appended a guid in the layer description. Something like this:

<LAYER guid='a9843c88-3caa-4953-ad96-ca9990b410e9' revision='1' />

I've got a DLL floating around that would slam these xml frags into each layer of an MXD (with enough cr/lf in front to scroll the xml fragment out of the layer description in ArcMap Layer Prop dialog) .

There's a help file in the 7z file (documentation is sparse because I'm doing other things): http://code.google.com/p/umbriel/downloads/list

OTHER TIPS

There is an ArcObjects Interface present for setting or getting an Id for a layer. You should look at ILayerDescriptor:ID, http://resources.esri.com/help/9.3/ArcGISDesktop/ArcObjects/esriCarto/ILayerDescriptor_ID.htm

Here is a VBA Snippet which shows how it can be used:

Public Sub layerInfo()

Dim app As IApplication '
Set app = Application

Dim mxDoc As IMxDocument
Set mxDoc = app.Document

Dim myMap As IMap
Set myMap = mxDoc.ActiveView

Dim mapServer As IMxdServer
Set mapServer = New MxdServer

'''Point to your .mxd...
mapServer.Start ("D:\Test.mxd")

Dim myArray As IArray
Set myArray = mapServer.LayerDescriptors(myMap.Name)

MsgBox myArray.Count

Dim x As ILayerDescriptor
Dim intX As Integer
intX = 0

For intX = 0 To myArray.Count - 1
Set x = myArray.Element(intX)
MsgBox x.ID
MsgBox x.Name
Next

End Sub

I like the idea of using a GUID. This can then be stored in the ModelName property which is a tool for developers of custom objects to use to guarantee the names of objects independent of the true name or alias name.

There are more details and sample code at http://geographika.co.uk/?p=58

Easy. A side effect of using COM and because how the vtables are laid out, is that you can use the memory address of the layer itself as your unique identifier. Inside the implementation of many ESRI GeoDatabase and Carto code itself, this trick is being used all over the place.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top