Tuesday, 24 February 2015

arcpy - Create a line perpendicular to an existing line in ArcGIS


For an existing set of line features, I would like to generate a perpendicular line, of say 1km, at the midpoint of each existing line feature in ArcGIS.


I imagine that The methodology would be to:



  1. create a tangent line at the midpoint of each existing line feature

  2. calculate the angle of the tangent line

  3. based on the angle of the tangent line, calculate the location of the endpoint of the new line

  4. create the new line with the calculated endpoint of the new line and the existing midpoint of the old line



Let's assume that we are in a UTM projection and can use coordinates to calculate distance and direction.


How would I do this with ArcGIS tools or with ArcPy?



Answer




  • Create midpoints, using one of multiple possible techniques.

  • Buffer them by small number, e.g. 0.5 m

  • Clip originals by buffer, output - SHAPEFILE


Use this field calculator expression on field Shape:


def RotateExtend(plyP,sLength):

l=plyP.length
ptX=plyP.positionAlongLine (l/2).firstPoint
ptX0=plyP.firstPoint
ptX1=plyP.lastPoint
dX=float(ptX1.X)-float(ptX0.X)
dY=float(ptX1.Y)-float(ptX0.Y)
lenV=math.sqrt(dX*dX+dY*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([leftP,rightP])
section=arcpy.Polyline(array)
return section


RotateExtend( !Shape!, 1000)

UPDATE:


Ooops! it creates perpendicular, not tangent. Can be easily modified


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...