'** Title: CalcArea ' **Author: Jennifer L. Jessip '**Created: May 29, 1999 '**Texas A&M University ' ' Description: Calculates area, perimeter, x centroid value, and y ' centroid value 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 active theme to calculate the items listed above ' ' The script will add the fields Area, Acres and Perimeter to polygon themes ' if they do not exist. If the fields exist, they are deleted and ' recalculated. Rerun the script if you change the projection of the view. ' ' The factor of 0.0002471 is used by default. Modify as necessary for your ' region. ' ' Requires: A View with at least one active polygon theme. You must have ' write access to the active theme(s). ' ' ' Version: 3.1 ' ' Get the view and its projection if any. ' theView = av.GetActiveDoc thePrj = theView.GetProjection if (thePrj.IsNull) then hasPrj = false else hasPrj = true end ' ' Get the active theme. If the first theme is not active, let the user know ' and exit. ' theActivethemeList = theView.GetActivethemes if (theActivethemeList.Count = 0) then MsgBox.Error("First theme is not active.","") Exit end ' ' Check to see if the first theme is editable thetheme = theActivethemeList.get(0) theFTab = thetheme.GetFTab if (theFTab.CanEdit.Not) then MsgBox.Info("Cannot edit table for theme:"++thetheme.AsString,"") 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 ' Acre and Perimeter. If they do not exist, create them. Also remove ' the extraneous fields created by the Hydrologic Modeling process. ' 'AREA if(theFtab.FindField("BasinArea") <> nil) then theBasinAreaField = theFtab.FindField("BasinArea") theFtab.RemoveFields({theBasinAreaField}) end if(theFtab.FindField("Area") = nil) then theAreaField = Field.Make("Area",#FIELD_DOUBLE,16,3) theFTab.AddFields({theAreaField}) else theBadAreaField = theFtab.FindField("Area") theFtab.RemoveFields({theBadAreaField}) theAreaField = Field.Make("Area",#FIELD_DOUBLE,16,3) theFTab.AddFields({theAreaField}) end 'ACRE if(theFTab.FindField("Acres") = nil)then theAcresField = Field.Make("Acres",#FIELD_DOUBLE,16,3) theFTab.AddFields({theAcresField}) else theBadAcreField = theFtab.FindField("Acres") theFtab.RemoveFields({theBadAcreField}) theAcresField = Field.Make("Acres",#FIELD_DOUBLE,16,3) theFTab.AddFields({theAcresField}) end 'PERIMETER if (theFtab.FindField("Perimeter") <> nil) then theBadPerField = theFtab.Findfield("Perimeter") theFtab.RemoveFields({theBadPerField}) end if (theFTab.FindField("Perimeter") = nil) then thePerimeterField = Field.Make("Perimeter",#FIELD_DOUBLE,16,3) theFTab.AddFields({thePerimeterField}) else theBadPerimeterField = theFtab.FindField("Perimeter") theFtab.RemoveFields({theBadPerimeterField}) thePerimeterField = Field.Make("Perimeter",#FIELD_DOUBLE,16,3) theFTab.AddFields({thePerimeterField}) end 'XCENTROID if(theFtab.FindField("Centroidx") <> nil) then theCentroidxField = theFtab.FindField("Centroidx") theFtab.RemoveFields({theCentroidxField}) end if(theFtab.FindField("XCentroid") = nil) then theXCentroidField = Field.make("XCentroid",#Field_Double,16,8) theFtab.AddFields({theXCentroidField}) else theBadXCentroidField = theFtab.FindField("XCentroid") theFtab.RemoveFields({theBadXCentroidField}) theXCentroidField = Field.make("XCentroid",#Field_Double,16,8) theFtab.AddFields({theXCentroidField}) end 'YCENTROID if(theFtab.FindField("Centroidy") <> nil) then theCentroidyField = theFtab.FindField("Centroidy") theFtab.RemoveFields({theCentroidyField}) end if(theFtab.FindField("YCentroid") = nil) then theYCentroidField = Field.make("YCentroid",#Field_Double,16,8) theFtab.AddFields({theYCentroidField}) else theBadYCentroidField = theFtab.FindField("YCentroid") theFtab.RemoveFields({theBadYCentroidField}) theYCentroidField = Field.make("YCentroid",#Field_Double,16,8) theFtab.AddFields({theYCentroidField}) end 'MU_ID if (theFtab.FindField("MU_ID") = nil) then theNewMUID = Field.Make("MU_ID",#Field_Long,10,0) theFtab.AddFields({theNewMUID}) else theBadMUID = theFtab.FindField("MU_ID") theFtab.RemoveFields({theBadMUID}) theNewMUID = Field.Make("MU_ID",#Field_Long,10,0) theFtab.AddFields({theNewMUID}) end ' ' Loop through the FTAB and find the projected area, perimeter, and centroid of each ' shape and set the field values appropriately. ' numRecs = theFtab.GetNumRecords For Each rec in 0..(NumRecs - 1) tShape = theFTab.ReturnValue(theFTab.FindField("shape"),rec) theShape = tShape.ReturnProjected(thePrj) theArea = theShape.ReturnArea theAcres = theArea*0.0002471 thePerimeter = theShape.ReturnLength theXCentroid = theShape.ReturnCenter.getX theYCentroid = theShape.ReturnCenter.gety theFTab.SetValue(theNewMUID,rec,rec) theFTab.SetValue(theAreaField,rec,theArea) theFTab.SetValue(theAcresField,rec,theAcres) theFTab.SetValue(thePerimeterField,rec,thePerimeter) theFTab.SetValue(theXCentroidField,rec,theXCentroid) theFTab.SetValue(theYCentroidField,rec,theYCentroid) end numRecs = theFtab.GetNumRecords theFtab.SetEditable(False) else MsgBox.Info("You must use this script on a polygon theme.", "View.CalculateAcreage") return Nil end 1