Select to view content in your preferred language

Using Batch Project Script to read from a text file as Input

983
0
05-29-2012 09:31 AM
FullMetalzinc
Emerging Contributor
Hi there,

I was wondering how I would modify the code of the Batch Project script shown below, to read from a text file of shapefile names as input.  A sample of the textfile is shown under the code:


#Import required modules
import ConversionUtils, time

#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 = ConversionUtils.gp.GetParameterAsText(0)
# The list is split by semicolons ";"
inputs = ConversionUtils.SplitMultiInputs(inputs)  

#Set the output workspace
output_workspace = ConversionUtils.gp.GetParameterAsText(1)

#Set the spatial reference
output_coordinate_system = ConversionUtils.gp.GetParameterAsText(2)

#Set the template dataset
template_dataset = ConversionUtils.gp.GetParameterAsText(3)
        
#Set the transformation
transformation = ConversionUtils.gp.GetParameterAsText(4)
#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) 
        #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)


Text File Sample:

[INDENT]C:\Users\revresources\scratch\shapefiles\Test_Output\Geochem Merged\Pyramid_PanCon.shp
C:\Users\revresources\scratch\shapefiles\Test_Output\Geochem Merged\Pyramid_Rocks.shp
C:\Users\revresources\scratch\shapefiles\Test_Output\Geochem Merged\Pyramid_Silts.shp
C:\Users\revresources\scratch\shapefiles\Test_Output\Geochem Merged\Pyramid_Soils.shp
C:\Users\revresources\scratch\shapefiles\Test_Output\test2\mod\interpretive alteration_point.shp
C:\Users\revresources\scratch\shapefiles\Test_Output\test2\mod\interpretive alteration_polyline.shp
C:\Users\revresources\scratch\shapefiles\Test_Output\test2\mod\Interpretive Pyramid geology_polyline.shp
C:\Users\revresources\scratch\shapefiles\Test_Output\test2\mod\Interpretive Pyramid geology_region.shp
[/INDENT]

Any help would be appreciated.

Thanks

Found the answer on GIS - Stack Exchange - I needed to change the block:

#Set the input datasets
                    inputs = ConversionUtils.gp.GetParameterAsText(0)
                    # The list is split by semicolons ";"
                    inputs = ConversionUtils.SplitMultiInputs(inputs)
        With:

                    inputs = [item.replace('\n', '') for item in open(filename).readlines()]
                    Where filename is your file with the list of SHP ("Text File Sample").
Tags (2)
0 Kudos
0 Replies