Select to view content in your preferred language

add a vertex to multiple polylines

2666
2
Jump to solution
02-04-2014 07:31 AM
deleted-user-MS0lE1F_0-sy
Deactivated User
I have a lot of lines in a feature class where the only vertices are the two end points.  I want to add one vertex 2 ft from the start point of the line.  I might be doing something wrong, but the Densify tool doesn't seem like it can pull this off.  Any suggestions would be very helpful.

Thank you,
Matt
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
FilipKrál
Frequent Contributor
Hi Matt,
How about something like the code below.

It assumes projected coordinate system and that all your lines are longer than the shift_by distance.

You might need to fix a row or two though cause I haven't tested exactly these lines.
Hope this helps.
Filip.

in_fc = r'c:\temp.gdb\infc' out_fc = r'c:\temp.gdb\outfc' shift_by = 2.0 # shift distance in linear units of your PROJECTED (!) coordinate system  import os, arcpy  def shiftPointInDirection(x, y, direction, shiftDistance):     """Return new (x, y) as shifted x, y in direction (dx, dy) by distance shiftDistance"""     d = (direction[0]**2 + direction[1]**2)**(0.5)     q = shiftDistance / d     x =  x + q * direction[0]     y = y + q * direction[1]     return (x, y)  # create a new polyline feature class and add field(s) out_fc = arcpy.CreateFeatureClass_management(os.path.dirname(out_fc), os.path.basename(out_fc), "POLYLINE", spatial_reference = arcpy.Describe(in_fc).spatialReference).getOutput(0) arcpy.AddField_management(out_fc, "ORIGID", "LONG")  # starting an edit session may not be necessary, but to stay on the safe side... with arcpy.da.Editor(workspace) as edit:      with arcpy.da.SearchCursor(in_fc, ["SHAPE@", "OID@"]) as sc:      with arcpy.da.InsertCursor(out_fc, ["SHAPE@", "ORIGID"]) as ic:       for row in sc:                  # if your geometries are simple 2D singlepart lines, something like this                 # should get the actual line vertices of a feature (sorry don't have Arc now to test)                 # search Reading Geometries and Writing Geometries in help        shp = row[0].getPart(0)                  # handle individual vertices                 p0 = shp.firstPoint                 p2 = shp.lastPoint                 x1,y1 = p0.X, p0.Y                 shift_dir = (p2.X - p0.X, p2.Y - p0.Y)                 x1,y1 = shiftPointInDirection(x1, y1, shift_dir, shift_by)                 p1 = arcpy.Point(x1, y1)                  # create new feature and store it                 new_geometry = arcpy.Polyline(arcpy.Array([p0, p1, p2]))                 new_row = [new_geometry, row[1]]                 ic.insertRow(newrow)  # then join attributes from in_fc to out_fc on in_fc.OBJECTID = out_fc.ORIGID

View solution in original post

0 Kudos
2 Replies
FilipKrál
Frequent Contributor
Hi Matt,
How about something like the code below.

It assumes projected coordinate system and that all your lines are longer than the shift_by distance.

You might need to fix a row or two though cause I haven't tested exactly these lines.
Hope this helps.
Filip.

in_fc = r'c:\temp.gdb\infc' out_fc = r'c:\temp.gdb\outfc' shift_by = 2.0 # shift distance in linear units of your PROJECTED (!) coordinate system  import os, arcpy  def shiftPointInDirection(x, y, direction, shiftDistance):     """Return new (x, y) as shifted x, y in direction (dx, dy) by distance shiftDistance"""     d = (direction[0]**2 + direction[1]**2)**(0.5)     q = shiftDistance / d     x =  x + q * direction[0]     y = y + q * direction[1]     return (x, y)  # create a new polyline feature class and add field(s) out_fc = arcpy.CreateFeatureClass_management(os.path.dirname(out_fc), os.path.basename(out_fc), "POLYLINE", spatial_reference = arcpy.Describe(in_fc).spatialReference).getOutput(0) arcpy.AddField_management(out_fc, "ORIGID", "LONG")  # starting an edit session may not be necessary, but to stay on the safe side... with arcpy.da.Editor(workspace) as edit:      with arcpy.da.SearchCursor(in_fc, ["SHAPE@", "OID@"]) as sc:      with arcpy.da.InsertCursor(out_fc, ["SHAPE@", "ORIGID"]) as ic:       for row in sc:                  # if your geometries are simple 2D singlepart lines, something like this                 # should get the actual line vertices of a feature (sorry don't have Arc now to test)                 # search Reading Geometries and Writing Geometries in help        shp = row[0].getPart(0)                  # handle individual vertices                 p0 = shp.firstPoint                 p2 = shp.lastPoint                 x1,y1 = p0.X, p0.Y                 shift_dir = (p2.X - p0.X, p2.Y - p0.Y)                 x1,y1 = shiftPointInDirection(x1, y1, shift_dir, shift_by)                 p1 = arcpy.Point(x1, y1)                  # create new feature and store it                 new_geometry = arcpy.Polyline(arcpy.Array([p0, p1, p2]))                 new_row = [new_geometry, row[1]]                 ic.insertRow(newrow)  # then join attributes from in_fc to out_fc on in_fc.OBJECTID = out_fc.ORIGID
0 Kudos
deleted-user-MS0lE1F_0-sy
Deactivated User
Thank you Filip, this looks great.  I will test it out as soon as I get the chance.  Thank you for your help, I will let you know if it works out.  Matt

I had to tweak it a little.  Three things to note:  arcpy.CreateFeatureclass - lower case c @ (C)lass, had to drop getValue(0) from shp to get it to work(?), and had to define the firstPoint/lastPoint before using that to define p0/p2(not sure that was necessary).


Thanks again!!


import os, arcpy

in_fc = arcpy.GetParameterAsText(0)
shift_by = 2.0 # shift distance in linear units of your PROJECTED (!) coordinate system

def shiftPointInDirection(x, y, direction, shiftDistance):
    """Return new (x, y) as shifted x, y in direction (dx, dy) by distance shiftDistance"""
    d = (direction[0]**2 + direction[1]**2)**(0.5)
    q = shiftDistance / d
    x =  x + q * direction[0]
    y = y + q * direction[1]
    return (x, y)

# create a new polyline feature class and add field(s)
out_fc = arcpy.CreateFeatureclass_management("Y:/GIS/Working/Test", "test.shp", "POLYLINE", spatial_reference = arcpy.Describe(in_fc).spatialReference).getOutput(0)
arcpy.AddField_management(out_fc, "ORIGID", "LONG")

# starting an edit session may not be necessary, but to stay on the safe side...
workspace = os.path.dirname(in_fc)
with arcpy.da.Editor(workspace) as edit:

    with arcpy.da.SearchCursor(in_fc, ["SHAPE@", "OID@"]) as sc:
     with arcpy.da.InsertCursor(out_fc, ["SHAPE@", "ORIGID"]) as ic:
      for row in sc:
# if your geometries are simple 2D singlepart lines, something like this
# should get the actual line vertices of a feature (sorry don't have Arc now to test)
# search Reading Geometries and Writing Geometries in help
       shp = row[0]
       firstPoint = shp.firstPoint
       lastPoint = shp.lastPoint
# handle individual vertices
       p0 = firstPoint
       p2 = lastPoint
       x1,y1 = p0.X, p0.Y
       shift_dir = (p2.X - p0.X, p2.Y - p0.Y)
       x1,y1 = shiftPointInDirection(x1, y1, shift_dir, shift_by)
       p1 = arcpy.Point(x1, y1)
# create new feature and store it
       new_geometry = arcpy.Polyline(arcpy.Array([p0, p1, p2]))
       new_row = [new_geometry, row[1]]
       ic.insertRow(new_row)
0 Kudos