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)
            
2
  • 1
    If you want to use column A to update column B, you need both columns in your field list.
    – Vince
    Commented May 2, 2022 at 2: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
    Commented May 2, 2022 at 15:37

1 Answer 1

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)
2
  • 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
    Commented May 2, 2022 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
    Commented May 3, 2022 at 16:45

Your Answer

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

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