Raster calculator

935
11
Jump to solution
01-29-2018 06:22 AM
FelixSchwarz
New Contributor

Hello,

is it possible to combine 2 rasters with the raster calculator defining the value of the rasters? It should look more or less like this but at the moment i'm getting an error for the expression.

Thanks for answers

0 Kudos
1 Solution

Accepted Solutions
XanderBakker
Esri Esteemed Contributor

So what Dan is explaining, you could use:

Con("AlnusSpp" + "BetulaSpp" > 20, "AlnusSpp")

... in case you want to create a new raster with the original values of "AlnusSpp" where the sum of "AlnusSpp" and "BetulaSpp" is higher than 20 (rest of the pixels in this case will be assigned NoData)

And you can use:

Con("AlnusSpp" + "BetulaSpp" > 20,"BetulaSpp")

... to create the same type of raster but now with the original values of "BetulaSpp" when the sum of the two raster is higher than 20 (rest of cells will be NoData)

Or you can use something like this:

Con("AlnusSpp" + "BetulaSpp" > 20,"AlnusSpp" + "BetulaSpp")

... when you want to create a raster with the sum of the values of the two raster when the sum is higher than 20 (rest of cells will be NoData)

View solution in original post

11 Replies
XanderBakker
Esri Esteemed Contributor

Can you try a Con statement like thois:

Con("AlnusSpp" + "BetulaSpp" > 20, 1, 0)

0 Kudos
FelixSchwarz
New Contributor

Thanks for the quick answer. This one works but I try to retain the exact values from the data and not the define them

as 1 or 0, because the exact values are important for further procedures.

0 Kudos
DanPatterson_Retired
MVP Emeritus

Once you have your indicator array (the 1's) you use that array/raster to extract all the locations where a 1 is found.  

In essence 1 indicates the locations where the sum of the two rasters is > 20.  Use the 1's to extract the values. from the 2 input rasters.

XanderBakker
Esri Esteemed Contributor

So what Dan is explaining, you could use:

Con("AlnusSpp" + "BetulaSpp" > 20, "AlnusSpp")

... in case you want to create a new raster with the original values of "AlnusSpp" where the sum of "AlnusSpp" and "BetulaSpp" is higher than 20 (rest of the pixels in this case will be assigned NoData)

And you can use:

Con("AlnusSpp" + "BetulaSpp" > 20,"BetulaSpp")

... to create the same type of raster but now with the original values of "BetulaSpp" when the sum of the two raster is higher than 20 (rest of cells will be NoData)

Or you can use something like this:

Con("AlnusSpp" + "BetulaSpp" > 20,"AlnusSpp" + "BetulaSpp")

... when you want to create a raster with the sum of the values of the two raster when the sum is higher than 20 (rest of cells will be NoData)

FelixSchwarz
New Contributor

The last one is exactly the one I needed, thank you very much!

One last question: Overall I got 9 species and want to create a raster where at least 2 or more of the species with >20 are displayed, is there maybe one expression for it? 

Or is it possible to  combine expressions maybe like this:

Con("AlnusSpp" + "BetulaSpp" > 20,"AlnusSpp" + "BetulaSpp") OR 
Con("AlnusSpp" + "CarpinusSpp" > 20,"AlnusSpp" + "CarpinusSpp")
0 Kudos
DanPatterson_Retired
MVP Emeritus

No.  You would have script a solution or try modelbuilder (which has its own issues)

XanderBakker
Esri Esteemed Contributor

As Dan mentioned, you will need a script to accomplish this. Find below an example of what you could use:

# load modules
import arcpy
import os
from arcpy.sa import *

# settings
ws_species = r'path to your folder of fgdb workspace'
species = ["BetulaSpp", "CarpinusSpp", "other species"]
specie_compare = "AlnusSpp"
output_postfix = 'Cmp'

# check out the SA extension
arcpy.CheckOutExtension("Spatial")

# create raster object from comparison raster AlnusSpp
compare_ras = arcpy.Raster(os.path.join(ws_species, specie_compare))

# loop through species
for specie in ws_species:
    specie_ras = arcpy.Raster(os.path.join(ws_species, specie))
    out_path = os.path.join(ws_species, specie + output_postfix)
    # calculate raster and store result
    result = Con(compare_ras + specie_ras > 20, compare_ras + specie_ras)
    result.save(out_path)

This script will loop through species based on a list defined on line 8 and create the output raster for each comparison. Output name will be specie name with "Cmp" at the end (see output_postfix on line 10). It assumes that all rasters are stored in the same workspace (folder or fgdb) as defined on line 7.

JayantaPoddar
MVP Esteemed Contributor

You could try

SetNull(("AlnusSpp" + "BetulaSpp" )<= 20, ("AlnusSpp" + "BetulaSpp"))



Think Location
0 Kudos
FelixSchwarz
New Contributor

Thanks for the script, I will try it. 

In general I was trying to create a map that shows a 'Mixed Forest' containing the particular species. So there have to be at least 2 species ( or more -  with >20) in one grid. Isn't there another solution or tool for this one?

0 Kudos