2

I am trying to create a python script for ArcMap which calculates the ndvi from two raster images for each RGB/NIR pair in a folder. However, I am unsure how to create two raster lists; one which lists only NIR images and the other which lists RGB images. Below is an example of the filename formats of the input images for the automated NDVI calculation.

'TER Jenkins Leroy 36000_Home Farm_Field 1_na_NIR_140825_0-50622.tif'

'TER Jenkins Leroy 36000_Home Farm_Field 1_na_RGB_140825_0-50622.tif'

'TER Jenkins Leroy 36000_Home Farm_Field 2_na_NIR_140825_1-50622.tif'

'TER Jenkins Leroy 36000_Home Farm_Field 2_na_RGB_140825_1-50622.tif'

'TZR Costanza George_Eden_Field 1_na_NIR_140825_2-50648.tif'

'TZR Costanza George_Eden_Field 1_na_RGB_140825_2-50648.tif'

Here is the code I have so far:

import arcpy, os
from arcpy import env
from arcpy.sa import *

env.workspace = r"C:\Users\chester\Documents\Test_Dataset"
outws = r'C:\Users\chester\Documents'
cirlist = arcpy.ListRasters(*CIR*)
rgblist = arcpy.ListRasters(*RGB*)

imagelist = zip(sorted(cirlist), sorted(rgblist))

for i in imagelist:
    name = os.path.join(outws, i + 'NDVI') 
    outras = (Raster(i[0]) - Raster(i[1]))/(Raster(i[0]) + Raster(i[1]))
    outras.save(name)
6
  • The names with wildcards need to be in quotes (e.g. arcpy.ListRaster("*CIR*")). You may need to avoid having spaces in the names, but I'm not positive about that. – Erica Sep 23 '14 at 17:59
  • Is there a difference between names of each raster pair? From your example it looks like the NIR files have a "CIR" in the name while the RGB files have CLR in the name. Is this the case? If so then cirlist = arcpy.ListRasters("*CIR*") will give you all NIR images" and rgblist = arcpy.ListRasters("*CLR*") will give you all RGB images – Dowlers Sep 23 '14 at 18:02
  • Erica, thank you for the advice. I added in the quotes which caused the script run to completion. However, it did not generate any output images. The process only took around 4 seconds to complete. – c_mck Sep 23 '14 at 18:48
  • I will try and remove the spaces from each file name. – c_mck Sep 23 '14 at 18:48
  • I removed the spaces from each filename, but it did not seem to resolve the issue. – c_mck Sep 23 '14 at 18:59
3

I prefer using glob.glob() instead of ListRaster, you have more control on the wildcards.

the second point is that you should not need the RGB image, because CIR is usually used to represent Color Infra-Red. So it includes both the red and NIR necessary for the computation of the NDVI.

List = glob.glob(path +"*CIR*")

for im in List:
    print im #just checking you have something
    outndvi = Float(Raster(im+"\\Layer_3")-Raster(im+"\\Layer_2"))/(Raster(im+"\\Layer_3")+Raster(im+"\\Layer_2"))
    outndvi.save(im[:-4]+"ndvi.tif")

Remark :As mentioned in Erica's comment, you should also avoid spaces in the file names.

EDIT : if you need the two rasters, you can also get the corresponding raster with replace()

 imb = im.replace("CIR","CLR")
6
  • Sorry, I forgot to specify that the CIR is actually just an NIR image. I did not come up with those naming conventions for the file name. – c_mck Sep 23 '14 at 19:37
  • Ive edited the file names so they say NIR and RGB instead of CIR and CLR – c_mck Sep 23 '14 at 19:47
  • 1
    so you need to mention name.tif\Layer_1 in order to use your red band. – radouxju Sep 23 '14 at 19:52
  • The images that I am subtracting are not multispectral. Each image in the pair was captured by either an NIR camera or an RGB camera, so band specifications become meaningless in this situation. – c_mck Sep 23 '14 at 21:11
  • 1
    RGB = red/green/blue = 3 bands. How does you image display when you add it to ArcGIS ? If you see "natural" colours, then you have multiple bands, if you have grey scale, then it is a single band, but you need to make sure that it is the RED band. I would be surprised if your camera in the visible spectrum has only one RED band. If it is a panchromatic camera ("black and white"), then it also covered the green and the blue band unless you used a red filter on top of the lens. – radouxju Sep 24 '14 at 5:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.