Hi,

This is one of the examples of the Jiffle "raster calculator" operator
approach.

 9        <Transformation>10          <ogc:Function
name="ras:Jiffle">11            <ogc:Function name="parameter">12
        <ogc:Literal>coverage</ogc:Literal>13
</ogc:Function>14            <ogc:Function name="parameter">15
     <ogc:Literal>script</ogc:Literal>16              <ogc:Literal>17
              nir = src[7];18                vir = src[3];19
     dest = (nir - vir) / (nir + vir);20              </ogc:Literal>21
           </ogc:Function>22          </ogc:Function>23
</Transformation>

See
https://docs.geoserver.org/latest/en/user/styling/sld/extensions/rendering-transform.html#running-map-algebra-on-the-fly-using-jiffle


- -
Jody Garnett


On Sep 2, 2025 at 10:47:35 PM, Roar Brænden <[email protected]>
wrote:

> 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:
>
>    1. Subtract Red from NIR: nir - red
>    2. Add NIR and Red: nir + red
>    3. Divide the difference by the sum: (nir - red) / (nir + red)
>
> Here is how you can do it:
>
> // Assume redCoverage and nirCoverage are your GridCoverage2D 
> objectsOperations 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
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users
>
_______________________________________________
GeoTools-GT2-Users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users

Reply via email to