Select to view content in your preferred language

loop through a layer and select each polygon and save as jpeg in ArcMap

2528
2
Jump to solution
12-24-2020 08:07 AM
Official4
New Contributor
1

I'm a beginner in ArcPy and I want to perform some tasks using ArcPy but I am not getting results.

I have a layer named survey in my current MXD and I want to select each polygon in surveyand export to JPEG and this is my script:

mxd = arcpy.mapping.MapDocument("CURRENT") 
df = arcpy.mapping.ListDataFrames(mxd)[0]
titleElement = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT")

rows = arcpy.SearchCursor("survey selection")
for row in rows: 
    titleElement = row.VendorFarm 
    query = '"FID"=' + str(row.FID)    
    arcpy.SelectLayerByAttribute_management("survey selection", "NEW_SELECTION", query)     
    df.zoomToSelectedFeatures()      
    arcpy.RefreshActiveView()

del rows 
del row
1 Solution

Accepted Solutions
RandyBurton
MVP Alum

As @DavidPike suggests, you should use the da.SearchCursor.  I noticed that you are attempting to access and set a map title using ListLayoutElements .  This will return a list, and you will need to access the proper text element.  It might be easier if you use the dynamic map "Title" as this property can be accessed and changed.  Here's a code snippet which illustrates these ideas.  Since it looks like you are using shape files, you may need to make some changes.

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

feature = 'survey selection'
fields = ['OID@', 'VendorFarm'] # OID@ is ObjectID token (see search cursor help)
jpgPath = r"C:\Path\For\JPGs" # save location for jpgs

# clear any existing selections
arcpy.SelectLayerByAttribute_management(feature, "CLEAR_SELECTION")

with arcpy.da.SearchCursor(feature, fields) as cursor:
    for FID, VendorFarm in cursor: # unpack each row by assigning to field name variables
        mxd.title = VendorFarm
        query = '"FID" = {}'.format(str(FID))
        arcpy.SelectLayerByAttribute_management(feature, "NEW_SELECTION", query)
        df.zoomToSelectedFeatures()
        arcpy.RefreshActiveView()

        jpg = "{}\\{}.jpg".format(jpgPath, VendorFarm) # assuming VendorFarm is valid filename
        arcpy.mapping.ExportToJPEG (mxd, jpg)

 

If you need to use the ListLayoutElements, I would suggest that your map's text elements be named as this will help you find the correct element.  Just a rough code snippet to illustrate:

titleElement = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT")

for t in titleElement:
    if t.name == 'MapTitle'
        t.text = 'Some New Text'

 

Hope this helps.

View solution in original post

2 Replies
DavidPike
MVP Frequent Contributor

You just need to throw in ExportToJPEG—Help | ArcGIS for Desktop within the loop (after refreshactiveview).

do something like concatenate an outpath with what you have already - str(row.FID)

 

I'd recommend using the arcpy.da cursor rather than the arcpy cursor.

SearchCursor—Help | ArcGIS for Desktop

It will require some change of syntax, though if you plan on working with cursors again, it's best to adopt the da cursor from the onset.

RandyBurton
MVP Alum

As @DavidPike suggests, you should use the da.SearchCursor.  I noticed that you are attempting to access and set a map title using ListLayoutElements .  This will return a list, and you will need to access the proper text element.  It might be easier if you use the dynamic map "Title" as this property can be accessed and changed.  Here's a code snippet which illustrates these ideas.  Since it looks like you are using shape files, you may need to make some changes.

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

feature = 'survey selection'
fields = ['OID@', 'VendorFarm'] # OID@ is ObjectID token (see search cursor help)
jpgPath = r"C:\Path\For\JPGs" # save location for jpgs

# clear any existing selections
arcpy.SelectLayerByAttribute_management(feature, "CLEAR_SELECTION")

with arcpy.da.SearchCursor(feature, fields) as cursor:
    for FID, VendorFarm in cursor: # unpack each row by assigning to field name variables
        mxd.title = VendorFarm
        query = '"FID" = {}'.format(str(FID))
        arcpy.SelectLayerByAttribute_management(feature, "NEW_SELECTION", query)
        df.zoomToSelectedFeatures()
        arcpy.RefreshActiveView()

        jpg = "{}\\{}.jpg".format(jpgPath, VendorFarm) # assuming VendorFarm is valid filename
        arcpy.mapping.ExportToJPEG (mxd, jpg)

 

If you need to use the ListLayoutElements, I would suggest that your map's text elements be named as this will help you find the correct element.  Just a rough code snippet to illustrate:

titleElement = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT")

for t in titleElement:
    if t.name == 'MapTitle'
        t.text = 'Some New Text'

 

Hope this helps.