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
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.
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.
Signed in members can post, follow updates, and more. New here? Register a free account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.