Select to view content in your preferred language

area calculation using python

645
1
Jump to solution
06-25-2012 01:20 PM
ElaineKuo
Regular Contributor
Hello

System: ArcGIS 9.3

Problem:

I want to calculate the polygon areas of ten shapefiles.
Each shapefile has more than one polygon, and I want to know the areas of each polygon respectively.

After searching for Arcscript and this forum, few answers met my demand.
Please kindly advise
1. python code to calculate polygon areas
2. better search method for area calculation using python

Thank you
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
ElaineKuo
Regular Contributor
##Script Name: Calculate areas ##Description: of polygons of a shapefile ##Created By: Elaine Kuo ##Date: 24/06/2012   #Import standard library modules import arcgisscripting import os  #Create the Geoprocessor object gp = arcgisscripting.create(9.3)  #Set the workspace. gp.Workspace= "H:/temp/test"  #Set the workspace. List all of the feature classes in the dataset outWorkspace= "H:/temp"  #Get a list of the featureclasses in the input folder fcs = gp.ListFeatureClasses()  # Loop through every item in the list that was just generated for fc in fcs:      # Break out the name, no path or extension, using the describe object.     desc = gp.describe(fc)     featureName = desc.name      # Add a field to this shapefile, of type LONG     gp.AddField (fc, "allarea_km", "double", 12,0)         #   Get a list of the fields in the featureclass     fields = gp.ListFields(fc, "C*", "String")               # Loop through every item in the list that was just generated      for field in fields:          gp.toolbox = "Data Management"          # get area         expression = "float('!shape.area@squarekilometers!')"          gp.CalculateField_management(fc, "allarea_km", expression, "PYTHON")         #Validate the new feature class name for the output workspace.     OutFeatureClass = outWorkspace + os.sep + gp.ValidateTableName(fc,outWorkspace)  gp.AddMessage(gp.GetMessages()) print gp.GetMessages()

View solution in original post

0 Kudos
1 Reply
ElaineKuo
Regular Contributor
##Script Name: Calculate areas ##Description: of polygons of a shapefile ##Created By: Elaine Kuo ##Date: 24/06/2012   #Import standard library modules import arcgisscripting import os  #Create the Geoprocessor object gp = arcgisscripting.create(9.3)  #Set the workspace. gp.Workspace= "H:/temp/test"  #Set the workspace. List all of the feature classes in the dataset outWorkspace= "H:/temp"  #Get a list of the featureclasses in the input folder fcs = gp.ListFeatureClasses()  # Loop through every item in the list that was just generated for fc in fcs:      # Break out the name, no path or extension, using the describe object.     desc = gp.describe(fc)     featureName = desc.name      # Add a field to this shapefile, of type LONG     gp.AddField (fc, "allarea_km", "double", 12,0)         #   Get a list of the fields in the featureclass     fields = gp.ListFields(fc, "C*", "String")               # Loop through every item in the list that was just generated      for field in fields:          gp.toolbox = "Data Management"          # get area         expression = "float('!shape.area@squarekilometers!')"          gp.CalculateField_management(fc, "allarea_km", expression, "PYTHON")         #Validate the new feature class name for the output workspace.     OutFeatureClass = outWorkspace + os.sep + gp.ValidateTableName(fc,outWorkspace)  gp.AddMessage(gp.GetMessages()) print gp.GetMessages()
0 Kudos