Hello,
I have 3 TAB files containing radio link paths. I want to write a script that takes a new specified TAB file containing points (easting/northing coordinates) and returns those points that are 10m or less from a link path perpendicularly.
My code is as follows:
' Declare variables
Dim hSTLinkTable As Integer
Dim hMWLinkTable As Integer
Dim hFLLinkTable As Integer
Dim hPointTable As Integer
Dim hResultTable As Integer
Dim xPoint As Float
Dim yPoint As Float
Dim xStart As Float
Dim yStart As Float
Dim xEnd As Float
Dim yEnd As Float
Dim dDistance As Float
' Open the link paths and points tables
hSTLinkTable = Open Table "W:\Windfarms\path\to\file.TAB" As STLinkTable
hMWLinkTable = Open Table "W:\Windfarms\path\to\file.TAB" As MWLinkTable
hFLLinkTable = Open Table "W:\Windfarms\path\to\file.TAB" As FLLinkTable
hPointTable = Open Table "N:\WindFarms\path\to\file.TAB" As PointTable
' Create a new table to store the results
Create Table ResultTable (ID Integer, X Float, Y Float) File "N:\path\to\file.TAB"
hResultTable = TableInfo(ResultTable, TAB_INFO_NUPD)
' Loop through each point in the points table
Fetch First From PointTable
Do While Not EOT(PointTable)
xPoint = CentroidX(ObjectGeography(PointTable))
yPoint = CentroidY(ObjectGeography(PointTable))
' Check against Scanning Links
Fetch First From STLinkTable
Do While Not EOT(STLinkTable)
xStart = NodeX(ObjectGeography(STLinkTable), 1)
yStart = NodeY(ObjectGeography(STLinkTable), 1)
xEnd = NodeX(ObjectGeography(STLinkTable), 2)
yEnd = NodeY(ObjectGeography(STLinkTable), 2)
' Calculate the perpendicular distance from the point to the line segment
dDistance = CalculatePerpendicularDistance(xPoint, yPoint, xStart, yStart, xEnd, yEnd)
' Check if the distance is less than or equal to 10 meters
If dDistance <= 10 Then
' Insert the point into the results table
Insert Into ResultTable (ID, X, Y) Values (RowID(PointTable), xPoint, yPoint)
Exit Do ' Exit the inner loop if a link is found within 10 meters
End If
Fetch Next From STLinkTable
Loop
' Check against Microwave Links
Fetch First From MWLinkTable
Do While Not EOT(MWLinkTable)
xStart = NodeX(ObjectGeography(MWLinkTable), 1)
yStart = NodeY(ObjectGeography(MWLinkTable), 1)
xEnd = NodeX(ObjectGeography(MWLinkTable), 2)
yEnd = NodeY(ObjectGeography(MWLinkTable), 2)
' Calculate the perpendicular distance from the point to the line segment
dDistance = CalculatePerpendicularDistance(xPoint, yPoint, xStart, yStart, xEnd, yEnd)
' Check if the distance is less than or equal to 10 meters
If dDistance <= 10 Then
' Insert the point into the results table
Insert Into ResultTable (ID, X, Y) Values (RowID(PointTable), xPoint, yPoint)
Exit Do ' Exit the inner loop if a link is found within 10 meters
End If
Fetch Next From MWLinkTable
Loop
' Check against Fixed Links
Fetch First From FLLinkTable
Do While Not EOT(FLLinkTable)
xStart = NodeX(ObjectGeography(FLLinkTable), 1)
yStart = NodeY(ObjectGeography(FLLinkTable), 1)
xEnd = NodeX(ObjectGeography(FLLinkTable), 2)
yEnd = NodeY(ObjectGeography(FLLinkTable), 2)
' Calculate the perpendicular distance from the point to the line segment
dDistance = CalculatePerpendicularDistance(xPoint, yPoint, xStart, yStart, xEnd, yEnd)
' Check if the distance is less than or equal to 10 meters
If dDistance <= 10 Then
' Insert the point into the results table
Insert Into ResultTable (ID, X, Y) Values (RowID(PointTable), xPoint, yPoint)
Exit Do ' Exit the inner loop if a link is found within 10 meters
End If
Fetch Next From FLLinkTable
Loop
Fetch Next From PointTable
Loop
' Close all tables
Close Table STLinkTable
Close Table MWLinkTable
Close Table FLLinkTable
Close Table PointTable
Close Table ResultTable
' Function to calculate the perpendicular distance from a point to a line segment
Function CalculatePerpendicularDistance(xPoint As Float, yPoint As Float, xStart As Float, yStart As Float, xEnd As Float, yEnd As Float) As Float
Dim dx As Float
Dim dy As Float
Dim mag As Float
Dim u As Float
Dim xClosest As Float
Dim yClosest As Float
Dim dDist As Float
dx = xEnd - xStart
dy = yEnd - yStart
mag = dx * dx + dy * dy
If mag = 0 Then
' The start and end points are the same, use the distance from the point to this point
dDist = Sqrt((xPoint - xStart) * (xPoint - xStart) + (yPoint - yStart) * (yPoint - yStart))
Else
u = ((xPoint - xStart) * dx + (yPoint - yStart) * dy) / mag
If u < 0 Then
' Closest point is the start point
xClosest = xStart
yClosest = yStart
ElseIf u > 1 Then
' Closest point is the end point
xClosest = xEnd
yClosest = yEnd
Else
' Closest point is somewhere on the line segment
xClosest = xStart + u * dx
yClosest = yStart + u * dy
End If
dDist = Sqrt((xPoint - xClosest) * (xPoint - xClosest) + (yPoint - yClosest) * (yPoint - yClosest))
End If
CalculatePerpendicularDistance = dDist
End Function
--------
This results in the following errors:
17 Unrecognized Command: Table. Too many errors. Error information has been lost.
19 Unrecognized Command: Table. Too many errors. Error information has been lost.
29 Incorrect number of arguments to function. Too many errors. Error information has been lost.
30 Incorrect number of arguments to function. Too many errors. Error information has been lost.
35 Unrecognized command: (. Too many errors. Error information has been lost.
46 Found [(] while searching for [)]. Too many errors. Error information has been lost.
56 Unrecognized command: (. Too many errors. Error information has been lost.
67 Found [(] while searching for [)]. Too many errors. Error information has been lost.
77 Unrecognized command: (. Too many errors. Error information has been lost.
88 Found [(] while searching for [)]. Too many errors. Error information has been lost.
106 No prototype declared for Sub procedure or Function CalculatePerpendicularDistance. Too many errors. Error information has been lost.
106 Sub procedure or Function CalculatePerpendicularDistance does not match Declare. Too many errors. Error information has been lost.
107 Sub procedure or Function CalculatePerpendicularDistance does not match Declare. Too many errors. Error information has been lost.
121 Unrecognized command: (. Too many errors. Error information has been lost.
It would be much appreciated if someone is able to shed some light on what I am doing wrong and how to fix the issues.
Thanks,
Jacob