English

How To: Edit polygon vertices with user-input coordinates

Summary

This sample script allows you to create a new tool that will move polygon vertices to specified coordinates.

Procedure

  1. Open a new script window.
    1. Activate the Project window.
    2. Click the Scripts icon.
    3. Click New.
  2. Paste the code below into the new script window:
    Code:
    '-- Script: MoveVertex2Coord.ave 
    ' 
    '-- This script is to used on the "Apply" property of a new 
    '-- tool on the View GUI. The script works on the selected 
    '-- polygon of the active theme. Use the tool to click on 
    '-- one of the selected polygon's vertices, then enter the 
    '-- coordinates of where you want that vertex moved, when 
    '-- prompted. 
    ' 
    '-- The polygon shapefile theme does not have to be in 
    '-- edit-mode, but the user must have write-permissions to 
    '-- the shapefile data, or the tool will not work. 
    
    '-- Set up the initial objects
    theView = av.getactivedoc 
    theDisplay = theView.GetDisplay 
    clickedPoint = theDisplay.ReturnUserPoint 
    theTheme = theView.GetActiveThemes.Get(0) 
    theFTab = theTheme.GetFTab 
    theFTab.SetEditable(true) 
    shapeFld = theFTab.FindField("Shape") 
    
    '- Make sure the user can edit this shapefile
    if (theFTab.CanEdit.Not) then
      MsgBox.Warning("The active theme cannot be edited","Stop") 
    end 
     
    '-- Make sure a polygon is selected
    selectedPolygonRecord = theFTab.GetSelection.GetNextSet(-1) 
    if (selectedPolygonRecord = -1) then 
      MsgBox.Warning("You must have one polygon selected","Stop") 
      return nil 
    end 
     
    '-- Get the new vertex coordinates from the user
    newVertex = MsgBox.MultiInput 
    ("Enter new vertex coordinates","",{"X:","Y:"},{"",""}) 
    
    '-- Find which vertex is closet to the clicked point
    thePolygon = theFTab.ReturnValue(shapeFld,selectedPolygonRecord) 
    listofPoints = {} 
    for each polypiece in thePolygon.AsList 
      for each aPt in polypiece.AsList 
        listofPoints.Add(aPt) 
      end 
    end 
    closestDist = 999999999 
    for each aPt in listofPoints 
      thisDist = clickedPoint.Distance(aPt) 
      if (thisDist < closestDist) then 
        closestDist = thisDist 
        closestPoint = aPt 
      end 
    end 
     
    '-- Make a new polygon. Test each vertext to determine which is selected,
    '-- if it is, change its location to the XY coordinate specified by the user
    newPolygonList = {} 
    for each polypiece in thePolygon.AsList 
      newPolypiece = {} 
      for each aPt in polypiece 
        if ((aPt.GetX = closestPoint.GetX) and (aPt.GetY = closestPoint.GetY)) then 
          aPt.SetX(newVertex.Get(0).AsNumber) 
          aPt.SetY(newVertex.Get(1).AsNumber) 
        end 
        newPolypiece.Add(aPt) 
      end 
      newPolygonList.Add(newPolyPiece) 
    end 
    
    '-- Make the modified polygon
    newPolygon = Polygon.Make(newPolygonList) 
     
    '- Replace the old polygon with the new one
    theFTab.SetValue(shapeFld,selectedPolygonRecord,newPolygon) 
     
    '-- Stop editing the theme's FTab
    theFTab.SetEditable(false) 
     
    '-- Redraw the view
    theView.Invalidate 
     
    '-- End of Script: MoveVertex2Coord.ave 
    
  3. Attach the script to the Apply event of a tool on the View GUI.
    1. Compile the script.
    2. Make the Project window active.
    3. Select Customize from the Project menu.
    4. Set the Type dropdown to View on the Customize dialog box.
    5. Set the Category dropdown to Tools.
    6. Click Tool.
    7. Double-click the Apply property at the bottom of the Customize dialog box.
    8. Type the name of the script in the Script Manager and click Select.
    9. Close the Customize dialog box.
    Note:
    For more information, refer to the ArcView Help topic "Customize (dialog box)."
  4. Open the view and make the polygon shapefile theme active.
  5. Select Start Editing from the Theme menu.
  6. Click a polygon with the Vertex Edit tool.
    [O-Image] Vertex Edit tool image
  7. Click the new tool button, then click the vertex of the selected polygon that you want to move.
  8. Type the new X and Y coordinates in the message box and click the OK button.