afischbach

updating extent of dataframe

Discussion created by afischbach on Mar 2, 2012
Latest reply on Mar 5, 2012 by afischbach
I wish to work through a long list of tracked animal behavior bouts,
updating my map layer definitions to the depict behaviors from this bout,
zooming to the behaviors within this bout, and exporting the resultant map as a graphic.

Here is a script outline to do this.

The problem is that the map extent does not get updated after the first bout is handled.

Have I missed a trick to ensure that the map extent is properly updated and the result
reflected in the output map?



import pyodbc
cnxn = pyodbc.connect(DRIVER, DBQ=  db, autocommit=True)
cursor = cnxn.cursor()
SQL = "SELECT * from trackedAnimalBehaviorBout_tbl"
cursor.execute(SQL)

import arcpy
for row in cursor.fetchall():
Bout = row.Bout ## grab behavior bout from trackedAnimalBehaviorBout_tbl

mxd=arcpy.mapping.MapDocument(MYMXD) ## Open an mxd
df=arcpy.mapping.ListDataFrames(mxd)[0] ## grab the first dataframe
for lyr in arcpy.mapping.ListLayers(mxd): ## Loop through list of layers in the dataframe
  if lyr.name == "BehaviorX":    ## Handle layer for 'BehaviorX'
            dfQ = "[BehaviorX] = 1 and [Bout] = '" + Bout + "'"
            lyr.definitionQuery = dfQ   ## Update layer def query
            BehaveBoutExtent = lyr.getExtent() ## grab hold of Exent of this layer

try: ## Try to zoom the dataframe to the extent of Behavior X for row.Bout
        print ("updating df extent from ")
        print df.extent
        print "to"
        print BehaveBoutExtent
        df.extent = BehaveBoutExtent   ## Zoom to the BehaveBoutExtent
        print("Updated Extent to")
        print df.extent

try: ## Export to a graphic file for later reveiw
  mxd.save()
  png = (r"\\" + r"MYFILELOCATION" + os.sep + str(Bout) + ".png")
        arcpy.mapping.ExportToPNG(mxd,png, resolution=80)

Outcomes