Skip to main content
added 34 characters in body
Source Link
Bera
  • 77.7k
  • 14
  • 78
  • 187

Your question is a "duplicate" of Sorting grid cells in fishnet grid by location and number using QGIS? but using arcpy:

import arcpy
fc = r'C:\data.gdb\features' #Change
field_to_update = 'somefield' #Change, needs to be added before running the code
decimals = 0 #Rounding. You might need to adjust to get desired output. My coordinates are in meters so I use zero decimals. If your grid is tilted, try a negative value, -1 will give 123.452->120

grids = [i for i in arcpy.da.SearchCursor(fc,['SHAPE@X','SHAPE@Y','OID@'])] #List all grids with coords and objectid
grids.sort(key=lambda k: (round(k[1],decimals),round(-k[0],decimals)), reverse=True) #Sort by coords
order = [i[2] for i in grids] #Fetch objectids from sorted grids
d = {k:v for (v,k) in list(enumerate(order))} #Add counting number to each objectid as a dictionary of id:counting number

#Update field
with arcpy.da.UpdateCursor(fc,['OID@',field_to_update]) as cursor:
    for row in cursor:
        row[1]=d[row[0]]+1
        cursor.updateRow(row)

Note the highlighted numbers, Is this the way you want it to number?. enter image description here

Your question is a "duplicate" of Sorting grid cells in fishnet grid by location and number using QGIS? but using arcpy:

import arcpy
fc = r'C:\data.gdb\features' #Change
field_to_update = 'somefield' #Change, needs to be added before running the code
decimals = 0 #Rounding. You might need to adjust to get desired output. My coordinates are in meters so I use zero decimals

grids = [i for i in arcpy.da.SearchCursor(fc,['SHAPE@X','SHAPE@Y','OID@'])] #List all grids with coords and objectid
grids.sort(key=lambda k: (round(k[1],decimals),round(-k[0],decimals)), reverse=True) #Sort by coords
order = [i[2] for i in grids] #Fetch objectids from sorted grids
d = {k:v for (v,k) in list(enumerate(order))} #Add counting number to each objectid as a dictionary of id:counting number

#Update field
with arcpy.da.UpdateCursor(fc,['OID@',field_to_update]) as cursor:
    for row in cursor:
        row[1]=d[row[0]]+1
        cursor.updateRow(row)

Note the highlighted numbers, Is this the way you want it to number? enter image description here

Your question is a "duplicate" of Sorting grid cells in fishnet grid by location and number using QGIS? but using arcpy:

import arcpy
fc = r'C:\data.gdb\features' #Change
field_to_update = 'somefield' #Change, needs to be added before running the code
decimals = 0 #Rounding. You might need to adjust to get desired output. My coordinates are in meters so I use zero decimals. If your grid is tilted, try a negative value, -1 will give 123.452->120

grids = [i for i in arcpy.da.SearchCursor(fc,['SHAPE@X','SHAPE@Y','OID@'])] #List all grids with coords and objectid
grids.sort(key=lambda k: (round(k[1],decimals),round(-k[0],decimals)), reverse=True) #Sort by coords
order = [i[2] for i in grids] #Fetch objectids from sorted grids
d = {k:v for (v,k) in list(enumerate(order))} #Add counting number to each objectid as a dictionary of id:counting number

#Update field
with arcpy.da.UpdateCursor(fc,['OID@',field_to_update]) as cursor:
    for row in cursor:
        row[1]=d[row[0]]+1
        cursor.updateRow(row)

Note the highlighted numbers. enter image description here

added 231 characters in body
Source Link
Bera
  • 77.7k
  • 14
  • 78
  • 187

Your question is a "duplicate" of Sorting grid cells in fishnet grid by location and number using QGIS? but using arcpy:

import arcpy
fc = r'C:\data.gdb\features' #Change
field_to_update = 'somefield' #Change, needs to be added before running the code
decimals = 0 #Rounding. You might need to adjust to get desired output. My coordinates are in meters so I use zero decimals

grids = [i for i in arcpy.da.SearchCursor(fc,['SHAPE@X','SHAPE@Y','OID@'])] #List all grids with coords and objectid
grids.sort(key=lambda k: (round(k[1],decimals),round(-k[0],decimals)), reverse=True) #Sort by coords
order = [i[2] for i in grids] #Fetch objectids from sorted grids
d = {k:v for (v,k) in list(enumerate(order))} #Add counting number to each objectid as a dictionary of id:counting number

#Update field
    with arcpy.da.UpdateCursor(fc,['OID@',field_to_update]) as cursor:
        for row in cursor:
            row[1]=d[row[0]]+1
            cursor.updateRow(row)

Note the highlighted numbers, Is this the way you want it to number? enter image description here

Your question is a "duplicate" of Sorting grid cells in fishnet grid by location and number using QGIS? but using arcpy:

import arcpy
fc = r'C:\data.gdb\features' #Change
field_to_update = 'somefield' #Change, needs to be added before running the code

grids = [i for i in arcpy.da.SearchCursor(fc,['SHAPE@X','SHAPE@Y','OID@'])] #List all grids with coords and objectid
grids.sort(key=lambda k: (k[1],-k[0]), reverse=True) #Sort by coords
order = [i[2] for i in grids] #Fetch objectids from sorted grids
d = {k:v for (v,k) in list(enumerate(order))} #Add counting number to each objectid as a dictionary of id:counting number

#Update field
    with arcpy.da.UpdateCursor(fc,['OID@',field_to_update]) as cursor:
        for row in cursor:
            row[1]=d[row[0]]+1
            cursor.updateRow(row)

Your question is a "duplicate" of Sorting grid cells in fishnet grid by location and number using QGIS? but using arcpy:

import arcpy
fc = r'C:\data.gdb\features' #Change
field_to_update = 'somefield' #Change, needs to be added before running the code
decimals = 0 #Rounding. You might need to adjust to get desired output. My coordinates are in meters so I use zero decimals

grids = [i for i in arcpy.da.SearchCursor(fc,['SHAPE@X','SHAPE@Y','OID@'])] #List all grids with coords and objectid
grids.sort(key=lambda k: (round(k[1],decimals),round(-k[0],decimals)), reverse=True) #Sort by coords
order = [i[2] for i in grids] #Fetch objectids from sorted grids
d = {k:v for (v,k) in list(enumerate(order))} #Add counting number to each objectid as a dictionary of id:counting number

#Update field
with arcpy.da.UpdateCursor(fc,['OID@',field_to_update]) as cursor:
    for row in cursor:
        row[1]=d[row[0]]+1
        cursor.updateRow(row)

Note the highlighted numbers, Is this the way you want it to number? enter image description here

added 73 characters in body
Source Link
Bera
  • 77.7k
  • 14
  • 78
  • 187

Your question is a "duplicate" of Sorting grid cells in fishnet grid by location and number using QGIS? but using arcpy:

import arcpy
fc = r'C:\data.gdb\features' #Change
field_to_update = 'somefield' #Change, needs to be added before running the code

grids = [i for i in arcpy.da.SearchCursor(fc,['SHAPE@X','SHAPE@Y','OID@'])] #List all grids with coords and objectid
grids.sort(key=lambda k: (k[1],-k[0]), reverse=True) #Sort by y,xcoords
order = [i[2] for i in grids] #Fetch objectids from sorted grids
d = {k:v for (v,k) in list(enumerate(order))} #Add counting number to each objectid as a dictionary of id:counting number

#Update field
    with arcpy.da.UpdateCursor(fc,['OID@',field_to_update]) as cursor:
        for row in cursor:
            row[1]=d[row[0]]+1
            cursor.updateRow(row)

Your question is a "duplicate" of Sorting grid cells in fishnet grid by location and number using QGIS? but using arcpy:

import arcpy
fc = r'C:\data.gdb\features' #Change
field_to_update = 'somefield' #Change, needs to be added before running the code

grids = [i for i in arcpy.da.SearchCursor(fc,['SHAPE@X','SHAPE@Y','OID@'])]
grids.sort(key=lambda k: (k[1],-k[0]), reverse=True) #Sort by y,x
order = [i[2] for i in grids]
d = {k:v for (v,k) in list(enumerate(order))}

with arcpy.da.UpdateCursor(fc,['OID@',field_to_update]) as cursor:
    for row in cursor:
        row[1]=d[row[0]]+1
        cursor.updateRow(row)

Your question is a "duplicate" of Sorting grid cells in fishnet grid by location and number using QGIS? but using arcpy:

import arcpy
fc = r'C:\data.gdb\features' #Change
field_to_update = 'somefield' #Change, needs to be added before running the code

grids = [i for i in arcpy.da.SearchCursor(fc,['SHAPE@X','SHAPE@Y','OID@'])] #List all grids with coords and objectid
grids.sort(key=lambda k: (k[1],-k[0]), reverse=True) #Sort by coords
order = [i[2] for i in grids] #Fetch objectids from sorted grids
d = {k:v for (v,k) in list(enumerate(order))} #Add counting number to each objectid as a dictionary of id:counting number

#Update field
    with arcpy.da.UpdateCursor(fc,['OID@',field_to_update]) as cursor:
        for row in cursor:
            row[1]=d[row[0]]+1
            cursor.updateRow(row)
Source Link
Bera
  • 77.7k
  • 14
  • 78
  • 187
Loading