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?

asked Jan 3, 2014 at 12:40
4
  • 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. Commented Jan 3, 2014 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. Commented Jan 3, 2014 at 13:22
  • OK, what's pt[]? where is that defined and what is in it? Commented Jan 3, 2014 at 13:45
  • pt is a list that contains the id of the point, i.e. pt=["1","2",...] Commented Jan 3, 2014 at 13:49

1 Answer 1

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)))
answered Jan 3, 2014 at 13:36
1
  • changing the x and y to upper case worked! Commented Jan 3, 2014 at 13:44

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.