Skript to sum up attribute values and copy them into a table

525
4
08-24-2011 02:56 AM
MatthiasAbele
New Contributor II
Hallo,

I ve a lot of gdb-features in different feature datasets. I would like to sum up all "shape_area" values of each feature and copy the result of the sum in an output table.
To identify the source of the number in the output table, I need an additional column with the name of the source file.

How can I do this?

Thanx

Matthias
Tags (2)
0 Kudos
4 Replies
markdenil
Occasional Contributor III
create an output table with name and area fields
get a list of the data sets
get a list of the feature classes in each data set
cycle through the feature classes, one data set at at time
create a search cursor on the first feature class
run through the records, adding the area of that record to a running total
You have the feature class name already
write the name and total area to a python list
add that list as a member of a list of lists
do the next feature class in that data set
after that, do the next data set
... and so on
then
create an insert cursor for the output table
cycle through the list of lists, adding each name and total area sub list as a new record

all done.
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Here is an example on how to do this.  The code uses the Summary Statistics tool to summarize the Area field in each polygon feature class within feature datasets, then writes the feature class name and Area sum to a previously created table.

import arcpy
from arcpy import env
env.overwriteOutput = True
env.workspace = r"C:\MapsandGeodatabase\LocalGovernment.gdb"

arcpy.CreateTable_management(env.workspace, "AREA_SUM")
arcpy.AddField_management("AREA_SUM", "Name", "Text", "75")
arcpy.AddField_management("AREA_SUM", "SUM_Shape_Area", "Double")

for dataset in arcpy.ListDatasets("*"):
    lstFCs = arcpy.ListFeatureClasses("*", "", dataset)
    for fc in lstFCs:
        desc = arcpy.Describe(fc)
        if desc.shapeType == "Polygon":
            rows = arcpy.InsertCursor("AREA_SUM")
            row = rows.newRow()
            row.Name = fc
            sumArea = arcpy.Statistics_analysis(fc, "Stats", [["Shape_Area", "SUM"]])
            rows2 = arcpy.SearchCursor(sumArea)
            for row2 in rows2:
                totArea = row2.getValue("SUM_Shape_Area")
            row.SUM_Shape_Area = totArea
            rows.insertRow(row)
            totArea = 0


del row, rows, row2, rows2

arcpy.Delete_management("Stats")
0 Kudos
MatthiasAbele
New Contributor II
Thanx for the code...do you any good sites which can help me to get used to python???

Yours,

Matthias
0 Kudos
JakeSkinner
Esri Esteemed Contributor
0 Kudos