Python and multiple calls to Hillshade function

Discussion created by bruce.simonson on Mar 24, 2013
Latest reply on Mar 25, 2013 by bruce.simonson
Hi Folks,

New to these forums, and python.  I wish to write a program which aggressively calls the Hillshade function.  Specifically, for two important watersheds near Juneau, I want to call this function with "SHADOWS" enabled, for 5 minute intervals over an entire year.  For our latitude, this means that, for the period from sunrise to sunset, over an entire year, there will be over 50000 calls to this function.

I have a working program, but I am hoping it can be streamlined.  Also, I can't get the program to clear locks, and I would like to add a call to build pyramids, but I can't get either of these to work.

I have built an external file, which I access with the module csv, that contains solar positions, at 5 minute intervals, over an entire year.  These solar positions are the azimuth and altitude needed by the hilllshade function.

Here are four sample rows from this file:


field 1:  filename (to name the created hillshade), with date, time, azimuth, and altitude in filename
field 2:  date
field 3:  azimuth
field 4:  altitude

Here's my program, at least the essential part:
#### generate hillshades
import arcpy, csv, string, os, win32com.client, datetime as dt
from arcpy import env

arcpy.env.workspace = "E:\\solar\\solar.gdb"

# file with solar positions
f = open('E:\\solar_data\\solarpos.csv', 'rt')

z = 'E:\\solar_data\\behlef120921'

    reader = csv.reader(f)
    for row in reader:
        filename = row[0]
        azimuth  = row[2]
        altitude = row[3]
        hs_pathfile = "E:\\solar\\solar.gdb\\"+filename

        print n, hs_pathfile, azimuth, altitude, (t1-t0).seconds

        # call hillshade function
        arcpy.HillShade_3d(z, hs_pathfile, azimuth, altitude, "SHADOWS", 1 )

        # arcpy.BuildPyramids_management(hs_pathfile)    # commented out, doesn't seem to work 
        # clear resulting hillshade from ArcMAP, so the TOC doesn't overload
        mxd = arcpy.mapping.MapDocument('CURRENT')
        df = arcpy.mapping.ListDataFrames(mxd)[0]
        for lyr in arcpy.mapping.ListLayers(mxd, '', df):
            arcpy.mapping.RemoveLayer(df, lyr)



1)  The documentation on hillshade seems to indicate that slope and aspect are computed as part of the hillshade function.  This is an immense amount of overhead.  Seems like there should be a way to pass the slope and aspect to this function, rather than computing them for each call.  Yikes.  Each iteration runs about 100 seconds.  Any ideas here?

2)  Looking at my directory (E:\\solar\\solar.gdb), where the file geodatabase stores the results of these calls to hillshade, record locks are being held, even after the last section of code in the read loop, which clears each of these rasters from the TOC after it is generated.  How can I clear these locks from the Python program?

3)  I can't get the BuildPyramids function to work.  I'd like to do this, and perhaps compute statistics, as part of the program, right after each hillshade raster is created.

Lots going on here.  Any help would be greatly, greatly appreciated.


PS:  I am running from ArcMap's Python window (ArcGIS 10.0).  I'd run from a different environment (e.g. command line), but I can't get this to work at the moment.  The ArcMAP Python window is my working access point to Python.  Python 2.7.3.

PPS:  I will post this to the raster forum as well.

PPPS:  It looks like my post may have lost indents needed for Python.  If these indents don't display correctly, you can assume I got them right.  Sorry for the inconvenience.