# Create parallel lines within polygon

How do I create parallel lines to the end of the polygon and then clip them with arcpy?

With the script I'm currently using, the lines are not created correctly.

The script I am using is this

``````import arcpy, math

offset = 3
offset_original = offset
features=[]

arcpy.env.overwriteOutput = True

infc="linha"
talhao = "poligono"

#arcpy.Intersect_analysis([infc, talhao], "AB_Intersect", "ALL", "", "INPUT")

spatialRef = arcpy.Describe(infc).spatialReference

def CopyParallel(plyP,sLength):
part=plyP.getPart(0)
lArray=arcpy.Array();rArray=arcpy.Array()
for ptX in part:
dL=plyP.measureOnLine(ptX)
ptX0=plyP.positionAlongLine (dL-0.01).firstPoint
ptX1=plyP.positionAlongLine (dL+0.01).firstPoint
dX=float(ptX1.X)-float(ptX0.X)
dY=float(ptX1.Y)-float(ptX0.Y)
lenV=math.hypot(dX,dY)
sX=(-dY*sLength/lenV);sY=dX*sLength/lenV
leftP=arcpy.Point(ptX.X+sX,ptX.Y+sY)
rightP=arcpy.Point(ptX.X-sX, ptX.Y-sY)
array = arcpy.Array([lArray])
section=arcpy.Polyline(array, spatialRef)
return section

with arcpy.da.UpdateCursor(infc,["SHAPE@"]) as cursor:
for shp in cursor:
i=0
while i <= 200:
if (i == 0):
twoLines=CopyParallel(shp[0],offset)
else:
twoLines=CopyParallel(features[-1],offset)
features.append(twoLines)
i=i+1

arcpy.CopyFeatures_management(features, "teste")
``````

Error

• "there comes a time when the lines start going wrong" - Please edit your question and tell us what's going wrong. You'll get better answers if we don't have to guess. Include a snippet of the code you're trying, and details of what happens when you run it including any error messages
– Midavalo
Mar 8, 2017 at 19:35
• @FilipeFontes You have added your code, but still haven't told us what is going wrong. What do you need help with?
– Midavalo
Mar 8, 2017 at 20:42
• @Midavalo give me a favour, reopen it, to end this never ending debacle with my script. I'll post cleaned up version of whatever he is trying to achieve. Mar 9, 2017 at 2:25
• Edited with error Mar 9, 2017 at 10:21

If this:

IS truly what you are trying to achieve, use cleaned up script below.

NOTE

• I have no room for 200 parallel lines, make change to the code accordingly
• this is a good idea when borrowing someone else script to make an efforts to understand it’s logic.
• If the script is not working a) explode your lines to single parts, b) repair geometries and c)read notes to this post again.

SCRIPT:

``````import arcpy, math
offset = 3
arcpy.env.overwriteOutput = True
infc="linha"
spatialRef = arcpy.Describe(infc).spatialReference
def CopyParallel(plyP,sLength):
part=plyP.getPart(0)
lArray=arcpy.Array();rArray=arcpy.Array()
for ptX in part:
dL=plyP.measureOnLine(ptX)
ptX0=plyP.positionAlongLine (dL-0.01).firstPoint
ptX1=plyP.positionAlongLine (dL+0.01).firstPoint
dX=float(ptX1.X)-float(ptX0.X)
dY=float(ptX1.Y)-float(ptX0.Y)
lenV=math.hypot(dX,dY)
sX=(-dY*sLength/lenV);sY=dX*sLength/lenV
leftP=arcpy.Point(ptX.X+sX,ptX.Y+sY)
section=arcpy.Polyline(lArray, spatialRef)
return section

curT = arcpy.da.InsertCursor("teste","Shape@")
with arcpy.da.SearchCursor(infc,["SHAPE@"]) as cursor:
for shp in cursor:
i=0
while i <= 3:
if (i == 0):previous=CopyParallel(shp[0],offset)
else: previous=CopyParallel(previous,offset)
curT.insertRow((previous,))
i+=1
``````
• I used your script, but after a while this happens with the lines imgur.com/a/E3MFv Mar 9, 2017 at 11:33