0

I have a task where I have several polygons and I have several data frames in which each has a different temporal image. I have linked the data frames to one main data frame in which I add a polygon and all the data frames zoom to that extent of the main frame and I have to export the map. I have thousands of polygons I have managed to automate the process. Problem is I need to delete the shapefile once the map is exported and add the next shapefile. The remove layer doesn't work and the maps show overlaying boundaries. Code:

folder = "D:\TNIAMP\shapes"
arcpy.env.workspace = folder
mxd = arcpy.mapping.MapDocument("CURRENT")
fcList = arcpy.ListFeatureClasses()
df = arcpy.mapping.ListDataFrames(mxd, "*")[0]
for fc in fcList:
    shape = arcpy.mapping.Layer(fc)
    arcpy.mapping.AddLayer(df, shape)
    ext = shape.getExtent()
    df.extent = ext
    arcpy.RefreshActiveView()
    arcpy.mapping.ExportToJPEG(mxd,"D:\TNIAMP\shapes\maps\{0}.jpg".format(Tank_name))
    arcpy.mapping.RemoveLayer(df, shape)
    arcpy.RefreshActiveView()
    arcpy.RefreshTOC()
    mxd.save()

2 Answers 2

1

The issues is that your layer object shape is constructed from a Feature Class which you then add to the map. It is not a layer you have found in a map. Look at the sample code in the help file, see how they remove layers that were found in the map.

0

I tried

for lyr in arcpy.mapping.ListLayers(mxd, "", df):
    arcpy.mapping.RemoveLayer(df, lyr)

and it worked...

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.