<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Issues setting Map extent in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/issues-setting-map-extent/m-p/624959#M48736</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I finally got this to work - though I had to create a copy of my queried feature class.&amp;nbsp; Here's the code in case it helps anyone.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;# 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)[0]

try:
 # 3. grab USAState layer 
 lyr = arcpy.mapping.ListLayers(mxd,"USAState",df)
 stLyr = lyr[0]
 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()[2]
 tbinfo = traceback.format_tb(tb)[0]
 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)&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sun, 12 Dec 2021 16:42:56 GMT</pubDate>
    <dc:creator>LauraBlackburn</dc:creator>
    <dc:date>2021-12-12T16:42:56Z</dc:date>
    <item>
      <title>Issues setting Map extent</title>
      <link>https://community.esri.com/t5/python-questions/issues-setting-map-extent/m-p/624954#M48731</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;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).&amp;nbsp; I think it has to do with the loop, but I'm not sure.&amp;nbsp; Any help or tips are greatly appreciated.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 01 Sep 2016 18:09:38 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/issues-setting-map-extent/m-p/624954#M48731</guid>
      <dc:creator>LauraBlackburn</dc:creator>
      <dc:date>2016-09-01T18:09:38Z</dc:date>
    </item>
    <item>
      <title>Re: Issues setting Map extent</title>
      <link>https://community.esri.com/t5/python-questions/issues-setting-map-extent/m-p/624955#M48732</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Can you be more specific about what you expect to happen and what is happening?&amp;nbsp; 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?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The script you uploaded seems incomplete.&amp;nbsp; For example, there is a call to Select Layer By Attribute to clear a selection but a selection is never made in the script.&amp;nbsp; Is there a selection being made before the script executes?&amp;nbsp; If so, what is it?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Where is this being executed?&amp;nbsp; In a tool, in the interactive Python window, etc...?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;What version of ArcMap are you using?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 02 Sep 2016 14:46:11 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/issues-setting-map-extent/m-p/624955#M48732</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2016-09-02T14:46:11Z</dc:date>
    </item>
    <item>
      <title>Re: Issues setting Map extent</title>
      <link>https://community.esri.com/t5/python-questions/issues-setting-map-extent/m-p/624956#M48733</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Joshua,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;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).&amp;nbsp; This part is working perfectly.&amp;nbsp; 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.&amp;nbsp; What I wanted to do is zoom to the extent of the state layer after the definition query is performed.&amp;nbsp; Right now the extent just keeps zooming out due to the line where I multiple the scale by 1.1.&amp;nbsp; If I remove that line, then the scale never changes.&amp;nbsp; I thought the getExtent method is what I needed to zoom to the visible extent of the layer after the definition query.&amp;nbsp; does that help clarify?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Oh, I am also doing this in IDLE so it runs outside of ArcMap v. 10.3.1 - a stand alone script.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 02 Sep 2016 14:55:51 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/issues-setting-map-extent/m-p/624956#M48733</guid>
      <dc:creator>LauraBlackburn</dc:creator>
      <dc:date>2016-09-02T14:55:51Z</dc:date>
    </item>
    <item>
      <title>Re: Issues setting Map extent</title>
      <link>https://community.esri.com/t5/python-questions/issues-setting-map-extent/m-p/624957#M48734</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Are you sure the definition queries are actually selecting records?&amp;nbsp; 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.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 02 Sep 2016 15:22:13 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/issues-setting-map-extent/m-p/624957#M48734</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2016-09-02T15:22:13Z</dc:date>
    </item>
    <item>
      <title>Re: Issues setting Map extent</title>
      <link>https://community.esri.com/t5/python-questions/issues-setting-map-extent/m-p/624958#M48735</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;the definition queries are working.&amp;nbsp; 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.&amp;nbsp; &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Anyway, that part works perfectly - its the zoom to layer that I can't get to work.&amp;nbsp; It seems to only zoom to the entire US, not the states that are involved in the definition query.&amp;nbsp; I thought it might have something to do with the format of the loop.&amp;nbsp; 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.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 02 Sep 2016 18:24:06 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/issues-setting-map-extent/m-p/624958#M48735</guid>
      <dc:creator>LauraBlackburn</dc:creator>
      <dc:date>2016-09-02T18:24:06Z</dc:date>
    </item>
    <item>
      <title>Re: Issues setting Map extent</title>
      <link>https://community.esri.com/t5/python-questions/issues-setting-map-extent/m-p/624959#M48736</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I finally got this to work - though I had to create a copy of my queried feature class.&amp;nbsp; Here's the code in case it helps anyone.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;# 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)[0]

try:
 # 3. grab USAState layer 
 lyr = arcpy.mapping.ListLayers(mxd,"USAState",df)
 stLyr = lyr[0]
 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()[2]
 tbinfo = traceback.format_tb(tb)[0]
 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)&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 16:42:56 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/issues-setting-map-extent/m-p/624959#M48736</guid>
      <dc:creator>LauraBlackburn</dc:creator>
      <dc:date>2021-12-12T16:42:56Z</dc:date>
    </item>
  </channel>
</rss>

