2

I was working through an exercise designed to teach basic Python skills in ArcMap using Python 2.7. The file below consists of 34 pairs of x,y coordinates. The script creates a new empty polyline feature class, and then use this list of coordinates to create a new polyline. This requires the use of an insert cursor to create new rows and an array object to contain the point objects. Here is the original proposed script:

Python 2.7 script for ArcMap In the Spyder IDE with the ArcGIS Pro conda installation for 2.3, I hit a number of exceptions that I worked through as follows:

import arcpy
import fileinput
import string
import os
from arcpy import env
env.workspace = "C:/Workspace"
env.overwriteOutput = True
arcpy.CreateFeatureclass_management(r"C:/Results", "newfc", "POLYLINE", None, "DISABLED", "DISABLED", None, None, 0, 0, 0, None)
infile = "C:/coordinates.txt"
cursor = arcpy.da.InsertCursor("newfc.shp", ["SHAPE@"])
array = arcpy.Array()
for line in fileinput.FileInput(infile):
    ID, X, Y = (float(infile.split(line," "))
    array.add(arcpy.Point(X, Y))
cursor.insertRow([arcpy.Polyline(array)])
fileinput.close()
del cursor

However, there is a syntax error related to the arcpy.Array() method.

      File "C:/create.py", line 21
array.add(arcpy.Point(X, Y))
    ^
SyntaxError: invalid syntax

Here is the point file.

enter image description here

My knowledge is limited and wondered if someone knows if this is a Python version issue and where to find more info, or if they have an answer to the syntax error problem I articulated above.

  • Welcome to GIS SE! We're a little different from other sites; this isn't a discussion forum but a Q&A site. Please check out our short tour to learn about our focussed Q&A format. What is the full error message (as formatted text) that you are getting? Try printing the contents of line in case its not what you are thinking. – PolyGeo May 12 at 23:26
  • Array is a valid class pro.arcgis.com/en/pro-app/arcpy/classes/array.htm what error message are you getting? How are you running the code? i.e. in ArcGIS python window or in your IDE. Can you give an example of the first half dozen lines in coordinates.txt please. As a side note consider using a with block for your cursor, it will automatically release locks when the array passes out of scope. – Michael Stimson May 12 at 23:50
  • Thank you Michael Stimson. Looking into both of these items. I really didn't know where to start. – 1969Paulie May 13 at 0:34
  • 1
    Thank you PolyGeo - I just signed up for your ArcGIS Pro Python class yesterday. Thank you for your feedback. – 1969Paulie May 13 at 0:36
  • 1
    Please remember to use ASCII to present as much information as possible. Using an image of a coordinate stream means than any volunteer who tried to help would need to retype pseudo random data. Error traces should always be in a form that can be indexed and found by others. – Vince May 13 at 4:18
2

Try changing your for loop to this:

for line in fileinput.FileInput(infile):
    ID, X, Y = line.split()
    array.add(arcpy.Point(float(X), float(Y)))

I'm not sure what you were trying to do with your infile/split/line line so I hope I've guessed your intentions correctly. In this case it simply splits each line (by any whitespace, by default). The cast from string to int or float is then done individually for the X and Y variables in-line on the next line.

Note that I've opted for floats rather than ints, as you would lose precision using ints, according to your screenshot of values... And because arcpy.Point expects floats (doubles).

  • Thank you! This works great. – 1969Paulie May 13 at 2:53
  • You're welcome. Please accept this as the answer if it solves your problem. – Son of a Beach May 13 at 3:46

Your Answer

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

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