def index(): if request.method == "POST": print(request.files) if ('file4' not in request.files) or ('file8' not in request.files): flash('No files Selected.') return redirect(request.url) file4 = request.files['file4'] file8 = request.files['file8'] # if user does not select file, browser also # submit an empty part without filename if file4.filename == '' or file8.filename == '': flash('Please Select files for both band') return redirect(request.url) if file4 and file8 and allowed_file(file4.filename) and allowed_file( file8.filename): filename4 = secure_filename(file4.filename) filename8 = secure_filename(file8.filename) dest = Path('static/NDVI.tiff') try: ndvi(filename4, filename8, str(dest)) except Exception as ex: flash("Internal Error") print(ex) redirect(request.url) else: return send_from_directory('static', 'NDVI.tiff', as_attachment=True) else: flash("Only tiff and jp2 files are allowed.") redirect(request.url) return render_template("base.html")
from gdal import Open from ndvi import ndvi # Abrindo banda NIR nir_tiff = Open(r'NIR_IMAGE.tif') #nir_tiff = Open(r'nir.tif') nir_band = nir_tiff.GetRasterBand(1) # Abrindo banda RED. red_tiff = Open(r'RED_IMAGE.tif') #red_tiff = Open(r'red.tif') red_band = red_tiff.GetRasterBand(1) # Get the rows and cols from one of the images (both should always be the same) rows, cols, geotransform = nir_tiff.RasterYSize, nir_tiff.RasterXSize, nir_tiff.GetGeoTransform() print(geotransform) # Arquivo de saida 16-bit (0-255) out_tiff_int16 = r'NDVI_16.tif' # Arquivo de saida 32-bit floating point (-1 to 1) out_tiff_float32 = r'NDVI_32.tif' # Gerando imagem NDVI 16-bit integer ndvi(nir_band, red_band, rows, cols, geotransform, out_tiff_int16, gdal.GDT_UInt16) # Gerando imagem NDVI 32-bit floating point ndvi(nir_band, red_band, rows, cols, geotransform, out_tiff_float32, gdal.GDT_Float32) print('done')
import gdal from gdal import Open from ndvi import ndvi # Open NIR image and get its only band. nir_tiff = Open(r'NIR_IMAGE.tif') nir_band = nir_tiff.GetRasterBand(1) # Open red image and get its only band. red_tiff = Open(r'RED_IMAGE.tif') red_band = red_tiff.GetRasterBand(1) # Get the rows and cols from one of the images (both should always be the same) rows, cols, geotransform = nir_tiff.RasterYSize, nir_tiff.RasterXSize, nir_tiff.GetGeoTransform() print(geotransform) # Set an output for a 16-bit unsigned integer (0-255) out_tiff_int16 = r'NDVI_INT16.tif' # Set the output for a 32-bit floating point (-1 to 1) out_tiff_float32 = r'NDVI_FLOAT32.tif' # Run the function for unsigned 16-bit integer ndvi(nir_band, red_band, rows, cols, geotransform, out_tiff_int16, gdal.GDT_UInt16) # Run the function for 32-bit floating point ndvi(nir_band, red_band, rows, cols, geotransform, out_tiff_float32, gdal.GDT_Float32) print('done')
print(red_band) blue_tiff = Open(r'BLUE_IMAGE.tif') blue_band = blue_tiff.GetRasterBand(1) green_tiff = Open(r'GREEN_IMAGE.tif') green_band = green_tiff.GetRasterBand(1) swir_tiff = Open(r'SWIR_IMAGE.tif') swir_band = swir_tiff.GetRasterBand(1) mir_tiff = Open(r'MIR_IMAGE.tif') mir_band = mir_tiff.GetRasterBand(1) (rows, cols) = (nir_tiff.RasterYSize, nir_tiff.RasterXSize) ndvi_value = ndvi(nir_band, red_band, rows, cols) print(ndvi_value) ndv = nan_to_num(result) print(ndv, "sd") result = ndvi_value row = len(result) - 1 col = len(result[0]) - 729 i = 0 sum = 0 for i in range(row): j = 0 for j in range(col): prev = sum sum += result[i, j] denom = row * col ndvi_value = sum / denom
import gdal from gdal import Open from ndvi import ndvi # Open NIR image and get its only band. nir_tiff = Open(r'NIR_IMAGE.tif') nir_band = nir_tiff.GetRasterBand(1) # Open red image and get its only band. red_tiff = Open(r'RED_IMAGE.tif') red_band = red_tiff.GetRasterBand(1) # Get the rows and cols from one of the images (both should always be the same) rows, cols, geotransform = nir_tiff.RasterYSize, nir_tiff.RasterXSize, nir_tiff.GetGeoTransform( ) print(geotransform) # Set the output for a 32-bit floating point (-1 to 1) out_tiff_float32 = r'NDVI_FLOAT32.tif' # Run the function for 32-bit floating point ndvi(nir_band, red_band, rows, cols, geotransform, out_tiff_float32) print('done')
def do_ndvi(): "do_ndvi(): function to read MODIS refl HDF file and calculate NDVI" ifile = 'MOD09GA.A2004154.h19v10.005.2008152141836.hdf' title = ifile ofile = 0 red = 'sur_refl_b01_1' nir = 'sur_refl_b02_1' xmin = 0 xmax = 500 ymin = 0 ymax = 500 xlabel = 'Col' ylabel = 'Row' # min and max values for colourbar on plot ndvimin = 0.1 ndvimax = 0.8 # default plot value plot = 1 if options.ifile: ifile = options.ifile if options.ofile: ofile = options.ofile if options.sdsName: sdsName = options.sdsName # read HDF file md = hdf_utils.r(ifile) if options.v: # o/p file datasets sys.stderr.write('ifile: %s\n' % (ifile)) md.datasets() sds_red = md.select(red) sds_nir = md.select(nir) # do we have extract i.e. xmin, xmax, ymin, ymax? if options.xmin: xmin = options.xmin if options.ymin: ymin = options.ymin if options.xmax: xmax = options.xmax if options.ymax: ymax = options.ymax # scale ndvi values? if options.ndvimin: ndvimin = options.ndvimin if options.ndvimax: ndvimax = options.ndvimax if options.xlabel: xlabel = options.xlabel if options.ylabel: ylabel = options.ylabel if options.title: title = options.title # check dims if (sds_red.dimensions('XDim').values()[0][0] != sds_nir.dimensions('XDim').values()[0][0]) or ( sds_red.dimensions('YDim').values()[0][0] != sds_nir.dimensions('YDim').values()[0][0]): sys.stderr.write( '%s: dimension error - x, y dims of SDS %s and %s do not match\n' % (ifile, red, nir)) # get extract if required, and cast to float32 if (options.xmin or options.ymin or options.xmax or options.ymax): sds_red = np.float32(sds_red[xmin:xmax, ymin:ymax]) sds_nir = np.float32(sds_nir[xmin:xmax, ymin:ymax]) n = np.zeros([xmax - xmin, ymax - ymin]) else: xdim = sds_red.dimensions('XDim').values()[0][0] ydim = sds_red.dimensions('YDim').values()[0][0] sds_red = np.float32(sds_red[0:xdim, 0:ydim]) sds_nir = np.float32(sds_nir[0:xdim, 0:ydim]) n = np.zeros([xdim, ydim]) # calculate ndvi from bands 1, 2 n = ndvi.ndvi(sds_red, sds_nir) # clip if required np.clip(n, -1., 1., out=n) if options.v: # o/p file datasets sys.stderr.write('ifile: %s\n' % (ifile)) md.datasets() if options.plot: # o/p file datasets plot = 1 if plot: plot_me.plot(n, title, xlabel, ylabel, 'NDVI', ofile, ndvimin, ndvimax)