0

I have a point layer and i need to connect all the points(4046) with each other. I tried it through python console from Arcgis but it gives me run time error. Here is my code :

    import arcpy
cursor = arcpy.da.SearchCursor("vertex_final",['POINT_X','POINT_Y'])
outlines= r"C:\Users\stelitsa\Desktop\descriptive_data"
i = 0
while (i<4064):
 X1=row[i]
 Y1=row[i+1]
 print  X1, Y1
   for row in cursor:
    X2=row[0]
    Y2= row[1]
    if X1!=X2:
      arcpy.XYToLine_management(cursor,outlines,X1,Y1,X2,Y2,"GEODESIC","id")      

And here is the error :

Runtime error Traceback (most recent call last): File "", line 13, in File "c:\program files (x86)\arcgis\desktop10.4\arcpy\arcpy\management.py", line 3063, in XYToLine raise e RuntimeError: Object: Error in executing tool

This is the first time that I have used Python.

  • 1
    XYToLine wants fields, not values and I'm not sure you're accessing the values correctly either. Sorry, I don't know arcpy well ehough to help beyond that. – mkennedy Aug 14 '17 at 17:41
3

You're using a cursor object as in input for your arcpy tool, which isn't valid. I'd suggest trying something else. One method is to create a line feature class in the same spatial reference as your points. Then iterate through your points, iterate again in a nested loop, and create line geometries from your two XY's. Insert these geometries into your newly created feature class. All lines will be doubled up, so a DeleteIdentical will be needed to remove duplicates.

Try:

import arcpy
import os

#point feature class path
pntFc = r"C:\GDB.gdb\TestPoints"
#output line feature class path
outLnFc = r"C:\GDB.gdb\OutLines"


#store points in set
pnts = set ()
with arcpy.da.SearchCursor (pntFc, "SHAPE@XY") as curs:
    for xy, in curs:
        pnts.add (xy)

#get spatial reference from point feature class
sr = arcpy.Describe (pntFc).spatialReference

#create line feature class
outPath, outName = os.path.split (outLnFc)
arcpy.CreateFeatureclass_management (outPath, outName, "POLYLINE", spatial_reference = sr)

#iterate points and insert
with arcpy.da.InsertCursor (outLnFc, "SHAPE@") as curs:
    for x1, y1 in pnts:
        for x2, y2 in pnts:
            #skip same points
            if (x1, y1) == (x2, y2):
                continue
            #create point array
            array = arcpy.Array ([arcpy.Point (x1, y1),
                                  arcpy.Point (x2, y2)])
            #create line feature
            line = arcpy.Geometry ("POLYLINE", array, sr)
            curs.insertRow ((line,))

#delete duplicates
shpFld = arcpy.Describe (outLnFc).shapeFieldName
arcpy.DeleteIdentical_management (outLnFc, shpFld)

Result:

enter image description here

  • Thank you very much. I ll try it now and i ll let you know. – stela girtsou Aug 14 '17 at 18:08
  • It worked! I' m really gratefull! I didnt delete the duplicates as i need the lines for routing(walking). – stela girtsou Aug 14 '17 at 19:09

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.