Here is the question:
There are two geodatabases (Village.gdb and Town.gdb). Village.gdb contains 277 feature classes, and every feature class has 10 fields (only Shape_area will be used) and many records. Town.gdb has only one feature class called town277, which has 6 fields (only town_name and Sum_area will be used) and 277 records.
Now, I want to sum the Shape_area of every feature class in village.gdb, and then assign the values to Sum_area, which the values of town_name equal to the name of 277 feature class in village.gdb.
So, how to write the python script?
TKS!!!
Solved! Go to Solution.
Hi Wi,
This is a sample code , that does the job . You can still modify it , to make things efficient
import arcpy from arcpy import env #Configuration goes here vill_fields = ['Shape_area'] town_fields = ['Sum_area'] env.workspace = r'D:\Geonet\geodbase\village.gdb' village_List = arcpy.ListFeatureClasses() #pushing all the fc's into a list town = r'D:\Geonet\geodbase\town.gdb\base' def findTotalArea(fc): with arcpy.da.SearchCursor(fc,vill_fields) as cursor: varea = 0 for row in cursor: varea = varea+row[0] return varea for fc in village_List: farea = findTotalArea(fc) with arcpy.da.UpdateCursor(town,town_fields,"town_name = '" + fc + "'" ) as cursor: for row in cursor: row[0] = farea cursor.updateRow(row)
If you can do it manually check out my blog post here and the one referenced in it. It goes through some of the options one has to create scripts for manual processes.
Dan, TKS for the reply.
I am a Chinese student, and a beginner in python language. So, it is a little difficult in reading all the python scripts in your blog.Could u please point out which post is?
TKS a lot!
Zheng,
Hi Wi, I think what Dan is trying to tell you, is that you can use various tools to make a start on writing a script. One method is to construct a model in model builder to do what you need, and then export the model as a python script. Basically you are going to need to iterate through the feature classes in your Village gdb.
Hi Wi,
This is a sample code , that does the job . You can still modify it , to make things efficient
import arcpy from arcpy import env #Configuration goes here vill_fields = ['Shape_area'] town_fields = ['Sum_area'] env.workspace = r'D:\Geonet\geodbase\village.gdb' village_List = arcpy.ListFeatureClasses() #pushing all the fc's into a list town = r'D:\Geonet\geodbase\town.gdb\base' def findTotalArea(fc): with arcpy.da.SearchCursor(fc,vill_fields) as cursor: varea = 0 for row in cursor: varea = varea+row[0] return varea for fc in village_List: farea = findTotalArea(fc) with arcpy.da.UpdateCursor(town,town_fields,"town_name = '" + fc + "'" ) as cursor: for row in cursor: row[0] = farea cursor.updateRow(row)
Jai, thank you for your help, it works!