What is the way to call spatial analysis tools in ArcMap from python

1096
3
03-15-2021 11:12 PM
mody_buchbinder
Occasional Contributor III

There are too many ways to call these tools, can some one explains what are the cons and pros.

Take the Slope for example:

 

Option 1:

From arcpy.sa import *

slopeRes = Slope(“raster”)

 

This will create a temporary raster (inside your default gdb?) and you must do slopeRes.save if you need it later

Most docs use this

 

Option2:

Arcpy.Slope_3d(“raster”,”outRaster”)

This will let you save the results to other raster but uses the 3D analysis (is it different?)

 

Option3:

arcpy.gp.Slope_sa(“raster”,”outRaster”)

This is what you get where you translate a Model

 

Option4:

arcpy.sa.Slope(“raster”)

Looks similar to option 1

 

Thanks

Mody

0 Kudos
3 Replies
DanPatterson
MVP Esteemed Contributor

Have you spent any time examining the scripts in

C:\...install_folder...\Resources\ArcPy\arcpy

__init__.py start the there, the initial import of arcpy, if you just import arcpy first

sa spatial analyst (folder) extension, check its __init__ as well

ddd 3D analyst beginings 

Eventually you will begin to see a pattern.  Some of the import approaches are legacy but still valid

arcpy.sa.Slope vs arcpy.gp.Slope_sa

Don't forget to examine the __init__ and _base scripts in

C:\...install_folder...\Resources\ArcPy\arcpy\geoprocessing

I could go on, but I won't because you will see that a lot was built up and added to (perhaps to prevent breaking things with version changes)

When you are done with that folder, you can also go to

C:\...install_folder...\Resources\ArcToolBox

That is where thing began to change with Pro 2.6 (pretty sure that was the version).

In short, all paths lead to the same place(s). 

The route that is followed and things that are picked up along the way, may differ.

No one way is "better" than the other.  If you understand what is there, you have choices.

PS

Spoiler alert!

Don't forget the arcobjects stuff (a lot of gift wrapping) and the 3rd party python modules that are used (eg numpy is huge).  


... sort of retired...
0 Kudos
mody_buchbinder
Occasional Contributor III

Hi Dan

Thanks for your answer, that's a lot of init's to read 🙂

I also found this: https://community.esri.com/t5/python-questions/what-is-the-difference-between-arcpy-gp-visibility-sa...

where you gave some answers too.

I found that the Slope() write the output to the default fgdb while the gp.slope_sa you can write it anywhere and in any format.

I am running a set of sa commands one after another and if it takes the same time (and it does) I would like to save my results for all the commands.

Thanks

Mody 

0 Kudos
by Anonymous User
Not applicable

On your end, you have some control over how you import the package and module. To explain what is different on the surface:

 

Option 1:

From arcpy.sa import *

slopeRes = Slope(“raster”)

 

imports the 'arcpy.sa' package.module so you can leave it off of the 'Slope' method (and any others in that package.module like Contour) like they did in the example because the interpreter looks in the 'arcpy.sa' package.module for 'Slope'.

I think the pro's and con's are personal preference.  Importing 'arcpy.sa' as 'from arcpy.sa import *' shortens code in the script, but it is difficult to tell where that tool is coming from. For example:

'Contour( )' vs 'arcpy.sa.Slope(“raster”)'

You could assign the import a name on the import:

from arcpy.sa import * as asa

then use it like:

asa.Contour(stuff)

but then you will have to remember/ note what asa is.  I just leave it as 'arcpy.sa.Tool'. A few characters is not an issue and since we code for people, it makes it easy for a person to interpret it and know where it comes from.  It keeps 'arcpy' in the line, so it follows the convention of any other arcpy method that is used.

arcpy.Tool()

 

Option4:

arcpy.sa.Slope(“raster”)

Looks similar to option 1

This example did not import the 'arcpy.sa' so you need to tell the interpreter where the method is by prepending the whole path to the 'Slope' (same would be done with any other method like Contour).

 

Option2:

Arcpy.Slope_3d(“raster”,”outRaster”)

This will let you save the results to other raster but uses the 3D analysis (is it different?)

 

Option3:

arcpy.gp.Slope_sa(“raster”,”outRaster”)

This is what you get where you translate a Model

The docs show that Slope_3d is available with the Spatial Analyst and 3D Analyst licenses. I think Dan's post explains this one well. arcpy.gp.Slope_sa could be the package.module.method that Slope_3d points to/ routes to in the backend arcpy code.

 

0 Kudos