Finding links within 10m of a point

22 views
Skip to first unread message

Jacob Chambers

unread,
Jul 23, 2024, 11:42:36 AM (4 days ago) Jul 23
to MapInfo-L
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

Peter Horsbøll Møller

unread,
Jul 23, 2024, 12:36:54 PM (4 days ago) Jul 23
to mapi...@googlegroups.com
Hi Jacob

Open Table is a statement and doesn’t return any ID

Change the statements from this:

hSTLinkTable = Open Table "W:\Windfarms\path\to\file.TAB" As STLinkTable

to this:

Open Table "W:\Windfarms\path\to\file.TAB" As STLinkTable

Let us know if you have more errors after that change and a recompile.

But the way, MapBasic comes with a function that can calculate the distance between two objects: 
ObjectDistance( object1, object2, unit_name )

Cheers
Peter Horsbøll Møller
Peter Horsbøll Møller
Principal Sales Engineer - Distinguished Engineer

Uffe Kousgaard

unread,
Jul 24, 2024, 1:49:55 AM (3 days ago) Jul 24
to mapi...@googlegroups.com
Hi,

Since you have a function, you need to declare it first and you also need to put the rest of your code in a "sub main" module, which also needs to be declared.

Declare Sub Main
Declare Function CalculatePerpendicularDistance(xPoint As Float, yPoint As Float, xStart As Float, yStart As Float, xEnd As Float, yEnd As Float) As Float

Sub Main
<main code here>
End Sub


Function CalculatePerpendicularDistance(xPoint As Float, yPoint As Float, xStart As Float, yStart As Float, xEnd As Float, yEnd As Float) As Float
<code here>
End Function

Kind regards

Uffe Kousgaard
routeware.dk
--
--
You received this message because you are subscribed to the
Google Groups "MapInfo-L" group.To post a message to this group, send
email to mapi...@googlegroups.com
To unsubscribe from this group, go to:
http://groups.google.com/group/mapinfo-l/subscribe?hl=en
For more options, information and links to MapInfo resources (searching
archives, feature requests, to visit our Wiki, visit the Welcome page at
http://groups.google.com/group/mapinfo-l?hl=en

---
You received this message because you are subscribed to the Google Groups "MapInfo-L" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mapinfo-l+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mapinfo-l/486b357a-c72d-4e50-81f8-c865c92ad691n%40googlegroups.com.

Reply all
Reply to author
Forward
0 new messages