Creating SHP file from X,Y ASCII file…

Below is the script I came up with yesterday by merging my previous script with part of a script I found here (thanks, Morgan, credit you !) and adding more functionality, e.g. selecting workspace and output file names using rawinput.

import arcgisscripting
gp = arcgisscripting.create()

# Set OverWriteOutput to “True” so script can over write files with the same name
gp.OverWriteOutput = True
    outFolder = raw_input (“Enter Workspace: “)
    print “Does this workspace exist ?”
    print gp.getmessage(2)
gp.Workspace = outFolder
gp.toolbox = “management”
# Open a source TXT file with coordinate data
    file_name = raw_input(“Enter file name: “)
    text_file = open(file_name, “r”)
    printCannot access this file. Does it exist ?
    print gp.getmessage(2)

# Read coordinates into a list, line by line
lines = text_file.readlines()
for line in lines: 
    # Collect the lists with coordinate data in another list

sr = gp.CreateSpatialReference (“C:\Program Files\ArcGIS\Coordinate Systems\Geographic Coordinate Systems\World\WGS 1984.prj”)
outFile = raw_input(“Enter Shapefile Name : “)

    gp.CreateFeatureClass (outFolder, outFile, “Point”, “#”, “#”, “#”, sr)
    printThere was a problem creating the shapefile.
    print gp.getmessage(2)
for borehole in newfile:
    cur = gp.InsertCursor(outFile)
    row = cur.NewRow()
    pnt = gp.CreateObject(“Point”)
    pnt.x = borehole[2]
    pnt.y = borehole[1]

    row.shape = pnt
del cur, row

Now, I don’t know how Pythonic this is, or whether I could’ve been more terse or accurate. I haven’t used try/except much, and maybe there is a better way of doing this. Please let me know. I did run into some errors initially, e.g.

Traceback (most recent call last):
  File “C:\GeoProc\”, line 43, in <module>
    cur = gp.InsertCursor(outFile)
RuntimeError: ERROR 999999: Error executing function.
A locator with this name does not exist.

I initially thought there might be something wrong with the gp.OverWriteOutput statement, and found that there are indeed differences between ArcGIS geoprocessing 9.2 and 9.3 with this statement, (see also here). But specifying 9.3 (gp = arcgisscripting.create(9.3) didn’t change anything. In fact, after that, I was told I had to use Python2.5 when I happened to be in 2.3 (too many versions on my computer). Finally, I figured out that entering an output name without the .SHP extension was causing the error. I will probably have to add a reminder/check that this is entered or added, if not entered by user.


1 Comment

Filed under Uncategorized

One response to “Creating SHP file from X,Y ASCII file…

  1. This looks great Arne. I agree that there are so many resources, each with little bits scattered about, that it is hard to find what one is looking for. Keep the good work.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s