English

How To: Perform a point cluster analysis using a single linkage algorithm

Summary

The document describes how to use a spatial application of the single linkage cluster analysis method through Avenue. The following script assigns each point in the theme a single regional, or cluster, value. A description of this process is included in the header of the script below.

Procedure

  1. Create a new script window.

    A. Activate the Project window.
    B. Click the Scripts icon.
    C. Click New.

  2. Paste the code below into the script window.

    Code:
    '----------------------------------
    'This script will cluster points based on a constant buffer distance
    'from each point. The script will assign a value to a region column in a table defining
    'the point's membership in a cluster(region). If the value is 0 then the point did
    'not fit into the clustering algorithm and is not a member of a cluster(region).
    '
    'The process works like this:
    ' user enters buffer distance in map units
    ' user enters number of points that constitute a cluster
    ' user prompted if he/she wants buffer graphics around each point
    '
    'Each point is evaluated:
    '- if region value is 0 then region value is assigned unique record id and selects other
    ' points within distance buffer
    '- if the number selected is >= the number of points that constitute a cluster
    ' then region values for each selected point are added to a list (excluding 0) and the
    ' lowest value in the list is assigned to all selected points region values
    '- if the number selected is < the number of points that constitute a cluster then the
    ' region value for each selected point is not changed and the point being evaluated will
    ' have a region value of 0
    '- if region value is >0 then selects other points within distance buffer
    '- if the number selected is >= the number of points that constitute a cluster
    ' then region values for each selected point are added to a list (excluding 0)the
    ' lowest value in the list is assigned to all selected points region values
    '- if the number selected is < the number of points that constitute a cluster then the
    ' region value for each selected point is not changed

    theview = av.getactivedoc
    thetheme = theview.getactivethemes.get(0)
    theftab = thetheme.getftab
    thebuf = msgbox.input("Enter buffer distance from point:","Buffer Distance","0")
    thepoints = msgbox.input("Enter minimum Number of points that constitute a cluster:","Cluster","1")
    thedrawcir = msgbox.miniyesno("Do you want to draw buffers around features", True)
    theregfld = theftab.findfield("region")
    therecfld = theftab.findfield("record")
    theshpfld = theftab.findfield("shape")
    theftab.seteditable(true)
    if (theregfld = nil) then
  3. thenewregfld = field.make("region", #Field_decimal, 10, 0)
    theftab.addfields({thenewregfld})
    theregfld = theftab.findfield("region")
    else
    for each rg in theftab
    theftab.setvalue(theregfld, rg, 0)
    end
    end
    if (therecfld = nil) then
    thenewrecfld = field.make("record", #Field_decimal, 10, 0)
    theftab.addfields({thenewrecfld})
    therecfld = theftab.findfield("record")
    for each r in theftab
    rval = (r + 1)
    theftab.setvalue(therecfld, r, rval)
    end
    else
    end
    thelist = list.make
    thereglist = list.make
    theglist = theview.getgraphics
    for each p in theftab
    thereglist.empty
    thefirstval = theftab.returnvalue(theregfld, p)
    if (thefirstval = 0) then
    therecnum = theftab.returnvalue(therecfld, p)
    theftab.setvalue(theregfld, p, therecnum)
    theshape1 = theftab.returnvalue(theshpfld, p)
    thex = theshape1.getx
    they = theshape1.gety
    thecir = circle.make(thex@they, thebuf.asnumber)
    thegcir = graphicshape.make(thecir)
    if (thedrawcir = TRUE) then
    theglist.add(thegcir)
    else
    end
    theftab.selectbypolygon(thecir.aspolygon, #VTAB_SELTYPE_NEW)
    thenumsel = theftab.getnumselrecords
    thebitmap = theftab.getselection
    if (thenumsel >= thepoints.asnumber) then
    for each b in thebitmap
    theselvalue = theftab.returnvalue(theregfld, b)
    if (theselvalue > 0) then
    thereglist.add(theselvalue)
    else
    end
    end
    if (thereglist.isempty = FALSE) then
    thereglist.sort(true)
    thenewregval = thereglist.get(0)
    for each bb in thebitmap
    theftab.setvalue(theregfld, bb, thenewregval)
    end
    else
    end
  4. else
    for each bbb in thebitmap
    thebbbselvalue = theftab.returnvalue(theregfld, bbb)
    thebbbrecvalue = theftab.returnvalue(therecfld, bbb)
    if (thebbbselvalue = thebbbrecvalue) then
    theftab.setvalue(theregfld, bbb, 0)
    else
    end
    end
    end
    '--------------------------------------------
    else
    theregnum = theftab.returnvalue(therecfld, p)
    theshape1 = theftab.returnvalue(theshpfld, p)
    thex = theshape1.getx
    they = theshape1.gety
    thecir = circle.make(thex@they, thebuf.asnumber)
    thegcir = graphicshape.make(thecir)
    if (thedrawcir = TRUE) then
    theglist.add(thegcir)
    else
    end
    theftab.selectbypolygon(thecir.aspolygon, #VTAB_SELTYPE_NEW)
    thenumsel = theftab.getnumselrecords
    thebitmap = theftab.getselection
    if (thenumsel >= thepoints.asnumber) then
    for each b in thebitmap
    theselvalue = theftab.returnvalue(theregfld, b)
    if (theselvalue > 0) then
    thereglist.add(theselvalue)
    else
    end
    end
    if (thereglist.isempty = FALSE) then
    thereglist.sort(true)
    thenewregval = thereglist.get(0)
    for each bb in thebitmap
    theftab.setvalue(theregfld, bb, thenewregval)
    end
    else
    end
    else
    end
    end
    end
    theftab.seteditable(false)
    thetheme.clearselection
    theview.invalidate
    theview = av.getactivedoc
    thetheme = theview.getactivethemes.get(0)
    theftab = thetheme.getftab
    theregfld = theftab.findfield("region")
    therecfld = theftab.findfield("record")
    theshpfld = theftab.findfield("shape")
    theftab.seteditable(true)
    thelist = list.make
    thereglist = list.make
    theglist = theview.getgraphics
    thereglista = list.make
    thereglist.empty
    for each ff in theftab
    thereglista.empty
    therecnuma = theftab.returnvalue(therecfld, ff)
    theregnuma = theftab.returnvalue(theregfld, ff)
    theshape1a = theftab.returnvalue(theshpfld, ff)
    thexa = theshape1a.getx
    theya = theshape1a.gety
    thecira = circle.make(thexa@theya, thebuf.asnumber)
    thegcira = graphicshape.make(thecira)
    theftab.selectbypolygon(thecira.aspolygon, #VTAB_SELTYPE_NEW)
    thenumsela = theftab.getnumselrecords
    thebitmapa = theftab.getselection
    if (thenumsela >= thepoints.asnumber) then
    for each ba in thebitmapa
    theselvaluea = theftab.returnvalue(theregfld, ba)
    if ((theselvaluea < theregnuma) and (theselvaluea <> 0)) then
    thereglista.add(theselvaluea)
    else
    end
    end
    if (thereglista.isempty = FALSE) then
    thereglista.sort(true)
    thenewregvala = thereglista.get(0)
    for each bba in thebitmapa
    theftab.setvalue(theregfld, bba, thenewregvala)
    end
    else
    end
    else
    for each bn in thebitmapa
    theselvaluea = theftab.returnvalue(theregfld, bn)
    if ((theselvaluea <= theregnuma) and (theselvaluea <> 0)) then
    thereglista.add(theselvaluea)
    else
    end
    end
    if (thereglista.isempty = FALSE) then
    thereglista.sort(true)
    thenewregvala = thereglista.get(0)
    theftab.setvalue(theregfld, ff, thenewregvala)
    else
    theftab.setvalue(theregfld, ff, 0)
    end
    end
    end
    theftab.seteditable(false)
    thetheme.clearselection
    theview.invalidate
    av.purgeobjects
    '--------------------------
  5. Attach the script to a button on the view graphical user interface.

    A. Compile the script.
    B. Switch to the Project window.
    C. Select Customize from the Project menu.
    D. On the Customize dialog box, click the Type dropdown arrow and click View.
    E. Select Buttons under Category.
    F. Click the New button.
    G. Double-click the Click property in the Customize dialog box.
    H. Type the name of the script in the Script Manager and click Select.
    I. Close the Customize dialog box.

    For more information, see "Customize dialog box" in ArcView Help.

  6. Open the view with the point shapefile.
  7. Make the point theme active.
  8. Click the New button and follow the directions on the dialogs.