I've created the following python script that extracts the position along a polyline based on a user defined interval. The chainage is determined for each point (i.e. first point = 0 ; second point =50m ; 100m ; 150m ; .... last point = 452m) The problem that I have is that it works great for a single polyline but as soon as you have more than one polyline there is no way of knowing the ID (Cross Section ID) that the points were derived from. I'm using the ArcPy Data Access SearchCursor to find the positions along the line based on the user defined interval and then the list of points are used to create a point feature class with the chainage added as a new field. I was orignally helped by someone else with adding the chainge (distance) values to the pts list using the python zip function.
I need assistance extracting the ID field from the polyline and adding the ID value to a new field within the points feature class that is being created. Any help would be appreciated.
I've attached my python script as you can't paste within GeoNET . The old user forum was so much better.
Regards
Peter Wilson
Hi
Peter
You
will have to change your program a little.
You
have to create an empty feature class with the attributes you need (chainge and LineId).
When you open your cursor to the lines you should get the
ID too.
Then you write each point separately into the feature
class and not into geometry list.
This way you can enter the LineId for each point you
insert.
Have fun
Hi Peter,
Here is an update to your script that creates the feature class, and then inserts the geometry and ID values into the feature class.
outpnts = os.path.join(gdb, os.path.join(gdb, os.path.basename(pipeline) + "_pnts"))
outpnts = arcpy.CreateFeatureclass_management(gdb, os.path.basename(pipeline) + "_pnts", "POINT", "", "", "", pipeline)
arcpy.AddField_management(outpnts,"chainage","DOUBLE")
arcpy.AddField_management(outpnts,"ID","SHORT")
pts = []
fields = ["SHAPE@", "ID"]
with arcpy.da.SearchCursor(pipeline, fields) as scur:
for row in scur:
cursor = arcpy.da.InsertCursor(outpnts, fields)
geometry = row[0]
l = 0
while l < geometry.length:
pts.append((geometry.positionAlongLine(l), l))
pt = (geometry.positionAlongLine(l))
row_values = [(pt, row[1])]
for row in row_values:
cursor.insertRow(row)
l += interval
leng = geometry.length
pt = (geometry.positionAlongLine(leng))
pts.append((geometry.positionAlongLine(leng), leng))
row_values = [(pt, row[1])]
for row in row_values:
cursor.insertRow(row)
del cursor, scur
pts, distances = zip(*pts)
with arcpy.da.UpdateCursor(outpnts,["chainage"]) as cursor:
n = 0
for row in cursor:
row[0] = distances
cursor.updateRow(row)
n += 1
del cursor
Also, here is a great post on how to post code in GeoNET.