HELP!!How to write the python script?

4088
5
Jump to solution
05-17-2015 06:29 PM
wiz
by
New Contributor II

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!!!

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JaiSiva1
New Contributor III

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)

View solution in original post

5 Replies
DanPatterson_Retired
MVP Emeritus

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.

wiz
by
New Contributor II

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,

0 Kudos
SepheFox
Frequent Contributor

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.

0 Kudos
JaiSiva1
New Contributor III

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)
wiz
by
New Contributor II

Jai, thank you for your help, it works!