I am using a definition query to produce species distribution maps and having issues setting the extent of the map (similar to zoom to layer in arcMap's TOC). I think it has to do with the loop, but I'm not sure. Any help or tips are greatly appreciated.
Can you be more specific about what you expect to happen and what is happening? Your script is doing a variety of things, have you stripped it down to focus only on the parts that you believe aren't working?
The script you uploaded seems incomplete. For example, there is a call to Select Layer By Attribute to clear a selection but a selection is never made in the script. Is there a selection being made before the script executes? If so, what is it?
Where is this being executed? In a tool, in the interactive Python window, etc...?
What version of ArcMap are you using?
My script uses a Look up table to grab a species code - that code is used for the definition query on two shapefiles (a state layer and a county layer). This part is working perfectly. The Select Layer by Attribute is unnecessary, I don't want to have to make a selection and I think I should be able to do this without making a selection. What I wanted to do is zoom to the extent of the state layer after the definition query is performed. Right now the extent just keeps zooming out due to the line where I multiple the scale by 1.1. If I remove that line, then the scale never changes. I thought the getExtent method is what I needed to zoom to the visible extent of the layer after the definition query. does that help clarify?
Oh, I am also doing this in IDLE so it runs outside of ArcMap v. 10.3.1 - a stand alone script.
Are you sure the definition queries are actually selecting records? It looks like you are building queries to select text, but I don't see embedded quotes around the text on the right side of the equals sign.
the definition queries are working. It is outputting roughly 90 maps which show the different distribution for each species. So the layer's symbology changes with each new definition query. I think it might look like it's selecting text because I use a variable name in the definition query.
Anyway, that part works perfectly - its the zoom to layer that I can't get to work. It seems to only zoom to the entire US, not the states that are involved in the definition query. I thought it might have something to do with the format of the loop. All the examples I have found of zooming to a layer's via the definition query seem so simple and straightforward - though it doesn't work for me - so it could very well be that the code is malfunctioning somewhere else.
I also wonder if it would just be easier to create a selection set to zoom to, but I don't want the selection highlighted on the map.
I finally got this to work - though I had to create a copy of my queried feature class. Here's the code in case it helps anyone.
# 1. Import modules and set environment import arcpy, os, sys, traceback from arcpy import env env.workspace = r"F:\Workspace\AFPE\NRS03_AFPE.gdb" path = 'F:\\Workspace\\AFPE\\2016\\RawMaps2\\' # stop temp layer from being added to map arcpy.env.addOutputsToMap = False # 2. Open a map document - F:\Workspace\AFPE\2016\RawMaps\test.mxd mxd = arcpy.mapping.MapDocument(r"F:\Workspace\AFPE\2016\RawMaps2\14004.mxd") df = arcpy.mapping.ListDataFrames(mxd) try: # 3. grab USAState layer lyr = arcpy.mapping.ListLayers(mxd,"USAState",df) stLyr = lyr print df.scale # 4. make duplicate layer of queried USAState and zoom to extent arcpy.CopyFeatures_management(stLyr, r"in_memory\temp") desc = arcpy.Describe(r"in_memory\temp") ext = desc.extent mxd.activeDataFrame.extent = ext arcpy.RefreshActiveView() print df.scale # 5. clean up del df, mxd, lyr, stLyr, env arcpy.Delete_management(r"in_memory\temp") print 'Map extent complete.' except: print 'Program failed.' del df, mxd, lyr, stLyr, env arcpy.Delete_management(r"in_memory\temp") tb = sys.exc_info() tbinfo = traceback.format_tb(tb) pymsg = "PYTHON ERRORS:\nTraceback Info:\n" + tbinfo + "\nError Info:\n " + str(sys.exc_type) + ": " + str(sys.exc_value) + "\n" msgs = "ARCPY ERRORS:\n" + arcpy.GetMessages(2) + "\n" arcpy.AddError(msgs) arcpy.AddError(pymsg) print msgs print pymsg arcpy.AddMessage(arcpy.GetMessages(1)) print arcpy.GetMessages(1)