I am having trouble figuring out which Con()
statement is the correct one, or if my renaming is incorrect.
Full code below
# Name: Ortho4Calculator.py
# Description: Performs map algebra on individual bands and composites them back together
# Requirements: Spatial Analyst Extension
# Import arcpy & os
import os
import arcpy
# Import spatial analyst extension
from ExtractData import LicenseError
from arcpy import env
from arcpy.sa import *
# Check out Spatial Analyst extension
try:
if arcpy.CheckExtension("Spatial") == "Available":
arcpy.CheckOutExtension("Spatial")
print ("Checked out \"Spatial\" Extension")
else:
raise LicenseError
except LicenseError:
print "Spatial Analyst license is unavailable"
except:
print arcpy.GetMessages(2)
# Set workspace as parameter
arcpy.env.workspace = (r"C:\ProjectData")
arcpy.env.scratchWorkspace = (r"C:\ProjectData\temp")
# overwrite files with the same name
env.overwriteOutput = True
# Get raster list
raster_List = arcpy.ListRasters("*", "TIF")
for raster in raster_List:
print(raster)
# raster_List.getRasterBands()
# Create for loop to iterate over raster names to create individual bands
for raster in raster_List:
BaseName = raster
print(BaseName) #correct
band1 = os.path.join(raster, "Band_1")
band2 = os.path.join(raster, "Band_2")
band3 = os.path.join(raster, "Band_3")
band4 = os.path.join(raster, "Band_4")
print(band1) #corrrect
# Create empty list for bands
bandList = []
# Raster Calculations
#rasBand1 = Con((band1 == 0) & (band2 == 0) & (band3 == 0) & (band4 > 0), 1, band1)
rasBand1 = Con(band1, 1, band1, (band1 == 0) & (band2 == 0) & (band3 == 0) & (band4 > 0))
rasBand2 = Con(((band1 == 0) & (band2 == 0) & (band3 == 0) & (band4 > 0)), 1, band2)
rasBand3 = Con(((band1 == 0) & (band2 == 0) & (band3 == 0) & (band4 > 0)), 1, band3)
rasBand4 = os.path.join(raster, "Band_4")
#arcpy.CopyRaster_management(raster, r"C:\Project Data")
arcpy.Rename_management(rasBand1, raster + "Band_1")
bandList.append(rasBand1)
arcpy.Rename_management(rasBand2, raster + "Band_2")
bandList.append(rasBand2)
arcpy.Rename_management(rasBand3, raster + "Band_3")
bandList.append(rasBand3)
arcpy.Rename_management(rasBand4, raster + "Band_4")
bandList.append(rasBand4)
print(bandList) #bands not renamed correctly
When I try the first way (#rasBand1) it gives an error
C:\Python27\ArcGIS10.6\python.exe C:/GISCustomization/FinalProject/Ortho4Calculator.py
Checked out "Spatial" Extension
a10seg595970.tif
a10seg595970.tif
a10seg595970.tif\Band_1
Traceback (most recent call last):
File "C:/GISCustomization/FinalProject/Ortho4Calculator.py", line 60, in <module>
arcpy.Rename_management(rasBand1, raster + "Band_1")
File "C:\Program Files (x86)\ArcGIS\Desktop10.6\ArcPy\arcpy\management.py", line 4528, in Rename
raise e
arcgisscripting.ExecuteError: ERROR 999999: Error executing function.
Failed to execute (Rename).
Process finished with exit code 1
When I try the second way (rasBand1) it gives an error
C:\Python27\ArcGIS10.6\python.exe C:/GISCustomization/FinalProject/Ortho4Calculator.py
Checked out "Spatial" Extension
a10seg595970.tif
a10seg595970.tif
a10seg595970.tif\Band_1
Traceback (most recent call last):
File "C:/GISCustomization/FinalProject/Ortho4Calculator.py", line 54, in <module>
rasBand1 = Con(band1, 1, band1, (band1 == 0) & (band2 == 0) & (band3 == 0) & (band4 > 0))
File "C:\Program Files (x86)\ArcGIS\Desktop10.6\ArcPy\arcpy\sa\Functions.py", line 269, in Con
where_clause)
File "C:\Program Files (x86)\ArcGIS\Desktop10.6\ArcPy\arcpy\sa\Utils.py", line 53, in swapper
result = wrapper(*args, **kwargs)
File "C:\Program Files (x86)\ArcGIS\Desktop10.6\ArcPy\arcpy\sa\Functions.py", line 263, in Wrapper
where_clause)
File "C:\Program Files (x86)\ArcGIS\Desktop10.6\ArcPy\arcpy\geoprocessing\_base.py", line 510, in <lambda>
return lambda *args: val(*gp_fixargs(args, True))
arcgisscripting.ExecuteError: ERROR 010416: Error in setting raster table filter for raster.
Failed to execute (Con).
Process finished with exit code 1