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
try:
    outFolder = raw_input (“Enter Workspace: “)
except:
    print “Does this workspace exist ?”
    print gp.getmessage(2)
   
gp.Workspace = outFolder
gp.toolbox = “management”
newfile=[]
# Open a source TXT file with coordinate data
try:
    file_name = raw_input(“Enter file name: “)
    text_file = open(file_name, “r”)
except:
    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: 
    alist=line.split()
    point=alist[0]
    latitude=alist[1]
    longitude=alist[2]
    # Collect the lists with coordinate data in another list
    newfile.append(alist)
text_file.close()

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

try:
    gp.CreateFeatureClass (outFolder, outFile, “Point”, “#”, “#”, “#”, sr)
except:
    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
    cur.InsertRow(row)
   
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\ShP_from_XY.py”, 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.

Advertisements

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:

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s