1

我创建了一个脚本来创建所有栅格的空间索引,它在单个目录中运行时运行良好,但是当我尝试修改它以作为 os.walk 的一部分在子目录上运行时,它开始出现许多错误。你们能帮忙解决我无法修复的当前问题吗?

谢谢,

Python 2.6.5 (r265:79096, Mar 19 2010, 21:48:26) [MSC v.1500 32 bit (Intel)] 输入“help”、“copyright”、“credits”或“license”以获取更多信息。

[评估 RasterExtent_toSHP_Ver2.py] 从 P:\2011\Job_154_PythonScript_for_AOI\Working\Orthophotomosaic 读取文件 将目录更改为:P:\2011\Job_154_PythonScript_for_AOI\Working\Orthophotomosaic\1 处理目录中的 2 个 .tif 文件。['308000_8105000.tif', '309000_8105000.tif'] 308000_8105000.tif Created: P:\2011\Job_154_PythonScript_for_AOI\Working\Orthophotomosaic\1\Temp_Polygon_Extent_1.shp Filling in: 308000_8105000 TIFF P:\2011\Job_154_PythonScript_for_AOI\Working\Orthophotomosaic\ 1 64000000 309000_8105000.tif 创建:P:\2011\Job_154_PythonScript_for_AOI\Working\Orthophotomosaic\1\Temp_Polygon_Extent_2.shp 填写:309000_8105000 TIFF P: \2011\Job_154_PythonScript_for_AOI\Working\Orthophotomosaic\1 64000000 [] 合并:[] 到:Spatial_Extent.shp arcgisscripting.ExecuteError:执行失败。参数无效。错误 000735:输入数据集:需要值 警告 000725:输出数据集:数据集 P:\2011\Job_154_PythonScript_for_AOI\Working\Orthophotomosaic\1\Spatial_Extent.shp 已存在。未能执行(合并)。

# Creates a shape file containing the extent of all the files of RasterType in the current directory and subdirectories.
# Options to only process files which have projection
# Option to move and rename files which don't have a projection
# Outputs extent to a shape file with a defined projection

#IMPORTANT - delete "NoProj" directory

import arcpy, glob, os, sys
from arcpy import env, mapping
path = os.getcwd()
env.workspace = path
env.overwriteOutput = True
os.chdir(path)
origPath = path

# IMPORTANT Options to SET
RasterType = ['tif', 'jpg', 'ecw'] # add/remove Raster extensions as required
RootDirectory = path
CheckProjection = 1 # '1' to check if files projected and '0' not to. With 0 will create extent of all RasterFiles in dirctory
RenameFiles = 0 # '1' to rename and move unprojected files projected and '0' not to.
geometry_type = "POLYGON"
ProjectionTemplate = r"C:\Python26\GDA_1994_MGA_Zone_55_Project.shp"
ProjectionRef = r"C:\Python26\GDA_1994_MGA_Zone_55_Project.prj"
has_m = "DISABLED"
has_z = "DISABLED"
#spatial_reference = arcpy.SpatialReference(ProjectionTemplate)
spatial_reference = arcpy.SpatialReference("C:\\Python26\\GDA_1994_MGA_Zone_55_Project.prj")
f = open(origPath+'\\NoProjection.txt', 'a')
f.write("Log of files without projection"+"\n")
f.close()

#FileList =[]
print 'Reading files from ' + path

if RenameFiles == 1:
    os.mkdir(origPath+'\\NoProj') # creates directory to place non-projected files in. Delete if it exists

# Misc counts
x=0
z=x+1
NoProjCount=0
ProjCount=0
for root, dirs, files in os.walk(RootDirectory+'\\'):
    for directory in dirs:
        currentPath=root+directory
        os.chdir(currentPath)
        print "Changing Directory to: " + os.getcwd()
        for Raster in RasterType:
            FileList = glob.glob("*." + Raster)
            FileCount = len(FileList)
            print "Processing " + str(FileCount) + " ." + str(Raster) + " files in the directory."
            print FileList
            for File in FileList:
                desc = arcpy.Describe(File)
                SR = desc.spatialReference
                PositionDot = File.find('.')
                FileNm = File[0:PositionDot]
                RasterObj = arcpy.Raster(File)
                if CheckProjection == 1:
                    if SR.name == "Unknown":
                        print "Projection of " + str(File) + " is " + SR.name + " so skipping and logging status."
                        f = open(origPath+'\\NoProjection.txt', 'a')
                        f.write(str(File)+"\n")
                        f.close()
                        if RenameFiles == 1: 
                            PositionDot = File.find('.')
                            ext =File[PositionDot:]
                            Name = File[0:PositionDot] # Character string to retain from xth character to yth character
                            newName=Name+ext
                            print Name+ext+" renamed as " + newName
                            os.rename(File,"NoProj\\"+newName)
                        else:
                            NoProjCount=NoProjCount+1
                    else:    
                        print File
                        ProjCount=ProjCount+1
                        RasterFile = arcpy.Raster(File)
                        RasterExtent = RasterFile.extent
                        FileFmt = RasterObj.format
                        FileSz=RasterObj.uncompressedSize
                        XMAX = RasterExtent.XMax
                        XMIN = RasterExtent.XMin
                        YMAX = RasterExtent.YMax
                        YMIN = RasterExtent.YMin
                        pnt1 = arcpy.Point(XMIN, YMIN)
                        pnt2 = arcpy.Point(XMIN, YMAX)
                        pnt3 = arcpy.Point(XMAX, YMAX)
                        pnt4 = arcpy.Point(XMAX, YMIN)
                        array = arcpy.Array()
                        array.add(pnt1)
                        array.add(pnt2)
                        array.add(pnt3)
                        array.add(pnt4)
                        array.add(pnt1)
                        polygon = arcpy.Polygon(array)

                        arcpy.CreateFeatureclass_management(currentPath,"\Temp_Polygon_Extent" + "_" + str(z), geometry_type, ProjectionTemplate, has_m, has_z, spatial_reference)
                        ShapeFile = currentPath+"\Temp_Polygon_Extent" + "_" + str(z) + ".shp"
                        arcpy.CopyFeatures_management(polygon, ShapeFile)             
                        print "Created: " + ShapeFile

                        arcpy.AddField_management(ShapeFile,'FileName','TEXT')
                        arcpy.AddField_management(ShapeFile,'FileFormat','TEXT')
                        arcpy.AddField_management(ShapeFile,'FileSource','TEXT')
                        arcpy.AddField_management(ShapeFile,'FileSize','INTEGER')
                        desc = arcpy.Describe(ShapeFile)
                        rows = arcpy.UpdateCursor(ShapeFile)

                        print 'Filling in: ', str(FileNm), str(FileFmt), str(currentPath), int(FileSz)
                        #rows.insertRow(row)
                        for row in rows:
                            row.FileName = str(FileNm)
                            row.FileFormat = str(FileFmt)
                            row.FileSource = str(currentPath)
                            row.FileSize = int(FileSz)
                            rows.updateRow(row)
                        x=x+1
                        z=z+1
                #cleaning up
                if x>1:
                    SpatialExtent='Spatial_Extent.shp'
                    #try:
                    #    os.remove(currentPath+"\\"+SpatialExtent)
                    #except:
                    #    print SpatialExtent +" Created and continuing."
                    arcpy.CreateFeatureclass_management(currentPath, SpatialExtent, geometry_type, ProjectionTemplate, has_m, has_z, spatial_reference)
                    list =[]
                    lstFCs = arcpy.ListFeatureClasses(currentPath+"\Temp_Polygon_Extent*")
                    print lstFCs
                    for fc in lstFCs:
                        print fc
                        list.append(fc)
                    print 'Merging: ' + str(list) + 'to: '+ SpatialExtent
                    arcpy.Merge_management(list, currentPath+'\\'+SpatialExtent)
                    #print 'Deleting identical entries and temp files'
                    #arcpy.DeleteIdentical_management("Extent.shp", ["SHAPE"])
                    #arcpy.Project_management("Extent.shp", "Extent_prj.shp", ProjectionRef)
                    arcpy.DefineProjection_management(currentPath+'\\'+SpatialExtent, ProjectionRef)
                    print 'Created and Projected '+ currentPath+'\\'+SpatialExtent+" to " + ProjectionRef +', deleting Temp files and exiting'
                    print 'PROCESSED ' +str(z-1) + ' files of which ' + str(NoProjCount) + ' were not projected'
                    for item in list:
                        arcpy.Delete_management(item)
                    #del row, rows
                    x=0
                    z=0
4

0 回答 0