Batch Reprojection

633
3
11-17-2011 12:10 AM
JohanL
by
New Contributor
Hi Everyone,

I've been working on some code to automatically sort a list of subdirectores and then to reproject those dems which end with *dem.tif. The function to find those files has worked (on its own) but the reprojection function does not seem to work at all, either on its own or with the find function described above. Here is the code...

# Batch Processing Script used to redefine projection 

import os, fnmatch, arcpy, sys, string

# Sort files by a Specified Filename
def File_Find(filepat,top):
    for path, dirlist, filelist in os.walk(top):
        for name in fnmatch.filter(filelist,filepat):
            yield os.path.join(path,name)

        
#Reproject Raster Datasets
def DefineProjection():
#Variables 
    InFolder = sys.argv[1] 
    OutFolder = sys.argv[2]
    incoordinate = sys.argv[3]
    outcoordinate = sys.argv[4]
    SFiles = File_Find('*dem.tif',InFolder) # Files Used in Reprojection
    arcpy.env.workspace = SFiles

#Reprojection Function   

try:
    RasterList = arcpy.ListRasters()
    RasterList.Reset()
    RasterImage = RasterList.Next()
    arcpy.AddMessage('\n' + 'Begin Processing.......' + '\n')

    while RasterImage:  
        arcpy.AddMessage(' Projecting  ' + RasterImage)
        InFileName = ReProject + "\\" + RasterImage
        arcpy.AddMessage( 'infilename = ' + InFileName)
        OutFileName = OutFolder + '\\ ' + RasterImage
        arcpy.AddMessage('outfilename = ' + OutFileName)
        arcpy.ProjectRaster_management(InFileName, OutFileName, outcoordinate, 'CUBIC', '#', '#', '#', incoordinate)
        arcpy.AddMessage(RasterImage + '  Projected')
        arcpy.AddMessage('\n')
        RasterImage = RasterClassList.Next()
except:
    print arcpy.GetMessages(2)
    print 'No Files Match That Criteria!'



The code runs without any error in ArcGIS, but it does not produce any results, so I suspect it has something to do with the way I have used the list rasters function.

Any help would be most appreciated,

Sincerely,
Bjorn
Tags (2)
0 Kudos
3 Replies
markdenil
Occasional Contributor III
Just a few observations.

The variables set from system arguments are buried in DefineProjection() def block, and as far as I can see, DefineProjection() never gets run. Python does not evaluate or execute code in a def block until it is called.

Since the try:except block is not in that function, and do not call it, where are the parameters used in the try block set?

Now, even if you are importing the whole module, (where you would be setting the workspace, for example) it is odd that you would go back to sys.srgv for the arguments... Why not pass the args to the function explicitly?
To wit:
def DefineProjection(InFolder, OutFolder, incoordinate, outcoordinate):
you can capture them from the command arguments before calling the function.

In any event, lists don't have Reset() or Next() functions.
Use a  >>  for rasterImage in RasterList:  <<  loop instead.
0 Kudos
JohanL
by
New Contributor
Just a few observations.

The variables set from system arguments are buried in DefineProjection() def block, and as far as I can see, DefineProjection() never gets run. Python does not evaluate or execute code in a def block until it is called.

Since the try:except block is not in that function, and do not call it, where are the parameters used in the try block set?

Now, even if you are importing the whole module, (where you would be setting the workspace, for example) it is odd that you would go back to sys.srgv for the arguments... Why not pass the args to the function explicitly?
To wit:
def DefineProjection(InFolder, OutFolder, incoordinate, outcoordinate):
you can capture them from the command arguments before calling the function.

In any event, lists don't have Reset() or Next() functions.
Use a  >>  for rasterImage in RasterList:  <<  loop instead.



Alright thanks mdenil... that solved the problem! I thought it didnt look quite right but I didnt think that rewritting the code was necessary if it had already been done before so I simply tried to remodify this script to the newest version of arcpy. I suppose Reset() and Next() functions are primarily used after yield statments?

Thanks again,
Bjorn
0 Kudos
markdenil
Occasional Contributor III
The ArcObjects code in that ArcScripts routine delivers an enmeration (IEnumGxObject), which you have to step through like a cursor.
All the geoprocessor List tools return python lists; a very different animal altogether.
Yield in Python is something yet again....
0 Kudos