A program compute NDVI from MODIS/HDF4 file

  • by

Just finished a c++ program to compute NDVI from MODIS/HDF4 file

Written in c++, depends on GDAL(Geospatial Data Abstraction Library)

Source code can be downloaded HERE

Compile

Before compiling, make sure you have make, g++, gdal installed.

go to the hdf2ndvi directory, type:

make

you’ll see

g++  -c hdf2ndvi.cpp
g++  -c enhance_gdal.cpp
g++  -o hdf2ndiv hdf2ndvi.o enhance_gdal.o -lgdal

and then it’s successfully complied.

Usage

##dealing with a single file
type:

./hdf2ndiv filename.hdf

and you’ll see:

computing filename.hdf...

after finished, you’ll get filename.hdf_NDVI.tiff in the same folder

dealing with multiple files

If the files to be computed in the same folder.
You can type:

./hdf2ndvi *.hdf

If you want to compute all the files in the folders and subfolders,
you can use xargs:

find . -name ".hdf" -print0 | xargs -0 ./hdf2ndvi

change band used

This program defaultly regard band0(band is counted from 0) as Red Band, and regard band1 as NIR(near infrared red) band. If you want to change the default value, for instance, set band-4 as NIR band and band-0 as Red Band, you can do like this:

./hdf2ndvi filename.hdf -red 0 -nir 4

and you’ll get file: filename.hdf_NDVI.tiff computed from band4 and band0

Algorithm

The NDVI is computed as following:

but some pixel value of MODIS data is negative,
so when meeting negative value in

or
,
ifthe program set NDVI = 1,
else ifthe program set NDVI = -1.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.