[GIS] Create lines from points arcpy

arcpy

I have a points shapefile, and a file that tells me which of the points have a line between them. I am using arcpy to create a new shapefile to hold the lines and then create the lines based on the specifications in the file. For creating the lines, I am reading the coords of the points from the point shapefile itself.

out_path = arcpy.env.workspace
out_name = arcpy.ValidateTableName("the_lines")
geometry_type = "POLYLINE"
has_m = "DISABLED"
has_z = "DISABLED"
spatial_reference = arcpy.Describe(points_shp).spatialReference # Use Describe to get a SpatialReference object
# Execute CreateFeatureclass
arcpy.CreateFeatureclass_management(out_path, out_name, geometry_type,spatial_reference = spatial_reference)

array = arcpy.Array()
point = arcpy.Point()
tie_shp_file = arcpy.env.workspace + "/" + out_name
insert_cursor = arcpy.InsertCursor(tie_shp_file)
feat = insert_cursor.newRow()

for line in all_lines:   #all_lines is of the form array([[0, 3],[0, 7],[1, 3]) indicating line [from, to]
    #from
    point.x=coords_pts[str(pt[line[0]])][0]
    point.y=coords_pts[str(pt[line[0]])][1]
    print "FROM: " + str(pt[line[0]]) + " COORDS_X: " + str(coords_pts[str(pt[line[0]])][0]) + " COORDS_Y: " + str(coords_pts[str(pt[line[0]])][1])
    array.add(point)
    #to
    point.x=coords_pts[str(pt[line[1]])][0]
    point.y=coords_pts[str(pt[line[1]])][1]
    print "TO: " + str(pt[line[1]]) + " COORDS_X: " + str(coords_pts[str(pt[line[1]])][0]) + " COORDS_Y: " + str(coords_pts[str(pt[line[1]])][1])
    array.add(point)
    print "ARRAY: " + str(array)
    polyline=arcpy.Polyline(array)# Create a Polyline object based on the array of points
    array.removeAll() #Clear the array
    feat.shape = polyline
    insert_cursor.insertRow(feat)

coords_points is a dictionary that has the form:

 {'10': (8594242.514466941, 3330642.8703512326)...}
i.e. {'Oid_str': (x,y)}

The code is creating the required number of entries in the shapefile but no lines are being shown and the lengths of the line is shown as 0.

Where am I going wrong?

Best Answer

Your code looks OK, but you use lower case x and y instead of upper case X and Y. Maybe that could help...

Also, I recommend using

arcpy.da.InsertCursor

if you have ArcGIS 10.1 or higher, for example :

cursor = arcpy.da.InsertCursor(fc2, ("SHAPE@","ID"))

for line in lines:
    i = i+1
    coordList=[]

    coordList.append([ coords_pts[str(pt[line[0]])][0], coords_pts[str(pt[line[0]])][1]])
    coordList.append([ coords_pts[str(pt[line[1]])][0], coords_pts[str(pt[line[1]])][1]])

   polyline = arcpy.Polyline(arcpy.Array([arcpy.Point(*coords) for coords in coordList]))
   cursor.insertRow((polyline,i)))