2

Can someone look at this script and see what the problem is? It used to work with arcgis 10.3 but now I can't get it to work with 10.4 on a different computer.

import arcpy
mxd = arcpy.mapping.MapDocument(r"C:\GIS\Data and Maps\2014MDCExport_CountyWide_localData.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
lyr = arcpy.mapping.ListLayers(mxd, "Grid")[0]  ##shapefile layer name called grid
rows = arcpy.SearchCursor(lyr)
x=1
for row in rows:
    df.extent = row.Shape.extent
    arcpy.mapping.ExportToJPEG(mxd, r"C:\GIS\OC Sheriffs\Image\L2\ " + str(row.getValue("PageName")) + ".jpg", df, df_export_width = 1200, df_export_height = 1200, resolution = 96, world_file=True, jpeg_quality = 100)
    print x
    x+=1
del mxd

Error:

Runtime error 
Traceback (most recent call last):
  File "<string>", line 9, in <module>
  File "c:\program files (x86)\arcgis\desktop10.4\arcpy\arcpy\utils.py", line 182, in fn_
    return fn(*args, **kw)
  File "c:\program files (x86)\arcgis\desktop10.4\arcpy\arcpy\mapping.py", line 1026, in ExportToJPEG
    layout.exportToJPEG(*args)
AttributeError: DataFrameObject: Error in executing ExportToJPEG
1
  • 4
    C:\GIS\Data and Maps\2014MDCExport_CountyWide_localData.mxd Is the path to your data correct? Can this other computer access the MXD?
    – Mintx
    Aug 26, 2016 at 20:02

1 Answer 1

3

This error message occurs when your output folder doesn't exist. Check that C:\GIS\OC Sheriffs\Image\L2 exists on the different computer you are now running the script on. You can use os.path.isdir() to test whether a folder exists.

outputFolder = r"C:\GIS\OC Sheriffs\Image\L2" # Your Output folder path to store the exported JPG files
# Check that the path exists
if os.path.isdir(outputFolder):
    ## the rest of your code goes here
else:
    print "The folder doesn't exist!"

Since you're running ArcGIS 10.4 I would migrate the script from the old arcpy.SearchCursor() to the more efficient arcpy.da.SearchCursor()

import arcpy, os
mxd = arcpy.mapping.MapDocument(r"C:\GIS\Data and Maps\2014MDCExport_CountyWide_localData.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
lyr = arcpy.mapping.ListLayers(mxd, "Grid")[0]  

outputFolder = r"C:\GIS\OC Sheriffs\Image\L2" # Your Output folder path to store the exported JPG files

# Check that the path exists
if os.path.isdir(outputFolder):
    with arcpy.da.SearchCursor(lyr, ['PageName', 'SHAPE@']) as cursor:
        for row in cursor:
            df.extent = row[1].extent
            outputFilePath = r"{}\{}.jpg".format(outputFolder, row[0])
            arcpy.mapping.ExportToJPEG(mxd, outputFilePath, df, df_export_width = 1200, df_export_height = 1200, resolution = 96, world_file=True, jpeg_quality = 100)
else:
    print "Folder {} doesn't exist!".format(outputFolder)

Have updated the script to add a extent buffer to add a bit of space around the current feature. This increases the extent scale by a specified percentage, then rounds that scale up to the next value that matches specified rounding factor (explained below the script)

import arcpy, os
def update_scale(df, extraScalePct, scaleRound):
    exScale = df.scale # Current scale
    newScale = exScale + (exScale * (float(1)/(float(100)/extraScalePct)))  # Increase DF scale by specified amount
    roundScale = (newScale + (scaleRound -1)) // scaleRound * scaleRound # Round the new scale up to the next value as specified
    return roundScale

mxd = arcpy.mapping.MapDocument(r"C:\GIS\Data and Maps\2014MDCExport_CountyWide_localData.mxd")
df = arcpy.mapping.ListDataFrames(mxd)[0]
lyr = arcpy.mapping.ListLayers(mxd, "Lines")[0]  ##shapefile layer name called grid

outputFolder = r"C:\GIS\OC Sheriffs\Image\L2" # Your Output folder path to store the exported JPG files
if os.path.isdir(outputFolder):
    with arcpy.da.SearchCursor(lyr, ['PageName', 'SHAPE@']) as cursor:
        for row in cursor:
            print row[0]
            df.extent = row[1].extent
            df.scale = update_scale(df, 10, 250) # Increase scale by 10% then round up to next 250
            outputFilePath = r"{}\{}.jpg".format(outputFolder, row[0])
            arcpy.mapping.ExportToJPEG(mxd, outputFilePath, df, df_export_width = 1200, df_export_height = 1200, resolution = 96, world_file=True, jpeg_quality = 100)
else:
    print "Folder {} doesn't exist!".format(outputFolder)

To set the extent buffer and the rounding, modify the line df.scale = update_scale(df, 10, 250). df is your dataframe, 10 is the percentage to increase the scale (e.g. 1:250 becomes 1:275), 250 is the rounding factor that the new scale is rounded up to (e.g. 1:275 becomes 1:500). Modify these two values to suit.

2
  • Also, I wanted to export a small amount of data around the polygon. Right now it cuts the data right to the polygon. Is there a way to do this?
    – Mark
    Aug 27, 2016 at 0:44
  • @Mark I have added some code that will allow you to add extra space after zooming to each feature.
    – Midavalo
    Aug 27, 2016 at 4:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.