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 - GIS Undergraduate -- View this message in context: http://postgis.17.n6.nabble.com/NDVI-Calculation-from-two-bands-within-one-Raster-tp4656995p4832397.html Sent from the PostGIS - User mailing list archive at Nabble.com. ___ postgis-users mailing list postgis-users@postgis.refractions.net http://postgis.refractions.net/mailman/listinfo/postgis-users
Re: [postgis-users] NDVI Calculation from two bands within one Raster
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-boun...@postgis.refractions.net [mailto:postgis-users- boun...@postgis.refractions.net] On Behalf Of JamesH Sent: Wednesday, April 11, 2012 10:56 AM To: postgis-users@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 - GIS Undergraduate -- View this message in context: http://postgis.17.n6.nabble.com/NDVI- Calculation-from-two-bands-within-one-Raster-tp4656995p4832397.html Sent from the PostGIS - User mailing list archive at Nabble.com. ___ postgis-users mailing list postgis-users@postgis.refractions.net http://postgis.refractions.net/mailman/listinfo/postgis-users ___ postgis-users mailing list postgis-users@postgis.refractions.net http://postgis.refractions.net/mailman/listinfo/postgis-users
Re: [postgis-users] NDVI Calculation from two bands within one Raster
Thanks, that has worked and can visualise through OpenJump. The Raster it produces (below), has only values of zero or one in its attributes. I'm assuming in the calculation its not keeping the values as Floats but its converting it to integer? Must be rounding them to either zero or one. Is there a way to solve this i.e. keep the pixels as floating integers? http://postgis.17.n6.nabble.com/file/n4832776/ndvi.jpg Regards, James - GIS Undergraduate -- View this message in context: http://postgis.17.n6.nabble.com/NDVI-Calculation-from-two-bands-within-one-Raster-tp4656995p4832776.html Sent from the PostGIS - User mailing list archive at Nabble.com. ___ postgis-users mailing list postgis-users@postgis.refractions.net http://postgis.refractions.net/mailman/listinfo/postgis-users
Re: [postgis-users] NDVI Calculation from two bands within one Raster
Thanks, that has worked and can visualise through OpenJump. The Raster it produces (below), has only values of zero or one in its attributes. I'm assuming in the calculation its not keeping the values as Floats but its converting it to integer? Must be rounding them to either zero or one. Is there a way to solve this i.e. keep the pixels as floating integers? http://postgis.17.n6.nabble.com/file/n4832782/ndvi.jpg Kind Regards, James - GIS Undergraduate -- View this message in context: http://postgis.17.n6.nabble.com/NDVI-Calculation-from-two-bands-within-one-Raster-tp4656995p4832782.html Sent from the PostGIS - User mailing list archive at Nabble.com. ___ postgis-users mailing list postgis-users@postgis.refractions.net http://postgis.refractions.net/mailman/listinfo/postgis-users
Re: [postgis-users] NDVI Calculation from two bands within one Raster
Are you using ST_dumpAsPolygons() to display in OpenJump? Depending on the GDAL version you're using, ST_DumpAsPolygons() might be converting floats to integers. Try looking at individual pixel values with ST_Value() or us ST_PixelAsPolygons() Pierre -Original Message- From: postgis-users-boun...@postgis.refractions.net [mailto:postgis-users- boun...@postgis.refractions.net] On Behalf Of JamesH Sent: Wednesday, April 11, 2012 12:39 PM To: postgis-users@postgis.refractions.net Subject: Re: [postgis-users] NDVI Calculation from two bands within one Raster Thanks, that has worked and can visualise through OpenJump. The Raster it produces (below), has only values of zero or one in its attributes. I'm assuming in the calculation its not keeping the values as Floats but its converting it to integer? Must be rounding them to either zero or one. Is there a way to solve this i.e. keep the pixels as floating integers? http://postgis.17.n6.nabble.com/file/n4832782/ndvi.jpg Kind Regards, James - GIS Undergraduate -- View this message in context: http://postgis.17.n6.nabble.com/NDVI- Calculation-from-two-bands-within-one-Raster-tp4656995p4832782.html Sent from the PostGIS - User mailing list archive at Nabble.com. ___ postgis-users mailing list postgis-users@postgis.refractions.net http://postgis.refractions.net/mailman/listinfo/postgis-users ___ postgis-users mailing list postgis-users@postgis.refractions.net http://postgis.refractions.net/mailman/listinfo/postgis-users
Re: [postgis-users] NDVI Calculation from two bands within one Raster
Are you using ST_dumpAsPolygons() to display in OpenJump? Try looking at individual pixel values with ST_Value() or us ST_PixelAsPolygons() I was using DumpAsPolygons. Used ST_PixelAsPolygons and this returned the same, either zero, one or NullData. If it helps my GDAL version is: GDAL=GDAL 1.9.0, released 2011/12/29 Try looking at individual pixel values with ST_Value() Examined and in the final output, St_ValueCount returns: (0, 32980) (1, 519) Looked closer at the calculation with ST_ValueCount and [rast1] - [rast2] = (0, 17458) [rast1]+[rast2] = (0, 16223) so at some point these must be dividing by zero? Could this be affecting the NDVI? Kind Regards, James - GIS Undergraduate -- View this message in context: http://postgis.17.n6.nabble.com/NDVI-Calculation-from-two-bands-within-one-Raster-tp4656995p4832963.html Sent from the PostGIS - User mailing list archive at Nabble.com. ___ postgis-users mailing list postgis-users@postgis.refractions.net http://postgis.refractions.net/mailman/listinfo/postgis-users
Re: [postgis-users] NDVI Calculation from two bands within one Raster
Add a pixeltype parameter to your ST_MapAlgebraExpr call. Pierre -Original Message- From: postgis-users-boun...@postgis.refractions.net [mailto:postgis-users- boun...@postgis.refractions.net] On Behalf Of JamesH Sent: Wednesday, April 11, 2012 1:44 PM To: postgis-users@postgis.refractions.net Subject: Re: [postgis-users] NDVI Calculation from two bands within one Raster Are you using ST_dumpAsPolygons() to display in OpenJump? Try looking at individual pixel values with ST_Value() or us ST_PixelAsPolygons() I was using DumpAsPolygons. Used ST_PixelAsPolygons and this returned the same, either zero, one or NullData. If it helps my GDAL version is: GDAL=GDAL 1.9.0, released 2011/12/29 Try looking at individual pixel values with ST_Value() Examined and in the final output, St_ValueCount returns: (0, 32980) (1, 519) Looked closer at the calculation with ST_ValueCount and [rast1] - [rast2] = (0, 17458) [rast1]+[rast2] = (0, 16223) so at some point these must be dividing by zero? Could this be affecting the NDVI? Kind Regards, James - GIS Undergraduate -- View this message in context: http://postgis.17.n6.nabble.com/NDVI- Calculation-from-two-bands-within-one-Raster-tp4656995p4832963.html Sent from the PostGIS - User mailing list archive at Nabble.com. ___ postgis-users mailing list postgis-users@postgis.refractions.net http://postgis.refractions.net/mailman/listinfo/postgis-users ___ postgis-users mailing list postgis-users@postgis.refractions.net http://postgis.refractions.net/mailman/listinfo/postgis-users
Re: [postgis-users] NDVI Calculation from two bands within one Raster
That has worked brilliantly and is exactly what I wanted. I added '32BF' to the the end. Thank you Pierre James - GIS Undergraduate -- View this message in context: http://postgis.17.n6.nabble.com/NDVI-Calculation-from-two-bands-within-one-Raster-tp4656995p4833176.html Sent from the PostGIS - User mailing list archive at Nabble.com. ___ postgis-users mailing list postgis-users@postgis.refractions.net http://postgis.refractions.net/mailman/listinfo/postgis-users
Re: [postgis-users] NDVI Calculation from two bands within one Raster
When altering the query to be: SELECT ST_MapAlgebraExpr(a.rast, 4, b.rast, 3, '(a.rast - b.rast) /(a.rast + b.rast)::float') FROM nclraster1 a, nclraster1 b; instead of rast1 - rast2 etc. Returns an error of ERROR: missing FROM-clause entry for table a LINE 1: SELECT ((a.rast - b.rast) /(a.rast + b.rast)::float)::double... Can anyone explain this? Kind Regards, James - GIS Undergraduate -- View this message in context: http://postgis.17.n6.nabble.com/NDVI-Calculation-from-two-bands-within-one-Raster-tp4656995p4683078.html Sent from the PostGIS - User mailing list archive at Nabble.com. ___ postgis-users mailing list postgis-users@postgis.refractions.net http://postgis.refractions.net/mailman/listinfo/postgis-users
Re: [postgis-users] NDVI Calculation from two bands within one Raster
SELECT ST_MapAlgebraExpr(a.rast, 4, b.rast, 3, '(a.rast - b.rast) /(a.rast + b.rast)::float') FROM nclraster1 a, nclraster1 b; instead of rast1 - rast2 etc. Returns an error of ERROR: missing FROM-clause entry for table a LINE 1: SELECT ((a.rast - b.rast) /(a.rast + b.rast)::float)::double... Can anyone explain this? In the expression, you don't refer to the pixels of a.rast with a.rast but with rast1. so your query should be: SELECT ST_MapAlgebraExpr(a.rast, 4, b.rast, 3, '(rast1 - rast2) /(rast1 + rast2)::float') FROM nclraster1 a, nclraster1 b; Pierre ___ postgis-users mailing list postgis-users@postgis.refractions.net http://postgis.refractions.net/mailman/listinfo/postgis-users
Re: [postgis-users] NDVI Calculation from two bands within one Raster
Have upgraded to Beta4, new error message returning for 'SELECT ST_MapAlgebraExpr(a.rast, 4, b.rast, 3, '(rast1 - rast2) /(rast1 + rast2)::float') FROM nclraster1 a, nclraster1 b;' As column rast1 does not exist. With the previous PostGIS raster version, this has not returned before. Any advice? Regards, James -- View this message in context: http://postgis.17.n6.nabble.com/NDVI-Calculation-from-two-bands-within-one-Raster-tp4656995p4678854.html Sent from the PostGIS - User mailing list archive at Nabble.com. ___ postgis-users mailing list postgis-users@postgis.refractions.net http://postgis.refractions.net/mailman/listinfo/postgis-users
Re: [postgis-users] NDVI Calculation from two bands within one Raster
NDVI is calculated on reflectance values in these bands, not raw DN. Your inputs should already be floating point values, and they should never be less than 0.0 or greater than 1.0. Ok, my raw data was 8BUI which I went back and changed to 64 bit float to try get past this but still, using the equation above is returning: NOTICE: Neither raster provided has a NODATA value for the specified band indices. NODATA value set to minimum possible for 64BF ERROR: division by zero CONTEXT: SQL statement SELECT (($1 - $2) /($1 + $2))::double precision I went back and re-edited the raw raster image to remove the black areas from where it has been geo-rectified but still can't seem to get to grips with what is going wrong in the calculation. -- View this message in context: http://postgis.17.n6.nabble.com/NDVI-Calculation-from-two-bands-within-one-Raster-tp4656995p4660390.html Sent from the PostGIS - User mailing list archive at Nabble.com. ___ postgis-users mailing list postgis-users@postgis.refractions.net http://postgis.refractions.net/mailman/listinfo/postgis-users
Re: [postgis-users] NDVI Calculation from two bands within one Raster
You did not really answer my previous questions... You can easily identify the faulty pixels by changing the expression: SELECT ST_ValueCount(ST_MapAlgebraExpr(a.rast, 4, b.rast, 3, 'rast1 + rast2'), 1, 0.0) FROM nclraster1 a, nclraster1 b; Pierre -Original Message- From: postgis-users-boun...@postgis.refractions.net [mailto:postgis-users- boun...@postgis.refractions.net] On Behalf Of JamesH Sent: Tuesday, March 27, 2012 7:28 AM To: postgis-users@postgis.refractions.net Subject: Re: [postgis-users] NDVI Calculation from two bands within one Raster NDVI is calculated on reflectance values in these bands, not raw DN. Your inputs should already be floating point values, and they should never be less than 0.0 or greater than 1.0. Ok, my raw data was 8BUI which I went back and changed to 64 bit float to try get past this but still, using the equation above is returning: NOTICE: Neither raster provided has a NODATA value for the specified band indices. NODATA value set to minimum possible for 64BF ERROR: division by zero CONTEXT: SQL statement SELECT (($1 - $2) /($1 + $2))::double precision I went back and re-edited the raw raster image to remove the black areas from where it has been geo-rectified but still can't seem to get to grips with what is going wrong in the calculation. -- View this message in context: http://postgis.17.n6.nabble.com/NDVI- Calculation-from-two-bands-within-one-Raster-tp4656995p4660390.html Sent from the PostGIS - User mailing list archive at Nabble.com. ___ postgis-users mailing list postgis-users@postgis.refractions.net http://postgis.refractions.net/mailman/listinfo/postgis-users ___ postgis-users mailing list postgis-users@postgis.refractions.net http://postgis.refractions.net/mailman/listinfo/postgis-users
Re: [postgis-users] NDVI Calculation from two bands within one Raster
Apologies Pierre, the black areas around the original image (below) were the nodata values from where it had been oreintated. http://postgis.17.n6.nabble.com/file/n4661301/Site127.jpg SELECT ST_ValueCount(rast, 3, 0.0) FROM nclraster1 returned 16742 and 16627 in bands 3 and 4 respectively. and SELECT ST_ValueCount(ST_MapAlgebraExpr(a.rast, 4, b.rast, 3, 'rast1 + rast2'), 1, 0.0) FROM nclraster1 a, nclraster1 b; returned 16223 pixels. Both bands 3 and 4 now have nodata values of 255. How would you remove the nodatavalues from the calculation? Kind Regards, James -- View this message in context: http://postgis.17.n6.nabble.com/NDVI-Calculation-from-two-bands-within-one-Raster-tp4656995p4661301.html Sent from the PostGIS - User mailing list archive at Nabble.com. ___ postgis-users mailing list postgis-users@postgis.refractions.net http://postgis.refractions.net/mailman/listinfo/postgis-users
Re: [postgis-users] NDVI Calculation from two bands within one Raster
SELECT ST_ValueCount(ST_MapAlgebraExpr(a.rast, 4, b.rast, 3, 'rast1 + rast2'), 1, 0.0) FROM nclraster1 a, nclraster1 b; returned 16223 pixels. Both bands 3 and 4 now have nodata values of 255. How would you remove the nodatavalues from the calculation? Nodata values, as far as they are properly set in the raster, are NOT part of the computation. 1) Make sure the nodata values are properly set with ST_BandNodataValue() 2) You have to get rid or decide what to do with all those pixels where the sum is equal to 0. Pierre ___ postgis-users mailing list postgis-users@postgis.refractions.net http://postgis.refractions.net/mailman/listinfo/postgis-users
[postgis-users] NDVI Calculation from two bands within one Raster
I am attempting to complete a NDVI calculation using two bands (3 and 4) from a Multiband Raster stored as a Table (nclraster1). From a previous question asked I am basing my calculation on: SELECT ST_MapAlgebra(ST_MapAlgebra(a.rast, 4, b.rast, 3, rast - b.rast, 8BUI), ST_MapAlgebra(a.rast, 4, b.rast, 3, a.rast + b.rast2, 8BUI), a.rast / b.rast, 8BUI) FROM nclraster1 a, nclraster1 b; This does not work for me, returning the column a.rast does not exist. Any pointers on where I'm going wrong much appreciated. James -- View this message in context: http://postgis.17.n6.nabble.com/NDVI-Calculation-from-two-bands-within-one-Raster-tp4656995p4656995.html Sent from the PostGIS - User mailing list archive at Nabble.com. ___ postgis-users mailing list postgis-users@postgis.refractions.net http://postgis.refractions.net/mailman/listinfo/postgis-users
Re: [postgis-users] NDVI Calculation from two bands within one Raster
SELECT ST_MapAlgebra(ST_MapAlgebra(a.rast, 4, b.rast, 3, rast - b.rast, 8BUI), ST_MapAlgebra(a.rast, 4, b.rast, 3, a.rast + b.rast2, 8BUI), a.rast / b.rast, 8BUI) FROM nclraster1 a, nclraster1 b; This does not work for me, returning the column a.rast does not exist. 1) Do you have a column of type raster in table nclraster1? 2) Is it named rast? 3) Is this raster tiled? (or Do you have more than one row in nclraster1?) 4) What if the results of SELECT (ST_SummaryStats(rast)).* FROM nclraster1 ? 5) What is the result of: SELECT PostGIS_Full_Version(); 6) Your query should probably more look like (if you have a recent version of PostGIS): SELECT ST_MapAlgebraExpr(a.rast, 4, b.rast, 3, '([rast1] - [rast2]) / ([rast1] + [rast2])::float') FROM nclraster1 a, nclraster1 b; Pierre ___ postgis-users mailing list postgis-users@postgis.refractions.net http://postgis.refractions.net/mailman/listinfo/postgis-users
Re: [postgis-users] NDVI Calculation from two bands within one Raster
Attempted SELECT ST_MapAlgebraExpr(a.rast, 4, b.rast, 3, '(rast1 - rast2) /(rast1 + rast2)::float') FROM nclraster1 a, nclraster1 b; Returned Neither raster provided has a NODATA value for the specified band indices. NODATA value set to minimum possible for 8BUI Updated NoDataValues for bands 3 and 4 to 255 (were previously blank) and when re-ran, returns: ERROR: division by zero CONTEXT: SQL statement SELECT (($1 - $2) /($1 + $2)::FLOAT)::double precision Regards, James -- View this message in context: http://postgis.17.n6.nabble.com/NDVI-Calculation-from-two-bands-within-one-Raster-tp4656995p4657361.html Sent from the PostGIS - User mailing list archive at Nabble.com. ___ postgis-users mailing list postgis-users@postgis.refractions.net http://postgis.refractions.net/mailman/listinfo/postgis-users
Re: [postgis-users] NDVI Calculation from two bands within one Raster
Attempted SELECT ST_MapAlgebraExpr(a.rast, 4, b.rast, 3, '(rast1 - rast2) /(rast1 + rast2)::float') FROM nclraster1 a, nclraster1 b; Returned Neither raster provided has a NODATA value for the specified band indices. NODATA value set to minimum possible for 8BUI Updated NoDataValues for bands 3 and 4 to 255 (were previously blank) and when re-ran, returns: ERROR: division by zero CONTEXT: SQL statement SELECT (($1 - $2) /($1 + $2)::FLOAT)::double precision Then you must have some 0s in band 3? What is the result of SELECT ST_ValueCount(rast, 3, 0.0) FROM nclraster1 What do you want/expect as result when band 3 = 0? ___ postgis-users mailing list postgis-users@postgis.refractions.net http://postgis.refractions.net/mailman/listinfo/postgis-users
Re: [postgis-users] NDVI Calculation from two bands within one Raster
Apologies, this was an grave error on my part as I didn't account for areas of the raster that have zero data values from where it has been georectified. The result was that I have 16742 zero values in band 3 and 16627 in band 4 so I need to introduce something that excludes zero values from the NDVI calculation. Is there a simple way of doing this as I seem to be over-complicating the calculation? Regards, James -- View this message in context: http://postgis.17.n6.nabble.com/NDVI-Calculation-from-two-bands-within-one-Raster-tp4656995p4657541.html Sent from the PostGIS - User mailing list archive at Nabble.com. ___ postgis-users mailing list postgis-users@postgis.refractions.net http://postgis.refractions.net/mailman/listinfo/postgis-users
Re: [postgis-users] NDVI Calculation from two bands within one Raster
Apologies, this was an grave error on my part as I didn't account for areas of the raster that have zero data values from where it has been georectified. They are zeros or nodata? If they are nodata setting them as nodata with ST_SetbandnodataValue() should be sufficient for ST_MapAlgebraExpr() to ignore them. The result was that I have 16742 zero values in band 3 and 16627 in band 4 so I need to introduce something that excludes zero values from the NDVI calculation. Is there a simple way of doing this as I seem to be over-complicating the calculation? As you might have understood, the problem does not really comes from the fact that some values are equal to 0, but that the sum is equal to 0... If they are zeros (not nodata), just make a CASE in the expression: 'WHEN (rast1 + rast2 = 0) THEN XXX ELSE (rast1 - rast2) /(rast1 + rast2)::float END' Pierre ___ postgis-users mailing list postgis-users@postgis.refractions.net http://postgis.refractions.net/mailman/listinfo/postgis-users
Re: [postgis-users] NDVI Calculation from two bands within one Raster
'WHEN (rast1 + rast2 = 0) THEN XXX ELSE (rast1 - rast2) /(rast1 + rast2)::float END' Returned Neither raster provided has a NODATA value for the specified band indices. NODATA value set to minimum possible for 8BUI Ummm, NDVI is calculated on reflectance values in these bands, not raw DN. Your inputs should already be floating point values, and they should never be less than 0.0 or greater than 1.0. The big concern here is that each band is likely to have a different relationship between DN and reflectance, making the above equation produce something...unexpected. This electronic message contains information generated by the USDA solely for the intended recipients. Any unauthorized interception of this message or the use or disclosure of the information it contains may violate the law and subject the violator to civil or criminal penalties. If you believe you have received this message in error, please notify the sender and delete the email immediately. ___ postgis-users mailing list postgis-users@postgis.refractions.net http://postgis.refractions.net/mailman/listinfo/postgis-users