Created
December 3, 2019 14:54
-
-
Save OterLabb/682aa098f81d748c6b19130828d796a2 to your computer and use it in GitHub Desktop.
Loop/zoom to features in a feature class and save as jpg
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment