Error When Running Calculate Value

2341
27
08-31-2016 03:21 PM
ChrisHolmes
Occasional Contributor III

Hello Everyone,

I have a model which runs the calculate value tool:

The code in the tool is:

When the model runs the following errors are displayed:

I'm not sure what the errors are related to. What's strange is that the code does seem to execute: it displays the extent of the site_polygon layer, sets the scale to 1:4000, then creates the JPG file.

Any insights appreciated. Thanks!

Tags (2)
0 Kudos
27 Replies
ChrisHolmes
Occasional Contributor III

Hi Xander,

I'm going to attach a file with a table showing on the left the code in the Calculate Value tool and on the right the code in the python script you sent me (thanks again!). The row below the row with the code shows the jpg file created using both methods. The one for the Calculate Value tool shows the correct image, the one for the python script shows the mxd as it appears when it's opened up (so it doesn't seem to do what's in the code). To me the code in both methods looks the same so I don't know why the code in the python script doesn't seem to run?

0 Kudos
XanderBakker
Esri Esteemed Contributor

Strange, supposedly arcpy will refresh the active view when something is changed. I would have to set up a similar situation te be able to track the issue, but I don't have time for that. Could you change the code to this (some additional messages):

 

def main():
    import arcpy
    # will overwrite existing jpg
    arcpy.env.overwriteOutput = True

    # read the parameter from the tool
    loc_no = arcpy.GetParameterAsText(0)

    # define the path of the mxd
    mxd_file = r'H:\chris\{0}\{0} - actual_use.mxd'.format(loc_no)
    jpg_file = r'H:\chris\{0}\{0} - actual_use.jpg'.format(loc_no)
    if arcpy.Exists(mxd_file):
        arcpy.AddMessage("Using MXD {0}".format(mxd_file))
        try:
            mxd = arcpy.mapping.MapDocument(mxd_file)
            df = arcpy.mapping.ListDataFrames(mxd)[0]
            arcpy.AddMessage("DF extent on startup is: {0}".format(df.extent))
            arcpy.AddMessage("DF scale on startup is: {0}".format(df.scale))
            lyr = arcpy.mapping.ListLayers(mxd, 'site_polygon', df)[0]
            ext = lyr.getExtent()
            arcpy.AddMessage("extent of lyr: {0}".format(ext))
            df.ext = ext
            df.scale = 4000
            arcpy.AddMessage("DF extent after assignment is: {0}".format(df.extent))
            arcpy.AddMessage("DF scale after assignment is: {0}".format(df.scale))
            arcpy.AddMessage("Exporting to JPEG: {0}".format(jpg_file))
            arcpy.mapping.ExportToJPEG(mxd, jpg_file)
            mxd.save()
            del mxd
        except Exception as e:
            arcpy.AddError("Error: {0}".format(e))
    else:
        arcpy.AddWarning("MXD {0} not found!".format(mxd_file))

if __name__ == '__main__':
    main()

... just to see what is going on?

ChrisHolmes
Occasional Contributor III

Thanks Xander. Here are the results:

Executing: ExportMXD2JPG2 LOC2016-003

Start Time: Thu Sep 01 13:22:29 2016

Running script ExportMXD2JPG2...

Using MXD H:\chris\LOC2016-003\LOC2016-003 - actual_use.mxd

DF extent on startup is: -9047.84819850156 5650698.4605047 -8349.34680149874 5651193.7614953 NaN NaN NaN NaN

DF scale on startup is: 4000.0

extent of lyr: -10106.7650000002 5656720.382 -10069.7270000003 5656763.791 NaN NaN NaN NaN

DF extent after assignment is: -9047.84819850156 5650698.4605047 -8349.34680149874 5651193.7614953 NaN NaN NaN NaN

DF scale after assignment is: 4000.0

Exporting to JPEG: H:\chris\LOC2016-003\LOC2016-003 - actual_use.jpg

Completed script ExportMXD2JPG2...

Succeeded at Thu Sep 01 13:22:59 2016 (Elapsed Time: 29.68 seconds)

0 Kudos
XanderBakker
Esri Esteemed Contributor

OK, so the dataframe extent is not being changed when the layer extent is assigned. Could you try this code?

def main():
    import arcpy
    # will overwrite existing jpg
    arcpy.env.overwriteOutput = True

    # read the parameter from the tool
    loc_no = arcpy.GetParameterAsText(0)

    # define the path of the mxd
    mxd_file = r'H:\chris\{0}\{0} - actual_use.mxd'.format(loc_no)
    jpg_file = r'H:\chris\{0}\{0} - actual_use.jpg'.format(loc_no)
    if arcpy.Exists(mxd_file):
        arcpy.AddMessage("Using MXD {0}".format(mxd_file))
        try:
            mxd = arcpy.mapping.MapDocument(mxd_file)
            df = arcpy.mapping.ListDataFrames(mxd)[0]
            arcpy.AddMessage("DF extent on startup is: {0}".format(df.extent))
            arcpy.AddMessage("DF scale on startup is: {0}".format(df.scale))
            lyr = arcpy.mapping.ListLayers(mxd, 'site_polygon', df)[0]
            ext = lyr.getExtent()
            arcpy.AddMessage("extent of lyr: {0}".format(ext))
            new_extent = df.ext
            new_extent.XMin, new_extent.YMin = ext.XMin, ext.YMin
            new_extent.XMax, new_extent.YMax = ext.XMax, ext.YMax
            arcpy.AddMessage("extent of new_extent: {0}".format(new_extent))
            df.ext = new_extent
            df.scale = 4000
            arcpy.AddMessage("DF extent after assignment is: {0}".format(df.extent))
            arcpy.AddMessage("DF scale after assignment is: {0}".format(df.scale))
            arcpy.AddMessage("Exporting to JPEG: {0}".format(jpg_file))
            arcpy.mapping.ExportToJPEG(mxd, jpg_file)
            mxd.save()
            del mxd
        except Exception as e:
            arcpy.AddError("Error: {0}".format(e))
    else:
        arcpy.AddWarning("MXD {0} not found!".format(mxd_file))

if __name__ == '__main__':
    main()‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
ChrisHolmes
Occasional Contributor III

Thanks Xander. When I run that code, I get the following error message:

Executing: ExportMXD2JPG3 LOC2016-003

Start Time: Thu Sep 01 14:21:14 2016

Running script ExportMXD2JPG3...

Using MXD H:\chris\LOC2016-003\LOC2016-003 - actual_use.mxd

DF extent on startup is: -9047.84819850156 5650698.4605047 -8349.34680149874 5651193.7614953 NaN NaN NaN NaN

DF scale on startup is: 4000.0

extent of lyr: -10106.7650000002 5656720.382 -10069.7270000003 5656763.791 NaN NaN NaN NaN

Error: 'DataFrame' object has no attribute 'ext'

Completed script ExportMXD2JPG3...

Failed to execute (ExportMXD2JPG3).

Failed at Thu Sep 01 14:21:32 2016 (Elapsed Time: 17.89 seconds)

Thanks for your help, very much appreciated!

0 Kudos
DanPatterson_Retired
MVP Emeritus

the df extent has to be set to the extent right now it isn't being assigned properly as it is further on... (I think... Xander you will have to check

ChrisHolmes
Occasional Contributor III

Thanks Dan. That seems to have fixed the error. The results now display as:

Executing: ExportMXD2JPG3 LOC2016-003

Start Time: Thu Sep 01 14:35:03 2016

Running script ExportMXD2JPG3...

Using MXD H:\chris\LOC2016-003\LOC2016-003 - actual_use.mxd

DF extent on startup is: -9047.84819850156 5650698.4605047 -8349.34680149874 5651193.7614953 NaN NaN NaN NaN

DF scale on startup is: 4000.0

extent of lyr: -10106.7650000002 5656720.382 -10069.7270000003 5656763.791 NaN NaN NaN NaN

extent of new_extent: -10106.7650000002 5656720.382 -10069.7270000003 5656763.791 NaN NaN NaN NaN

DF extent after assignment is: -9047.84819850156 5650698.4605047 -8349.34680149874 5651193.7614953 NaN NaN NaN NaN

DF scale after assignment is: 4000.0

Exporting to JPEG: H:\chris\LOC2016-003\LOC2016-003 - actual_use.jpg

Completed script ExportMXD2JPG3...

Succeeded at Thu Sep 01 14:35:40 2016 (Elapsed Time: 36.85 seconds)

Still seems though that the extents are not changing.

0 Kudos
DanPatterson_Retired
MVP Emeritus

ahhhh/!  new_extent = df.extent   or you will have to wait for Xander

ChrisHolmes
Occasional Contributor III

It seems to have dealt with the error. But the data frame extents still do not seem to take the value of new_extent.

Thank you Dan

0 Kudos
XanderBakker
Esri Esteemed Contributor

double oops, line 22 as Dan mentions should be

new_extent = df.extent

and line 26 should be :

df.extent = new_extent