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")
1 Answer 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 define
ibefore your
with`.
I also changed the df
line to df = mxd.activeDataFrame
out of habit (I think it's nicer to read) but either will work.
-
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.Sam Dotson– Sam Dotson2017年03月14日 18:15:38 +00:00Commented Mar 14, 2017 at 18:15
Explore related questions
See similar questions with these tags.
arcpy.MakeFeatureLayer_management("pole","lyr","FID="+str(i))