0

I am currently building a script to zoom to each providence within a country and export the providence and major cities (I have a layer for each) as a map package.

I need it to select the record, zoom to selected record, run a definition query, and export the result as a map package with labels as annotations, and repeat for each value within the preselected field.

This is what I have so far:

import arcpy, os

arcpy.env.overwriteOutput = True
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]

layerName = arcpy.GetParameterAsText(0)
fieldName = arcpy.GetParameterAsText(1)
imgLocation = arcpy.GetParameterAsText(2)
whereList = []

layerFile = arcpy.mapping.Layer(layerName)
layerFile.definitionQuery = ""

print df.name

with arcpy.da.SearchCursor(layerName, fieldName) as cursor:
    for row in cursor:
        for item in row:
            whereList.append(item)

for wl in whereList:
    layerFile.definitionQuery = fieldName + "= '" + wl + "'"
    outFile =  imgLocation + "\\" + wl + ".png"
    arcpy.RefreshActiveView()
    arcpy.mapping.ExportToPNG(mxd, outFile, df, resolution=300)

del mxd

Currently the script does export a .png image, however it does not zoom to each selected record. I need some tips for fixing this as well as exporting out each individual result as a map package with annotative labels.

  • Welcome to GIS SE! As a new user be sure to take the Tour to learn about our focussed Q&A format. You say that "Currently the script ... does not zoom to each selected record." so that is what you should focus your question on, by removing any code that is not related to that. Have you searched on "zoom selected feature" to see if the solution to that is already available here or elsewhere? – PolyGeo Dec 8 '17 at 0:59
  • By "each providence within a country" do you perhaps mean "each province within a country"? – PolyGeo Dec 8 '17 at 1:00
2

You need to get the extent of the selected feature then apply that extent to the data frame.

I've done something similar with a feature class that gets created via geoprocessing, you could adapt the logic.

traceExtent = arcpy.Describe("C4i_Trace_Buffer").Extent #this gets the extent of the input feature class - substitute with the extent of your selected feature.

newExtent = df.extent #this gets the current data frame extent
# set the new extent values
newExtent.XMin = traceExtent.XMin
newExtent.YMin = traceExtent.YMin
newExtent.XMax = traceExtent.XMax
newExtent.YMax = traceExtent.YMax

# apply the new extent to the dataframe
df.extent = newExtent

EDIT

You'd need to put it in the for wh in whereList: loop

In pseudo code it would be something like:

for wl in whereList:
    create feature layer based on definition query
    get extent of feature layer
    get extent of data frame as newExtent
    set extent of newExtent (based on feature)
    apply extent to data frame
    do other stuff (export png etc.)
  • Thank you for your input. Where in the above script should I fit this in? Thank you! – A. S. Dec 8 '17 at 1:19
  • When I run my script now, I am getting the following error: Traceback (most recent call last): File "<PATHNAME>, line 24, in <module> File "c:\program files (x86)\arcgis\desktop10.4\arcpy\arcpy_init_.py", line 1253, in Describe return gp.describe(value) File "c:\program files (x86)\arcgis\desktop10.4\arcpy\arcpy\geoprocessing_base.py", line 376, in describe self._gp.Describe(*gp_fixargs(args, True))) IOError: "C4i_Trace_Buffer" does not exist – A. S. Dec 8 '17 at 1:52
  • You'll need to substitute in your own values...I was only providing an example of the logic. – Dan Dec 8 '17 at 1:54
  • Have a look at the third answer on this question...gis.stackexchange.com/questions/1711/… – Dan Dec 8 '17 at 1:57

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