Performing Simple Calculations in ArcGIS
In many situations it is required to know the area, perimeter and centroids of the polygons from the shape files. The personal geodatabse of ArcGIS directly provides the values for area and perimeter of the polygons, but in shape files these values are not automatically added as an attribute. To add the area/perimeter/centroids of the polygons in attribute table VBA (Visual Basic for Application) script is required to be used. To use VBA script the knowledge of programming in Visual basic and Arcobjects is required. But still some of the preliminary tasks as adding the area/ perimeter etc. do not require much expertise and the scripts can be used by copy paste method.
Procedure to calculate Area of polygons
- 1. Start ArcMap.
- 2. Go to Add Data and add shape file containing polygons.
- 3. Right click on the opened layer in Table of contents and select Open Attribute Table.
- 4. Click the Options button and select Add Field. Specify a Name (e.g. polyArea) and specify Type as Double and click OK.
- 5. Right-click the new field heading and select Calculate Values. Click Yes to calculate outside the edit session.
- 6. Check the Advanced check box in the Field Calculator.
- 7. Copy the code given below into the pre-logic VBA script code box.
-
Dim Output As Double
Dim poly As IArea
Set poly = [Shape]
Output = poly.area
- 8. Type the word 'output' into the text box at the bottom.
- 9. Click OK to calculate the area of the polygon.
Procedure to calculate Centroid of polygons
- 1. Start ArcMap.
- 2. Go to Add Data and add shape file containing polygons.
- 3. Right click on the opened layer in Table of contents and select Open Attribute Table.
- 4. Click the Options button and select Add Field. Specify a Name (e.g. PolyCentreX) and specify Type as Double and click OK.
- 5. Right-click the new field heading and select Calculate Values. Click Yes to calculate outside the edit session.
- 6. Check the Advanced check box in the Field Calculator.
- 7. Copy the code given below into the pre-logic VBA script code box.
-
Dim Output As Double
Dim poly As IArea
Set poly = [Shape]
Output = poly.centroid.x
- 8. Type the word 'output' into the text box at the bottom.
- 9. Click OK to calculate the X coordinate of centroid the polygon.
- 10. Repeat steps 4 to 6 ( for adding Y coordinate of centroid)
- 11. Copy the code given below into the pre-logic VBA script code box.
-
Dim Output As Double
Dim poly As IArea
Set poly = [Shape]
Output = poly.centroid.y
- 12. Type the word 'output' into the text box at the bottom.
- 13. Click OK to calculate the Y coordinate of centroid the polygon.
Procedure to extract X and Y coordinates from Point shape in attribute table
- 1. Start ArcMap.
- 2. Go to Add Data and add shape file containing points.
- 3. Right click on the opened layer in Table of contents and select Open Attribute Table.
- 4. Click the Options button and select Add Field. Specify a Name (e.g. PointX) and specify Type as Double and click OK.
- 5. Right-click the new field heading and select Calculate Values. Click Yes to calculate outside the edit session.
- 6. Check the Advanced check box in the Field Calculator.
- 7. Copy the code given below into the pre-logic VBA script code box.
-
Dim Output As Double
Dim pPoint As IPoint
Set pPoint = [Shape]
Output = pPoint.x
- 8. Type the word 'output' into the text box at the bottom.
- 9. Click OK to calculate the X coordinate of Point.
- 10. Repeat steps 4 to 6 (for adding Y coordinate)
- 11. Copy the code given below into the pre-logic VBA script code box.
-
Dim Output As Double
Dim pPoint As IPoint
Set pPoint = [Shape]
Output = pPoint.y
- 12. Type the word 'output' into the text box at the bottom.
- 13. Click OK to calculate the Y coordinate of Point.