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:
And calculating the colourised ndvi
for this section (picture from QGIS to illustrate):
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:
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:
gdalwarp
could do that, just specify both as inputs