' ********************************************************** ' Overlap V2.AVE ' Finds overlaps. Makes new shapefile which gives overlaps ' to the smaller of the two polygons, and writes all other ' polygons normally. Writes a comma-delimited file to keep ' record of the overlaps. ' ' Modified by Tim Thomas on 12/14/01. ' ' Based on Fast neighbors.ave ' It was written by Timothy S. Thomas. ' Editing began on 11/27/00, and was last edited on 11/27/00. ' It is based on "Find neighbors little program". ' ********************************************************** ' Set up basic variables ' ********************************************************** theProject = av.GetProject theView = av.GetActiveDoc theThemeList = theView.GetActiveThemes theTheme = theThemeList.Get(0) theFTab = theTheme.GetFTab numRecs = theFTab.GetNumRecords ' "Shape" is a special word in FTabs shapeField = theFTab.FindField("Shape") ' ********************************************************** ' This allows the user to pick a field as the basic unit for ' evaluation. ' ********************************************************** ' Gets new field list, reflecting deletions of fields fieldList = theFTab.GetFields.Clone fieldList.Insert("") idField = MsgBox.ChoiceAsString(fieldList,"Select polygon ID field", "Poly ID field") ' ********************************************************** ' Create a new shapefile to hold the corrected shapes and ' make a comma-delimited file to keep a record of the ' corrections ' ********************************************************** ' Shows message in message bar av.ShowMsg("Creating overlap shapefile") ' Creates file FTabFN = theFTab.GetSrcName.GetFileName baseString = FTabFN.GetBaseName extString = FTabFN.GetExtension newString = FTabFN.AsString.Substitute(baseString,"") new2String = baseString.Substitute(extString,"shp") new3String = baseString.Substitute(extString,"csv") newFTabFN = FileDialog.Put( (newString + "no overlaps " + new2String).AsFileName, "*.shp","No overlaps shapefile name") newFTab = FTab.MakeNew(newFTabFN,Polygon) new2FTabFN = FileDialog.Put( (newString + "overlaps " + new2String).AsFileName, "*.shp","Overlaps shapefile name") new2FTab = FTab.MakeNew(new2FTabFN,Polygon) newcsvFN = FileDialog.Put( (newString + "overlap record " + new3String).AsFileName, "*.csv","Data file name") newcsvFile = LineFile.Make(newcsvFN,#FILE_PERM_WRITE) msg1 = "Divide by this to convert map units to area units" areaconv = MsgBox.Input(msg1,"Converstion factor","10000").AsNumber ' ********************************************************** ' Clone fields for newFTab ' ********************************************************** newshapeField = newFTab.FindField("Shape") inFields = theFTab.GetFields newFields = List.Make for each f in inFields if (f.GetName <> "shape") then newFields.Add(f.Clone) end end newFTab.AddFields(newFields) new2shapeField = new2FTab.FindField("Shape") new2idField = Field.Make("ItsctID", #FIELD_LONG, 8, 0) id1Field = Field.Make("PolyID1", #FIELD_LONG, 8, 0) id2Field = Field.Make("PolyID2", #FIELD_LONG, 8, 0) new2FTab.AddFields({new2idField,id1Field,id2Field}) ' ********************************************************** ' Check the shapefile for overlaps ' ********************************************************** for each recnum in 0..(numRecs-1) thePoly = theFTab.ReturnValue(shapeField,recnum).Clean id = theFTab.ReturnValueString(idfield,recnum) area1 = (thePoly.ReturnArea / areaconv).SetFormat("d.dddddd") theFTab.SelectByPolygon(thePoly,#VTAB_SELTYPE_NEW) dcut = 0 for each s in theFTab.GetSelection id2 = theFTab.ReturnValueString(idfield,s) if (id<>id2) then otherPoly = theFTab.ReturnValue(shapeField,s).Clean itsctShape = thePoly.ReturnIntersection(otherPoly) ' The GetDimension was supposed to only give a value of 2 if ' the intersection was a polygon. In practice, it often ' gives a 2 if the intersection is an arc. So, have to use area ' as the measure of whether it is a polygon. ' dim = itsctShape.GetDimension ' if (dim = 2) then area = itsctShape.ReturnArea.SetFormat("d.dddddd") ' The reason for doing the following second area computation ' was that sometimes intersecting one way incorrectly returns ' 0 for area. It is commented out now because I was given ' the impression if I do a deep clean on the data before ' running this program, then I should not have a problem ' with area computation. Also, because I now look only at ' polygons, I should not have a problem with null values. ' if (area.IsNull) then ' area = otherPoly.ReturnIntersection(thePoly).ReturnArea ' end area = (area / areaconv) if (area.IsNull.Not) then area2 = (otherPoly.ReturnArea / areaconv).SetFormat("d.dddddd") ' Writing a line of output to the csv file if (id < id2) then theString = id + ", " + id2 + ", " + area.SetFormat("d.dddddddd").AsString + " , " + area1.SetFormat("d.dddddddd").AsString + " , " + area2.SetFormat("d.dddddddd").AsString newcsvFile.WriteElt(theString) new2rec = new2FTab.AddRecord new2FTab.SetValue(new2shapeField,new2rec,itsctShape) new2FTab.SetValue(new2idField,new2rec,new2rec+1) new2FTab.SetValue(id1Field,new2rec,id) new2FTab.SetValue(id2Field,new2rec,id2) end ' Adusting thePoly, if necessary if (area1 > area2) then newPoly = thePoly.ReturnDifference(itsctShape) thePoly = newPoly end end end ' This is the END for (each s in theFTab.GetSelection) end newrec = newFTab.AddRecord newFTab.SetValue(newshapeField,newrec,thePoly) for each fld in theFTab.GetFields fldName = fld.GetName if (fldName <> "shape") then newfld = newFTab.FindField(fldName) val = theFTab.ReturnValue(fld,newrec) newFTab.SetValue(newfld,newrec,val) end end av.SetStatus(100*(recnum+1)/numrecs) end ' Clears selected features in the theme and theme table theTheme.ClearSelection ' Clears message in message bar av.ClearMsg ' Turns off editing in the FTab newFTab.SetEditable(false) newFTheme = FTheme.Make ( newFTab ) theView.AddTheme ( newFTheme ) new2FTab.SetEditable(false) new2FTheme = FTheme.Make ( new2FTab ) theView.AddTheme ( new2FTheme ) ' Removes garbage objects av.PurgeObjects