2

I have a mxd with 3 dataframe: 'layers', 'geography' and 'book'. 'layers' dataframe has 2 layers: 'gridIndexFeatures' and 'zone'. 'geography' and 'book' datasets are empty.

I want to add the 'gridIndexFeatures' layer to 'book' dataframe by means of a Python script, but I want to check if that layers was previously added so as to not add it again.

I wrote the following code:

def cargacapas(capa, alias, dataframe, mxd_path):
 arcpy.MakeFeatureLayer_management(capa, alias)
 DF = arcpy.mapping.ListDataFrames(mxd_path)[dataframe]
 layer = arcpy.mapping.Layer(alias)
 if not arcpy.Exists(layer):
 arcpy.mapping.AddLayer(DF, layer, "AUTO_ARRANGE")
 else:
 print "it exists so I don't add it again"
 mxd_path.save()
 del mxd_path
mxd = arcpy.mapping.MapDocument(r"U:\Proyectos\book1.mxd")
grid_path = r"U:\Proyectos\EDAR.gdb\gridIndexFeatures"
inFeatures = r"U:\Proyectos\EDAR.gdb\zone" 
cargacapas(inFeatures, "Zona_de_ocupacion", 2, mxd)
cargacapas(grid_path, "Grid", 2, mxd)

My problem is that the EXISTS function detect 'gridIndexFeatures' exists in the 'layers' dataframe and return a True. But I need to detect if it exists in the 'book' dataframe.

Any help with this issue?


I have changed the code like this:

def cargacapas(capa, alias, dataframe, mxd_path):
 arcpy.MakeFeatureLayer_management(capa, alias)
 DF = arcpy.mapping.ListDataFrames(mxd_path)[dataframe]
 layer = arcpy.mapping.Layer(alias)
 if layer not in [lyr.name for lyr in arcpy.mapping.ListLayers(mxd_path,"", DF)]:
 arcpy.mapping.AddLayer(DF, layer, "AUTO_ARRANGE")
 else:
 print "it exists"
 mxd_path.save()
 del mxd_path

But it not seems to work propertly. Each time I run the code, it add the two layers, so it doesn't detect that those layers are already in the dataframe.

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Mar 1, 2018 at 13:06
0

3 Answers 3

2

arcpy.Exists checks for the existence of a layer in your current workspace (Esri), not in the current map dataframe. If you want to check if it exists in a dataframe (e.g. "book"), you could try something like...

def cargacapas(capa, alias, dataframe, mxd_path):
 arcpy.MakeFeatureLayer_management(capa, alias)
 bookDF = arcpy.ListDataFrames(mxd, "book")[0]
 layersDF = arcpy.ListDataFrames(mxd, "Layers")[0]
 layer = arcpy.mapping.ListLayers(mxd_path, alias, layersDF)[0]
 if layer.alias not in [lyr.alias for lyr in arcpy.mapping.ListLayers(mxd_path, "", bookDF)]:
 arcpy.mapping.AddLayer(bookDF, layer, "AUTO_ARRANGE")
 else:
 print "it exists so I don't add it again"
mxd_path.save()
del mxd_path
mxd = arcpy.mapping.MapDocument(r"U:\Proyectos\book1.mxd")
grid_path = r"U:\Proyectos\EDAR.gdb\gridIndexFeatures"
inFeatures = r"U:\Proyectos\EDAR.gdb\zone" 
cargacapas(inFeatures, "Zona_de_ocupacion", 2, mxd)
cargacapas(grid_path, "Grid", 2, mxd)

I've changed your reference to dataframe here as that's dependent on the order of your dataframes in the map doc. Feel free to remove the wildcards and change that back to your previous method, as long as bookDF and layersDF refer to the appropriate positions of those dataframes.

answered Mar 1, 2018 at 13:40
1
  • also check the second example here for some more info Commented Mar 1, 2018 at 14:46
0

Using the proposal of @Lovette but with some changes in the code I get one that seems to work fine.

def cargacapas(capa, alias, dataframe, mxd_path):
 arcpy.MakeFeatureLayer_management(capa, alias)
 bookDF = arcpy.mapping.ListDataFrames(mxd_path, "Book")[0]
 layer = arcpy.mapping.Layer(alias)
 if layer.name not in [lyr.name for lyr in arcpy.mapping.ListLayers(mxd_path,"", bookDF)]:
 arcpy.mapping.AddLayer(bookDF, layer, "AUTO_ARRANGE")
 else:
 print "it exists so I don't add it again"
 mxd_path.save()
 del mxd_path
mxd = arcpy.mapping.MapDocument(r"U:\Proyectos\book1.mxd")
grid_path = r"U:\Proyectos\EDAR.gdb\gridIndexFeatures"
inFeatures = r"U:\Proyectos\EDAR.gdb\zone" 
cargacapas(inFeatures, "Zona_de_ocupacion", 2, mxd)
cargacapas(grid_path, "Grid", 2, mxd)
answered Mar 1, 2018 at 16:30
-1

layer is a layer object, and you're checking it against lyr.name, which would be a string. Try if layer.name not in [lyr.name....

answered Mar 1, 2018 at 14:57

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.