1

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
  • 1
    I think you need to split your line, you indents are funny but it looks like you need to use LineSplit = line.split(',') to break up into a list then set x = LineSplit[1] and y = LineSplit[2] (name is LineSplit[0]) but I can't be sure. Can you include the first 10 lines of your text file as an example please and fix the indenting of the code block so it matches what you have in your code. – Michael Stimson Oct 19 '18 at 0:14
  • The code above is indented properly from what I can tell and reads like it should and does in my script. – user129835 Oct 19 '18 at 1:31
  • It is preferable to edit those into your question rather than add each line as comments. Does the Name change? if so does the change of name make a new polyline? – Michael Stimson Oct 19 '18 at 1:36
  • FredRanch1_1, 529018.125025, 4108038.05548 FredRanch1_1, 529005.718792, 4108028.20659 FredRanch1_1, 528993.340503, 4108018.73931 FredRanch1_1, 528980.990158, 4108009.65364 FredRanch1_1, 528968.667757, 4108000.94958 FredRanch1_1, 528956.373301, 4107992.62713 FredRanch1_1, 528944.106788, 4107984.68629 FredRanch1_1, 528931.86822, 4107977.12705 FredRanch1_1, 528919.657595, 4107969.94943 FredRanch1_1, 528907.474915, 4107963.15341 – user129835 Oct 19 '18 at 1:40
  • 1
    Comments are not for extended discussion; this conversation has been moved to chat. – Mapperz Oct 19 '18 at 1:47
2

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.

  • Okay, while this worked to an extent. It did no create multiple lines. it created on big crazy jumbled line. How do I get it to create separate lines? – user129835 Oct 19 '18 at 1:54
  • I have edited in the new code @JackJenkins. It's from a script I wrote to turn POS information (very similar to GPS) into a line of travel, though in that script I used the distance between vertices to break the lines (more than 100 metres made a new line), hopefully the changes I've made are bug free. – Michael Stimson Oct 19 '18 at 1:56
  • cursor.insertRow([polyline, oldName]) NameError: name 'polyline' is not defined – user129835 Oct 19 '18 at 2:19
  • I'm guessing that polyline is referring to 'fc'...yes? – user129835 Oct 19 '18 at 2:19
  • doh! I'll fix that now. A fumble in the edit.. the original generated the polyline on insert like: cursor.insertRow([arcpy.Polyline(pointList),oldName]) but when I changed the code to match your variable names I forgot to instantiate the polyline object. – Michael Stimson Oct 19 '18 at 2:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy