I'm new to Python and Arcpy mapping. Has anyone built any code for exporting a layer of polygons one record at a time, I want to select each record, Zoom to selected and then export each view from the dataframe to jpeg with the worldfile. I want to name the files with the name of the polygone that is stored in the field name.
This is the start of my code. I'm stuck on selecting each record in a loop and then exporting to jpeg from the datafram not the layout view. I need to store the worrld file with each export.
import arcpy
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
for row in arcpy.SearchCursor (r"C:\GIS\Grid.shp"):
df.extent = row.Shape.extent
df.scale = df.scale *1.07
arcpy.mapping.ExportToJPEG(mxd, r"C:\GIS\OutputPages\Grid_name.jpg", df,
df_export_width=1600,
df_export_height=1200,
world_file=True)
I actually found someone elses code that worked. How to I change this to make the file name the name of each polygon. It's stored in the attribute table.
import arcpy
from arcpy import env
from arcpy import mapping
env.workspace = "CURRENT"
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
for lyr in arcpy.mapping.ListLayers(mxd):
lyr.visible = True
fc = r"C:\GIS\Grid.shp"
count = str(arcpy.GetCount_management(fc))
x = 0
while x < int(count) + 1:
rows = arcpy.SearchCursor(fc, "FID = " + str(x))
arcpy.SelectLayerByAttribute_management(lyr, "NEW_SELECTION", "FID = " + str(x))
for row in rows:
df.zoomToSelectedFeatures()
df.extent = lyr.getSelectedExtent()
arcpy.RefreshActiveView()
mapping.ExportToJPEG(mxd, r"C:\Temp\Map_" + str(x) + ".jpg", df, df_export_width=3004, df_export_height=2125, world_file=True)
print('Exported image',x, 'of', count)
x += 1
print("MXD to JPEGs Export successful")
print("Thank you!")
Assuming this code is working, Change this line from
mapping.ExportToJPEG(mxd, r"C:\Temp\Map_" + str(x) + ".jpg", df, df_export_width=3004, df_export_height=2125, world_file=True)
to
mapping.ExportToJPEG(mxd, r"C:\Temp\Map_" + str(row.getValue(<<fieldName>>)) + ".jpg", df, df_export_width=3004, df_export_height=2125, world_file=True)
where <<fieldName>>, is the name of the field which has polygon data.
Hi Mark,
I have some observations on the code you posted, since there some potential errors in the code:
So based on what I described above I would suggest using code like this:
import arcpy, os
def getLayerOnName(mxd, lyr_name):
for lyr in arcpy.mapping.ListLayers(mxd):
if lyr.name.upper() == lyr_name.upper():
return lyr
break
# some settings and variables
mxd_path = "CURRENT" # or use a path to the mxd file
df_name = "Layers"
lyr_name = "Grid"
fld_id = "FID" # "Name of the field that has a unique number for the output files"
jpg_folder = r"C:\Temp"
jpg_prefix = "Map_"
show_only_current_polygon = True
mxd = arcpy.mapping.MapDocument(mxd_path)
df = arcpy.mapping.ListDataFrames(mxd, df_name)[0]
lyr = getLayerOnName(mxd, lyr_name)
lyr_ds = lyr.dataSource
# loop through all features
flds = ("SHAPE@", fld_id)
with arcpy.da.SearchCursor(lyr_ds, flds) as curs:
for row in curs:
pol_ext = row[0].extent
pol_id = row[1]
# set a whereclause
if show_only_current_polygon:
wc = "{0} = {1}".format(arcpy.AddFieldDelimiters(lyr_ds, fld_id), pol_id)
lyr.definitionQuery = wc
df.extent = pol_ext
jpg_name = "{0}{1}.jpg".format(jpg_prefix, pol_id)
jpg_path = os.path.join(jpg_folder, jpg_name)
arcpy.mapping.ExportToJPEG(mxd, jpg_path, df, df_export_width=3004,
df_export_height=2125, world_file=True)
print "Exported: {0}".format(jpg_name)
What happens in this code is:
Give it a shot and see if it works...
Kind regards, Xander