adding layer twice

510
7
Jump to solution
04-02-2014 12:52 PM
NoahHuntington
Occasional Contributor
I am not sure why layers are being added twice here?

import arcpy from arcpy import env import pythonaddins  class ButtonClass1(object):     """Implementation for add_Layers_addin.button (Button)"""     def __init__(self):         self.enabled = True         self.checked = False     def onClick(self):         #set the map document reference         mxd = arcpy.mapping.MapDocument("CURRENT")          #set the dataframe reference using the map document and the first data frame in the list         df = arcpy.mapping.ListDataFrames(mxd)[0]          #set the layer reference using the map document reference and the first layer in the list of layers         layer1 = arcpy.mapping.ListLayers(mxd)[0]          symbologyLayer = r'G:\Texas\Potter-Randall\Database\Ownership.lyr'          #create a search cursor to get relevant information about a parcel         sc = arcpy.SearchCursor(layer1)          #for loop to get the selected data         for row in sc:             #select the relevant fields to be put on the map             ap = row.getValue("MAP_NUMBER")             addLayer = arcpy.mapping.Layer(r'G:\Texas\Potter-Randall\Database' + '\\' + str(ap) + '\\' + str(ap) + ".gdb" + '\\' + str(ap))             arcpy.ApplySymbologyFromLayer_management (addLayer, symbologyLayer)             arcpy.mapping.AddLayer(df, addLayer, "TOP")          #zoom to selected features         df.zoomToSelectedFeatures()         #refresh the active view         arcpy.RefreshActiveView()          #delete map, layer and dataframe variables         del mxd         del layer         del df         del ap         del sc         del row 
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JasonScheirer
Occasional Contributor III
Ah, yes, because the output of the ApplySymbologyFromLayer tool is a layer it adds it to the TOC once it's executed, then you were manually adding it again.

View solution in original post

0 Kudos
7 Replies
MikeMacRae
Occasional Contributor III
Within your loop on layer1, are the items you are adding the same as what is already in the CURRENT mxd?
0 Kudos
AdamCox1
Occasional Contributor II
It may have something to do with your geoprocessing options.  Try Geoprocessing > Geoprocessing Options and experiment with the "Add results of geoprocessing operations to the display" checkbox.
0 Kudos
NoahHuntington
Occasional Contributor
Within your loop on layer1, are the items you are adding the same as what is already in the CURRENT mxd?


No.  One (different) layer in mxd unitl addLayer is added.  Sorry about your leg...
0 Kudos
NoahHuntington
Occasional Contributor
Alhtough I do not completely understand why? It looks like I was able to fix the problem by commenting out the arcpy.mapping.addLayer function.  I wasnt aware it had the functionality but it looks as if the applysymbology function adds the data to the map?

anyway the following works?
    def __init__(self):
        self.enabled = True
        self.checked = False
    def onClick(self):
        #set the map document reference
        mxd = arcpy.mapping.MapDocument("CURRENT")

        #set the dataframe reference using the map document and the first data frame in the list
        df = arcpy.mapping.ListDataFrames(mxd)[0]

        #set the layer reference using the map document reference and the first layer in the list of layers
        layer1 = arcpy.mapping.ListLayers(mxd, "AP_Map_Numbers", df)[0]

        symbologyLayer = r'G:\Texas\Potter-Randall\Database\Ownership.lyr'

        #create a search cursor to get relevant information about a parcel
        sc = arcpy.SearchCursor(layer1)

        #for loop to get the selected data
        for row in sc:
            #select the relevant fields to be put on the map
            ap = row.getValue("MAP_NUMBER")
            addLayer = arcpy.mapping.Layer(r'G:\Texas\Potter-Randall\Database' + '\\' + str(ap) + '\\' + str(ap) + ".gdb" + '\\' + str(ap))
            arcpy.ApplySymbologyFromLayer_management (addLayer, symbologyLayer)
            #arcpy.mapping.AddLayer(df, addLayer, "TOP")

        #zoom to selected features
        df.zoomToSelectedFeatures()
        #refresh the active view
        arcpy.RefreshActiveView()

        #delete map, layer and dataframe variables
        del mxd
        del layer
        del df
        del ap
        del sc
        del row
0 Kudos
JasonScheirer
Occasional Contributor III
Ah, yes, because the output of the ApplySymbologyFromLayer tool is a layer it adds it to the TOC once it's executed, then you were manually adding it again.
0 Kudos
AdamCox1
Occasional Contributor II
Looks like in 10.1 (though not in 10.0) this can be controlled as a property of the env class.

http://resources.arcgis.com/en/help/main/10.1/index.html#/env/018z0000004s000000/
0 Kudos
NoahHuntington
Occasional Contributor
Thank you!! This has all been very helpful.
0 Kudos