Hello,
I'm trying to create an mxd that can dynamically create and name group layers, and populate them with various feature layers. I've seen some examples of how to do this, such as : How to add a new Group Layer using arcpy
The basics seem to be: reference an empty layer:
groupLayer = arcpy.mapping.Layer(r"D:\Test\GroupTest.lyr")
Add it to the map:
arcpy.mapping.AddLayer(df, groupLayer, "BOTTOM")
listedGroupLayer = arcpy.mapping.ListLayers(mxd, "GroupTest", df)[0]
Then add some layer to it;
addLayer = arcpy.mapping.layer(r"D\Test\Rivers.lyr")
arcpy.mapping.AddLayerToGroup(df, listedGroupLayer, addLayer, "BOTTOM")[0]
I'm getting stuck on the first step: I need to dynamically create an empty layer file (based off of group layer names in a spreadsheet), before referencing it.
Any examples of how to create an empty layer file (rather than reference one), within python?
Thank you,
David
I'm working on something similar (replacing broken links) and yesterday, I just reached the point I'm working on Server connections. With those I will be doing an addLayer and removeLayer in a python script..
The way I am dynamically creating the new layer names is to read it from my excel file, saved to a .csv that I can then read in. You would have to work out how to organize you structures with loops of some type (assuming you are creating a variety of groups and/or layers.
In short, I'm using
with open(inFile) as inFix: reader = csv.reader(inFix) for row in reader:
to open and read the file. I then have to do parsing to get the correct string for the input parameters. Again, just started working with the parsing for my new Layer last night, so not much more to offer right now, plus, I'm not working with groups right now. Hope this helps point you in he right direction.
Thank you, Rebecca, this is a help.
David
This link might help as a workaround. Basically create an empty group layer file manually to use in your script (as long as you don't save it back out and overwrite it you can keep using it):
Matt,
Thanks! That's a great link. I'm looking to see if I could somehow do that first manual step - dynamically in arcpy. Your solution totally works, and it's not the end of the world if I do it manually, but I'm just trying to modularize my script a bit more (using it in a lot of places, with a lot of mxd's and group layers).
David
I don't think arcpy provides a way to create a group layer you may have to create an empty group layer and package it with your code.Here is how to get the base path of where the script lives
hmpath = os.path.realpath(__file__) h,t = os.path.split(hmpath)
This will work great if he's creating a python add-in.