1

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
1

You can't rename a band, it's part of a (file/database/object) raster not a raster in itself, you can copy a band as a new file though, that makes it a new raster in its own right.

According to the help for Con the where_clause should be surrounded in quotes though that's not the only problem here. According to the SQL syntax reference the and is and not & or && - that's raster calculator syntax and should go into the first clause and not the 4th so your Con statement should be more like rasBand1 = Con((band1 == 0) & (band2 == 0) & (band3 == 0) & (band4 > 0), 1, band1) but that will create a raster full of 0's and 1's as the result is 0 if all the bands are 0 and 1 if any of the bands are not 0 - perhaps that's what you're after... though that conflicts with your original statement rasBand1 = Con(band1, 1, band1, (band1 == 0) & (band2 == 0) & (band3 == 0) & (band4 > 0)) which will produce a raster if 0 where band1 is 0 and nodata for any nonzero value in any of band 1 to 4 - even if the SQL was corrected.

Cell statistics might be of use here to compound the bands into a single raster which makes the Con run faster, as it stands each equality is created as a binary raster object then each & is evaluated which is tediously slow, cell statistics is one operation performed by an executable which would reduce the conditional temporary rasters as python objects. Your code would then look like rasBand1 = Con(CellStatistics([band1,band2,band3,band4],"maximum") == 0,1,band1) which would run much faster.

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.