Hi,

I would like to compute NDVI from a Sentinel 2 scene using bands B04 and B08. I 
asked Copilot if he had a suggestion how to do it, and here is its answer.

To compute NDVI (Normalized Difference Vegetation Index) using Geotools from 
two GridCoverage2D objects (representing the NIR and Red bands), use the 
Operations class to perform raster math:
Subtract Red from NIR: nir - red
Add NIR and Red: nir + red
Divide the difference by the sum: (nir - red) / (nir + red)
Here is how you can do it:

// Assume redCoverage and nirCoverage are your GridCoverage2D objects
Operations op = new Operations(null);

// Step 1: Compute numerator (nir - red)
GridCoverage2D numerator = (GridCoverage2D) op.subtract(nirCoverage, 
redCoverage);

// Step 2: Compute denominator (nir + red)
GridCoverage2D denominator = (GridCoverage2D) op.add(nirCoverage, redCoverage);

// Step 3: NDVI = (nir - red) / (nir + red)
GridCoverage2D ndviCoverage = (GridCoverage2D) op.divide(numerator, 
denominator);

This will give you a new GridCoverage2D containing the NDVI values.


So far, so good. Exactly as I would have thought. The problem is that both 
op.subtract and op.divide doesn't exists. It is only possible to subtract by a 
constant or divide by a constant, not by a coverage.
That seems strange because add and multiply have functions for both converage 
and constants and two coverages.
Do anyone have an explanation for that?

And I know this is old code, and someone might suggest other alternatives. That 
would be fine as well, but still the code is there and bothers me.

Best regards,
Roar Brænden


_______________________________________________
GeoTools-GT2-Users mailing list
GeoTools-GT2-Users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users

Reply via email to