Get/Extract/Determine the DIRECTION of a line

2967
7
09-28-2011 08:17 AM
by Anonymous User
Not applicable
Original User: DeSchlong

We all know that simple polylines, when digitized, are given a DIRECTION.  This is why the function "Flip Lines" exists in ArcToolbox.  However, I would like to select all the lines of a certain direction (left to right, or Clockwise/Counter-Clockwise, for example, or however they are described), and FLIP only these lines.  How do I do this?

The purpose of this is to add line decorations which need to point a certain way - in this case I have depression contours symbolized with tick marks that need to point inward but the direction of the tick marks is dependent on the direction the line was digitized originally.
0 Kudos
7 Replies
by Anonymous User
Not applicable
Original User: DeSchlong

Hi everyone. There is really no ready-made answer for this problem, so I worked through it myself. 

To first understand the nature of the problem, read and understand this discussion  on determining the direction (CW/CCW) of a vector (this is the key to  the whole exercise). Practice some sample calculations in Excel to  verify for yourself that it works. Now that you've got the calculus  under your belt, the remaining tasks in ArcGIS are tedious but fairly  trivial.


  1. Download and install ET GeoWizards, and convert your lines to vertexes using the default settings.

  2. Add the fields, "X_DIFF", "Y_ADD" and "XY_MULT_DIR" as a 'Double' to the point file.

  3. Adapt this  very helpful forums post about how to obtain values from the next line and subtract/add them from/to the first, using Python scripts.

  4. Calculate values for the "XY_MULT_DIR" field.  Then, set all  values of "XY_MULT_DIR" that are calculated between separate polylines  to '0'. In my case, this is where "ET_ORDER" is also '0'.

  5. Calcluate summary stats on the point file, calculating SUM for the "XY_MULT_DIR" column on each unique polyline ID.

  6. Join the summary stats table to the polyline layer, then select  the lines going in the "wrong" direction (+ or -), and use Flip Line function on  these.

 
I've built a 2-step model to determine the line direction as above then  flip those going the wrong way. It might be possible to package it into a  tool some day but until then if you want to see what I did you should  just send me a message.

Good luck! :cool:
0 Kudos
DanLee
by Esri Regular Contributor
Esri Regular Contributor
Hi DeSchlong,

I am glad you have solved the problem. To make future use of this tool easier, I have proposed an enhancement for this tool to include a new parameter, Flow Direction, with the following choices:

NONE      - the default, flips all input lines, as the tool does currently.
DOWN_Z  - flips a line only if its starting Z < ending Z, so that the line runs down-hill.
UP_Z       - flips a line only if its starting Z > ending Z , so that the line runs up-hill.
DOWN_M  - flips a line only if its starting M < ending M.
UP_M       - flips a line only if its starting M > ending M.

You have a new case, so perhaps we can include the CW and CCW choices as well. These choices should only apply to closed lines (contours), correct? Do you or other users have additional thoughts on this?

Thanks for your input.
0 Kudos
by Anonymous User
Not applicable
Original User: DeSchlong

Glad you asked! I sure do have more thoughts:



  1. The process of splitting a line into its vertex point components is not a native function in ArcGIS - as far as I can find - even though it probably should be. Seems like a basic Geoprocessing function that should make up a core component of a GIS.

  2. Splitting up the line into its vertices is a key component to determining direction, which is, as I described, a multi-step and non-trivial process to determine.  To determine the CW or CCW direction of a line would require some pre-processing in a Flip Line tool, but yeah - as I sit here and think about it, it could definitely be implemented in such a way that you could just tick a box to flip only these lines. There would just be a lot of behind-the-scenes activity to determine all the correct lines to flip.

  3. There should be a more helpful tool to *show* line direction.  Adding line decorations or arrow ends is kludgy and doesn't actually show if closed contour lines are CCW or CW. (To do this, I labeled the vertex points of the line with its distance from origin - increasing values indicates direction.)

  4. This process applies to ANY line, NOT just closed contours.

  5. However, my implementation of the maths (as described in link above) is not perfect, but good enough for my purposes.  Essentially, I omit the calculation of direction for the last segment of line between the two last vertices. (Refer to step where I set values to '0'). So, I cheated.  Not a big deal for lines that have >>3 segments, nor (I believe) for closed loops like Depression isolines. Maybe not a big deal for non-closed lines either, but definitely look into this.  In fact - I might be totally off-base with this line of thought that it's a problem, but have a look into it anyway.

  6. Determining direction *may* be difficult for complex lines such as figure-8s.  I didn't test this, but process should still get pretty close estimation - but note that it is not a guarantee.  I'd put this caveat in the help file. 🙂

  7. Not tested with multi-part features.

0 Kudos
DanLee
by Esri Regular Contributor
Esri Regular Contributor
Glad you answered!  🙂

Here are some helpful tools:

Feature Vertices To Points - you have options to get start/end/both_ends/mid/dangle/all vertices from line or polygon.
Add XY Coordinates - it addes the xy values to new fields.
Split Line At Vertices - it gives you the straight lines between consecutive vertices.

About CW/CCW options, it is not a problem for us to determine the two directions for closed lines, but I am still not sure about applying it to non-closed lines. So the enhancement may have limitations, unless the rules can be clearly defined for implementation.

Thanks for your thoughts!
0 Kudos
by Anonymous User
Not applicable
Original User: DeSchlong

Ooh - thanks! There those tools are! Would have made my life easier, probably. 🙂  I can improve automation of the model now, too.  Funny - you Google and search before settling in on the best method from the results you get ... on a different day you can use different key words and get different results.  I was probably searching for "polyline to points" and was directed to ET Geowizards.

If you check the math link I posted, you'll see that it can apply to closed and non-closed lines equally (doesn't matter).  Importantly, note that you will need to check if it can be applied to lines with only 2 points - perhaps you would need to use a different method to determine direction for a simple line like this - this is pretty easy though.
0 Kudos
DanLee
by Esri Regular Contributor
Esri Regular Contributor
Yeah, if you are doing "feature" related operations, the tools in Features toolset can be very helpful.

Thanks for the math link. The math might produce a value for any line. The question is: do the values reflect the reality of non-closed lines. I will look into this.  🙂
0 Kudos
by Anonymous User
Not applicable
Original User: DeSchlong

Update:

I have just processed a set of ~23,000 depression lines using the steps I detailed above, covering the province of Saskatchewan, of which ~6600 were located and "flipped".  A random inspection of about 50 lines shows a 100% success rate of no false positives.  This includes both completely closed and partial lines that are clipped at borders (due to the tiled nature of the data structure). 

Dataset used was the 1:50K CanVec.
0 Kudos