1

Can someone help me figure out what is not working in this piece of code:

import arcpy
import fileinput
import os
import string

from arcpy import env

env.workspace = "C:/EsriPress/Python/Data/Exercise08"
env.overwriteOutput = True
outpath = "C:/EsriPress/Python/Data/Exercise08"
newfc = "Results/newpolyline.shp"
arcpy.CreateFeatureclass_management(outpath, newfc, "Polyline")

infile = "C:/EsriPress/Python/Data/Exercise08/coordinates.txt"

cursor = arcpy.da.InsertCursor(newfc, ["SHAPE@"])
array = arcpy.Array()

for line in fileinput.input(infile):
    ID, X, Y = string.split(line, " ")
    array.add(arcpy.Point(X,Y))
cursor.insertRow([arcpy.Polyline(array)])
fileinput.close()
del cursor

I looked up some other questions/responses related to this error and they mention that perhaps the text file being read is not formatted correctly. Each line in my text file has an object ID, and an X & Y coordinates, with only one space in between each field (also, there is no extra line or space after the final object).

I'm wondering what else can be giving me this error.

10
  • Does your newfc have 3d coordinates? Try setting arcpy.env.outputZFlag = "disabled" and arcpy.env.outputMFlag = "disabled" to temporarily assert no Z/M values in created feature classes. The other thing you might try is array.add(arcpy.Point(float(X),float(Y))), python isn't strong typed but there might be something internal that doesn't like strings. Commented Sep 11, 2017 at 5:28
  • Insert , between ] and )
    – FelixIP
    Commented Sep 11, 2017 at 5:30
  • @MichaelStimson I tried adding the env settings , as well as using the float function on the X and Y variables as you suggests. Unfortunately I am still receiving the same value error message.
    – Gabe
    Commented Sep 11, 2017 at 22:44
  • @Felix You're saying I should try inserting a comma (,) in between the right bracket ] and parentheses ) ? Am I reading that correctly? If so, which part of the code should I insert the comma ?
    – Gabe
    Commented Sep 11, 2017 at 22:49
  • pLine= arcpy.Polyline(array); cursor.insertRow((pLine,))
    – FelixIP
    Commented Sep 11, 2017 at 22:58

1 Answer 1

2

With a minimum of modification to your code:

array = arcpy.Array()

with arcpy.da.InsertCursor(newfc, "SHAPE@") as cursor:
    with open(infile,'r') as inHand:
        for line in inHand:
                ID, X, Y = line.split(" ")
                Xco = float(X)
                Yco = float(Y)
                array.add(arcpy.Point(Xco,Yco))

    cursor.insertRow([arcpy.Polyline(array)])

Works for me. There could be a lock, something wrong with your text file or your implementation of fileinput, but reading the docs it seems that your implementation is correct therefore it seems the likely culprit is your text file or a persistent lock on the shapefile.

To release an existing lock, evidenced by a lock file (sr.lock, wr.lock etc) present with the shapefile, close all Esri products and hopefully it will disappear, if not, you might need to restart your computer (being that severe would be a rare event).

To try to find garbage in the text file try printing the line (add print(line) in your code on the line right after for line in..) so that when it crashes out you know which line caused the error and try to guess why.

Normally I'd put in a try: except: block on the next line after for line in inHand: because I don't trust text files to have 3 (or more) elements, the latter two being numeric, all the time regardless of how sure the creator of the file is - I've been burnt too may times by garbage in a text file to be complacent about it:

with arcpy.da.InsertCursor(newfc, "SHAPE@") as cursor:
    with open(infile,'r') as inHand:
        for line in inHand:
            LineAllGood = True
            try:
                ID, X, Y = line.split(" ")
            except:
                LineAllGood = False # don't do the next step.
                arcpy.AddWarning('Line {} produces an error in splitting'.format(line))

            if LineAllGood: # The splitting worked, now try to float both
                try:
                    Xco = float(X)
                    Yco = float(Y)
                    array.add(arcpy.Point(Xco,Yco))
                except:
                    arcpy.AddWarning('Conversion error X {} or Y {} not a float'.format(X,Y))

    cursor.insertRow([arcpy.Polyline(array)])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.