0

I am trying to take a sentinel 2-A jp2 file and along with coordinates for a given polygon, generate the colourised ndvi of that polygon and where everything outside of the polygon is transparent. I then want to convert it to a png.

Given I know where in the map the polygon is located, I got the tile from sentinel that covers that location.

I then used gdal_calc.py with the --projwin option to limit the affected area to an area that contains the polygon. From there I am capable of colourising the resulting ndvi image.

Now I want to take the coordinates that I have for that polygon

"geo_json": {
    "coordinates": [
      [
        [
          -0.5183105267238857,
          52.70755889050349
        ],
        [
          -0.5138501418205424,
          52.707355479805585
        ],
        [
          -0.5090106495574819,
          52.70424627990798
        ],
        [
          -0.5139704021925185,
          52.70204813127995
        ],
        [
          -0.5175195079000033,
          52.70175750758153
        ],
        [
          -0.5183105267238857,
          52.70755889050349
        ]
      ]
    ]

and apply them to the resulting ndvi image so that only the cells contained inside the polygon are colourised and everything outside of the polygon is transparent.

I might not be following the right sequence of commands to achieve my goal, so am open to different approaches. I also don't know which of the gdal commands I now need to use to reach my goal.

So far I have gone from taking this image:

enter image description here

And calculating the colourised ndvi for this section (picture from QGIS to illustrate):

enter image description here

The commands I've used so far have been:

docker run --rm -v /Users:/Users osgeo/gdal:alpine-normal-latest gdal_calc.py --calc="((B.astype(float)-A.astype(float))/(B.astype(float)+A.astype(float)))" -A /Users/hyprstack/Documents/gdal-test/T30UXD_20210531T110621_B04.jp2 -B /Users/hyprstack/Documents/gdal-test/T30UXD_20210531T110621_B08.jp2 --outfile=/Users/hyprstack/Documents/gdal-test/ndvi.tif --type=Float32 --overwrite --projwin 652300 5861239 692881 5835090

Which produced:

enter image description here

And then ran:

docker run --rm -v /Users:/Users osgeo/gdal:alpine-normal-latest gdaldem color-relief /Users/hyprstack/Documents/gdal-test/ndvi.tif /Users/hyprstack/Documents/gdal-test/color_map.txt /Users/hyprstack/Documents/gdal-test/ndvi_color.tif To produce: enter image description here

3
  • You mean you want to stack your result back onto the original image? gdalwarp could do that, just specify both as inputs
    – mikewatt
    Commented Jun 30, 2021 at 16:39
  • No, I want to save the result as a png so I can display it in an app, using openlayers
    – hyprstack
    Commented Jun 30, 2021 at 19:31
  • From my original question "Now I want to take the coordinates that I have for that polygon and apply them to the resulting ndvi image so that only the cells contained inside the polygon are colourised and everything outside of the polygon is transparent."
    – hyprstack
    Commented Jul 1, 2021 at 8:29

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.