1

I created a fishnet grid and assigned numeric attributes 1-16 to a new field using the arcpy.da.UpdateCursor function. It however assigned the attributes north to south (see image).

fishnet attributes

How can I change it so that the attributes are assigned from top-left to right i.e row by row as opposed to column by column? I know this could be achieved by the grid index tool but I used the fishnet tool because it allows me to draw the grid starting at the northwest corner drawing to the east which the grid index tool cannot achieve.

with arcpy.da.UpdateCursor("myfish1",("OID","switch")) as cursor:
for row in cursor:
    row[1]=row[0]
    cursor.updateRow(row)
3
  • 1
    The Sort tool has a Spatial sort option once you add the Shape field. UL - sorting starts from the upper left corner.
    – klewis
    Sep 7, 2017 at 17:14
  • If you include the geometry in the cursor column list, you could use the X and Y of its centroid to locate the desired value. Populating a fishnet is so trivial, I'd just create in manually, but there is at least one question here which assigns IDs via a PostgreSQL SQL expression.
    – Vince
    Sep 7, 2017 at 17:29
  • @klewis Wow that worked!!! Thank you so much. I'll accept your answer if you post it.
    – ketar
    Sep 7, 2017 at 18:30

1 Answer 1

2

As suggested by user klewis, you can perform a spatial Sort by using the Shape field* (before updating the column switch with arcpy.da.UpdateCursor, if needed). *Requires a Desktop Advanced license.

Set the spatial_sort_method parameter equal UL (upper left). It will reorder rows based on position of rectangular cells in the fishnet starting from top, and going from left to right through horizontally aligned features. For example:

arcpy.Sort_management("myfish1", "myfish1_sorted", sort_field = [["Shape", "ASCENDING"]], spatial_sort_method = "UL")

Take a look at illustrations from this article to have a better idea how the sort method works.

Your Answer

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

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