I have a DEM with a horizontal resolution of 1x1m. I need to calculate the slope, but instead of the usual slope calculation with neighboring cells, I require the slope to be determined as follows:
I have been unable to specify a fixed distance for slope calculation, so I attempted to write a code to perform this process (I converted the raster to points beforehand):
File1="points"
File2="points_1"
Field="slope"
with arcpy.da.SearchCursor(File2, ["SHAPE@", "OBJECTID","grid_code"]) as cursor:
for row in cursor:
objectid = row[1]
sql = "OBJECTID = {}".format(objectid)
arcpy.management.SelectLayerByAttribute(File2, "NEW_SELECTION", sql)
val=row[2]
arcpy.management.SelectLayerByLocation(File1, 'WITHIN_A_DISTANCE', File2,'19.5 Meters', 'NEW_SELECTION')
arcpy.management.SelectLayerByAttribute(File1, 'SWITCH_SELECTION')
arcpy.management.SelectLayerByLocation(File1, 'WITHIN_A_DISTANCE', File2,'20.5 Meters', 'SUBSET_SELECTION')
with arcpy.da.SearchCursor(File1, "grid_code") as cursor2:
values=[]
for row2 in cursor2:
value=row2[0]
values.append(value)
max_value=max(values)
min_value=min(values)
slope=max(abs(max_value - val),abs(min_value - val))/5
arcpy.management.CalculateField(File2, Field, slope)
Things to note:
The code works as intended; however, the number of points from almost any region is colossal. I attempted to extract just a small area to test the code, but that 'small' area resulted in 523568 points, which means the code would take weeks to process them all. I need to be able to do this for much larger areas, so it is currently unviable.
I need help to achieve slope calculations without taking up a significant amount of time. I appreciate any assistance. Thanks.
Julian
After many tests, I finally found a way to do it faster. I think it is still not as efficient as it could be, but now it processes the half million points in around 20 min, instead of several months. The solution I got was using numpy and arrays instead of the selection tools from arcgis, here is how it looks: