0

How do I determine if a varying hypothetical set of lines (example in image) needs to be sorted by X (longitude) rather than Y (latitude) programmatically to ensure they are ordered from 1 to 14 as described by the arrow?

The example set of lines is only an example and can occur in an number of different orientations and permutations.

I will always know which line needs to be 1 for each set of lines, but the rest of the lines need to be ordered in proper sequence.

UPDATE_05_18_2017: To clarify this post a bit if anyone comes across it in the future.

The problem:

  1. Lines can come into the program in any arrangement;
  2. Due to digitizing when creating these lines the (OBJECTID, OID, FID whatever) will also get scrambled;
  3. A method was needed to order these lines;
  4. The Sort tool, an SQL "ORDER BY" statement with a da.UpdateCursor, etc. would sort the records, but since the lines can come in any orientation, to my knowledge there is no way to achieve an accurate sort.

My work-around was to educate the editors so that while editing they maintained some logical OID order. With an ordered OID field I could then query and loop though the data reliably as desired.

Problem

I thought to calculate average bearings, and sort on their basis since lines will follow a defined orientation, but the lines shown in the example are 118.9degrees which would lend them to be sorted by Y:

    if 45 <= avgGbearing <=135 or 225 <= avgGbearing <= 315:

using the .firstPoint(shown), .midPoint or .lastPoint coordinate would not sort the lines 1 to 14 appropriately:

enter image description here

Compass Bearing

The data is stored in a Microsoft (JET) Access personal geodatabase. I am using ArcGIS Desktop version 10.1 ArcInfo License.

3
  • This question is in context of a larger script. I have isolated and targeted this question to get more focused answers. As a bit of background here is a related post: gis.stackexchange.com/questions/238153/…
    – pyRsq
    May 5, 2017 at 19:11
  • To properly iterate over them in correct order when assigning a subsequent ID as described in the attached link above. The example set of lines is only an example and can occur in an number of different orientations and permutations.
    – pyRsq
    May 5, 2017 at 19:18
  • 1
    A question asking for help writing code should include a snippet of the code you've actually tried, and details of what happens when you try it. Please edit your question to include your code snippet and details. Also include the extra information you've added in comments.
    – Midavalo
    May 6, 2017 at 1:27

1 Answer 1

1

If you want to sort the features by X coordinate, you can use the Sort tool either by python script as shown below or manually by adding a field, calculating and sorting. This should work if your data looks like your screenshot:

Code:

import arcpy

fc=r'C:\test.gdb\polylines'
outfc=r'C:\test.gdb\polylines_sortedByX'

#Add a field to store X coordinates of each polyline centroid
arcpy.AddField_management(in_table=fc, field_name='Xsort', field_type='DOUBLE')

#Populate the field with X coordinates
with arcpy.da.UpdateCursor(fc,['Xsort','SHAPE@X']) as cursor:
    for row in cursor:
        row[0]=row[1]
        cursor.updateRow(row)

#Sort by the calculated X coordinates and output a new feature class
arcpy.Sort_management(in_dataset=fc, out_dataset=outfc, sort_field='Xsort')
5
  • Thanks Bera, but the issue is not so much with how to sort. This issue is more how to determine if I should sort by X or Y to properly order the lines from 1 to 14 (in the example). Sorting by X would certainly accomplish the task, but I need some way to programmatically determine this.
    – pyRsq
    May 5, 2017 at 18:47
  • Unfortunately I won't know because the intention is to keep this behind the scenes and performed programmatically, but I'm not sure if that is possible. I'm looking for suggestions on ways to accomplish this. You're right though it may not be possible...
    – pyRsq
    May 5, 2017 at 18:56
  • 1
    @pyRsq How will you know if you need to sort or not? What about it requires sorting or not sorting? I'm talking about the logic of your problem, not the actual coding. How do you define the requirement for the features to be sorted? You need to put all that into your question.
    – Midavalo
    May 6, 2017 at 1:29
  • I'm truly sorry Midavalo, but that isn't entirely clear to me either. That's what trying i'm trying to sort out. The input polyline feature class can come in theoretically any form. I'm realizing that a simple sort by X, or Y won't appropriately sort the lines. I need a method to sort lines which don't have a clear order to them. I'm thinking about constructing a line down the polyline centroids then identifying lines as they intersect the drawn line, but i'm not entirely sure how to implement it yet.
    – pyRsq
    May 8, 2017 at 21:57
  • To clarify this post a bit if anyone comes across it in the future. The issue is A) Lines can come into the program in any arrangement; B) Due to digitizing when creating these lines the (OBJECTID, OID, FID whatever) will also get scrambled; C) A method was needed to order these lines; D) The Sort tool, an SQL "ORDER BY" statement with an UpdateCursor would sort the records, but since the lines can come in any orientation there is no way to achieve an accurate sort. My work-around was to educate the editors so that while editing they maintained some logical OID order.
    – pyRsq
    May 18, 2017 at 3:58

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