Can't go into detail about what these features actually are but I will just refer to them as the green line and the black line. The black lines are not able to ever overlap the blue polygons. Black lines have a specific orientation depending on which side the closest green line is.

enter image description here

What I'm trying to do is create a script which would be able to determine if the closest green line is either right or left to the black line, then determine if the black line is oriented correctly or not based on the rules I drew out within the attached image. If it isn't then it would flip the black line.

I've used the FeatureVerticesToPoints_management tool and SplitLine_management tool to split both the polylines into individual parts and retrieve midpoint X and Y coordinates as well as individual vertex X and Y coordinates of both lines.

enter image description here

I've been using the formula (Bx - Ax) * (Cy - Ay) - (By - Ay) * (Cx - Ax) where A and B are line vertices of the green line and C is the midpoint vertex of the black line. This has created a positive or negative value signifying which side point C is on compared to the line but I do not know how to setup rules for how the black line should be orientated afterwards. I believe the answer lies within checking the black line's azimuth.

My original idea was that using that formula I could determine which side the black line was on, then check if the azimuth of the black line is within 180deg of the the azimuth of the green line, and if it wasn't, it would flip the black line. But since the green line can be orientated in it's opposite direction, and be acceptable, the 180deg rule turns into a 360deg rule and the black line can then go any direction it wants.

I've been calculating the azimuth of each line using:

def CalculaAzimuth(linea):
if (hasattr(linea,'type') and linea.type == 'polyline'):
    xf = linea.firstPoint.X
    yf = linea.firstPoint.Y
    xl = linea.lastPoint.X
    yl = linea.lastPoint.Y
    dX = xl - xf
    dY = yl - yf
    PI = math.pi
    Azimuth = 0 #Default case, dX = 0 and dY >= 0
    if dX > 0:
        Azimuth = 90 - math.atan( dY / dX ) * 180 / PI
    elif dX < 0:
        Azimuth = 270 - math.atan( dY / dX )* 180 / PI
    elif dY < 0:
        Azimuth = 180
    return Azimuth
    return False

2 Answers 2


Hopefully I understood correctly what you are asking, here is a method to determine if a point is on the left or right of a vector.

Given the a triangle created from the first and second point on the black line and the nearest point on the green line.

enter image description here

Determining if this is Clockwise or Anti Clockwise will indicate if the nearest point is on the left or the right side of the initial vector.

Calculating the determinent where x1,y1 = Point 1; x2,y2 = Point 2 and x3,y3 = Nearest Point.

The positive value for (x2 * y3 + x1 * y2 + x3 * y1) - (x2 * y1 + x1 * y3 + x3 * y2) indicates left hand side whereas negative is the right hand side.

So if this returns a positive value you will need to reverse your line.

  • Thank you! I've been testing it out and it seems to be working every time, I'll post if I run into any issues
    – Jaytee
    Feb 8, 2015 at 21:51
  • @Jaytee There may instances where it will not work. For example if the near point is on the left but the vector is mostly on the right. It may be worthwhile doing a double check to make sure point 1 is to the left of nearest vector.
    – MickyT
    Feb 8, 2015 at 22:05

Depending on position of line segments this process might help:

  1. Create polygon using end points and existing order of points:

enter image description here

  1. Create polygon using end points - existing order of points for black line, flipped green line: enter image description here
  2. Compare 2 polygons' areas. They are shown as red labels on the pictures. Note that it is smaller for 'same' direction segments. You might as well compare parts count of resulting shapes.
  • This is an interesting idea and i'm starting to see how it may work, my only concern is that the green line's vertices could be flipped and still be valid no matter which end the endpoint is on, so i'm not sure how I would be able to designate the connections of the polygon in that case.
    – Jaytee
    Feb 8, 2015 at 21:01
  • It should work unless they are perpendicular. When creating polygons use try, except block, because creating multipart polygon using just 5 points might result in error. Meaning of error - 'same' direction lines.
    – FelixIP
    Feb 8, 2015 at 21:53

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.