I have been coding in Python for only a few months, so maybe I'm just overlooking a minor detail as a newbie.
I have written the script below for a class project. It is supposed to clip two rasters, perform and unsupervised classification with the ISO tool, and then perform a change detection using map algebra. However, as you will see in error code below, the ISO tool is unable to open my clipped rasters. The script runs very fast. It's almost as if the ISO tool is attempting to open the rasters before the clip tool is done creating them. One two occasions, the script actually managed to produce the first classified image and only failed on creating the second one.
The script and error messages are included below:
## Import Necessary Modules
import arcpy
import os
from arcpy.sa import *
## Define repeat functions
def clip_it(AOI, raster, date):
"""This function clips file raster to supplied AOI file. It always uses
AOI extent for clipping extent. Output file will be a TIF image with the
supplied date as a filename"""
try:
arcpy.Clip_management(raster, '#', '{}clip.tif'.format(date), AOI, '0', 'ClippingGeometry')
except:
print('Clip Failed')
print
print arcpy.GetMessages()
print
return
def classify_it(raster, num_class, min_class, samp_int, date):
try:
outUnsupervised = IsoClusterUnsupervisedClassification(raster, num_class, min_class, samp_int)
outUnsupervised.save("{}class.tif".format(date))
del outUnsupervised
except:
print('Classify failed')
print
print arcpy.GetMessages()
print
return
## Set up workspace
from arcpy import env
env.workspace = "u:/shared/gis/studata/jpdees0754/gitprojects/Final_Project/files"
env.overwriteOutput = True
path = env.workspace
## Prompt user for AOI and LANDSAT Rasters
## Promt user for raster data month and year
AOI = 'AOI.shp'
new_image = 'g2011Clip.img'
new_date = '2011'
old_image = 'g2000Clip.img'
old_date = '2000'
num_class = '5'
min_class_size = '20'
sample_int = '10'
## Clip both rasters to area of interest
clip_it(AOI, new_image, new_date)
clip_it(AOI, old_image, old_date)
## Perform unsupervised classification on raster images
classify_it('{}clip.tif'.format(new_date), num_class, min_class_size, sample_int, new_date)
classify_it('{}clip.tif'.format(old_date), num_class, min_class_size, sample_int, old_date)
## Perfom change detection on classified rasters
new_class = arcpy.Raster("{}class.tif".format(new_date))
old_class = arcpy.Raster("{}class.tif".format(old_date))
outEqualTo = Raster(old_class) == Raster(new_class)
outEqualTo.save("{}_{}_Change.tif".format(old_date,new_date))
Error Message:
Classify failed
Executing: IsoClusterUnsupervisedClassification u:/Shared/GIS/StuData/jpdees0754/gitprojects/Final_Project/files\2000clip.tif 5 u:/Shared/GIS/StuData/jpdees0754/gitprojects/Final_Project/files\isocluster6 20 10 # Start Time: Tue Jul 29 22:10:23 2014 Running script IsoClusterUnsupervisedClassification... ERROR 999999: Error executing function. Failed to open raster dataset Failed to execute (MLClassify).
Error in executing Iso Cluster Unsupervised Classification. Completed script IsoClusterUnsupervisedClassification... Failed to execute (IsoClusterUnsupervisedClassification). Failed at Tue Jul 29 22:10:25 2014 (Elapsed Time: 2.00 seconds)