Hi Mike

Sorry, I got drawn into some other business and never got back to you.

I have now built the function into a small tool that publishes the calculation as a function inside MapInfo Pro.

You can use it via the SQL Window or MapBasic window and update a query or an entire table with one statement:

`Set CoordSys Table LINESWithBearing`

`Update LINESWithBearing `

` Set BEARING = CalculateBearingSperical(OBJ)`

When testing the tool I did however notice that the horizontal lines seem to give the same bearing no matter if they go from west to east or from east to west. I guess my formula has an error.

Maybe someone here in the community can tell me where the mistake is:

`'Formula taken from StackExchange: `

`'https://stackoverflow.com/questions/8123049/calculate-bearing-between-two-locations-lat-long`

`'double dLon = (lng2-lng1);`

`'double y = Math.sin(dLon) * Math.cos(lat2);`

`'double x = Math.cos(lat1)*Math.sin(lat2) - Math.sin(lat1)*Math.cos(lat2)*Math.cos(dLon);`

`'double brng = Math.toDegrees((Math.atan2(y, x)));`

`'brng = (360 - ((brng + 360) % 360));`

`fLong1 = ObjectNodeX(oSel, 1, 1)`

`fLat1 = ObjectNodeY(oSel, 1, 1)`

`fLong2 = ObjectNodeX(oSel, 1, ObjectInfo(oSel, OBJ_INFO_NPNTS))`

`fLat2 = ObjectNodeY(oSel, 1, ObjectInfo(oSel, OBJ_INFO_NPNTS))`

`fLongDiff = (fLong2 - fLong1)`

`' Print "Long Diff.: " & FormatNumber$(fLongDiff)`

`fY = Sin(fLongDiff) * Cos(fLat2)`

`fX = Cos(fLat1) * Sin(fLat2) - Sin(fLat1) * Cos(fLat2) * Cos(fLongDiff)`

`fBearing = Atn(fY / fX) * RAD_2_DEG '57.29577951`

`' Print "Bearing 1: " & FormatNumber$(fBearing)`

`fBearing = IIf(fBearing >= 0, fBearing, fBearing + 360)`

`' Print "Bearing 2: " & FormatNumber$(fBearing)`

I have attached the application and the MapBasic sourcecode

------------------------------

Peter Horsbøll Møller

Principal Presales Consultant | Distinguished Engineer

Precisely | Trust in Data

------------------------------

Original Message:

Sent: 08-27-2021 09:13

From: Uffe Kousgaard

Subject: Calculate the Segment Line Direction in MapInfo in a geographic coordinate system (GCS)

What Peter suggests, is the alternative formula here:

https://edwilliams.org/avform147.htm#Crs

------------------------------

Uffe Kousgaard

ROUTEWARE

Roskilde

Original Message:

Sent: 08-26-2021 04:04

From: Mike Woodbury

Subject: Calculate the Segment Line Direction in MapInfo in a geographic coordinate system (GCS)

Hi,

Does anyone know how to calculate the line direction of a line segment in MapInfo in a geographic coordinate system (GCS) over a projected coordinate system (PCS)

There is a function in Discover however, you need to work in a projected coordinate system (PCS) and it will not work in a geographic coordinate system (GCS)

As well, we work over the entire Australia, so we cross a number of zones e.g. MGA GDA94 z49-52 in WA alone

I have also checked out Peter H PPT however, the Calc function described works on a single line / segment (using MapCAD)

** **

**Aim:**

Calc the Segment Line direction = azimuth for all of WA faults in geographic coordinate system (GCS); Long/Lat GDA94

Please assist with tools or suggestion in MapInfo, as Discover tool doesn't suite our needs and will not work with GCS

Cheers

PS. A links for QGIS to calc the line direction below:

https://gis.stackexchange.com/questions/55449/how-do-i-find-vector-line-bearing-in-qgis-or-grass

With PyQGIS in the Python console, see How to add Direction and Distance to attribute table? for the azimuths of the segments of a line (with the azimuth functions of Points: **point1.azimuth(point2**))

------------------------------

Mike Woodbury

Knowledge Community Shared Account

------------------------------