Script tool working fine in IDLE, but not as a tool in ArcGIS Pro? It is creating a bounding rectangle

742
1
Jump to solution
09-08-2020 03:11 PM
ElijahAllan
New Contributor III

Hello everyone. I am fairly new to creating ArcGIS Pro scripts tools. This is the second script tool I have made. I created a script tool I named "Land Cover Change Type Tool" in ArcGIS Pro 2.5.0 that will take input two different years of NLCD data, then Extract By Mask the rasters using a user shapefile input, then run the Combine tool and add some new fields and using Field Calculator, populate the new fields. When I run my stand alone script with IDLE, I get the exact results I want, which in ArcGIS Pro looks like this:

However, in my script tool "Land Cover Change Type Tool", everything pretty much runs right but I get a large rectangular box with unwanted "Unclassified" data/pixels in the product, as you can see here:

It looks like my two Extract By Mask rasters products also have the bounding rectangle in them:

However, when I look up the Extract By Mask tool on ArcGIS Pro's Tool Reference website, there is no information in the syntax about enabling or disabling a bounding rectangle option. 

Would anyone know what might be possibly causing this change/issue I am having with the unwanted rectangular box of unclassified pixels showing up in my script tool. Here is my script for the script tool:

#Import modules
import os
import sys
import arcpy
from arcpy import env
from arcpy.sa import *

#Set workspace environment
arcpy.env.workspace = out_folder_path = arcpy.GetParameterAsText(0)

# Check out the ArcGIS Spatial Analyst extension license
arcpy.CheckOutExtension("Spatial")

#Over write outputs
arcpy.env.overWriteOuput = True

# EXTRACT BY MASK RASTER TOOL (Will "Clip" Rasters)
# Set Local Variables and get user input for NLCD Rasters
inputNLCD1 = arcpy.GetParameterAsText(1)
inputNLCD2 = arcpy.GetParameterAsText(2)
inputForExtractingExtent = arcpy.GetParameterAsText(3)

#Execute ExtractByMask Tool for first NLCD raster.
outExtractByMask = ExtractByMask(inputNLCD1,inputForExtractingExtent)
# Save the ouput
outExtractByMask.save("ExtractedNLCD_1.img")

#Execute ExtractByMask Tool for second NLCD raster.
outExtractByMask2 = ExtractByMask(inputNLCD2,inputForExtractingExtent)
# Save the ouput
outExtractByMask2.save("ExtractedNLCD_2.img")


# COMBINE RASTER TOOL
# Set local variables for Combine tool.
inRaster01 = out_folder_path + "/ExtractedNLCD_1.img"
inRaster02 = out_folder_path + "/ExtractedNLCD_2.img"
outCombinedRaster = arcpy.GetParameterAsText(4)

# Execute Combine tool.
outCombine = Combine([inRaster01,inRaster02])
# Save the output.
outCombine.save(outCombinedRaster)


#ADD TWO NEW FIELDS AND RECLASSIFY THEM USING AN EXISTING FIELD
# Set local variables for Adding two new fields for reclassification use.
inFeatures = outCombinedRaster
fieldName1 = "NLCDClss1"
fieldName2 = "NLCDClss2"
fieldType = "TEXT"
fieldLength = 35

# Execute AddField tool twice for two new fields for reclassification.
arcpy.AddField_management(inFeatures, fieldName1, fieldType,
 field_length = fieldLength, field_is_nullable="NULLABLE")
arcpy.AddField_management(inFeatures, fieldName2, fieldType,
 field_length = fieldLength, field_is_nullable="NULLABLE")

#CALCULATE NEW FIELD for 1st Raster
#Set local variables to be used in Calculate Field to reclass NLCD01Clss from FlightPat1.
inFeatures = outCombinedRaster
fieldName_NLCDClss1 = "NLCDClss1"
expression = "Reclass(!ExtractedN!)"
expressionType = "PYTHON_9.3"

# Code Block for Calculate Field to reclass NLCD01Clss field using integer values from FlightPat1 field.
codeBlock1 ="""def Reclass(extractedN):
 result = None
 if (extractedN == 0):
 return "Unclassified"
 elif (extractedN == 11):
 return "Open Water"
 elif (extractedN == 12):
 return "Perennial Snow/Ice"
 elif (extractedN == 21):
 return "Developed, Open Space"
 elif (extractedN == 22):
 return "Developed, Low Intensity"
 elif (extractedN == 23):
 return "Developed, Medium Intensity"
 elif (extractedN == 24):
 return "Developed, High Intensity"
 elif (extractedN == 31):
 return "Barren Land"
 elif (extractedN == 41):
 return "Deciduous Forest"
 elif (extractedN == 42):
 return "Evergreen Forest"
 elif (extractedN == 43):
 return "Mixed Forest"
 elif (extractedN == 52):
 return "Shrub/Scrub"
 elif (extractedN == 71):
 return "Herbaceuous"
 elif (extractedN == 81):
 return "Hay/Pasture"
 elif (extractedN == 82):
 return "Cultivated Crops"
 elif (extractedN == 90):
 return "Woody Wetlands"
 elif (extractedN == 95):
 return "Emergent Herbaceuous Wetlands"
 return result
"""

#Reclassify NLCD01Clss field based off integer value from FlightPat1 field.
arcpy.CalculateField_management(inFeatures, fieldName_NLCDClss1,
 expression, expressionType,
 codeBlock1)

# CALCULATE NEW FIELD for second raster
#Set local variables to be used in Calculate Field to reclass NLCD04Clss from FlightPat2.
inFeatures = outCombinedRaster
fieldName_NLCDClss2 = "NLCDClss2"
expression = "Reclass(!Extracte_1!)"
expressionType = "PYTHON_9.3"

# Code Block for Calculate Field to reclass NLCD04Clss field using integer values from FlightPat2 field.
codeBlock2 ="""def Reclass(extracte_1):
 result = None
 if (extracte_1 == 0):
 return "Unclassified"
 elif (extracte_1 == 11):
 return "Open Water"
 elif (extracte_1 == 12):
 return "Perennial Snow/Ice"
 elif (extracte_1 == 21):
 return "Developed, Open Space"
 elif (extracte_1 == 22):
 return "Developed, Low Intensity"
 elif (extracte_1 == 23):
 return "Developed, Medium Intensity"
 elif (extracte_1 == 24):
 return "Developed, High Intensity"
 elif (extracte_1 == 31):
 return "Barren Land"
 elif (extracte_1 == 41):
 return "Deciduous Forest"
 elif (extracte_1 == 42):
 return "Evergreen Forest"
 elif (extracte_1 == 43):
 return "Mixed Forest"
 elif (extracte_1 == 52):
 return "Shrub/Scrub"
 elif (extracte_1 == 71):
 return "Herbaceuous"
 elif (extracte_1 == 81):
 return "Hay/Pasture"
 elif (extracte_1 == 82):
 return "Cultivated Crops"
 elif (extracte_1 == 90):
 return "Woody Wetlands"
 elif (extracte_1 == 95):
 return "Emergent Herbaceuous Wetlands"
 return result
"""
#Reclassify NLCD04Clss field based off integer value from FlightPat2 field.
arcpy.CalculateField_management(inFeatures, fieldName_NLCDClss2,
 expression, expressionType,
 codeBlock2)


#ADD NEW FIELD
# Set local variables for Adding new Land Cover Change field.
inFeatures = outCombinedRaster
fieldName_LanCovChng = "LanCovChng"
fieldType = "TEXT"
fieldLength = 80

# Execute AddField tool to create new Land Cover Change (LanCovChng) field.
arcpy.AddField_management(inFeatures, fieldName_LanCovChng, fieldType,
 field_length = fieldLength, field_is_nullable = "NULLABLE")

# CALCULATE NEW FIELD
#Set local variables to be used in Calculate Field for new Land Cover Change field
inFeatures = outCombinedRaster
fieldName_LanCovChng = "LanCovChng"
expression = "Reclass(!NLCDClss1!, !NLCDClss2!)"
expressionType = "PYTHON_9.3"

# Code Block for Calculate Field to show "No Change", if no change, or NLCD01Clss + "to" + NLCD04Clss, if there was a change.
codeBlock3 ="""def Reclass(NLCDClss1, NLCDClss2):
 if NLCDClss1 == NLCDClss2:
 return "No Change"
 else:
 return NLCDClss1 + " to " + NLCDClss2
"""

#Run Calculate Field tool for new Land Cover Change field if NLCD01Clss + NLCD04Clss are equal reclass to "No Change"
# else combine reclassifications showing land cover change that has happened.
arcpy.CalculateField_management(inFeatures, fieldName_LanCovChng,
 expression, expressionType,
 codeBlock3)


# ADD NEW FIELD 
# Set local variables for Adding new Land Cover True/False field.
inFeatures = outCombinedRaster
fieldName_LnCvTruFls = "LnCvTruFls"
fieldType = "TEXT"
fieldLength = 5

# Execute AddField tool to create new Land Cover True/False field.
arcpy.AddField_management(inFeatures, fieldName_LnCvTruFls, fieldType,
 field_length = fieldLength, field_is_nullable = "NULLABLE")

# CALCULATE NEW FIELD
# Set local variables to be used in Calculate Field for new Land Cover Change field
inFeatures = outCombinedRaster
fieldName_LnCvTruFls = "LnCvTruFls"
expression = "Reclass(!NLCDClss1!, !NLCDClss2!)"
expressionType = "PYTHON_9.3"

# Code Block for Calculate Field to show O for "No Change" and 1 for "Change"
codeBlock4 ="""def Reclass(NLCDClss1, NLCDClss2):
 if NLCDClss1 == NLCDClss2:
 return False
 else:
 return True
"""

#Run Calculate Field tool for new Land Cover True/False field
arcpy.CalculateField_management(inFeatures, fieldName_LnCvTruFls,
 expression, expressionType,
 codeBlock4)


# ADD NEW FIELD AND CALCULATE THE SQUARE KILOMETERS OF UNIQUE CLASS CHANGES
# Set local variables for Adding new Area Square Kilometer field.
inFeatures = outCombinedRaster
fieldName_Area_sqkm = "Area_sqkm"
fieldType = "DOUBLE"

# Execute AddField tool to create new Area Square Kilometer field.
arcpy.AddField_management(inFeatures, fieldName_Area_sqkm, fieldType,
 field_is_nullable = "NULLABLE")

#CALCULATE NEW FIELD
#Set local variables to be used in Calculate Field for new Area Square Kilometer field
inFeatures = outCombinedRaster
fieldName_Area_sqkm2 = "Area_sqkm"
expression = "(!Count! * 900) / 1000"
expressionType = "PYTHON_9.3"

#Run Calculate Field tool for new Land Cover True/False field
arcpy.CalculateField_management(inFeatures, fieldName_Area_sqkm2,
 expression, expressionType)


#Tell user if Script ran successfully.
print("Successfully Extracted, Combined, and Populated New Fields of NLCD Data")
0 Kudos
1 Solution

Accepted Solutions
ElijahAllan
New Contributor III

So added this and had my script run the ExtractByMask tool a second time at the very end,

finalRaster = ExtractByMask(outCombinedRaster, inputForExtractingExtent)
finalRaster.save(outCombinedRaster) ‍‍


and got the results I needed!

I'm wondering if this part of my script is what is causing the bounding rectangle:

#Execute ExtractByMask Tool for first NLCD raster.
outExtractByMask = ExtractByMask(inputNLCD1,inputForExtractingExtent)
# Save the ouput
outExtractByMask.save("ExtractedNLCD_1.img")

#Execute ExtractByMask Tool for second NLCD raster.
outExtractByMask2 = ExtractByMask(inputNLCD2,inputForExtractingExtent)
# Save the ouput
outExtractByMask2.save("ExtractedNLCD_2.img")


# COMBINE RASTER TOOL
# Set local variables for Combine tool.
inRaster01 = out_folder_path + "/ExtractedNLCD_1.img"
inRaster02 = out_folder_path + "/ExtractedNLCD_2.img"
outCombinedRaster = arcpy.GetParameterAsText(4)

# Execute Combine tool.
outCombine = Combine([inRaster01,inRaster02])
# Save the output.
outCombine.save(outCombinedRaster)

‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Should I just use

outExtractByMask 

and

outExtractByMask2 

as my variable inputs for Executing Combine Tool? So,

outCombine = Combine([outExtractByMask,outExtractByMask2])

instead?

View solution in original post

0 Kudos
1 Reply
ElijahAllan
New Contributor III

So added this and had my script run the ExtractByMask tool a second time at the very end,

finalRaster = ExtractByMask(outCombinedRaster, inputForExtractingExtent)
finalRaster.save(outCombinedRaster) ‍‍


and got the results I needed!

I'm wondering if this part of my script is what is causing the bounding rectangle:

#Execute ExtractByMask Tool for first NLCD raster.
outExtractByMask = ExtractByMask(inputNLCD1,inputForExtractingExtent)
# Save the ouput
outExtractByMask.save("ExtractedNLCD_1.img")

#Execute ExtractByMask Tool for second NLCD raster.
outExtractByMask2 = ExtractByMask(inputNLCD2,inputForExtractingExtent)
# Save the ouput
outExtractByMask2.save("ExtractedNLCD_2.img")


# COMBINE RASTER TOOL
# Set local variables for Combine tool.
inRaster01 = out_folder_path + "/ExtractedNLCD_1.img"
inRaster02 = out_folder_path + "/ExtractedNLCD_2.img"
outCombinedRaster = arcpy.GetParameterAsText(4)

# Execute Combine tool.
outCombine = Combine([inRaster01,inRaster02])
# Save the output.
outCombine.save(outCombinedRaster)

‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Should I just use

outExtractByMask 

and

outExtractByMask2 

as my variable inputs for Executing Combine Tool? So,

outCombine = Combine([outExtractByMask,outExtractByMask2])

instead?

0 Kudos