Saturday, 28 July 2018

geometry - Creating Polyline shapefile from text file using ArcPy?


I'm trying to create a polyline shapefile from a text file which provides Name, coordX, coordY. I've created the following script, which I feel will work. However, I can't seem to get it to read the text file properly. I am getting the following error, RuntimeError: Object: CreateObject cannot create geometry from inputs.


import arcpy

arcpy.env.workspace = "C:\\Data"

outFolder = "C:\\Data"
fc = "Paths.shp"


spatRef = arcpy.SpatialReference(26913)
arcpy.CreateFeatureclass_management(outFolder, fc, "POLYLINE", "", "", "", spatRef)

coordinateList = open("C:\\Data\\Paths.txt")

for line in coordinateList.readlines():
print line

pointList = arcpy.Array()


for x, y in coordinateList:
point = arcpy.Point(x,y)
pointList.add(point)

polyline = arcpy.Polyline(pointList)

cursor = arcpy.da.InsertCursor(fc, "SHAPE@")
cursor.insertRow([polyline])

del cursor


Answer



This is the simplest edit to make your code work:


import arcpy

arcpy.env.workspace = "C:\\Data"
outFolder = "C:\\Data"
fc = "Paths.shp"

spatRef = arcpy.SpatialReference(26913)
arcpy.CreateFeatureclass_management(outFolder, fc, "POLYLINE", "", "", "", spatRef)


coordinateList = open("C:\\Data\\Paths.txt")

pointList = arcpy.Array()
for line in coordinateList.readlines():
print line

SplitLine = line.split(',') # break up the string into elements
# 'FredRanch1_1, 529018.125025, 4108038.05548' becomes
# ['FredRanch1_1', '529018.125025', '4108038.05548'] which can be indexed

x = float(SplitLine[1]) # turn the strings into numbers
y = float(SplitLine[2])

point = arcpy.Point(x,y)
pointList.add(point)

polyline = arcpy.Polyline(pointList)

cursor = arcpy.da.InsertCursor(fc, "SHAPE@")
cursor.insertRow(polyline)


del cursor
coordinateList.close() # don't forget to close your file

Using with statements condenses the code and ensures you don't forget to free the cursor and close the file:


import arcpy

arcpy.env.workspace = "C:\\Data"
outFolder = "C:\\Data"
fc = "Paths.shp"


spatRef = arcpy.SpatialReference(26913)
arcpy.CreateFeatureclass_management(outFolder, fc, "POLYLINE", "", "", "", spatRef)

with open("C:\\Data\\Paths.txt",'r') as coordinateList:
with arcpy.da.InsertCursor(fc,'SHAPE@') as cursor:
pointList = arcpy.Array()
for line in coordinateList:
SplitLine = line.split(',') # break up the string into elements
# 'FredRanch1_1, 529018.125025, 4108038.05548' becomes

# [FredRanch1_1, 529018.125025, 4108038.05548] which can be indexed
x = float(SplitLine[1])
y = float(SplitLine[2])

point = arcpy.Point(x,y)
pointList.add(point)
cursor.insertRow(polyline)

With the added complication of starting a new line when the name changes:


import arcpy


arcpy.env.workspace = "C:\\Data"
outFolder = "C:\\Data"
fc = "Paths.shp"

spatRef = arcpy.SpatialReference(26913)
arcpy.CreateFeatureclass_management(outFolder, fc, "POLYLINE", "", "", "", spatRef)
arcpy.AddField_management(fc,'Name','TEXT',field_length=250)

oldName = None

pointList = arcpy.Array()
with open("C:\\Data\\Paths.txt",'r') as coordinateList:
with arcpy.da.InsertCursor(fc,['SHAPE@','Name']) as cursor:
for line in coordinateList:
SplitLine = line.split(',') # break up the string into elements
# 'FredRanch1_1, 529018.125025, 4108038.05548' becomes
# ['FredRanch1_1', '529018.125025', '4108038.05548'] which can be indexed
if oldName == None:
oldName = SplitLine[0] # only should happen on the first iteration


x = float(SplitLine[1]) # turn the strings into numbers
y = float(SplitLine[2])

if oldName.upper() != SplitLine[0].upper():
# the name has changed!
polyline = arcpy.Polyline(pointList)
cursor.insertRow([polyline,oldName]) # insert this line on name change
pointList = arcpy.Array() # reset the array
oldName = SplitLine[0] # make this name the old name


point = arcpy.Point(x,y)
pointList.add(point)
polyline = arcpy.Polyline(pointList)
cursor.insertRow([polyline,oldName]) # clean up the last line

Caveat I have not tested this code, it's from a copy of some of my existing code to do a similar purpose... I may have deleted a necessary line or neglected to change a variable name.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...