How to create an offset polyline in arcpy?
We came up with this technique at work that uses geoprocessing tools to do the job:
First off, make sure your line segs have unique IDs.
- Buffer "FLAT" in each direction of the line, add a new field of the same name to each and give them a direction ("L","R").
- Merge the buffers together.
- Buffer the original line again, this time "FULL".
- Convert the FULL buffer to lines (PolygonToLine: "IGNORE_NEIGHBORS"), then convert its vertices to points (FeatureVerticesToPoints: "BOTH_ENDS").
- Buffer each point by a very small width (e.g. 0.1 m).
- Split the lines by vertex (SplitLine).
- Select by location to select all lines that intersect with the point buffers.
- Delete these lines.
- Dissolve ("SINGLE_PART") on the segment unique ID.
- Spatial Join ("SHARE_A_LINE_SEGMENT_WITH") the new lines to the merged "LEFT" and "RIGHT" buffers to attribute them with direction.
Voila! Offset lines without ArcObjects.
Here is the code to do the same in arcpy
import arcpy
tmpwrkspace = "C:/tempgdb/temp.gdb/"
fc_line = tmpwrkspace + "MultiLineLayer"
# Set some variables
tempFeatureClass = tmpwrkspace + "temp_pline"
#set the offset to +n or -n for direction
xOffset = 0.2
yOffset = 0.2
if arcpy.Exists(tempFeatureClass):
arcpy.Delete_management(tempFeatureClass)
arcpy.CopyFeatures_management(fc_line, tempFeatureClass)
# Perform the move
with arcpy.da.UpdateCursor(tempFeatureClass, ["SHAPE@XY"]) as cursor:
for row in cursor:
cursor.updateRow([[row[0][0] + xOffset, row[0][1] + yOffset]])