' Name: View.CalculateDistance ' ' Title: Calculates distances from points in one theme to points in another ' ' Topics: Analysis ' ' Description: This script will prompt you for two point themes in the ' active view. The first is the point theme containing the selected points ' that you wish to calculate the distance FROM. The second is the point theme ' containing points that you wish to calculate the distance TO. ' ' You will also be prompted for an identifying field in the ' FROM theme. The value of this field will be used to name to ' distance field in the TO theme. ' ' A distance field will be added to the TO theme for each point ' selected in the FROM theme. This distance field will be populated ' with the distance between the selected From point and the To point. ' ' If the view is projected, the distance will be returned in distance units. ' ' Requires: This script should be attached to a button in the view GUI. To ' prepare to use this script: ' '- 1. Add to your view two point themes. '- 2. Determine which is to be theFromTheme and which is to be theToTheme. '- 3. Determine which item is to be the identifying field. This is most likely '- a field that contains a key number or name. '- 4. Create a selected set of points in theFromTheme, if you like. '- 5. Attach this script to a button inthe Veiw GUI, if you like. ' ' Self: ' ' Returns: theThemeList = av.GetActiveDoc.GetThemes.Clone thePrj = av.GetActiveDoc.GetProjection ' Use these lines to hard-code the input parameters... 'theFromFtab = av.GetProject.FindDoc("View1").FindTheme("FromTheme").GETFTab 'theFromIDField = theFromFtab.FindField("Bnmb") 'theToTheme = av.GetProject.FindDoc("View1").FindTheme("ToTheme") 'theToFtab = theToTheme.GetFTab ' Use these lines to prompt the user for input parameters... theFromTheme = (MsgBox.List(theThemeList,"to calculate the distance FROM.","Please select a theme...")) if (theFromTheme = nil) then exit end theFromFtab = theFromTheme.GetFTab theThemeList.RemoveObj(theFromTheme) theToTheme = (MsgBox.List(theThemeList,"to calculate the distance TO.","Please select a theme...")) if (theToTheme = nil) then exit end theToFtab = theToTheme.GetFTab theFromIDField = MsgBox.ListAsString(theFromFtab.GetFields,"containing the point identifier.","Please select a field...") if (theFromIDField = nil) then exit end theFromShapeField = theFromFTab.FindField("Shape") theToShapeField = theToFtab.FindField("Shape") theToFtab.SetEditable(true) for each f in theFromFtab.GetSelection theFromIDValue = theFromFtab.ReturnValueString(theFromIDField,f) theNewFieldName = "DistTo"+theFromIDValue ' 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 theToFtab.Calculate("0",theToFtab.FindField(theNewFieldName)) theDistanceField = theToFTab.FindField(theNewFieldName) end ' Get the point location you are measuring FROM. ' If the view is projected, get the the point location in projected units. theFromShape = theFromFTab.ReturnValue(theFromShapeField,f) if (thePrj.IsNull.Not) then theFromShape = theFromShape.ReturnProjected(thePrj) end for each t in theToFtab ' 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 theToFtab.SetEditable(false) av.ShowMsg("Distance calculation complete")