Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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:

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

Any help would be appreciated.

share|improve this question

2 Answers 2

up vote 5 down vote accepted

Change your 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").

share|improve this answer
    
Awesome, this worked like a charm. Thanks, so much. –  Visceral May 29 '12 at 18:51
    
+1, much better than my lame answer :) –  Chad Cooper Jun 2 '12 at 12:43

Open up the file, read it into memory, iterate through each line, and do something with the input. I put your text file sample into a file called inputs.txt:

Python 2.6.5 (r265:79096, Mar 19 2010, 21:48:26) [MSC v.1500 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> f = open("inputs.txt").readlines()
>>> for line in f:
...     print line
...
C:\Users\revresources\scratch\shapefiles\Test_Output\Geochem Merged\Pyramid_PanC
on.shp C:\Users\revresources\scratch\shapefiles\Test_Output\Geochem Merged\Pyram
id_Rocks.shp

C:\Users\revresources\scratch\shapefiles\Test_Output\Geochem Merged\Pyramid_Silt
s.shp C:\Users\revresources\scratch\shapefiles\Test_Output\Geochem Merged\Pyrami
d_Soils.shp

C:\Users\revresources\scratch\shapefiles\Test_Output\test2\mod\interpretive alte
ration_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 Pyra
mid geology_polyline.shp

C:\Users\revresources\scratch\shapefiles\Test_Output\test2\mod\InterpretivePyram
idgeology_region.shp
>>>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.