Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Using Python, how do you add a new field to your shape file and calculate the area in km2?

share|improve this question
Use arcpy.AddField_management(). For the area calculation, check out this question:… – Martin Mar 24 '14 at 15:48
up vote 7 down vote accepted

this is an example

import arcpy
from arcpy import env 
env.workspace = "D:/"
fc = "test.shp" 
 expression1 = "{0}".format("!SHAPE.area@SQUAREKILOMETERS!")        
arcpy.CalculateField_management(fc, "area", expression1, "PYTHON", )
share|improve this answer
from osgeo import ogr

shapefile = "C:/StudyArea.shp"
driver = ogr.GetDriverByName("ESRI Shapefile")
dataSource = driver.Open(shapefile, 1)
layer = dataSource.GetLayer()
new_field = ogr.FieldDefn("Area", ogr.OFTInteger)

for feature in layer:
    geom = feature.GetGeometryRef()
    area = geom.GetArea() * (0.000000092903)
    feature.SetField("Area", area)

dataSource = None

I assumed the coordinate system you're using is in US feet. If it's not, you'll need to change the area calculation.

share|improve this answer
+1 It is always nice to see a pure python approach – Aaron Mar 24 '14 at 18:26

You can use an Update Cursor and a shape area token SHAPE@AREA to make the calculations.

import arcpy, os

file = r'C:\temp\polygon.shp'
arcpy.AddField_management (file, "area", "FLOAT")

with arcpy.da.UpdateCursor(file, ["SHAPE@AREA", "area"]) as cursor:
    for row in cursor:
        row[1] = row[0] / 1E6 # if spatial reference in meters, convert to km
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.