<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Issues With .sort()/lambda to Spatially Sort Polygon Features in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/issues-with-sort-lambda-to-spatially-sort-polygon/m-p/1131852#M63452</link>
    <description>&lt;P&gt;This appears to be it! I had checked this, but only out to about 6 decimal places, so I was not handling float precision appropriately.&lt;/P&gt;&lt;P&gt;I changed the sorting function to round both the X and the Y values, to avoid any issues with tiny differences in position:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;all_sheet_polys.sort(key=lambda poly: (dir1 * round(getattr(poly.centroid, axis1), 2),
                                       dir2 * round(getattr(poly.centroid, axis2), 2)))&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here are the "actual" coordinate values of the bottom row (X-values truncated for display), shown in the following image. When those values are sorted (using raw SN-WE coordinate values), the ordering was somewhat ambiguous along a given row because of your point about precision.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;1	X 11,768,434  Y 7,022,867.011901856400
2	X 11,767,927  Y 7,022,867.011901855469
3	X 11,767,421  Y 7,022,867.011901854537
4	X 11,766,914  Y 7,022,867.011901856400
5	X 11,766,407  Y 7,022,867.011901855469
6	X 11,765,901  Y 7,022,867.011901854537
7	X 11,765,394  Y 7,022,867.011901856400
8	X 11,764,887  Y 7,022,867.011901855469
9	X 11,764,381  Y 7,022,867.011901854537
10	X 11,763,874  Y 7,022,867.011901856400
11	X 11,763,367  Y 7,022,867.011901855469&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; After the rounding, problem solved (example of bottom row):&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Sort_With_Round.png" style="width: 647px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/31070i043F8D5E72AF0154/image-dimensions/647x55?v=v2" width="647" height="55" role="button" title="Sort_With_Round.png" alt="Sort_With_Round.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Thanks a ton, I appreciate the assistance.&lt;/P&gt;</description>
    <pubDate>Sat, 08 Jan 2022 20:32:54 GMT</pubDate>
    <dc:creator>VinceE</dc:creator>
    <dc:date>2022-01-08T20:32:54Z</dc:date>
    <item>
      <title>Issues With .sort()/lambda to Spatially Sort Polygon Features</title>
      <link>https://community.esri.com/t5/python-questions/issues-with-sort-lambda-to-spatially-sort-polygon/m-p/1130894#M63404</link>
      <description>&lt;P&gt;I am attempting to use Python's .sort() with a lambda function to spatially sort an existing list of Polygon objects, to then write to a feature class with an InsertCursor. Sorting will be based on user-supplied specifications (north to south first, followed by east to west as a secondary sort, for example). This snippet is part of a larger script that has been pared down so it's easier to look at (the list of polygons and the output feature class are created at an earlier stage, not shown here).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# User supplied sort priorities.
sort_1 = 'EW'
sort_2 = 'SN'

# Example: for N to S sorting, sort polygon using centroid.Y attribute, descending.
sort_dict = {"NS": (-1, 'Y'),
             "SN": ( 1, 'Y'),
             "WE": ( 1, 'X'),
             "EW": (-1, 'X'),}

# Getting the direction and axes (primary and secondary) to sort by.
dir1, axis1 = sort_dict[sort_1]
dir2, axis2 = sort_dict[sort_2]

# An existing list of polygon objects, sorted using specifications above.
# "dir" handles ASC/DSC, getattr() gets the appropriate x or y value from the
# centroid.
all_sheet_polys.sort(key=lambda poly: (dir1 * getattr(poly.centroid, axis1),
                                       dir2 * getattr(poly.centroid, axis2)))

# Write sorted polygons to an existing blank feature class.
with ap.da.InsertCursor(out_fc, ['SHAPE@', 'PageNumber']) as icurs:
    sheet_count = 1
    for sheet in all_sheet_polys:
        icurs.insertRow([sheet, sheet_count])
        sheet_count += 1&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;The problem is that the two-part lambda sort does not always work. If the FIRST sorting is along the x-axis (EW/WE), it will. The first image is east to west, then south to north. The second is west to east, then north to south. Both are created as expected.&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="East to West, then South to North." style="width: 791px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/30844iD7A578FE5B110CC8/image-size/large?v=v2&amp;amp;px=999" role="button" title="EWSN.png" alt="East to West, then South to North." /&gt;&lt;span class="lia-inline-image-caption" onclick="event.preventDefault();"&gt;East to West, then South to North.&lt;/span&gt;&lt;/span&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="West to East, then North to South." style="width: 782px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/30845i249549D9CECD967A/image-size/large?v=v2&amp;amp;px=999" role="button" title="WENS.png" alt="West to East, then North to South." /&gt;&lt;span class="lia-inline-image-caption" onclick="event.preventDefault();"&gt;West to East, then North to South.&lt;/span&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;The problem occurs when x-axis sorting is done as a SECONDARY priority. Here is an example using inputs "SN" and "WE" as inputs, for south to north followed by west to east. The primary sort, south to north, works fine. However, the west to east does not, and the ordering appears arbitrary.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="South to North, failed sorting West to East." style="width: 790px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/30847iDDF886FE23BDD694/image-size/large?v=v2&amp;amp;px=999" role="button" title="SNWE.png" alt="South to North, failed sorting West to East." /&gt;&lt;span class="lia-inline-image-caption" onclick="event.preventDefault();"&gt;South to North, failed sorting West to East.&lt;/span&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt; Any ideas would be appreciated. Seems like something silly I am overlooking, but it has got me stumped.&lt;/P&gt;</description>
      <pubDate>Wed, 05 Jan 2022 19:20:34 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/issues-with-sort-lambda-to-spatially-sort-polygon/m-p/1130894#M63404</guid>
      <dc:creator>VinceE</dc:creator>
      <dc:date>2022-01-05T19:20:34Z</dc:date>
    </item>
    <item>
      <title>Re: Issues With .sort()/lambda to Spatially Sort Polygon Features</title>
      <link>https://community.esri.com/t5/python-questions/issues-with-sort-lambda-to-spatially-sort-polygon/m-p/1131562#M63431</link>
      <description>&lt;P&gt;Have you checked the Y components of the centroids? Are they exactly equal for every polygon in each row? If not, the N-S sorting will override the W-E sorting&lt;/P&gt;</description>
      <pubDate>Fri, 07 Jan 2022 13:02:43 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/issues-with-sort-lambda-to-spatially-sort-polygon/m-p/1131562#M63431</guid>
      <dc:creator>AdminGIS2</dc:creator>
      <dc:date>2022-01-07T13:02:43Z</dc:date>
    </item>
    <item>
      <title>Re: Issues With .sort()/lambda to Spatially Sort Polygon Features</title>
      <link>https://community.esri.com/t5/python-questions/issues-with-sort-lambda-to-spatially-sort-polygon/m-p/1131852#M63452</link>
      <description>&lt;P&gt;This appears to be it! I had checked this, but only out to about 6 decimal places, so I was not handling float precision appropriately.&lt;/P&gt;&lt;P&gt;I changed the sorting function to round both the X and the Y values, to avoid any issues with tiny differences in position:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;all_sheet_polys.sort(key=lambda poly: (dir1 * round(getattr(poly.centroid, axis1), 2),
                                       dir2 * round(getattr(poly.centroid, axis2), 2)))&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here are the "actual" coordinate values of the bottom row (X-values truncated for display), shown in the following image. When those values are sorted (using raw SN-WE coordinate values), the ordering was somewhat ambiguous along a given row because of your point about precision.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;1	X 11,768,434  Y 7,022,867.011901856400
2	X 11,767,927  Y 7,022,867.011901855469
3	X 11,767,421  Y 7,022,867.011901854537
4	X 11,766,914  Y 7,022,867.011901856400
5	X 11,766,407  Y 7,022,867.011901855469
6	X 11,765,901  Y 7,022,867.011901854537
7	X 11,765,394  Y 7,022,867.011901856400
8	X 11,764,887  Y 7,022,867.011901855469
9	X 11,764,381  Y 7,022,867.011901854537
10	X 11,763,874  Y 7,022,867.011901856400
11	X 11,763,367  Y 7,022,867.011901855469&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; After the rounding, problem solved (example of bottom row):&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Sort_With_Round.png" style="width: 647px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/31070i043F8D5E72AF0154/image-dimensions/647x55?v=v2" width="647" height="55" role="button" title="Sort_With_Round.png" alt="Sort_With_Round.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Thanks a ton, I appreciate the assistance.&lt;/P&gt;</description>
      <pubDate>Sat, 08 Jan 2022 20:32:54 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/issues-with-sort-lambda-to-spatially-sort-polygon/m-p/1131852#M63452</guid>
      <dc:creator>VinceE</dc:creator>
      <dc:date>2022-01-08T20:32:54Z</dc:date>
    </item>
  </channel>
</rss>

