'**Title: MergeArea '**Author: Jennifer L. Jessip '**Created: May 15, 1999 '**Texas A&M University ' 'Description: Selecting Polygons that are smaller than what the user defines and 'merging them with polygons of similar mean elevations ' theProject = av.GetProject theView = av.GetActiveDoc thePrj = theView.GetProjection ' 'This will perform the merge on the first active theme in the theme list ' theActiveTheme = theView.GetActiveThemes.Get(0) theFtab = theActiveTheme.GetFtab ' 'Check to see if the first theme is editable ' if (theFTab.CanEdit.Not) then MsgBox.Info("Cannot edit table for theme:"++theActivetheme.AsString,"") end ' 'Make the FTAB editable, and find out which type of feature it is. ' theFTab.SetEditable(TRUE) ' ' the next two lines clear any polygons that might be selected ' this is important for the merging process because it looks at all ' the polygons that are selected ' theFtab.GetSelection.ClearAll theFtab.UpdateSelection ' ' Makes sure that there is an Feature Table associated with the theme ' because this is where all the merging is done (within the dbf) if (theFTab = Nil) then exit end ' 'This makes sure that the user understands that the Hydro menu (Hydrologic Modeling) 'was used to calculate mean elevation and clean up the polygons. If they select NO - 'they will exited out of the script and will be able to run the process to calculate mean 'elevation ' theFirstQ = MsgBox.YesNo("Have you created watersheds with the hydro menu that are a desirable size? Did you check the boxes to include Mean Elevation and Mean Slope as fields in a Table?","Watersheds",True) if (theFirstQ = False) then theFirstS = MsgBox.Info("You need to use the Hydro Extension and create watersheds and include Mean Elevation and Mean Slope as fields in the table as well as any other ones that you wish","Use Hydro") exit end ' 'This runs the CalcArea script to make sure that the areas have been properly calculated ' av.run("CalcArea","") ' 'The following series of FindFields makes sure that the user has followed ' the manual and has done the hydrologic modeling and selected MeanElev/ ' This also makes sure that the CalcArea script has been run ' and was run successfully ' theSelField = theFTab.FindField("Acres") if (theSelField = Nil) then MsgBox.Info("The CalcArea script has not run properly - make sure the first theme is active and editable","Cannot Continue") exit end ' theXField = theFTab.FindField("XCentroid") if (theXField = Nil) then MsgBox.Info("The CalcArea script has not run properly - make sure the first theme is active and editable","Cannot Continue") exit end ' theYField = theFTab.FindField("YCentroid") if (theYField = Nil) then MsgBox.Info("The CalcArea script has not run properly - make sure the first theme is active and editable","Cannot Continue") exit end ' theMeanElevField = theFTab.FindField("MeanElev") if (theMeanElevField = Nil) then MsgBox.Info("The Mean Elevation box was not checked when creating watersheds","Redo Watersheds and check the requested box") exit end ' theMeanSlopeField = theFtab.FindField("BasinSlop") if (theMeanSlopeField = Nil) then MsgBox.Info("The Mean Slope box was not checked when creating watersheds","Redo Watersheds and check the requested box") exit end ' theIDField = theFTab.FindField("ID") if (theIDField = Nil) then exit end ' theMUIDField = theFtab.FindField("MU_ID") if (theMUIDField = Nil) then exit end ' 'This asks the user to input the smallest management unit that he/she wishes to manage ' userValue = MsgBox.Input("Pick the size (in Acres) of the smallest management unit that you wish to work with.", "MU size", "2.5").AsNumber If (userValue = nil) then exit end ' 'Gets the Number of Records in the Feature Table of the first theme ' NumRecords = theFtab.GetNumRecords ' 'Picks the Acre field as the Field to perform all comparisons ' controlField = theFtab.FindField("Acres") ' '*The While loop is very important ' Breaking out of the while loop means that the table is complete - ' all the polygons are the same or larger than what the user selected ' to be the minimum size '* While inside the while loop ' the record is found that contains the size of the polygon that is smaller ' than what the user specified ' That number is selected as the LowValue and the search begins to find the closest ' set of polygons ' This does not always return polygons that are adjacent to the small polygon ' this is a problem ' The polygon that is closest in characteristics to the small polygon is selected ' as the larger polygon for the merge ' The characteristics of the larger polygon will be the characteristics of the merged ' polygon, however the acreage will be the addition of the two '* The newly merged polygons (based on acreage) will be tested for size based on ' farming criteria (length and width) ' The same merging process will take place While (true) ControlNum = (-1) theFtab.SetEditable(False) theFTab.SetEditable(true) NumRecords = theFtab.GetNumRecords for each i in 0..(NumRecords-1) FirstValue = theFtab.ReturnValueNumber(controlField,i) if (FirstValue < UserValue) then ControlNum = FirstValue theLowRecord = i NewCount = (1000+i) break end end ' ' This if statement breaks out of the loop if the area of all the MUs are less than what ' the user specified ' if (ControlNum = -1) then break end theLowXValue = theFTab.ReturnValueNumber(theXField, theLowRecord) theLowYValue = theFTab.ReturnValueNumber(theYField, theLowRecord) theLowMEValue = theFtab.ReturnValueNumber(theMeanElevField, theLowRecord) minDist = 100000000.0 for each j in (0..(numRecords - 1)) DistList = {} if (theFTab.FindField("Dist") = nil) then theDistField = Field.Make("Dist",#FIELD_DOUBLE,12,6) theFTab.AddFields({theDistField}) else theBadDistField = theFtab.FindField("Dist") theFtab.RemoveFields({theBadDistField}) theDistField = Field.Make("Dist",#FIELD_DOUBLE,12,6) theFTab.AddFields({theDistField}) end for each k in (0..(numRecords - 1)) theNewXValue = theFTab.ReturnValueNumber(theXField, k) if (theNewXValue = theLowXValue) then continue end theNewYValue = theFTab.ReturnValueNumber(theYField, k) XNumber = (theLowXValue-theNewXValue)^2 YNumber = (theLowYValue-theNewYValue)^2 dist = ((XNumber+YNumber).sqrt) theFtab.SetValueNumber(theDistField,k,dist) theBigTest = theFtab.ReturnValueNumber(theDistField,k) DistList.Insert(theBigTest) end DistList.Sort(True) zero = DistList.Get(0) one = DistList.Get(1) two = DistList.Get(2) NumberRecs = theFtab.GetNumRecords 'The purpose of this loop is to find the record number that is associated with the ' appropriate MU to be merged so the Mean Elevation can be found and compared for each m in 0..(NumberRecs - 1) FindRecord = theFtab.ReturnValueNumber(theDistField,m) if (zero = FindRecord) then ZeroRecord = m ZeroMEValue= theFtab.ReturnValueNumber(theMeanElevField,ZeroRecord) end if (FindRecord = one) then OneRecord = m OneMEValue = theFtab.ReturnValueNumber(theMeanElevField,OneRecord) end if (FindRecord = two) then TwoRecord = m TwoMEValue = theFtab.ReturnValueNumber(theMeanElevField,TwoRecord) end end theMEZeroCalc = (theLowMEValue - ZeroMEValue).abs theMEOneCalc = (theLowMEValue - OneMEValue).abs theMETwoCalc = (theLowMEValue - TwoMEValue).abs if ((theMEZeroCalc