4

I'm in the process of learning to use python to automate some tasks in ArcGIS, and while I've done a couple of on line courses about python I'm still learning to use it within ArcGIS. I've had some success, but I have a question about the correct way of doing things sometimes. I can get it to work (which is important, of course), but don't always understand the purpose of doing something this way or that.

My question is pretty simple I think. Using the example of the Spatial Analyst operation 'Plus', why would you choose to use the syntax outlined in the ESRI help page for the command over the syntax used by building the command in model builder and then exporting as a python script. I understand how they are each working, but don't understand why one would be chosen over the other.

Example:
ESRI help
#Import modules, environment settings, set variables, get license
#Execute plus tool and save result to a file
outPlus = Plus(inRaster1, inRaster2)
outPlus.save("C:/sapyexamples/output/outplus")

Model Builder export
#Import modules, environment settings, set variables, get license
#Execute plus tool, and output to variable raster3
arcpy.gp.Plus_sa(raster1, raster2, raster3)

Is this just a style preference? I assume both of these would run outside of ArcGIS, so why would someone choose on over the other?

  • 1
    What version of the ArcGIS arcpy site package are you using? – Aaron Apr 20 '15 at 19:13
  • Last week I installed ArcGIS 10.3, but have observed this in 10.2 previous to this. Python version 2.7.8. – David R Apr 20 '15 at 20:37
  • 2
    Yes, in versions previous to 10 the spatial analyst functions produced a direct output. Now in 10.x they create a raster object that needs to be saved to make it permanent and accessible... if you're doing a few functions like Plus, Int, IsNull then use the raster object as an input, that way you only need keep your final output rather than every step in between; it really is just a file, it's in your %Temp% directory as a GRID raster. When reading older scripts, especially with SA functions, refer to the current help document using the function names to see if they've changed. – Michael Stimson Apr 20 '15 at 22:37
2

The arcpy.gp.Plus_sa() is probably an older method of doing the same thing. I would go with the ESRI help version because it is Actually Documented and probably won't be dropped from support as quickly.

0

I can't speak to the intricacies of memory management but I can show you a couple examples that suggest it is more efficient. First lets compare the spatial analyst (SA) method to the 3d method that more resembles model builder (for this example I created a small constant integer raster "raster 1" and used a constant constant2 = 10, there is a lot of variability in execute times this is just for demonstrative purposes):

outPlus1 = Plus(raster1, constant2)

Execute time = ~0.0098 sec

outPlus1.save(outRaster)

Execute time = ~0.0384 sec (total ~0.0482)

arcpy.Plus_3d(raster1, constant2, outRaster3d)

Execute time = ~0.16729 sec.

So even outside of model builder something is more efficient. Now my suspicion was that the SA method doesn't commit results to memory in the same way. The added advantage of this would be versatility, where if you were performing multiple math functions, like you might do in raster calculator, you can just save the results you need in the last step. However, testing this (with constant3 = 2) there was a loss in efficiency and the intermediates are still saved to the default location:

outPlus1 = Plus(raster1, constant2)

Execute time = ~0.0590

outPlus2 = Plus(outPlus1, constant3)

Execute time = ~0.0032

outPlus2.save(outRaster)

Execute time = ~4.3161

The preferred method when doing multiple math functions is to use raster objects, you'll notice that the syntax is similar to the SA method:

outPlus2 = Raster(raster1) + constant2 + constant3

Execute time = ~0.0114

outPlus2.save(outRaster)

Execute time = ~0.0405 (total = ~0.0519)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.