HOW TO
You can use Avenue to calculate a distance from a known point to a series of polygon centroids. This is an example of coding using the Avenue requests: ReturnCenter, GetX, GetY, and SqRt.
Code:
'-- Calculating Distance Script
'-- This script requires:
'-- 1. A polygon theme in the view
'-- 2. A field called 'zipcode' in the attribute table
TheView = av.GetActiveDoc
theThm = theView.GetActiveThemes.Get(0)
thl = theThm.GetFtab
thl.SetEditable(TRUE)
if (thl.FindField("zipcode") = nil) then
MsgBox.Error("zipcode field does not exist","")
exit
end
'-- Get search point location
fldval = MsgBox.Input("Enter the Target ZipCode","Target ZipCode","")
MsgBox.Info(fldval,"")
s = 0
zipfld = thl.FindField("zipcode")
shpfld = thl.FindField("shape")
found = "no"
for each n in thl
if ((thl.ReturnValue(zipfld,s).AsString = fldval)) then
poly = thl.ReturnValue(shpfld,s)
polps = poly.ReturnCenter
xcord = polps.GetX
ycord = polps.GetY
found = "yes"
end
s = s + 1
end
if (found = "no") then
MsgBox.Error("Can't find the Zip id you entered","")
exit
end
'-- Check for the existence of fields
fldnames = thl.GetFields
count = 0
for each x in fldnames
if (x.AsString = "xcord") then
count = count + 1
end
if (x.AsString = "ycord") then
count = count + 1
end
if (x.AsString = "Distance") then
count = count + 1
end
end
if ((count = 1) or (count = 2)) then
MsgBox.Error("The file "+thl.AsString+"+NL+
some but not all of the fields needed. You must create them manually","")
exit
end
if (count = 3) then
xcords = thl.FindField("xcord")
ycords = thl.FindField("ycord")
Dist = thl.FindField("Distance")
end
'-- Create the xcord, ycord, Distance fields and add them to shape file
if (count <> 3) then
xcords = Field.Make("xcord",#FIELD_DECIMAL,16,6)
ycords = Field.Make("ycord",#FIELD_DECIMAL,16,6)
Dist = Field.Make("Distance",#FIELD_DECIMAL,16,6)
FldLst = {xcords,ycords,Dist}
thl.AddFields(FldLst)
end
n = 0
'-- Perform field calculations
plyFtab = Thethm.GetFtab.FindField("shape")
for each n in thl
'-- Find the shape field and return its value "polygon"
what = theThm.getFtab.ReturnValue(plyFtab,n)
'-- Get the x and y value of the centers of each polygon
pnts = what.ReturnCenter
xnum = pnts.GetX
ynum = pnts.GetY
'-- Set the x and y of the center to a record number
theThm.GetFtab.SetValue(xcords,n,xnum)
theThm.GetFtab.SetValue(ycords,n,ynum)
'-- Calculate the distance for the search point and the
'-- centroid of the polygons and set that value in the distance field
calnum = (((xnum - xcord)^2) + ((ynum - ycord)^2)).SqRt
theThm.GetFtab.SetValue(Dist,n,calnum)
n = n + 1
end
'-- End of Script
Get help from ArcGIS experts
Download the Esri Support App