1

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)

1

It may not be the cause but its good practise to check out the spatial extension and then check it back in at the end of your code. So after the line from arcpy.sa import * put

arcpy.CheckOutExtension("Spatial")

then at the end of your code use:

arcpy.CheckInExtension("Spatial")

If you look closely at the error message its trying to create an output called isocluster6, where did you specify this? You did not! This is a temporary file before it gets saved as 2000Class.tif.

As there is no file extension to this name is MUST be an ESRI raster but it is an invalid file name as you can only have 9 characters (search the help file for the topic ESRI Grid Format). So you need to set your output workspace to a file geodatabase for this file name to be valid. I believe this tool is expecting the output workspace to be a file GeoDatabase where the output name would be valid.

Finally I would adopt best practise of NOT starting your dataset names with a number. If it were me I would have created the final raster as Class_2000.tif

One other final note, nice to see some comments in your code, keep that up!

|improve this answer|||||
  • Thank you so much! I changed my workspace to a file geodatabase. It works like a charm! Later, I plan on adding the creation of the file geodatabase to the script. For now, I just wanted proof of principle! P.S. I corrected my naming conventions and check in/out of the arcpy.sa tools to match best practices and will continue to in the future – John Dees Jul 30 '14 at 22:22

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.