[Tutor] Help needed with script to batch-create shapefiles

Chris Bromley lgxjcb at nottingham.ac.uk
Thu Feb 17 17:05:58 CET 2005


Hello again,

First off, please accept my apologies for my last message, which was sorely=
 lacking in the detail department. I'm such a beginner with programming tha=
t I assumed the error would be glaringly obvious to an experienced programm=
er and would jump of the page/screen right away. This wasn't the case, thou=
gh.=20

Second, thanks to everyone who did reply, despite the absence of detail, an=
d the info. I got from these enable me to modify the script a little and al=
so get more information about what might be going wrong.

So, to start again, and with much more detail...=20=20

I have several thousand files of x,y,z coordinates in dBaseIV format (.dbf =
file extension) that I am trying to convert into shapefiles in order to be =
able to perform cut/fill analyses in ArcGIS 9.0. I am thus trying to automa=
te this conversion using a Python script (in Python 2.1). I=92ve taken as m=
y starting point the batch clip tool that can be found at:=20

http://support.esri.com/index.cfm?fa=3Dknowledgebase.techarticles.articleSh=
ow&d=3D26892=20

and have tried to modify it to suit my purpose. I have replaced the clip to=
ol with the Make XY Event Layer tool (from the the Data Management Tools to=
olbox), in order to create an xy Layer from the .dbf files, and with the Fe=
ature class to Shapefile (multiple) tool (from the Conversion Tools toolbox=
) in order to turn this xy layer into a shapefile. The full script as I hav=
e modified it is below.

Prior to running the script I use the =91check=92 button in the PythonWin a=
nd the script=92s syntax is fine. When I run the script though, the message=
=20

=91Script =91C:\ dBase_File_To_Shapefile.py=92 returned exit code 0=92=20=
=20=20

appears in the status bar at the bottom of the PythonWin window. The follow=
ing text also appears in the Interactive window=85

Traceback (most recent call last):
  File "C:\dBase_File_To_Shapefile2.py", line 37, in ?
    GP.FeatureClassToShapefile_conversion(outxyLayer, outputShapefiles)
  File "", line 2, in FeatureClassToShapefile_conversion
com_error: (-2147467259, 'Unspecified error', None, None)
Executing: FeatureClassToShapefile C:/xyLayerFiles/R302190.dbf C:\Shapefile=
s C:\Shapefiles
Start Time: Thu Feb 17 14:23:09 2005
Running script FeatureClassToShapefile...
Error in script FeatureClassToShapefile.
Error in executing: cmd.exe /C C:\PROGRA~1\ArcGIS\ARCTOO~1\Scripts\FEATUR~1=
.PY  "C:/xyLayerFiles/R302190.dbf" "C:\Shapefiles" "C:\Shapefiles"

Failed to execute (FeatureClassToShapefile).
End Time: Thu Feb 17 14:23:10 2005 (Elapsed Time: 1.00 secs)

What I think my code should be doing is using the .dbf files from the "C:/O=
ne" folder, storing the xy layer files in the "C:/xyLayerFiles" folder and,=
 finally, storing the shapefiles in the "C:/Shapefiles" folder. After I've =
run the code, however, there is nothing in either of the latter two folders=
.=20

One possibilty is that there is a problem with the way that I am passing th=
e input and output variables from the first toolbox command to the second, =
and perhaps also with the way that these variable are, or are not, being sa=
ved in the folders in which I think they should be saved.

Another possibility is that all the slashes in the workspace paths in the c=
ode are forward slashes, whereas all paths appear with back slashes in the =
address bar in my computer. However, if I try changing the forward slashes =
to back slashes in the code I get a warning about syntax when I use the 'ch=
eck' button.=20=20

I=92ve been trying to automate this conversion process since Monday of this=
 week and it is beginning to drive me insane!

Regards,=20

Chris Bromley.


Here is the script as I have modified it:

#Import standard library modules
import win32com.client, sys, os

#Create the Geoprocessor object
GP =3D win32com.client.Dispatch("esriGeoprocessing.GpDispatch.1")

import traceback

#Set the input workspace
GP.workspace =3D "C:/One"

#Set the xyLayer output workspace
xyLayerFiles =3D "C:/xyLayerFiles"

#Set the shapefile output workspace
outputShapefiles =3D "C:/Shapefiles"

try:
    # Load required toolboxes...=20=20=20=20
   GP.AddToolbox("C:/Program Files/ArcGIS/ArcToolbox/Toolboxes/Data       M=
anagement Tools.tbx")
    GP.AddToolbox("C:/Program Files/ArcGIS/ArcToolbox/Toolboxes/Conversion =
Tools.tbx")
=20=20=20=20
    #Get a list of dBase files in the input folder
    fcs =3D GP.ListTables("*","dBASE")

    #Loop through the list of dBase files
    fcs.Reset()
    fc =3D fcs.Next()

       while fc:
           # Set the outputname for each output to be the same as the input.
           outxyLayer =3D xyLayerFiles + "/" + fc

           #Convert each dBase table in the list into an xyLayer.
           GP.MakeXYEventLayer_management(fc, "X", "Y", outxyLayer, "")
           #Convert each xyLayer into a shapefile
           GP.FeatureClassToShapefile_conversion(outxyLayer, outputShapefil=
es)
           #Move to the next fc in the list.
           fc =3D fcs.Next()

except:
    traceback.print_exc()
    # If an error occurred print the message to the screen
    print GP.GetMessages()


This message has been checked for viruses but the contents of an attachment
may still contain software viruses, which could damage your computer system:
you are advised to perform your own checks. Email communications with the
University of Nottingham may be monitored as permitted by UK legislation.



More information about the Tutor mailing list