Select to view content in your preferred language

Arcpy "Slope" Function does not work.

498
1
11-17-2023 03:47 AM
Labels (1)
MikeMoehlmann
New Contributor II

I'm using ArcGIS Pro 3.1.1

I'm encountering an issue when I try to run the "Slope" tool in arcpy. First of all, when I run the tool manually the tool runs without any problems. But in Arcpy, it does not work, even if I use the code from "Send To Python Window" from the manual execution.

Here is my code:

 

 

out_raster = arcpy.sa.Slope(
    in_raster="dtm_mask",
    output_measurement="DEGREE",
    z_factor=1,
    method="PLANAR",
    z_unit="METER",
    analysis_target_device="GPU_THEN_CPU"
)
out_raster.save(r"C:\Users\Desktop\slope_test")

 

 

Resulting in the following error:

 

 

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\sa\Functions.py", line 8685, in Slope
    return Wrapper(
  File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\sa\Utils.py", line 55, in swapper
    result = wrapper(*args, **kwargs)
  File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\sa\Functions.py", line 8632, in Wrapper
    z_meters_per_unit = linear_unit_factors[zlu]
KeyError: 'METRE'

 

 

 

So it says that there is a KeyError with "METRE", but I defined in the "z_unit" "METER".
How can I solve this problem? Is there maybe something wrong in the source code of the "slope" function?

Tags (3)
0 Kudos
1 Reply
MikeMoehlmann
New Contributor II

I found that the error "KeyError: 'METRE' only appears when using the 'arcpy.sa.Slope()' function from the Spatial Analyst toolbox. When using the slope function from the 3D-Analyst toolbox it works:

try:
    
    for raster in arcpy.ListRasters():
        if "DTM_mask" in raster:
            raster_name = raster
    
            arcpy.ddd.Slope(
                in_raster= raster,
                out_raster=f"{raster}_slope",
                output_measurement="DEGREE",
                z_factor=1,
                method="PLANAR",
                z_unit="METER",
                analysis_target_device="GPU_THEN_CPU"
            )

except Exception as e:
    print(f'Error: {str(e)}')

 

Still, when I use the function manually, no matter from the Spatial Analyst or 3D-Analyst toolbox it works without any problems. Also the documentation specifies a 'Slope()' function, which seems to does not even exist (I'm using ArcGIS Pro 3.1.1):

Error: name 'Slope' is not defined

 

0 Kudos