Home arrow GIS Data & Resources arrow Scripts and Code arrow ArcMap Label & .CAL arrow Sum Contained Features For Polygon .CAL Script
Sum Contained Features For Polygon .CAL Script PDF Print E-mail

Written by AGRC Administrator,


This ArcMap .cal script can be used in place of a spatial join, summary,and rejoin process. For each polygon, the script uses a spatial containment filter to select fully contained features in a specified layer. Then it sums an attribute (as specified in the script) value for the contained features. The sum value is then used to populate the polygon attribute field on which the field calculator was opened. Use example: You have a SCHOOLS point feature class with Total_Students field and SCHOOLDISTRICT Polygon feature class where you want to have the total enrollment for each district polygon. Returns a long integer but can be modified for decimal values.

To use, select script below, copy & paste into Notepad or equivalent. Save as yourfilename.cal. Load into ArcMap field calculator. Within the script, specify the index position of your point layer and the name of the point feature class's attribute field that you wish to sum.

---------- SCRIPT STARTS BELOW ------------

'This .cal script is for polygon feature classes. It stores the numeric
'sum for a specifed attribute in a point feature class for all the
'features in that feature class that are fully contained by each polygon

'Make sure that the index number for the point feature class is set.
'Also, make sure that the field name is changed to the attribute field
'in the point feature class containing the numeric values that you wish
'to be summed. These two items can be found by searching below for 'SET THIS.
'(note this should also work with line and polygon feature classes too)

'** Declare variable to store population sum in
Dim popCount As Long

'** Get pointer variables to current arcmap project
Dim pMxDoc As IMxDocument
Dim pMap As IMap
Set pMxDoc = ThisDocument
Set pMap = pMxDoc.FocusMap

'** Get pointer variable to the point layer
Dim pPointLayer As IFeatureLayer
Dim popFieldIndex As Integer

'SET THIS...Point Layer must be 1st in ArcMap TOC
Set pPointLayer = pMap.Layer(0)
'SET THIS...Field Name in Point Layer to be summed
popFieldIndex = pPointLayer.FeatureClass.FindField("Id") 'Field Name in Point Layer to be summed

'** Set up a spatial filter and feature cursor to select
'** and iterate through contained points for each polygon
Dim pFCursor As IFeatureCursor
Dim pFeature As IFeature
Dim pPolygon As IPolygon

Dim pSpatialFilter As ISpatialFilter
Set pSpatialFilter = New SpatialFilter
Set pSpatialFilter.Geometry = [Shape] 'gets reference to current polygon
pSpatialFilter.SpatialRel = esriSpatialRelContains

Set pFCursor = pPointLayer.Search(pSpatialFilter, True)
Set pFeature = pFCursor.NextFeature

'** iterate through selected points for each polygon
popCount = 0
Do Until pFeature Is Nothing
popCount = popCount + pFeature.Value(popFieldIndex)
Set pFeature = pFCursor.NextFeature
Loop
__esri_field_calculator_splitter__
popCount



Users' Comments  
 

No comment posted

Add your comment

27, Oct. 2006
Last Updated ( 16, Dec. 2008 )
 
< Prev

AGRC Contacts | UGIC Contacts

feed image feed image

Utah GIS Portal © 2009 AGRC

Optimized for