# [postgis-users] NDVI Calculation from two bands within one Raster

Pierre Racine Pierre.Racine at sbf.ulaval.ca
Wed Apr 11 08:47:43 PDT 2012

```Your CASE syntax is wrong:

http://www.postgresql.org/docs/9.1/interactive/functions-conditional.html#FUNCTIONS-CASE

Should look like:

ST_MapAlgebraExpr(a.rast, 4, b.rast, 3, 'CASE WHEN ([rast1] + [rast2]) = 0 THEN 999 ELSE ([rast1] - [rast2]) /([rast1] + [rast2])::float END')
FROM ndvi a, ndvi b;

Pierre

> -----Original Message-----
> From: postgis-users-bounces at postgis.refractions.net [mailto:postgis-users-
> bounces at postgis.refractions.net] On Behalf Of JamesH
> Sent: Wednesday, April 11, 2012 10:56 AM
> To: postgis-users at postgis.refractions.net
> Subject: Re: [postgis-users] NDVI Calculation from two bands within one Raster
>
> Sorry to keep reviving this thread, espcially with a basic question.
>
> I am using:
> SELECT ST_MapAlgebraExpr(a.rast, 4, b.rast, 3, '([rast1] - [rast2]) /([rast1] +
> [rast2])::float') WHEN (rast1 + rast2 = 0) THEN = 999 ELSE ([rast1] - [rast2])
> /([rast1] + [rast2])::float END'
> FROM ndvi a, ndvi b;
>
> Where the WHEN statement is being used to prevent the error division by zero.
> However this returns: syntax error at or near "WHEN"
>
> Can anyone advise on where I'm structuring this query wrongly.
>
> Regards,
> James
>
> -----