Select to view content in your preferred language

Buffer not added to map

1467
6
Jump to solution
03-28-2012 05:29 AM
Zeke
by
Honored Contributor
When creating a buffer in a script, isn't it supposed to be added to the map by default? The code below creates the buffer just fine, but I have to add it to the map manually. Neither refreshing via code or with F5 or View - Refresh helps. I think this is the reason setting update_layer in the try block doesn't work. I know it's update_layer throwing the error because when I included it in the error message, that threw an error stating update_layer was undefined. Code & error below. Thanks for any ideas.

Code:
 import arcpy from arcpy import env  env.workspace = r"\\TestServer\TestCases_Geodatabase.gdb" mxd = arcpy.mapping.MapDocument("CURRENT")  df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]  case_number = arcpy.GetParameterAsText(0) #------------------------------------------------------------------------------- # Buffer Processing # def CreateBuffer():     # set buffer parameters     case = "Cases"     output ="Buffer_200"     dist = "200 feet"     side = "OUTSIDE_ONLY"       # delete existing temporary buffer, if any     if arcpy.Exists(output):         arcpy.Delete_management(output)      # create buffer     arcpy.Buffer_analysis(case, output, dist, side)      # update symbology     try:         source_layer = r"\\TestServer\Map Templates\GIS_Lyr_Files\Buffer.lyr"         update_layer = arcpy.mapping.ListLayers(mxd, "B*", df)[0]         arcpy.mapping.UpdateLayer(df, update_layer, source_layer, True)     except Exception as e:         arcpy.AddError("Updating symbology \n sl = " + source_layer + "\n" + e.message)  def main():     CreateBuffer()      arcpy.RefreshTOC     arcpy.RefreshActiveView  if __name__ == '__main__':     main()


Error:
Updating symbology   sl = \\TestServer\Map Templates\GIS_Lyr_Files\Buffer.lyr list index out of range Completed script Buffer200... Failed to execute (Buffer200). Failed at Wed Mar 28 08:22:06 2012 (Elapsed Time: 56.00 seconds)
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
DarrenWiens2
MVP Alum
I think your problem is stemming from the fact that ArcMap isn't smart enough to figure out that a path to a .lyr is a "Layer Object". You need to add arcpy.mapping.Layer() statements to make it clear. This is what I have for inside your try:
        source_layer = "H:/GIS_Data/Buffer.lyr" # point to your own .lyr         sourceLayer = arcpy.mapping.Layer(source_layer)          arcpy.MakeFeatureLayer_management(output, "Buffs")         addLayer = arcpy.mapping.Layer("Buffs")          arcpy.mapping.AddLayer(df,addLayer)                  update_layer = arcpy.mapping.ListLayers(mxd, "Buffs", df)[0]                  arcpy.AddMessage(source_layer)         arcpy.mapping.UpdateLayer(df, update_layer, sourceLayer, True)

View solution in original post

0 Kudos
6 Replies
MathewCoyle
Honored Contributor
To make that automatic, without adding any code, I believe you need to change the setting in ArcMap under Geoprocessing > Geoprocessing Options "Add results of geoprocessing operations to the display"
0 Kudos
Zeke
by
Honored Contributor
Thanks, that's a good thing to double check. The box in options is checked, though, so doesn't seem to be the issue.
0 Kudos
MathewCoyle
Honored Contributor
You can try using the make feature layer tool (http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//00170000006p000000) then updating the amphibology (edit: symbology of course, darn autocorrect, but was too funny to delete out) with the existing layer you have. If I am following what you are trying to accomplish.
0 Kudos
Zeke
by
Honored Contributor
Thanks, I'll look at that. My goal is to create a 200' buffer (that's working), display it on the map (nope, should be default?) and symbolize it based on a .lyr file - 2.00 red line (also no, although my guess is that's because the buffer itself doesn't show up).

Making another layer seems redundant, especially when it's not a problem to do manually, but maybe that's how it'll have to be. In tutorials the buffer layer always shows up.

This leads me into a pet peeve about ArcGIS help. It sometimes uses the term layer to refer to a layer in the map - shapefile, feature class, etc - and sometimes to only refer to a .lyr file only. Kind of sloppy editing there. Also, why have to use a .lyr file for operations that manually can use feature classes? Maybe 10.1 will do something about this.
0 Kudos
DarrenWiens2
MVP Alum
I think your problem is stemming from the fact that ArcMap isn't smart enough to figure out that a path to a .lyr is a "Layer Object". You need to add arcpy.mapping.Layer() statements to make it clear. This is what I have for inside your try:
        source_layer = "H:/GIS_Data/Buffer.lyr" # point to your own .lyr         sourceLayer = arcpy.mapping.Layer(source_layer)          arcpy.MakeFeatureLayer_management(output, "Buffs")         addLayer = arcpy.mapping.Layer("Buffs")          arcpy.mapping.AddLayer(df,addLayer)                  update_layer = arcpy.mapping.ListLayers(mxd, "Buffs", df)[0]                  arcpy.AddMessage(source_layer)         arcpy.mapping.UpdateLayer(df, update_layer, sourceLayer, True)
0 Kudos
Zeke
by
Honored Contributor
Well that does the job! Thanks Darren & Mathew, I very much appreciate it. Wish I could mark both posts as an answer, but gave both points.
I'm going to go through the code to see exactly what it's doing; still don't know why the buffer wouldn't be added automatically, but whatever. It's working and will make my job in the future much easier by letting others run the script and stop asking me to do it for them manually.
0 Kudos