Trying to modify Batch Project to Output to a mirrored path tree

684
1
06-01-2012 09:52 AM
FullMetalzinc
New Contributor
Hi there, 

I am attempting to modify the Batch Project script to:
[INDENT]a) read from a text file of file names located on our server
b) build a mirror of the paths to those files on my local machine
c) output to my local machine at the end of those paths. The paths should also be appended to a user specified location
[/INDENT]
My code works to an extent, but stops after the first projection, as per:

[INDENT]Projected S:\FMM\_Projects\Pyramid\Reports\Pyramid\Data_Transfer\Pyramid_FromJoey2010oct\GIS\Geochem\Geochem Merged\Pyramid_PanCon.shp to C:\Users\revresources\scratch\shapefiles\Test_Output\test3\FMM\_Projects\Pyramid\Reports\Pyramid\Data_Transfer\Pyramid_FromJoey2010oct\GIS\Geology\SWAK 2008 mapping shapefiles\2008_Alteration.shp successfully.

Failed to project S:\FMM\_Projects\Pyramid\Reports\Pyramid\Data_Transfer\Pyramid_FromJoey2010oct\GIS\Geochem\Geochem Merged\Pyramid_Rocks.shp. Failed to execute. Parameters are not valid.
ERROR 000725: Output Dataset or Feature Class: Dataset C:\Users\revresources\scratch\shapefiles\Test_Output\test3\FMM\_Projects\Pyramid\Reports\Pyramid\Data_Transfer\Pyramid_FromJoey2010oct\GIS\Geology\SWAK 2008 mapping shapefiles\2008_Alteration.shp already exists.
Failed to execute (Project).

Failed to project S:\FMM\_Projects\Pyramid\Reports\Pyramid\Data_Transfer\Pyramid_FromJoey2010oct\GIS\Geochem\Geochem Merged\Pyramid_Silts.shp. Failed to execute. Parameters are not valid.
ERROR 000725: Output Dataset or Feature Class: Dataset C:\Users\revresources\scratch\shapefiles\Test_Output\test3\FMM\_Projects\Pyramid\Reports\Pyramid\Data_Transfer\Pyramid_FromJoey2010oct\GIS\Geology\SWAK 2008 mapping shapefiles\2008_Alteration.shp already exists.
Failed to execute (Project).[/INDENT]

Here is my code:

#Import required modules
import ConversionUtils, time, sys, os, arcpy
 
#Define message constants so they may be translated easily
msgWorkspace = ConversionUtils.gp.GetIDMessage(86109) # Message "Output workspace does not exist: "
msgCoordinateSystem = ConversionUtils.gp.GetIDMessage(86110)  #Message "Must Enter a Spatial Reference or Template Feature Class."
msgFail = ConversionUtils.gp.GetIDMessage(86111) # Message "Failed to project "
 
#Set the input datasets
inputs = [item.replace('\n', '') for item in open(sys.argv[1]).readlines()]  
 
 
for line in inputs:
    line = line.replace("\\\\", "\\")#changing slashes to single slashes
    
#print the stripped line
    (drive, path) = os.path.splitdrive(line)
    arcpy.AddMessage("path before the join is " + path)
    print "path before the join is " + path
    arcpy.AddMessage(" ConversionUtils.gp.GetParameterAsText(1) before the join is " + ConversionUtils.gp.GetParameterAsText(1))
    outdata=os.path.normpath(ConversionUtils.gp.GetParameterAsText(1)+path)
    arcpy.AddMessage(" ConversionUtils.gp.GetParameterAsText(1) after the join is " + ConversionUtils.gp.GetParameterAsText(1))
    arcpy.AddMessage("path after join is " + path)
#    print "outdata is " + outdata
#    print "path after join is " + path
    arcpy.AddMessage("outdata is " + outdata)
    try:
        (path2,fileName) = os.path.split(outdata)
        os.makedirs(path2)
    except OSError:
        print "Skipping creation of the following path because it exists already: " + path2
        #if path exists, this will be printed
        arcpy.AddMessage("Skipping creation of the following path because it exists already: " + path2)
    
    #print line.strip()
 
output_workspace = ConversionUtils.gp.GetParameterAsText(2)    
#Set the spatial reference
output_coordinate_system = ConversionUtils.gp.GetParameterAsText(3)
 
#Set the template dataset
template_dataset = ConversionUtils.gp.GetParameterAsText(4)
        
#Set the transformation
transformation = ConversionUtils.gp.GetParameterAsText(5)
#Message 86112   "Projecting multiple datasets ..."
ConversionUtils.gp.SetProgressor("step", ConversionUtils.gp.GetIDMessage(86112), 0, len(inputs))
 
if (output_coordinate_system == "" or output_coordinate_system == "#") and (template_dataset == "" or template_dataset == "#"):
    raise ConversionUtils.GPError(msgCoordinateSystem) 
elif (output_coordinate_system != "") and (output_coordinate_system != "#"):
    sr = output_coordinate_system
elif (template_dataset != "") and (template_dataset != "#"):
    dsc = ConversionUtils.gp.Describe(template_dataset)
    sr = dsc.SpatialReference
 
for input in inputs:
    try:
        #outdata = ConversionUtils.GenerateOutputName(input, output_workspace)
                #Message 86113 "Projecting "
        ConversionUtils.gp.SetProgressorLabel(ConversionUtils.gp.GetIDMessage(86113) + input)        
        ConversionUtils.gp.Project_management(input, outdata, sr, transformation)
        print outdata
        arcpy.AddMessage(outdata) 
        #Message 86114 "Projected %s to %s successfully."
        ConversionUtils.gp.AddMessage(ConversionUtils.gp.GetIDMessage(86114) % (input, outdata))
        
    except Exception, ErrorDesc:
        msgWarning = msgFail + "%s" % input
        msgStr = ConversionUtils.gp.GetMessages(2)
        ConversionUtils.gp.AddWarning(ConversionUtils.ExceptionMessages(msgWarning, msgStr, ErrorDesc))
        
    ConversionUtils.gp.SetProgressorPosition()
 
time.sleep(0.5)


Any help would be appreciated
0 Kudos
1 Reply
NobbirAhmed
Esri Regular Contributor
Your output data is:

outdata=os.path.normpath(ConversionUtils.gp.GetParameterAsText(1)+path)


You are using the same outdata variable in your repeated call to Project tool:

ConversionUtils.gp.SetProgressorLabel(ConversionUtils.gp.GetIDMessage(86113) + input)   
ConversionUtils.gp.Project_management(input, outdata, sr, transformation)


In between, you have commented out this line:

outdata = ConversionUtils.GenerateOutputName(input, output_workspace)


What GenerateOutputName function does is, checks whether the an output with the same exists and if so, creates a new name by appending an underscore and a number at the end of the name. As you are using the same name, Project tries to overwrite the same output when it runs 2nd time and on. Try setting the overwrite output to True and see whether you get the same error. I guess you'll get only one output.

If you don't want to (or having trouble) use the commented out line, my suggestion would be modify your code as follows:

index = 1

for input in inputs:
    try:
        outdata = outdata + "_" + str(index)
        .....        
        ConversionUtils.gp.Project_management(input, outdata, sr, transformation)
        ...
        ...
        index += 1


Remember that instead of using GenerateOutputName, if you go for my suggested code, and you run your code again then the output generated in your first run will be overwritten.
0 Kudos