Quantcast
Channel: Forums - Python
Viewing all articles
Browse latest Browse all 2485

Not Getting Full Paths in Directory List

$
0
0
Greetings,

I'm using "arcpy.GetParameterAsText" to get multiple directories for my script to loop through. I've set the mutivalue parameter property to "Yes" and my input form does allow me to select multiple directories.

However, when I begin to loop through the list of directories, my script returns only the drive letter (in my case "H"), not the full directory path. When I "hard code" my variable (instead of using GetParameterAsText), I preface each variable with "r" and that works.

Right after I begin my loop through the directories (starting at "For CurrentFolder in DOQQFolders:"), I added an arcpy.AddMessage to check the value of CurrentFolder. All I get back is "H" not the full directory path.

My third script variable is also a directory and that one works fine. Any idea why I don't get the full directory path?

Thanks,

Jon Mulder
California Department of Water Resources

Code:

import os
import arcpy
# Overwrite pre-existing files
arcpy.env.overwriteOutput = True
##arcpy.env.workspace = r"H:\Documents\GIS\BaseData\Raster\Imagery\39121DOQQs"

InRasterMosaic = arcpy.GetParameterAsText(0)
DOQQFolders = arcpy.GetParameterAsText(1)
OutDirectory = arcpy.GetParameterAsText(2)
ClipBoundary = arcpy.GetParameterAsText(3)
OutNamePrefix = arcpy.GetParameterAsText(4)


##InRasterMosaic = r"H:\Documents\GIS\BaseData\Raster\Imagery\AerialPhotos\1958\MosaicGeoDatabase\1958_ButtePhotos.gdb\ButtePhotos_1958_20131123"
##DOQQFolders = (r"H:\Documents\GIS\BaseData\Raster\Imagery\DOQQs\39121",r"H:\Documents\GIS\BaseData\Raster\Imagery\DOQQs\39122")
##OutDirectory = r"H:\Documents\GIS\BaseData\Raster\Imagery\AerialPhotos\1958\MosaicedRastersForDOQQs\AAX"
##ClipBoundary = r"H:\Documents\GIS\BaseData\Raster\Imagery\AerialPhotos\1958\ClipShapes\AAX\AAX_ClipBoundary.shp"
##OutNamePrefix = "1958_Butte"

##Messages for debugging.
arcpy.AddMessage("First DOQQ Directory: {0}.".format(DOQQFolders[0]))
arcpy.AddMessage("Number of DOQQ Directories: {0}.".format(len(DOQQFolders)))
arcpy.AddMessage("Directory for Clipped Rasters: {0}.".format(OutDirectory))
arcpy.AddMessage("ClipBoundary Directory: {0}.".format(ClipBoundary))

for CurrentFolder in DOQQFolders:
    print CurrentFolder
    ##Message for debugging.  The following message returns only "H", not the full path.
    arcpy.AddMessage("Checking DOQQ Extents in {0}.".format(CurrentFolder))
    arcpy.env.workspace = CurrentFolder
    in_raster_datasets = arcpy.ListRasters()
##    ##Get total count of raster.
##    RasterCount = len(in_raster_datasets)
##    print RasterCount
##    ##Set the progressor.
##    arcpy.SetProgressor("step", "Comparing extent of DOQQ rasters to Raster Mosaic Boundary.", 0, RasterCount, 1)
    ##Copy ClipBoundary into CurrentFolder and create layer file.
    arcpy.CopyFeatures_management(ClipBoundary,"ClipBoundary")
    arcpy.MakeFeatureLayer_management("ClipBoundary.shp","ClipBoundary_lyr")
    for CurrentRaster in in_raster_datasets:
        RasterName = CurrentRaster
        print RasterName
##        ##Update the progressor label for current raster.
##        arcpy.SetProgressorLabel("Checking {0}.".format(RasterName))
##        ##Update the progressor position.
##        arcpy.SetProgressorPosition()
        DOQQShapefile = os.path.join(CurrentFolder,CurrentRaster)
##        print DOQQShapefile
##        print os.path.dirname(DOQQShapefile)
##        print os.path.basename(DOQQShapefile)
        arcpy.CreateFeatureclass_management(os.path.dirname(DOQQShapefile),
                                          os.path.basename(DOQQShapefile),
                                          "POLYGON")
        x = len(CurrentRaster) - 4
        DOQQShapefile = CurrentRaster[0:x] + ".shp"
        print DOQQShapefile
        arcpy.AddField_management(DOQQShapefile,"RasterName", "String","","",250)
        arcpy.AddField_management(DOQQShapefile,"RasterPath", "String","","",250)

        cursor = arcpy.InsertCursor(DOQQShapefile)
        point = arcpy.Point()
        array = arcpy.Array()
        corners = ["lowerLeft", "lowerRight", "upperRight", "upperLeft"]
        feat = cursor.newRow() 
        r = arcpy.Raster(CurrentRaster)
        for corner in corners:   
            point.X = getattr(r.extent, "%s" % corner).X
            point.Y = getattr(r.extent, "%s" % corner).Y
            array.add(point)
        array.add(array.getObject(0))
        polygon = arcpy.Polygon(array)
        feat.shape = polygon
        feat.setValue("RasterName", CurrentRaster)
        feat.setValue("RasterPath", CurrentFolder + "\\" + CurrentRaster)
        cursor.insertRow(feat)
        array.removeAll()
        del feat
        del cursor
        arcpy.MakeFeatureLayer_management(DOQQShapefile,"DOQQLayer_lyr")
        arcpy.SelectLayerByLocation_management("DOQQLayer_lyr","intersect","ClipBoundary_lyr")
        matchcount = int(arcpy.GetCount_management("DOQQLayer_lyr").getOutput(0))
        print matchcount
        if matchcount <> 0:
            ##Update the progressor label for current raster.
##            arcpy.SetProgressorLabel("Clipping Raster Mosaic to Extent of {0}.".format(RasterName))
            arcpy.AddMessage("Clipping Raster Mosaic to Extent of {0} .".format(RasterName))
            OutDOQQImage = os.path.join(OutDirectory,OutNamePrefix + "_" + RasterName)
            print OutDOQQImage
            ##Clip Raster Dataset with feature geometry
            arcpy.Clip_management(InRasterMosaic, "#", OutDOQQImage,DOQQShapefile, "0", "ClippingGeometry")
        arcpy.Delete_management(DOQQShapefile)
    arcpy.Delete_management("ClipBoundary.shp")   
print "All done."


Viewing all articles
Browse latest Browse all 2485

Trending Articles