1

I'm just trying to create a tool that zooms in on a layer and then exports the image as PDF, I'll add more functionality later. The code below will run and produce a PDF, but won't zoom to the layer. While running the same basic code in the Python window inexplicably works just fine. I've tried using zoomToSelectedFeatures and definitionQuery, both yield the same result. So now I'm rather lost.

import arcpy, csv, os
pole=arcpy.GetParameterAsText(0)
worksheet=arcpy.GetParameterAsText(1)
out=arcpy.GetParameterAsText(2)+"\_"
mxd=arcpy.mapping.MapDocument("CURRENT")
df=arcpy.mapping.ListDataFrames(mxd)[0]
i=0
worksheet=r'C:\Users\Samuel\worksheet.csv'
with open(worksheet) as f:
 for row in csv.reader(f):
 i=row[0]
 text=row[1]
 arcpy.MakeFeatureLayer_management("pole","lyr","FID="+str(i))
 arcpy.SelectLayerByAttribute_management("lyr","NEW_SELECTION")
 df.zoomToSelectedFeatures()
 arcpy.SelectLayerByAttribute_management("lyr","CLEAR_SELECTION")
 arcpy.RefreshActiveView()
 arcpy.mapping.ExportToPDF(mxd, out+"_pole_"+i,"PAGE_LAYOUT")
 break
arcpy.Delete_management("lyr")
Vince
20.5k16 gold badges49 silver badges65 bronze badges
asked Mar 11, 2017 at 20:13
3
  • How are you running the script? From a script tool or from an IDE? Commented Mar 11, 2017 at 20:28
  • 1
    Remove the quotes from around pole as that is a string variable not text, from this line: arcpy.MakeFeatureLayer_management("pole","lyr","FID="+str(i)) Commented Mar 11, 2017 at 20:29
  • 2
    You can't us "current" outside arcmap. Current doesn't exist when it's just a stand alone script. Replace it with the path to the mxd on disk Commented Mar 11, 2017 at 23:09

1 Answer 1

1

I've had varied results trying to use df.zoomToSelectedFeatures() so I tend to use lyr.getSelectedExtent() and df.extent() instead.

import arcpy, csv, os
pole = arcpy.GetParameterAsText(0)
#worksheet=arcpy.GetParameterAsText(1)
out = arcpy.GetParameterAsText(2) + "\_"
worksheet=r'C:\Users\Samuel\worksheet.csv'
mxd = arcpy.mapping.MapDocument("CURRENT")
df = mxd.activeDataFrame
worksheet = r"D:\GIS\SE\output\worksheet.csv"
with open(worksheet) as f:
 for row in csv.reader(f):
 i = row[0]
 text = row[1]
 arcpy.MakeFeatureLayer_management(pole, "lyr", 'FID = {0}'.format(i))
 lyr = arcpy.mapping.Layer("lyr")
 arcpy.SelectLayerByAttribute_management(lyr, "NEW_SELECTION")
 df.extent = lyr.getSelectedExtent()
 df.scale *= 1.10 # Zoom it out by 10% to give a little space around the edges if required.
 arcpy.SelectLayerByAttribute_management(lyr, "CLEAR_SELECTION")
 arcpy.RefreshActiveView()
 arcpy.mapping.ExportToPDF(mxd, "{0}_pole_{1}".format(out, i), "PAGE_LAYOUT")
 arcpy.Delete_management(lyr)
 break

Also note that you had worksheet defined twice in your code (I commented out the first one for my testing so I didn't need to define in a tool, and there was no need to defineibefore yourwith`.

I also changed the df line to df = mxd.activeDataFrame out of habit (I think it's nicer to read) but either will work.

answered Mar 11, 2017 at 22:02
1
  • Thanks to all! Now I'm experiencing a very strange bug (might not be fixable). When imagery is in the map (ESRI basemaps) every iteration through the CSV (the for loop) the causes the map scale to increase until it shows the entire Continental US while maintaining the pole as the center. My best option would seem to be not using imagery or adjust the map scale according how much it zooms out each time. Commented Mar 14, 2017 at 18:15

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.