Select to view content in your preferred language

Raster Calculator, Problem with 0 and CON

4572
7
Jump to solution
10-09-2012 10:32 AM
DanielBecker
Deactivated User
Hi,

this is a little embarassing but i have the following problem, and i'm sure there's a very simple solution:

I want to multiply the values of 2 rasters, using one of them as a factor to reduce the values of the first raster.
The problem is, the cells of the second raster contains 0, which, of course, results in 0 in the output-raster, which is wrong. 🙂

I want the Raster Calculator to do just nothing if this case occurs.

I hope someone can help me!

My calculation looks something like that:


(("second_raster.tif") * 0.6) * "first_raster"
0 Kudos
1 Solution

Accepted Solutions
JeffreyEvans
Frequent Contributor
Your interpretation of the Con statement is incorrect. A conditional statement takes the form: IF, THEN, ELSE. 

Using ArcToolbox > Spatial Analyst Tools > Map Algebra > Raster Calculator, it should look like this:
Con("arroyo_h" == 0, "vcf_tree", (("vcf_tree" / 100) * 0.6) * "arroyo_h")

Translation:
IF: "arroyo_h" == 0
THEN: assign value from "vcf_tree"
ELSE: (("vcf_tree" / 100) * 0.6) * "arroyo_h"

It looks like your rasters are floating point. The default resampling method, performed on the fly, is nearest neighbor. This is not appropriate for floating point rasters and will yield undesirable results. I would recommend resampling your rasters to a consistent resolution prior to analysis. You can find the Resample tool under: Data Management Tools > Raster > Raster Processing > Resample. Under the "Resampling Technique" drop-down select "BILINEAR".

View solution in original post

0 Kudos
7 Replies
MarkBoucher
Honored Contributor
You'll need to use the Con() function. It would look something like this:

     Con("first_raster"=0,"second_raster.tif",(("second_raster.tif") * 0.6) * "first_raster")

In the above, if first_raster=0, the result will be whatever is in second_raster.tif, if first_raster not =0, perform equation.
0 Kudos
DanielBecker
Deactivated User
Thanks for your help! I had to change a few things (i think?) because no expression was allowed for the first parameter.. and i think you have to put the condition into the {where clause}?

But it still doesn't work, i even put the vcf_tree raster into my .gdb (which was even weirder.. better don't use "." >8 characters as filename..grr) and now i use this expression:


Con("vcf_tree",(("vcf_tree" / 100) * 0.6) * "arroyo_h","arroyo_h","Value"  > 0)

also tried

Con("vcf_tree",(("vcf_tree" / 100) * 0.6) * "arroyo_h","arroyo_h","vcf_tree"  > 0)

But i get this everytime:

ERROR 000539: Error running expression: rcexec() <class 'arcgisscripting.ExecuteError'>: ERROR 010416: Error in setting raster table filter for E:\Documents\ArcGIS\Analyse\analyse.gdb\vcf_tree.
Failed to execute (Con).

Failed to execute (RasterCalculator).


Also both rasters are in different resolution, and i want to use "Minimum of inputs" as cell size.


PS.: Further explanation: i try to change a cost-raster (which contains walking time in hours per cell) by the vegetation index (MODIS VCF).
0 Kudos
SteveLynch
Esri Regular Contributor
the syntax (from the help)

Con(in_conditional_raster, in_true_raster_or_constant, {in_false_raster_or_constant}, {where_clause})
0 Kudos
JeffreyEvans
Frequent Contributor
Your interpretation of the Con statement is incorrect. A conditional statement takes the form: IF, THEN, ELSE. 

Using ArcToolbox > Spatial Analyst Tools > Map Algebra > Raster Calculator, it should look like this:
Con("arroyo_h" == 0, "vcf_tree", (("vcf_tree" / 100) * 0.6) * "arroyo_h")

Translation:
IF: "arroyo_h" == 0
THEN: assign value from "vcf_tree"
ELSE: (("vcf_tree" / 100) * 0.6) * "arroyo_h"

It looks like your rasters are floating point. The default resampling method, performed on the fly, is nearest neighbor. This is not appropriate for floating point rasters and will yield undesirable results. I would recommend resampling your rasters to a consistent resolution prior to analysis. You can find the Resample tool under: Data Management Tools > Raster > Raster Processing > Resample. Under the "Resampling Technique" drop-down select "BILINEAR".
0 Kudos
MarkBoucher
Honored Contributor
Steve, and Jeffrey:  Thanks for the clarifications.

Daniel:   Jeffery's equation should work given the actual layer names.

I usually build the equations using the "pad" in the raster calculator. Had I done that, my "=" would have been "==". The "pad" is handy in that quotes and other syntax can be kept free-er from human error.

Also the was a remnant of sloppy cutting and pasting.
0 Kudos
DanielBecker
Deactivated User
Your interpretation of the Con statement is incorrect. A conditional statement takes the form: IF, THEN, ELSE. 

Using ArcToolbox > Spatial Analyst Tools > Map Algebra > Raster Calculator, it should look like this:
Con("arroyo_h" == 0, "vcf_tree", (("vcf_tree" / 100) * 0.6) * "arroyo_h")

Translation:
IF: "arroyo_h" == 0
THEN: assign value from "vcf_tree"
ELSE: (("vcf_tree" / 100) * 0.6) * "arroyo_h"

It looks like your rasters are floating point. The default resampling method, performed on the fly, is nearest neighbor. This is not appropriate for floating point rasters and will yield undesirable results. I would recommend resampling your rasters to a consistent resolution prior to analysis. You can find the Resample tool under: Data Management Tools > Raster > Raster Processing > Resample. Under the "Resampling Technique" drop-down select "BILINEAR".


Thanks, this works now, but i use: Con("vcf_tree" == 0, "arroyo_h", (("vcf_tree" / 100) * 0.6) * "arroyo_h")
since arroyo_h contains the values that should be changed by vcf_tree or stay unchanged if vcf_tree == 0. But my first results show me that i have to change some things.. 🙂


Of course i tried to build my equation considering the syntax from the help @Stevelynch, but i think this is quite confusing. I did some programming in university, so i know how conditional statements work. The problem is that i still don't understand what the {where clause} is for? And the example from the help (OutRas = Con(InRas1, 40, 30, "Value >= 2")) kind of represents what i tried in my second post, or am i wrong?


Actually the arroyo_h raster is floating point, the vcf_tree is 8bit unsigned (i think nearest neighbor was the right choice when i reprojected this?), where 0-100 is the percentage of tree coverage, 253 is water (which i still have to filter out of this).


Anyways, THANK YOU!
0 Kudos
SteveLynch
Esri Regular Contributor
A few comments...

The signature for Con in pre ArcGIS 10 is
- Con_sa <in_conditional_raster> <in_true_raster_or_constant> <out_raster> {in_false_raster_or_constant} {where_clause}
and from 10 onwards we have output on the left hand side and the signature is
- Con (in_conditional_raster, in_true_raster_or_constant, {in_false_raster_or_constant}, {where_clause})


If the geoprocessing tool exists then one need not use it inside the RasterCalculator tool. However, in some cases you can, for example (in ArcGIS10)
Con("elev", 0, 1, "value > 1000") - where the cell value > 1000 use 0 else use 1
can be written in Python or in the RasterCalculator as
Con(Raster("elev") > 1000, 0, 1)

Regards
Steve
0 Kudos