0

I am new to Python.

I'm creating a Script Tool in 10.8.2 that generates polygons from Create Fishnet of 1 deg x 1 deg and a new field GEOCELL. I need to update field GEOCELL with the lower left corner extents latitude and longitude (XMin and YMin presumably) so that each polygon updates to "N50E015" format. Currently it is crashing on the extents line which originally came from an arcpy.SearchCursor script I found, however that script updated every polygon with the same string from the last polygon.

AttributeError: 'NoneType' object has no attribute 'extent'

    with arcpy.da.UpdateCursor(layer1x1, "GEOCELL") as cursor:
        for row in cursor:
            extent = row[0].extent
            lat = int(extent.YMin)
            lon = int(extent.XMin)
        # Set variables to buffer single and double lat/lon digits with leading zeros
        # North/South
        if lat > -1 and lat < 10:
            ns = "N0"
        elif lat > 9 and lat < 80:
            ns = "N"
        if lat > -10 and lat < 0:
            ns = "S0"
        elif lat > -81 and lat < -9:
            ns = "S"

        # East/West
        if lon > -1 and lon < 10:
            ew = "E00"
        elif lon > 9 and lon < 100:
            ew = "E0"
        elif lon > 99 and lon < 180:
            ew = "E"
        if lon > -10 and lon < 0:
            ew = "W00"
        elif lon > -100 and lon < -9:
            ew = "W0"
        elif lon > -181 and lon < -99:
            ew = "W"

        # North/South Absolute values
        if lat < 0:
            nspos = str(lat * -1)
        elif lat > -1:
            nspos = str(lat)

        # East/West Absolute values
        if lon < 0:
            ewpos = str(lon * -1)
        elif lon > -1:
            ewpos = str(lon)

        # Geocell Name
        geocell = ns + nspos + ew + ewpos 
        rows.updateRow(row)

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
atv5150
  • 63
  • 6
  • 1
    If you want to use column A to update column B, you need both columns in your field list. – Vince May 02 '22 at 02:36
  • 1
    You need to get the polygons geometry object before you can get the extents. As you have it you are asking for the extents of GEOCELL (which looks to be a text field in your data. – GBG May 02 '22 at 15:37

1 Answers1

1

You need to get the polygons geometry object before you can get the extents for each polygon. As you have it you are asking for the extents of GEOCELL (which looks to be a text field in your data).

Try

with arcpy.da.UpdateCursor(layer1x1, ["GEOCELL", 'SHAPE@']) as cursor:
for row in cursor:
    extent = row[1].extent
    lat = int(extent.YMin)
    lon = int(extent.XMin)

'#Your if/else statements here to build your string'

row[0] = 'your string here' cursor.updateRow(row)

GBG
  • 9,737
  • 1
  • 15
  • 44
  • Perfect! Thank you very much. I had tried using SHAPE@ before but didn't add it with GEOCELL. I think the one thing I don't understand yet is what is happening with extent=row[1].extent vs. using [0] which I had? – atv5150 May 02 '22 at 23:13
  • 1
    Your cursor is returning your row data as a list. row[0] would reference the first item in the list which is GEOCELL, row[1] is getting the second list item which in this case is the polygon geometry. – GBG May 03 '22 at 16:45