Integer in python 2.6.5 with ArcGIS 10

447
2
06-20-2011 10:27 AM
MariellaCarbajal
New Contributor
I've just started to work with python in ArcGIS 10.
I'm trying to make a simple program to transform a raster in float format to Integer, I only replaced my variables in the example help and it doesn't work. Here I put my code if anyone can help me.

import arcpy
import sys
import string
import os
from arcpy import env
from arcpy.sa import *

env.workspace = "D:/floats"
desc="_14p"
# Define the files and number of files to process
for root,files,dir in os.walk("D:/floats"): 
    #print dir
    num=len(dir)
    print "number of files: ", num

try:
    for n in range(0,num):
        inRaster = dir
        ext='.flt'
        outInt = inRaster[:-len(ext)]+str(desc)
        arcpy.CheckOutExtension("Spatial")
        outInt=Int(inRaster)
        outInt.save(outint)
        print n

except:
    #print error message if an error occurs
        print arcpy.GetMessages()
Tags (2)
0 Kudos
2 Replies
curtvprice
MVP Esteemed Contributor
It may help figure out what went wrong if we saw the original example. Did the example come from the online help?

In your code, I'm pretty sure that "dir" is a list of folders, not a list of rasters. The usual approach would be to use ListFiles or glob.glob() to get a list of .flt files for each folder and loop on that.

What format do you want for the integer output?
0 Kudos
MariellaCarbajal
New Contributor
Thanks Curtis, but I used "dir" many times in the same way and it works well.
However, I changed some things and also I found the main error was that I have to do Float -> Raster and then Raster -> Integer. Another error was that "ArcToolBox help" indicate "Int" (for scripts) but Python doesn't reconize, instead of that Python accept "arcpy.gp.Int_sa" for Integers.

Now, It works excellent manually with ArcToolbox and in python with only one file, however when I put "for" I have an error ("pythonw.exe has encountered a problem and needs to close. We are sorry for the inconvenience"). Rarely, I tested the same code in other computer .... and voila!!! it's ok!!!
Anybody knows what's wrong?

Here it's my code:

# Import arcpy module
import arcpy
import sys
import string
import os
from arcpy import env
from arcpy.sa import *

# Check out any necessary licenses
arcpy.CheckOutExtension("spatial")

path="D:/Documents and Settings/mcarbajal/My Documents/"
ruta=path+"gridss/"


try:
    for n in range(1,13):
        inFloat=ruta+"grid"+str(n)+".flt"
        outRaster=ruta+"grid"+str(n)+"_rt"
        outInt=ruta+"grid"+str(n)+"_int"
        # Process: Float to Raster
        arcpy.FloatToRaster_conversion(inFloat,outRaster)
        arcpy.gp.Int_sa(outRaster,outInt)
        print n

except:
    #print error message if an error occurs
    print arcpy.GetMessages()
0 Kudos