'How can I create multiple hillshade maps using a for loop that searches a table?

 import arcpy.sa

 arcpy.env.workspace = r"C:\Users\nhaddad\Desktop\project_8"

 arcpy.env.overwriteOutput = True

 altitude_cursor = arcpy.da.SearchCursor("solar_points", "Altitude")

 azimuth_cursor = arcpy.da.SearchCursor("solar_points", "Azimuth")

 for i,j in zip(altitude_cursor,azimuth_cursor):

       output = arcpy.sa.Hillshade(r"C:\Users\nhaddad\Desktop\final_exam\worcester_dem", j[0], i[0], "SHADOWS", 0.348)

I can only create 1 output map, when I need the loop to iterate through the 10 rows of the table and make 10 maps.



Solution 1:[1]

You never save your hillshade or add it to a list (or other data structure) to use the results later. You can save the hillshade result by simply using output.save("/path/to/destination/file/name").

Something else, please use with when working with arcpy.da.SearchCursor to ensure that the cursor is closed again after you don't need it anymore. Furthermore, you don't need two cursors.

import os
import arcpy, arcpy.sa

DEM = r"C:\Input\Hillshade\worcester_dem"
SOLAR_POINTS_FOLDER = r"C:\Input\SolarPoints"

OUTPUT_FOLDER = r"C:\Output"

with arcpy.EnvManager(workspace=SOLAR_POINTS_FOLDER):

    with arcpy.da.SearchCursor("solar_points", ["Azimuth", "Altitude"]) as cursor:
        
        for azimuth, altitude in cursor:
            
            output = arcpy.sa.Hillshade(DEM, azimuth, altitude, "SHADOWS", 0.348)
            
            # either save file (or add it to an array for later use)
            output.save( \
                os.path.join(OUTPUT_FOLDER, f"worcester_{azimuth}_{altitude}"))

Note: Above example is written from memory without being tested/executed.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Thomas