0

I have the following script, where it adds fields to a shapefile. The fields are a code and "Sup_ha", which is the area in hectares:

import arcpy

shapefile = "path/shapefile"
CODE_TEST = "2022"

arcpy.AddField_management(shapefile, "CODE", "TEXT", 10, "", "", "refcode", "NULLABLE", "REQUIRED")

cur = arcpy.UpdateCursor(shapefile) 
for row in cur :
    row.setValue("CODE", CODE_TEST)
    cur.updateRow(row)


arcpy.AddField_management(shapefile, "SUP_HA", "DOUBLE", 10, 2, "", "refcode", "NULLABLE", "REQUIRED")

The result is as follows:

enter image description here

In the field "SUP_HA" I need to calculate the area in hectares. What code should go after what is shown?

2
  • 1
    Do you have to use ArcPy cursor? Can you use a Geoprocessing Tool because Calculate Geometry Attributes (Data Management) - ArcGIS Pro | Documentation should work.
    – bixb0012
    Commented Dec 22, 2022 at 19:41
  • 1
    1. Never use deprecated arcpy.UpdateCursor -- Only use arcpy.da.UpdateCursor. 2. Calculate Field and Calculate Geometry Attributes are going to be much faster than a cursor on a large table. 3. "path/shapefile" isn't a valid shapefile path, but "path/shapefile.shp" is.
    – Vince
    Commented Dec 22, 2022 at 20:23

1 Answer 1

0

First off, I would reccomend using a with statement when implementing your cursors. The with statement will automatically close your cursor when the code exits the code block, freeing up the resources used by the cursor. as to exactly how you calculate the area into hectares will depend on the units the shapefile is currently in. In the code below, I have assumed that your shapefile is in meters.

import arcpy

shapefile = "path/shapefile"
CODE_TEST = "2022"

# Add CODE field
arcpy.AddField_management(shapefile, "CODE", "TEXT", 10, "", "", "refcode", "NULLABLE", "REQUIRED")

# Update CODE field with CODE_TEST value
with arcpy.da.UpdateCursor(shapefile, ["CODE"]) as cur:
    for row in cur:
        row[0] = CODE_TEST
        cur.updateRow(row)

# Add SUP_HA field
arcpy.AddField_management(shapefile, "SUP_HA", "DOUBLE", 10, 2, "", "refcode", "NULLABLE", "REQUIRED")

# Calculate area in hectares and update SUP_HA field
with arcpy.da.UpdateCursor(shapefile, ["SHAPE", "SUP_HA"]) as cur:
    for row in cur:
        # Get geometry of shapefile
        geom = row[0]
        # Calculate area in hectares
        area_ha = geom.area / 10000 # 1 hectare is equal to 10000 square meters. Change the calculation in this line if geom.area is not in square meters
        # Update SUP_HA field with calculated area
        row[1] = area_ha
        cur.updateRow(row)

The second cursor will access the geometry of your shapefile in the units it currently is in, and calculate the conversion in to hectares (again, assuming the area is in square meters)

3
  • 3
    with only works with DA cursors (another reason to use them). Note that Polygon.area isn't going to be correct with a GCS coordinate reference, but Polygon.getArea() could be (with appropriate parameters).
    – Vince
    Commented Dec 22, 2022 at 20:27
  • It tells me that in line 24 ("area_ha = geom.area") there is an error: "AttributeError: 'tuple' object has no attribute 'area'
    – MrShaggy87
    Commented Dec 26, 2022 at 16:02
  • @pmurbina87, you are getting that error because the UpdateCursor should be retrieving SHAPE@ and not SHAPE. The former is a token that retrieves an ArcPy Geometry object while the latter just retrieves an X,Y tuple of the geometry's centroid.
    – bixb0012
    Commented Feb 14, 2023 at 15:10

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.