Select to view content in your preferred language

Python: Calculate Area

6618
11
11-03-2010 07:27 AM
LornaMurison
Regular Contributor
Hi everyone,
Here is my daily python question just to keep you all on your toes...

I would like to add a field called "LCArea" and calculate, you guessed it, the area.
But I don't know what to put in the codeblock because I can only find area calculation codes in VBA.
I would appreciate some guidance as to how to use the expression and codeblock parameters.
Thanks.


And as a side note, when using the Advanced Field Calculator in ArcMap, it defaults to VBA.  How do I do field calculations using python in ArcMap?

Thanks!



# If the output is a geodatabase feature class...
if outType == "FeatureClass":
    # Then export the attribute table
    gp.MakeTableView_management(dissolveOutputFC, tableView, "", "", "UCID Catchment VISIBLE; GRIDCODE LandCov VISIBLE; Shape_Area LCArea VISIBLE")
    gp.TableToDbase_conversion (tableView, tableFolder)
# If the output is a shapefile...
elif outType == "ShapeFile":
    # Then add and calculate an area field...
    gp.MakeTableView_management(dissolveOutputFC, tableView, "", "", "UCID Catchment VISIBLE; GRIDCODE LandCov VISIBLE")
    gp.AddField_management (tableView, "LCArea", "DOUBLE", "", "", "", "", "", "", "")
    expression =
    codeblock = 
    gp.CalculateField_management (tableView, "LCArea", expression, "PYTHON_9.3", codeblock)
    gp.TableToDbase_conversion (tableView, tableFolder)
    # And export the attribute table
#else:
    # Error handler to exit the script and return some message
#print 'done'
0 Kudos
11 Replies
ChrisSnyder
Honored Contributor
Is your "shape" field called "shape"? Sometimes it isn't!

Does this code populate values:

gp.CalculateField_management(edgeDissolveFC, "ACRES", "!SHAPE.AREA!", "PYTHON", ""); showGpMessage()


How about this code:

gp.CalculateField_management(edgeDissolveFC, "ACRES", "!" + gp.describe(edgeDissolveFC).shapefieldname + ".AREA@ACRES!", "PYTHON", ""); showGpMessage()
0 Kudos
LornaMurison
Regular Contributor
Excellent!
Thank-you Chris, your second code snippet worked:

# Create a table, add and caculate the area
    gp.MakeTableView_management("Dissolve.shp", tableView, "", "", "UCID Catchment VISIBLE; GRIDCODE LandCov VISIBLE")
    LCArea = "LCArea"
    tableView = tableFolder + "\\" + tableName
    gp.AddField_management (tableView, LCArea, "DOUBLE", "#", "#", "", "LCArea", "NULLABLE", "NON_REQUIRED", "")    
    gp.CalculateField_management("Dissolve.shp", LCArea, "!" + gp.describe("Dissolve.shp").shapefieldname + ".AREA@SQUAREMETERS!", "PYTHON", "")
    # Export the table to dBASE
    gp.TableToDbase_conversion (tableView, tableFolder)
0 Kudos