2

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?

  • Within your for loop you need to create the from and to point objects, at the moment its simply overwriting the x and y of point. Also you only create 1 feature row BEFORE your for loop, move that within the loop. – Hornbydd Jan 3 '14 at 13:18
  • I am following the answer here gis.stackexchange.com/questions/34647/… . The to and from points are getting stored in the array which is being used to create the line. And cursor.nextRow() automatically creates a new line. – DotPi Jan 3 '14 at 13:22
  • OK, what's pt[]? where is that defined and what is in it? – Hornbydd Jan 3 '14 at 13:45
  • pt is a list that contains the id of the point, i.e. pt=["1","2",...] – DotPi Jan 3 '14 at 13:49
6

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)))
  • changing the x and y to upper case worked! – DotPi Jan 3 '14 at 13:44

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.