How to dynamically create an empty layer file (for use as a group layer)?

4653
6
08-20-2015 08:21 AM
DavidMarquardt
New Contributor III

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

0 Kudos
6 Replies
RebeccaStrauch__GISP
MVP Emeritus

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.

DavidMarquardt
New Contributor III

Thank you, Rebecca, this is a help.

David

0 Kudos
MatSavage1
New Contributor III

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):

http://gis.stackexchange.com/a/4684/2436

DavidMarquardt
New Contributor III

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

0 Kudos
WesMiller
Regular Contributor III

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)
MatSavage1
New Contributor III

This will work great if he's creating a python add-in.

0 Kudos