2

enter image description here

I have reviewed and tried multiple different answers to similar questions but have not got the results expected. I have a layer with 4425 polyline features and want to make a perpendicular line feature for every existing line that is in a specific layer that will go from the centerpoint of the current line features and intersect through other line features. The black arrow is an example of the output, red is the layer I am getting the midpoint from, and blue is the layer I want the perpendicular lines to intersect. My current code is:

cursor = arcpy.SearchCursor("SpatialJoinLayer")
features = []
distance = 10
for row in cursor:
   #Get midpoint of line feature
   midpoint = row.shape.positionAlongLine(0.5, True).firstPoint
   angle = row.Angle
   startX = midpoint.X
   startY = midpoint.Y
   #Formula that I believe is wrong but cannot figure out where the problem is
   endX = distance * math.cos(angle) + startX
   endY = distance * math.sin(angle) + startY
   arr = arcpy.Array([arcpy.Point(startX, startY), arcpy.Point(endX, endY)])
   polyline = arcpy.Polyline(arr)
   features.append(polyline)
#Add a feature class and add the newly created lines to it
arcpy.management.CreateFeatureclass(arcpy.env.workspace, "PerpLines")
arcpy.management.CopyFeatures(features, "PerpLines")
7
  • There is an ArcGIS 10 toolbox (and the accompanying set of scripts) to create perpendicular lines here. github.com/ShuksanGeomatics/Create_Perpendicular_Lines_ArcGIS. Maybe this is a good start for your task.
    – GBG
    Commented Sep 2, 2022 at 15:42
  • I am struggling to implement some of that code into my project. I have most of the values that I need already, I am trying to find where the problem is in my code. Do you have any ideas on what it could be?
    – Josh
    Commented Sep 2, 2022 at 15:55
  • 1
    Please Edit the Question to specify the coordinate system in use (if it's a geographic CS, you need to use a different methodology).
    – Vince
    Commented Sep 2, 2022 at 17:15
  • 2
    Also note that using the arcpy.Polyline constructor without a spatial_reference parameter is an anti-pattern that can corrupt your geometry coordinates.
    – Vince
    Commented Sep 2, 2022 at 18:37
  • 1
    gis.stackexchange.com/questions/201867/…
    – FelixIP
    Commented Sep 2, 2022 at 19:24

1 Answer 1

1

The error is just a geometry issue: you need to modify it as follows:

...
# need 90deg in order to find perpendicular
pi_by_2 = math.pi / 2.0

# now add that value to the row's angle
angle = row.angle + pi_by_2
...

Also, as commented by another user, please add an SRID to the geometry when using the arcpy.Polyline geometry factory to create the feature!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.