HOW TO
Code:
'--- Calcdist.ave
'--- This script is a modification of the calcdist.ave sample script.
'--- It calculates distances from every selected point in
'--- one point-theme to every point in another point theme.
'--- It will only calculate distances between points that have a common ID value.
theThemeList = av.GetActiveDoc.GetThemes.Clone
newThemeList = {}
for each thm in theThemeList
if (thm.getftab.findfield("shape").gettype = #FIELD_ShapePOINT) then
newThemeList.add(thm)
end
end
thePrj = av.GetActiveDoc.GetProjection
'--- Use these lines to prompt the user for input parameters...
theFromTheme = (MsgBox.List (newThemeList,"to calculate the distance FROM.", "Please select a point theme..."))
if (theFromTheme = nil) then exit end
theFromFtab = theFromTheme.GetFTab
newThemeList.RemoveObj(theFromTheme)
theToTheme = (MsgBox.List(newThemeList,"to calculate the distance TO.", "Please select a point theme..."))
if (theToTheme = nil) then exit end
theToFtab = theToTheme.GetFTab
theFromIDField = MsgBox.ListAsString(theFromFtab.GetFields, "containing the point identifier.","From Theme: Please select a field...")
if (theFromIDField = nil) then
exit
end
theToIDField = MsgBox.ListAsString(theToFtab.GetFields,"containing the point
identifier.", "To Theme: Please select a field...")
if (theToIDField = nil) then
exit
end
theFromShapeField = theFromFTab.FindField("Shape")
theToShapeField = theToFtab.FindField("Shape")
theToFtab.SetEditable(true)
theNewFieldName = msgbox.input
'--- If the field name already exists in the table,
'--- its values will get overwritten",
"Input a field name for your new distance field:","dist_2_id(x)")
'--- If a distance field doesn't exist, add it...
if (theToFtab.FindField(theNewFieldName) = nil) then
'--- Choose the field size commensurate with the view's projection.
if (thePrj.IsNull) then
theDistanceField = field.make(theNewFieldName,#field_decimal,8,4)
else
theDistanceField = field.make(theNewFieldName,#field_decimal,8,2)
end
theToFtab.addfields({theDistanceField})
'--- If a distance field does exist, clear it...
else
if (theToFtab.FindField(theNewFieldName).IsTypeString = true) then
MsgBox.Info("Your input field name cannot be overwritten with number
values", "Please start over and select a different field name...")
exit
end
theToFtab.Calculate("0",theToFtab.FindField(theNewFieldName))
theDistanceField = theToFTab.FindField(theNewFieldName)
end
if (theFromFtab.GetSelection.Count = 0) then
theRecordsToCalc = theFromFtab
numRecs = theFromFtab.GetNumRecords
else
theRecordsToCalc = theFromFtab.GetSelection
numRecs = theRecordsToCalc.Count
end
for each f in theRecordsToCalc
av.ShowMsg("Calculating Distance Values")
av.SetStatus( (f / numRecs) * 100 )
'--- Get the point location you are measuring FROM.
'--- If the view is projected, get the point location in projected units.
theID = theFromFtab.ReturnValue(theFromIDField,f)
theFromShape = theFromFTab.ReturnValue(theFromShapeField,f)
if (thePrj.IsNull.Not) then
theFromShape = theFromShape.ReturnProjected(thePrj)
end
for each t in theToFtab
theToID = theToFtab.returnvalue(thetoidfield,t)
'msgbox.info("thetoid",thetoid.asstring)
if (thetoid = theid) then
' Get the point location you are measuring TO.
' Get it in projected units, if the view is projected.
theToShape = theToFTab.ReturnValue(theToFtab.FindField("Shape"),t)
if (thePrj.IsNull.Not) then
theToShape = theToShape.ReturnProjected(thePrj)
end
'--- Calculate the distance between the two points.
'--- Add the value to the output (TO) branch table.
theDistance = theFromShape.Distance(theToShape)
theToFtab.SetValue(theDistanceField,t,theDistance)
end
end
end
av.ClearMsg
av.ClearStatus
theToFtab.SetEditable(false)
av.ShowMsg("Distance calculation complete")
'--- End of Script
Note:
This script can be downloaded from the ArcScripts page:
http://arcscripts.esri.com/
Article ID:000001551
Get help from ArcGIS experts
Download the Esri Support App