Solved! Go to Solution.
import arcpy fc = r'c:\yourFGDB.gdb\yourLines' x_shift = 5 y_shift = 5 # update cursor (be careful with this and try it on a copy of your data) fields = ["SHAPE@"] with arcpy.da.UpdateCursor(fc,fields) as curs: for row in curs: geom = row[0] array = arcpy.Array() for part in geom: lastpnt = part[len(part) - 1] lastpnt.X += x_shift lastpnt.Y += y_shift part[len(part) - 1] = lastpnt array.add(part) curs.updateRow([arcpy.Polyline(array)])
Hi All,
I have a feature class made up of lines. Each of these lines are loops. Therefore they have the same beginning and ending xy coordinates. I am looking to move the end vertices for each line a known value away to disconnect each loop. I understand that through editor I can manually change the end vertices for each loop in sketch properties but I am looking for an automated way to move the last/end vertices of each line in my feature class the same known distance.
Example: I move each line's end vertice 5 meters in the x direction and 5 meters in the y direction.
Any guidance would be greatly appreciated.
Thanks!
If you move the end point of the line it most likely will still be a closed polyline.
You will need to manually split them to break them up.
You could also try the explode function and see if that will get you anywhere.
Here is an off approach fix.
use calculate geometry to calculate the X,Y of the end points of each line.
Then take those x,y locations and create point features from them. BUFFER those point features into a polygon layer with a 1 inch buffer. Select all those buffer polygons and clip the polyline layer.
Now of you have some that are NOT closed you can calculate beginning and ending x,y and and then only create the points on those features that have identical fromx, fromy and tox toy
Sound arduous but the whole thing should take a couple of minutes. '
I would recommend experimenting on a copy first.
import arcpy fc = r'c:\yourFGDB.gdb\yourLines' x_shift = 5 y_shift = 5 # update cursor (be careful with this and try it on a copy of your data) fields = ["SHAPE@"] with arcpy.da.UpdateCursor(fc,fields) as curs: for row in curs: geom = row[0] array = arcpy.Array() for part in geom: lastpnt = part[len(part) - 1] lastpnt.X += x_shift lastpnt.Y += y_shift part[len(part) - 1] = lastpnt array.add(part) curs.updateRow([arcpy.Polyline(array)])
Hi David,
Try the code below on a copy of your data and see if this is what you want.import arcpy fc = r'c:\yourFGDB.gdb\yourLines' x_shift = 5 y_shift = 5 # update cursor (be careful with this and try it on a copy of your data) fields = ["SHAPE@"] with arcpy.da.UpdateCursor(fc,fields) as curs: for row in curs: geom = row[0] array = arcpy.Array() for part in geom: lastpnt = part[len(part) - 1] lastpnt.X += x_shift lastpnt.Y += y_shift part[len(part) - 1] = lastpnt array.add(part) curs.updateRow([arcpy.Polyline(array)])
Kind regards,
Xander
import arcpy fc = r'c:\yourFGDB.gdb\yourLines' # update cursor (be careful with this and try it on a copy of your data) fields = ["SHAPE@","XCentroid","YCentroid"] with arcpy.da.UpdateCursor(fc,fields) as curs: for row in curs: geom = row[0] x_shift = row[1] y_shift = row[2] array = arcpy.Array() for part in geom: lastpnt = part[len(part) - 1] lastpnt.X = x_shift lastpnt.Y = y_shift part[len(part) - 1] = lastpnt array.add(part) curs.updateRow([arcpy.Polyline(array)])
import arcpy fc = r'c:\yourFGDB.gdb\yourLines' # update cursor (be careful with this and try it on a copy of your data) fields = ["SHAPE@","XCentroid","YCentroid"] with arcpy.da.UpdateCursor(fc,fields) as curs: for row in curs: geom = row[0] x_new = row[1] y_new = row[2] array = arcpy.Array() for part in geom: lastpnt = part[len(part) - 1] lastpnt.X = x_new lastpnt.Y = y_new part[len(part) - 1] = lastpnt array.add(part) curs.updateRow([arcpy.Polyline(array), x_new, y_new])
Hi David,
You're nearly there...
I guess the problem is that the row you write should have the same dimensions as the row you read. I renamed the x_shift and y_shift variables to x_new and y_new for readability. I didn't test this, but if I think this could work.import arcpy fc = r'c:\yourFGDB.gdb\yourLines' # update cursor (be careful with this and try it on a copy of your data) fields = ["SHAPE@","XCentroid","YCentroid"] with arcpy.da.UpdateCursor(fc,fields) as curs: for row in curs: geom = row[0] x_new = row[1] y_new = row[2] array = arcpy.Array() for part in geom: lastpnt = part[len(part) - 1] lastpnt.X = x_new lastpnt.Y = y_new part[len(part) - 1] = lastpnt array.add(part) curs.updateRow([arcpy.Polyline(array), x_new, y_new])
Kind regards,
Xander
import arcpy fc = r'c:\yourFGDB.gdb\yourLines' # update cursor (be careful with this and try it on a copy of your data) fields = ["SHAPE@","XCentroid","YCentroid"] with arcpy.da.UpdateCursor(fc,fields) as curs: for row in curs: geom = row[0] x_new = row[1] y_new = row[2] array = arcpy.Array() for part in geom: firstpnt = part[len(part) + 0] firstpnt.X = x_new firstpnt.Y = y_new part[len(part) - 1] = firstpnt array.add(part) curs.updateRow([arcpy.Polyline(array), x_new, y_new])