Select to view content in your preferred language

Add data to existing layer file

1473
5
01-08-2013 09:15 PM
DamianMilne
Emerging Contributor
Ok, I thought this would be simple (perhaps it is)...how do I add additional data layers to an existing layer (.lyr) file using python?
Cheers, Damian
Tags (2)
0 Kudos
5 Replies
ArkadiuszMatoszka
Frequent Contributor
Hi,
What does "add additional data layers to an existing layer (.lyr)" stands for? You want to chage featureclass, definition query, addaditional data to GroupLayer lyr, or what?
Cheers
Arek
0 Kudos
DamianMilne
Emerging Contributor
Hi Arek, thanks for responding. If I wanted to manually add data to a .lyr file, I would go into ArcCatalog, right mouse click my .lyr file, then go "properties" -> "add", then select the data a want to add. I want to do the same thing except using python script (I have  approx 200 .lyr files I want to add data too).
Cheers, D
0 Kudos
ArkadiuszMatoszka
Frequent Contributor
OK, now i know what you trying to do.
Not sure this is the easiest way, but got no better idea.
My proposition is:
1) Create empty mxd file. - this is manual part - one mxd is enough.
Loop for each lyr:
2) Add lyr file to data frame in this mxd.
3) Read name of grouplayer from ListLayer in mxd.
4) Use AddLayerToGroup with data you want to use and group name read in previous point.
5) Save layer from mxd to lyr (function save() or saveACopy() on layer object from mxd).
6) Remove all layers from mxd
That's only idea I have. Maybe someone know easiest way.
Cheers
Arek
0 Kudos
DamianMilne
Emerging Contributor
Thanks Arek, I suspected there wasn't anyway to edit a .lyr file directly. Of well, I guess it's only a few extra lines of python code.
Cheers, D
0 Kudos
T__WayneWhitley
Honored Contributor
Just to add to the answer, use InsertLayer if you need additional control on exactly where to add the layer - the AddLayerToGroup only provides TOP and BOTTOM additional user-specified params (AUTO_ARRANGE is the default)...
Of course, you may ask, why 2 commands that do almost the same thing?  InsertLayer cannot add to an empty Data Frame or group layer.

InsertLayer
Resource Center » Professional Library » Geoprocessing » The ArcPy site package » Mapping module » Functions » Managing Documents and Layers
http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//00s30000000z000000
0 Kudos