# Calculates NDSSI from multispectral composite images from a folder

1793
6
12-31-2016 11:35 AM New Contributor

I an new to arcpy and python. I have been trying to calculate NDSSI from 100 composite raster images from a folder using python script. Any how i tried my luck using a script but its not working. Taking list of rasters from one workspace and calculate NDSSI and store results in .tif format in another folder.

```import arcpy, string
from arcpy import envfrom arcpy.sa import*
arcpy.CheckOutExtension("spatial")
env.workspace = r'E:\Landsat All Images\Processing\LT5\Composite'
outws = r'E:\Landsat All Images\Processing\LT5\NDSSI'
rasters = arcpy.ListRasters("*.tif")
for raster in rasters:
```Blue = raster + "\Band_1"NIR = raster + "\Band_4"Num = arcpy.sa.Float(Raster(Blue) - Raster(NIR))Denom = arcpy.sa.Float(Raster(Blue) + Raster(NIR))NIR_eq = arcpy.sa.Divide(Num, Denom) NIR_eq.save(outws)
print "Processing complete"``````

Tags (5)
1 Solution

Accepted Solutions by Esri Esteemed Contributor

I just executed the code below and it worked for me:

``````# Calculates NDVI from multispectral imagery
import arcpy
import os

from arcpy import env
arcpy.CheckOutExtension("Spatial")
arcpy.env.overwriteOutput = True

# env.workspace = r'E:\Landsat All Images\Processing\LT5\Composite'
inws = r'C:\Esri EPM\Drone2Map\EPM\products\2D\ortho_tiles'
outws = r'C:\Esri EPM\Drone2Map\EPM\products\2D'
env.workspace = inws

rasters = arcpy.ListRasters("*.tif")
for raster in rasters:
# You may need to change the band combinations.
# This is for 4-band NAIP imagery or Landsat TM.
Blue = arcpy.Raster(os.path.join(inws, raster + "\Band_1"))
NIR = arcpy.Raster(os.path.join(inws, raster + "\Band_4"))

NIR_eq = arcpy.sa.Float(Blue - NIR) / arcpy.sa.Float(Blue + NIR)
out_name = raster.lower().replace('.tif', '_NDVI.tif')
out_ras = os.path.join(outws, out_name)
NIR_eq.save(out_ras)

print "Processing complete"‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍``````
6 Replies by MVP Regular Contributor

Please never just say "it's not working". You are much more likely to get an answer by specifying what should happen and what actually happens, i.e does your script actually run at all, does it output the wrong result, does it output nothing, does it crash with an exception, what was the exception message, does python crash, does any smoke come out of your pc, does your pc vanish in a fiery explosion? Etc... by MVP Legendary Contributor

You will need to format your code using the appropriate syntax highlighter.... It won't run as shown and impossible to dissect any errors that may be due to code structure or format.... have a look at New Contributor

Thank You Dan for Guidance....I am beginner in programming. Here is code with using online syntax highlighter.

``````# Calculates NDVI from multispectral imagery

import arcpy, string

from arcpy import env
from arcpy.sa import*

arcpy.CheckOutExtension("spatial")

env.workspace = r'E:\Landsat All Images\Processing\LT5\Composite'

outws = r'E:\Landsat All Images\Processing\LT5\NDSSI'

rasters = arcpy.ListRasters("*.tif")

for raster in rasters:

# You may need to change the band combinations.
# This is for 4-band NAIP imagery or Landsat TM.
Blue = raster + "\Band_1"
NIR = raster + "\Band_4"

Blue_out = "Blue.tif"
NIR_out = "NIR.tif"

arcpy.CopyRaster_management(Blue,Blue_out)
arcpy.CopyRaster_management(NIR, NIR_out)

Num = arcpy.sa.Float(Raster(Blue) - Raster(NIR))
Denom = arcpy.sa.Float(Raster(Blue) + Raster(NIR))
NIR_eq = arcpy.sa.Divide(Num, Denom)

NIR_eq.save(outws)
print "Processing complete"‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍`````` by MVP Legendary Contributor

If memory serves, you will have to specify the filename and not just the environment workspace as in this helpfile example Raster—Help | ArcGIS Desktop

Perhaps NIR_eq.save(outws+"/output.tif" by Esri Esteemed Contributor

Another thing to keep in mind, and what will probably create problems, is that fact that you save the Blue and NIR for each TIF to the same output name (in you input folder). Since you have not specified the "arcpy.env.overwriteOutput = True" it will produce an error for the second raster in the list. by Esri Esteemed Contributor

I just executed the code below and it worked for me:

``````# Calculates NDVI from multispectral imagery
import arcpy
import os

from arcpy import env
arcpy.CheckOutExtension("Spatial")
arcpy.env.overwriteOutput = True

# env.workspace = r'E:\Landsat All Images\Processing\LT5\Composite'
inws = r'C:\Esri EPM\Drone2Map\EPM\products\2D\ortho_tiles'
outws = r'C:\Esri EPM\Drone2Map\EPM\products\2D'
env.workspace = inws

rasters = arcpy.ListRasters("*.tif")
for raster in rasters:
# You may need to change the band combinations.
# This is for 4-band NAIP imagery or Landsat TM.
Blue = arcpy.Raster(os.path.join(inws, raster + "\Band_1"))
NIR = arcpy.Raster(os.path.join(inws, raster + "\Band_4"))

NIR_eq = arcpy.sa.Float(Blue - NIR) / arcpy.sa.Float(Blue + NIR)
out_name = raster.lower().replace('.tif', '_NDVI.tif')
out_ras = os.path.join(outws, out_name)
NIR_eq.save(out_ras)

print "Processing complete"‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍``````
{"ft_lang_label":"__label__en","ft_lang_prob":0.66692305,"math_prob":0.8483457,"size":4634,"snap":"2023-40-2023-50","text_gpt3_token_len":1240,"char_repetition_ratio":0.12980561,"word_repetition_ratio":0.43435982,"special_character_ratio":0.24838153,"punctuation_ratio":0.17627119,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9671673,"pos_list":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30],"im_url_duplicate_count":[null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-12-03T15:01:26Z\",\"WARC-Record-ID\":\"<urn:uuid:56bc8e7c-a29a-4d9f-a486-40c270337f61>\",\"Content-Length\":\"335252\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:221baae4-3027-47cf-8f41-09186d16f99d>\",\"WARC-Concurrent-To\":\"<urn:uuid:46aad64f-ecb6-4f14-afb7-b2e65b6a210e>\",\"WARC-IP-Address\":\"13.32.151.62\",\"WARC-Target-URI\":\"https://community.esri.com/t5/python-questions/calculates-ndssi-from-multispectral-composite/td-p/361802\",\"WARC-Payload-Digest\":\"sha1:FO2Q5J5RENFLER2RDCQSLI6UBUA3FVTM\",\"WARC-Block-Digest\":\"sha1:USHT7OWSTBNMFT2JHH664WPBS62JVJVA\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-50/CC-MAIN-2023-50_segments_1700679100508.23_warc_CC-MAIN-20231203125921-20231203155921-00396.warc.gz\"}"}