1

enter image description hereI am looking for a python code for this polygon. I need to calculate the area inside this polygon and I am new to programming. (Very new). Can anybody assist me with this? I have to use a python code for this, I cannot use any other means.

4
  • 2
    Your title mentions Perimeter and yet your question asks about Area. In any event, if your polygon is in a file geodatabase you should be able to read both from the attribute table, or if it is in a shapefile just add two fields and use Calculate Geometry.
    – PolyGeo
    Commented Apr 30, 2015 at 23:53
  • Hi, yea I meant to say calculate the area inside the polygon. I was able to show the area just by using the measurement tool. The problem is that I have to show a python code for this process.
    – user51399
    Commented Apr 30, 2015 at 23:57
  • 2
    As an added bonus the Calculate Geometry will do perimeter or area in a different spatial reference and several units... particularly handy if you data is in WGS84 and you need to know how many acres it is, without having to remember how many square feet is it in an acre. There's a discourse on how to do it in python here help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//… Commented Apr 30, 2015 at 23:59
  • Please use the edit button to provide clarifications/corrections to your question. The parts of a question are described at meta.gis.stackexchange.com/a/3353. I suspect Python code to do this will involve using a SearchCursor the read the aforementioned fields.
    – PolyGeo
    Commented Apr 30, 2015 at 23:59

3 Answers 3

8

Here's an example of how you could get the area using a search cursor:

polygon_layer = "C:\\Polygon.shp"

with arcpy.da.SearchCursor(polygon_layer, ("OID@", "SHAPE@")) as search_cursor:
    for row in search_cursor:
        oid = row[0]
        geometry = row[1]

        print "OID {0}: area is {1}".format(oid, geometry.area)
1
  • 1
    Could use the 'SHAPE@AREA' token too, and will be a bit faster.
    – DWynne
    Commented May 4, 2015 at 16:27
1
import os
from osgeo import ogr

daShapefile = r"C:\Users\new1.shp" #path where your shape file is present 

driver = ogr.GetDriverByName('ESRI Shapefile')

dataSource = driver.Open(daShapefile,1) # 0 means read-only. 1 means writeable.

# Check to see if shapefile is found.
if dataSource is None:
    print 'Could not open %s' % (daShapefile)
else:
    print 'Opened %s' % (daShapefile)
    layer = dataSource.GetLayer()
    featureCount = layer.GetFeatureCount()
    print "Number of features in %s: %d" % (os.path.basename(daShapefile),featureCount)
    print "\n"


new_field = ogr.FieldDefn("Area", ogr.OFTReal)
new_field.SetWidth(32)
new_field.SetPrecision(2) #added line to set precision
layer.CreateField(new_field)

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

''' this code here finds the area of the polygons present in your shape file and adds a field named "Area" to your database with the value of the area of each polygon

1

same as the accepted answer but it is a one-liner.

area = arcpy.da.FeatureClassToNumPyArray(polygon_layer, 'SHAPE@AREA')[0][0]

for multiple if you want total area

from arcpy import da total_area = sum(i[0] for i in da.FeatureClassToNumPyArray(water_feature, 'SHAPE@AREA'))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.