Select to view content in your preferred language

Raster Calculator to Map Algebra

1127
5
Jump to solution
10-24-2018 11:51 AM
EricEagle
Frequent Contributor

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.

0 Kudos
1 Solution

Accepted Solutions
DarrenWiens2
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)

Con—Help | ArcGIS for Desktop 

View solution in original post

5 Replies
DarrenWiens2
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)

Con—Help | ArcGIS for Desktop 

DanPatterson_Retired
MVP Emeritus

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

DarrenWiens2
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)
0 Kudos
DanPatterson_Retired
MVP Emeritus

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

0 Kudos
Luke_Pinner
MVP Regular Contributor

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

stream_raster = outFlowAccum >= 150 
0 Kudos