How to give value for a polygon in fishnet

839
2
09-22-2020 11:36 PM
Bmohammad
New Contributor III

Dear All, 

            I created a fishnet in ArcMap. I want to give 1 value to the lowest row then 2 to next row when go up and again 1 to upper and repeat this in my whole grid. As I have lot of feature I want any Python Script for automation of process.

Thanks 

0 Kudos
2 Replies
JoshuaSharp-Heward
Occasional Contributor III

Hi,

I put this together quickly using Pro but I'm pretty sure it should run from the Python window in ArcMap too:

fishnet = "" #layer name goes here
fieldname = "" #name of field storing new value (1, 2)
fields = ["SHAPE@Y"].append(fieldname)
ys = [row[0] for row in arcpy.da.SearchCursor(fishnet, "SHAPE@Y")]
ysDict = {}

uniqueys = sorted(list(set(ys)))

for y in luniqueys:
    if luniqueys.index(y) % 2 == 0:
        ysDict[y] = 1
    else:
        ysDict[y] = 2

with arcpy.da.UpdateCursor(fish, ["SHAPE@Y", "val"]) as ucur:
    for row in ucur:
        row[1] = valueDict[row[0]]
        ucur.updateRow(row)

Just enter in the name of the layer and the field name that stores the values (1 or 2) in there. Also make sure that field is an integer field. Basically this script finds all the features unique y values, lays them out in order and assigns them a 1 or 2 depending on if its index is odd or even, then adds that value back to the feature.

Bmohammad
New Contributor III

Thanks alot  Joshua it works