' Name: View.CalculateAreas ' ' Title: Calculates area, acres, square miles, perimeter for polygon shapes. ' ' Topics: GeoData ' ' Description: Calculates area and perimeter for polygon themes. ' If the View has been projected the calculations are in ' projected units. Otherwise the calculations are in 'native' map units. ' The script processes the list of active themes to calculate ' area, acres, square miles, and perimeter. Modified from ESRI sample script ' by Andy Rowan, andy@giscenter.org, 6/13/2001 ' ' The script will add the fields Area, Acres, SqMiles and Perimeter to polygon ' themes if they do not exist. If the fields exist, their values are ' recalculated. Rerun the script if you change the projection of the view. ' ' Conversion factor for acres and square miles is based on map units in view ' properties. ' ' Requires: A View with at least one active theme. You must have write access ' to the active theme(s). Map units must be set in view properties. ' ' Self: ' ' Returns: ' ' Version: 3.1 ' ' Get the window, make sure it's a view, get its projection (if any). ' theView = av.GetActiveDoc if (theView.Is(View).not) then msgbox.error("Active window must be a view","") return nil end thePrj = theView.GetProjection if (thePrj.IsNull) then hasPrj = false else hasPrj = true end ' ' Get the list of active themes. If there aren't any, let the user know ' and exit. ' theActivethemeList = theView.GetActivethemes if (theActivethemeList.Count = 0) then MsgBox.Error("No active themes.","") return nil end ' Get view units. If not set, tell user and exit ' DistUnits = theView.GetUnits if (DistUnits = #UNITS_LINEAR_UNKNOWN) then msgbox.error("Must set map units in View Properties.","") return nil end ' Set conversion factor for acres ... first use the built-in request to convert ' existing units to square feet, then convert that to acres. ' Then set the conversion for square miles from the built-in request AcreConversion = units.ConvertArea(1, DistUnits, #UNITS_LINEAR_FEET) * 0.000022956 SqMiConversion = units.ConvertArea(1, DistUnits, #UNITS_LINEAR_MILES) ' ' Loop through the list of active themes. If you can't edit the theme ' inform the user. ' For Each thetheme in theActivethemeList theFTab = thetheme.GetFTab if (theFTab.CanEdit.Not) then MsgBox.Info("Cannot edit table for theme:"++thetheme.AsString,"") Continue end ' ' Make the FTAB editable, and find out which type of feature it is. ' theFTab.SetEditable(TRUE) theType = theFTab.FindField("shape").GetType if (theType = #FIELD_SHAPEPOLY) then ' ' If it's polygonal check for the existence of the fields "Area" and ' Perimeter. If they do not exist, create them. ' Except for area, ask first before creating them. DoAny = MsgBox.YesNo("(Re)Calculate area/perimeter values for"++thetheme.getName+"?", "Calculate", true) if (DoAny.not) then continue end 'AREA if (theFTab.FindField("Area") = nil) then theAreaField = Field.Make("Area",#FIELD_DOUBLE,16,3) theFTab.AddFields({theAreaField}) else theAreaField = theFTab.FindField("Area") end 'ACRE if(theFTab.FindField("Acres") = nil)then DoAcres = MsgBox.YesNo("Create Acres field in"++thetheme.getName+"?", "Create Field", true) if (DoAcres) then theAcresField = Field.Make("Acres",#FIELD_DOUBLE,16,3) theFTab.AddFields({theAcresField}) end else DoAcres = TRUE theAcresField = theFTab.FindField("Acres") end 'Sq_Miles if(theFTab.FindField("Sq_Miles") = nil)then DoSqMi = MsgBox.YesNo("Create Sq_Miles field in"++thetheme.getName+"?", "Create Field", true) if (DoSqMi) then theSqMiField = Field.Make("Sq_Miles",#FIELD_DOUBLE,16,7) theFTab.AddFields({theSqMiField}) end else DoSqMi = TRUE theSqMiField = theFTab.FindField("Sq_Miles") end 'PERIMETER if (theFTab.FindField("Perimeter") = nil) then DoPerim = MsgBox.YesNo("Create Perimeter field in"++thetheme.getName+"?", "Create Field", true) if (DoPerim) then thePerimeterField = Field.Make("Perimeter",#FIELD_DOUBLE,16,3) theFTab.AddFields({thePerimeterField}) end else DoPerim = TRUE thePerimeterField = theFTab.FindField("Perimeter") end ' ' Loop through the FTAB and find the projected area and perimeter of each ' shape and set the field values appropriately. ' theShape = theFTab.ReturnValue(theFTab.FindField("shape"),0) For Each rec in theFTab theFTab.QueryShape(rec,thePrj,theShape) theArea = theShape.ReturnArea theFTab.SetValue(theAreaField,rec,theArea) if (doAcres) then theAcres = theArea*AcreConversion theFTab.SetValue(theAcresField,rec,theAcres) end if (doSqMi) then theSqMi = theArea*SqMiConversion theFTab.SetValue(theSqMiField,rec,theSqMi) end if (doPerim) then thePerimeter = theShape.ReturnLength theFTab.SetValue(thePerimeterField,rec,thePerimeter) end end theFTab.SetEditable(FALSE) else MsgBox.Info("You must use this script on a polygon theme.", "View.CalculateAreas") return Nil end end