Hi. I'm trying to learn how to read a txt file of coordinates into an arcpy script, and create a featureclass from those coordinates. The only examples I find in the community posts address/solve different aspects than where my script fails. I have copied the key steps directly from the Zandbergen (2013) text, which includes an exercise/code to convert a txt file into a polyline (I had hoped to creat a point file first, then a polyline, and then modify to create polygons from a different txt file.
My script is:
import arcpy
import fileinput
import string
import os
arcpy.env.workspace = r"E:\advAppScripting\testing\hawaii"
arcpy.env.overwriteOutput = True
out_path = r"E:\advAppScripting\testing\hawaii"
new_fc = "myline.shp"
fc_geom = "POLYLINE"
newSR = 26904
try:
arcpy.CreateFeatureclass_management(out_path, new_fc, fc_geom)
# it should be possible to include the spatial reference as the final parameter in CreateFeatureclass, but it fails
# unless I remove it, and do the searate DefineProjection below...???
arcpy.DefineProjection_management(new_fc, newSR)
#I've confirmed successful operation ofhte script though line 16..
infile = r"E:\advAppScripting\testing\hawaii\coordinates.txt"
cursor = arcpy.da.InsertCursor(new_fc, ["SHAPE@"])
array = arcpy.Array()
for line in fileinput.input(infile):
ID, X, Y = string.split(line," ")
# I think this is supposed to read each line of the infile and parse it as the column headings in the new_fc.
# generates a "module 'string' has no attribute 'split'" error message
cursor.insertRow([arcpy.Polyline(array)])
# I thought this line should be part of the iteration through the rows, but in the example I've followed, its not indented
fileinput.close()
del cursor
except Exception as e:
print("Error: " + e.args[0])
I think the problem may be that the string.split() is 'old Python' and no longer works,
but I'm unsure how to resolve.
Would appreciate any help/advice. I am attaching the associated txt file
Solved! Go to Solution.
Just in case anyone else is finding this same problem: Have been able to get the XYTabletoPoint to work by including None for the Vertical CS, instead of leaving that parameter blank, and without doing anything special to call the horizontal spatial ref., as shown below.
arcpy.XYTableToPoint_management(infile, r"E:\advAppScripting\testing\hawaii\testing.gdb\myNEWpoints",
"EASTING", "NORTHING", None, 26904)
I've never used the split() function as shown above. Typically the split function is used with just a delimiter like this:
x = '123,456'
newX = x.split(',')
# which returns a list of ['123', 456']
All that said, I would simplify things and start with replacing the spaces in your text file with a comma, and call the new file coords.csv. You'll need to add a line on the top naming the fields. Then use XY table to Point
Thanks! This does seem like a much simpler approach. I've been trying to implement it for 3 hours now....with repeated failures for 'invalid geometry'--even though if I run the XYTabletoPoint tool within Pro (2.8) it works; it just fails if I try to call that tool within arcpy. I'm pasting my new script, and also the new (stripped down) version of the table, in case you are willing to provide further assistance in spite of your desire to retire....
import arcpy
from arcpy import env
env.workspace = r"E:\advAppScripting\testing\hawaii"
env.overwriteOutput = True
infile = r"E:\advAppScripting\testing\hawaii\testpoints3.csv"
try:
arcpy.CreateFileGDB_management(r"E:\advAppScripting\testing\hawaii", "testing")
# had originaly tried to just create a .shp file, but after it failed, changed to creating a featureclass
# hence the need to create a new gdb first
arcpy.XYTableToPoint_management(infile, r"E:\advAppScripting\testing\hawaii\testing.gdb\myNEWpoints",
"EASTING", "NORTHING")
# the tool help claims you can specify the spatial ref. as a tool parameter, but when I do, script fails with
# 'not a field' error meesage, so switched to definine projection as a separate step
arcpy.DefineProjection_management(r"E:\advAppScripting\testing\hawaii\testing.gdb\myNEWpoints",
arcpy.SpatialReference(26904))
# this script creates a new gdb and a new feature class with the correct spatail ref. BUT no points are included
# and a 'invalid geometry error is generated
Just in case anyone else is finding this same problem: Have been able to get the XYTabletoPoint to work by including None for the Vertical CS, instead of leaving that parameter blank, and without doing anything special to call the horizontal spatial ref., as shown below.
arcpy.XYTableToPoint_management(infile, r"E:\advAppScripting\testing\hawaii\testing.gdb\myNEWpoints",
"EASTING", "NORTHING", None, 26904)