import arcpy from arcpy import env # for setting overall extent layerDict = {} # dictionary of name (key) and layer (value) for all map layers in the folder def GetExtentOfAllLayers(extents): # takes a list of extents and finds the overall minimum bounding rectangle (MBR) # This works, but can't be used as I wanted to set full extent for data frame # initialize overall MBR values XMin = 180.0 # init min values with max possible value XMax = -180.0 # init max values with min possible value YMin = 90.0 YMax = -90.0 # for each extent, see if the extent exceeds previous overall extent for e in extents: if e.XMin < XMin: XMin = e.XMin if e.XMax > XMax: XMax = e.XMax if e.YMin < YMin: YMin = e.YMin if e.YMax > YMax: YMax = e.YMax # instantiate and return an arcpy.Extent object with final MBR values return arcpy.Extent(XMin, YMin, XMax, YMax, None, None, None, None) def ZoomToLayer(layerName): mxd = arcpy.mapping.MapDocument("CURRENT") df = arcpy.mapping.ListDataFrames(mxd, '')[0] lyr = layerDict[layerName] # this code searches for a name as a dictionary key (faster) df.extent = lyr.getExtent() #Refresh Table of Contents arcpy.RefreshTOC() #Refresh Active View arcpy.RefreshActiveView() def DeleteAllLayers(): # Delete all layers # this is just a demo--real method would ask for confirmation mxd = arcpy.mapping.MapDocument("CURRENT") df = arcpy.mapping.ListDataFrames(mxd, '')[0] for i in arcpy.mapping.ListLayers(mxd , ""): print "Deleting layer", i arcpy.mapping.RemoveLayer(df , i) arcpy.RefreshTOC() arcpy.RefreshActiveView() def BringToTop(layerName): mxd = arcpy.mapping.MapDocument("CURRENT") df = arcpy.mapping.ListDataFrames(mxd, '')[0] topLayer = arcpy.mapping.ListLayers(mxd , "")[0] for lyr in arcpy.mapping.ListLayers(mxd , ""): if lyr.name == layerName: arcpy.mapping.MoveLayer(df, topLayer, lyr, 'BEFORE') # Following is correct approach but broken: # print lyr.name, ' is a layer == ', isinstance(lyr, arcpy.mapping.Layer) # lyr = layerDict[layerName] # this code searches for a name as a dictionary key (faster) # print 'layer name: ', lyr.name # arcpy.mapping.MoveLayer(df, topLayer, lyr, 'BEFORE') def LoadAllMyShapefiles(mypath): # Set the workspace for the ListFeatureClass function env.workspace = mypath # Use the ListFeatureClasses function to return a list of # all shapefiles. fcList = arcpy.ListFeatureClasses() print 'the workspace ' + env.workspace + ' contains ' + str(len(fcList)) + ' shapefiles.' # get the current map document and first dataframe mxd = arcpy.mapping.MapDocument("CURRENT") df = arcpy.mapping.ListDataFrames(mxd, '')[0] for fc in fcList: # for each shapefile found in the folder... fcStr = str(fc) # convert the unicode string fc to a regular string fcStrBase = fcStr.split('.') # split shapefilename into 2 pieces separated by . char pathToFC = env.workspace + '/' + fc # create a complete path that includes the shapefile newlayer = arcpy.mapping.Layer(pathToFC) # create a layer from the path layerDict[fcStrBase[0]] = newlayer # use shapefile base name as key in dictionary arcpy.mapping.AddLayer(df,newlayer,"BOTTOM") # add the layer to the dataframe #Refresh Table of Contents arcpy.RefreshTOC() #Refresh Active View arcpy.RefreshActiveView() print layerDict