2

I want to create polylines linking a bunch of points. Individual polylines must be created between every point. I started by reading the x & y cords and then created unique pairs as well as all combinations. Unfortunately my python is lacking and I'm also stuck with ArcGIS 10.0.

import arcpy
import itertools
mxd = arcpy.mapping.MapDocument("CURRENT")

myt = ()
newt = ()
pairt = ()

# Get the XY coords from the point featureclass
inputfc = 'C:\\Temp\\testing.gdb\\BunchofPoints'
desc = arcpy.Describe(infc)
shapename = desc.ShapeFieldName
rows = arcpy.SearchCursor(inputfc)
for row in rows:
    feat = row.getValue(shapename)
    pnt = feat.getPart()
    myt = myt +((pnt.X),(pnt.Y))

#Create pairs
newt =zip(myt[::2],myt[1::2])

#Create combinations
for pair in itertools.combinations(newt,2):
    pairt = pairt + pair

#Check
print pairt

#Create lines
mypoint = arcpy.Point()
myarray = arcpy.Array()
featList = []
for feature in pairt:
    for coPair in feature:
        mypoint.X = coPair[0]
        mypoint.Y = coPair[1]
        myarray.add(mypoint)
    polyline = arcpy.Polyline(myarray)
    myarray.removeAll()
    featList.append(polyline)
arcpy.CopyFeatures_management(featureList, 'C:\\Temp\\all_lines.shp')

I get the following error: Runtime error : 'float' object is unsubscriptable

I'm struggling with how to read the pair of coordinates I've created and also with creating the individual lines.

  • print pairt does it make it that far and if so what is the output? and you are using and empty tuple to collect the points, why not a list? – user681 Jan 12 '16 at 6:36
  • yes it prints as so: ((-4416739.9874999998, 1752062.4517000001), (-211810.60020000022, -2405686.8023000006)), ((-4416739.9874999998, 1752062.4517000001), (283580.80020000041, 47680.133300000802))...etc ....although admittedly I'm not sure how to deal with this output.... – wunderkind Jan 12 '16 at 7:00
  • @wunderkind - the output from 'pairt' is a list of coordinate pairs. when you use for feature in pairt: you step through each pair of coordinates (feature) and add to your point array – Midavalo Jan 12 '16 at 7:29
2

There were a couple of errors in your code:
desc = arcpy.Describe(infc) should be desc = arcpy.Describe(inputfc) You've used "infc" instead of "inputfc"

for feature in pairt:
    for coPair in feature:
        mypoint.X = coPair[0]
        mypoint.Y = coPair[1]
        myarray.add(mypoint)
    polyline = arcpy.Polyline(myarray)
    myarray.removeAll()
    featList.append(polyline)

should be:

for feature in pairt:
    mypoint.X = feature [0]
    mypoint.Y = feature [1]
    myarray.add(mypoint)
polyline = arcpy.Polyline(myarray)
myarray.removeAll()
featList.append(polyline)

feature is your pair of coordinates, so feature[0] is x and feature[1] is y. The copair[0] won't work as there is no pair of coordinates in the list.

arcpy.CopyFeatures_management(featureList, 'C:\\Temp\\all_lines.shp') should be arcpy.CopyFeatures_management(featList, 'C:\\Temp\\all_lines.shp')
You used 'featureList' rather than 'featList'

This produces an output for me joining each point to every other point. Points to Lines

Complete code that worked for me:

import arcpy
import itertools
#mxd = arcpy.mapping.MapDocument("CURRENT")

myt = ()
newt = ()
pairt = ()

# Get the XY coords from the point featureclass
inputfc = 'C:\\Temp\\testing.gdb\\BunchofPoints'
desc = arcpy.Describe(inputfc)
shapename = desc.ShapeFieldName
rows = arcpy.SearchCursor(inputfc)
for row in rows:
    feat = row.getValue(shapename)
    pnt = feat.getPart()
    myt = myt +((pnt.X),(pnt.Y))

#Create pairs
newt =zip(myt[::2],myt[1::2])

#Create combinations
for pair in itertools.combinations(newt,2):
    pairt = pairt + pair

#Check
print pairt

#Create lines
mypoint = arcpy.Point()
myarray = arcpy.Array()
featList = []

for feature in pairt:
    mypoint.X = feature[0]
    mypoint.Y = feature[1]
    myarray.add(mypoint)

polyline = arcpy.Polyline(myarray)
myarray.removeAll()
featList.append(polyline)

arcpy.CopyFeatures_management(featList, 'C:\\Temp\\all_lines.shp')
  • Fantastic! I really appreciate the help. I had changed the code before posting to conceal the 'real' paths and FCs but this obviously introduced errors. It is good to know that I was on the right path though...thanks again! – wunderkind Jan 12 '16 at 7:32
  • How do I create separate line 'connectors' rather than a single line? how can I use feature[a,b] to select the pairs and create the lines? – wunderkind Jan 12 '16 at 12:28
  • Solved! I used the size of the array equal to 2 to create each segment. – wunderkind Jan 12 '16 at 13:14

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.