2

Here is my code so far:

import arcpy
from arcpy.sa import *

arcpy.CheckOutExtension("Spatial")


workspace = arcpy.env.workspace = "C:/Users/KJanus/Desktop/Intro_ArcGIS/RasterData_Lab6"
arcpy.CreateFolder_management(workspace, "NDVI")
arcpy.env.overwriteOutput = True


RED_1 = arcpy.Raster("C:/Users/KJanus/Desktop/Intro_ArcGIS/RasterData_Lab6/landtm1.img/Layer_3")
NIR_1 = arcpy.Raster("C:/Users/KJanus/Desktop/Intro_ArcGIS/RasterData_Lab6/landtm1.img/Layer_4")

RED_2 = arcpy.Raster("C:/Users/KJanus/Desktop/Intro_ArcGIS/RasterData_Lab6/landtm2.img/Layer_3")
NIR_2 = arcpy.Raster("C:/Users/KJanus/Desktop/Intro_ArcGIS/RasterData_Lab6/landtm2.img/Layer_4")

RED_3 = arcpy.Raster("C:/Users/KJanus/Desktop/Intro_ArcGIS/RasterData_Lab6/landtm3.img/Layer_3")
NIR_3 = arcpy.Raster("C:/Users/KJanus/Desktop/Intro_ArcGIS/RasterData_Lab6/landtm3.img/Layer_4")

print "Program recognizes raster bands."

NDVI_1 = (NIR_1 - RED_1)/(NIR_1 + RED_1)

sr = arcpy.sa.Con(RED_1<>0, NDVI_1, -9999)

NDVI_2 = (NIR_2 - RED_2)/(NIR_2 + RED_2)

sr = arcpy.sa.Con(RED_2<>0, NDVI_2, -9999)

NDVI_3 = (NIR_3 - RED_3)/(NIR_3 + RED_3)

sr = arcpy.sa.Con(RED_3<>0, NDVI_3, -9999)


print "NDVIs passed"


arcpy.CopyRaster_management("NDVI_1", "C:/Users/KJanus/Desktop/Intro_ArcGIS/RasterData_Lab6/NDVI", 
format= "TIFF")

When I run the code I get these two errors: ExecuteError: Failed to execute. Parameters are not valid. ERROR 000732: Input Raster: Dataset NDVI_1 does not exist or is not supported

  • 1
    What happened when you run the code? – ahmadhanb Dec 8 '17 at 5:04
  • sr is being overwritten twice, do you want the 3 X sr to be the 3 bands of a raster? If so make the variables sr1, sr2 and sr3 then use CompositeBands_management([sr1,sr2,sr3], "C:/Users/KJanus/Desktop/Intro_ArcGIS/RasterData_Lab6/NDVI/NDVI_1.tif") resources.arcgis.com/en/help/main/10.2/index.html#//… - Arc is smart enough to understand you want to export to GeoTIFF format if you supply the file extension. Also I believe that Not Equals is != in Con resources.arcgis.com/en/help/main/10.2/index.html#//… and -9999 isn't a good value to use. – Michael Stimson Dec 8 '17 at 5:09
  • 1
    It will promote the pixels from 8 bit unsigned to 32 bit signed integer type.. which will cause havoc in trying to use the data in a 3 band image. I can see the code but as @ahmadhanb is alluding, what is the actual problem you're having? – Michael Stimson Dec 8 '17 at 5:13
  • When I run the code I get these two errors: ExecuteError: Failed to execute. Parameters are not valid. ERROR 000732: Input Raster: Dataset NDVI_1 does not exist or is not supported – K.J Dec 8 '17 at 5:15
3

This should be the correct syntax:

NDVI_1.save("C:/Users/KJanus/Desktop/Intro_ArcGIS/RasterData_Lab6/NDVI.tif")
  • That is true for a single band but we're not really sure what the actual problem is. Still, you are correct so +1 from me. – Michael Stimson Dec 8 '17 at 5:15
  • 1
    Yes, the code is a bit vague so I just went with the simple answer. – Dan Dec 8 '17 at 5:17
  • Oh sweet. It works! Yeah I'm extremely new to arcpy and python in general so my code is still clunky, but it's getting there. Thanks Dan. – K.J Dec 8 '17 at 5:19
  • Looking at your band syntax, it doesn't look quite right K.J. Are you sure the bands are called 'Layer_'? If you browse to the rasters in ArcCatalog in contents view are the bands called Layer_1, Layer_2 etc or is it Band_1, Band_2 etc..? – Michael Stimson Dec 8 '17 at 5:27
  • It's Layer_n with n ranging from 1 to 7. It comes from landsat TM data – K.J Dec 8 '17 at 5:42

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.