# Raster Calculator to Map Algebra

589
5
10-24-2018 11:51 AM
Occasional Contributor III

I have a model that does a really simple Raster Calculator operation:

``"%Output accumulation raster%" >= 150‍``

That's it.  I have been racking my brain trying to figure out how do I get this into a python script?  I have been looking at A Quick Tour of Using Map Algebra and frankly I'm overwhelmed because I don't have bundles of time on my hands, and I'm just trying to do something very straightforward somewhat quickly.  I feel like I need to see a very specific, hand-holding, with reasons, example of taking this Raster Calculator expression and putting it into Python (3.6, ArcGIS Pro 2.2).

Part of the code in question looks like this (yes, I'm using Spatial Analyst hydrology tools here):

``````outfill = Fill(raster)

outFlowDirection = FlowDirection(outfill, "NORMAL")

outFlowAccum = FlowAccumulation(outFlowDirection)

stream_raster = Raster(outFlowAccum >= 150) #this is the part that fails
‍‍‍‍‍‍‍``````

I'm sure this is really 101 but nevertheless, I'd appreciate any guidance.

Tags (3)
1 Solution

Accepted Solutions
MVP Honored Contributor

I'm not exactly sure what the non-tool equivalent is, but I default to Con, something like:

``stream_raster = Con(outFlowAccum >= 150, outFlowAccum)‍``

5 Replies
MVP Honored Contributor

I'm not exactly sure what the non-tool equivalent is, but I default to Con, something like:

``stream_raster = Con(outFlowAccum >= 150, outFlowAccum)‍``

MVP Legendary Contributor

If it fails, what is the error message.

If it were to work, then the you would probably want to add a stream_raster.save(… some path...)

It could be as simple as your model vs script not know the work environment and where files are coming from and going to.

Also the difference between .... outFlowAccum >= 150  and Darren's Con representation, is that yours will return a simple binary raster (0 or 1) and the Con variant will return 1 or the outFlowAccum.  You could simply replace that portion with a 0 to get the binary raster that you want to represent stream vs non-stream

MVP Honored Contributor

Hmmm, I think the Con will return the raster or nothing. If you want a full binary raster, use:

``stream_raster = Con(outFlowAccum >= 150, 1, 0)``
MVP Legendary Contributor

used to numpy which gives binary automatically ( 0, 1 ) rather than whatever

MVP Regular Contributor

If you just want a binary 1/0 True/False output, use:

``stream_raster = outFlowAccum >= 150 ``