Jump to content
The simFlight Network Forums
idoen

Cant read nearest icao in c#

Recommended Posts

Hi Ido,

 

Declare the offset like this:

private Offset<string> nearestICAO = new Offset<string>(0x0658, 4);

Then in your code, after the process(), read the value like this:

FSUIPCConnection.Process();
MessageBox.Show("Nearest airport is " + this.nearestICAO.Value);

Paul

Share this post


Link to post
Share on other sites

Hi Ido,

 

Declare the offset like this:

private Offset<string> nearestICAO = new Offset<string>(0x0658, 4);

Then in your code, after the process(), read the value like this:

FSUIPCConnection.Process();
MessageBox.Show("Nearest airport is " + this.nearestICAO.Value);

Paul

Hi Paul,

 

Everything works now.

Thank you very much for your quickly reply.

 

Ido

Share this post


Link to post
Share on other sites

In the zip file that you downloaded there is a docs folder. In there is a file called userguide.pdf. On page 7 there is a table explaining how to choose the variable type from the information given in Pete's document "FSUIPC4 Offset Status.pdf".

 

Paul

Share this post


Link to post
Share on other sites

In the zip file that you downloaded there is a docs folder. In there is a file called userguide.pdf. On page 7 there is a table explaining how to choose the variable type from the information given in Pete's document "FSUIPC4 Offset Status.pdf".

 

Paul

Thank you very much!

Share this post


Link to post
Share on other sites

Hi Paul,

 

Is there a way to have the nearest airport from given coordinates ?

 

private Offset<string> nearestICAO = new Offset<string>(0x0658, 4);

...

double Lat = 48.4

double Lon = -4.4

 

private string NearestAirpot(Lat, Lon)

{

...

FSUIPCConnection.Process();
...

return this.nearestICAO.Value;  // "LFRB"

}

 

Thank you for your help !

 

Motus

Share this post


Link to post
Share on other sites

Is there a way to have the nearest airport from given coordinates ?

 

No. FSX/P3D only provides nearest airports to the current user aircraft position. You'd need to search a database for your needs.

 

Pete

Share this post


Link to post
Share on other sites

Ou tu peux lire le fichier runway.csv fait à partir de Makerunways 

 

Ex: http://forum.simflight.com/topic/79503-howto-find-runway-in-use-by-wind-direction/#entry481072

 

You can read runway.csv to know the nearest ICAO

Ex: http://forum.simflight.com/topic/79503-howto-find-runway-in-use-by-wind-direction/#entry481072

 

Look at here (thanks to Paul again).

 

Regards Fred

Share this post


Link to post
Share on other sites
On 13/12/2014 at 2:52 PM, Paul Henty said:

Hi Ido,

 

Declare the offset like this:


private Offset<string> nearestICAO = new Offset<string>(0x0658, 4);

Then in your code, after the process(), read the value like this:


FSUIPCConnection.Process();
MessageBox.Show("Nearest airport is " + this.nearestICAO.Value);

Paul

Hello Paul,

i'm trying this code but in vb.net and only show icao from p3d v4. From fs9/fsx sp2/p3d v3.4 is not showing.

Is there another workaround to show on all simulators? Didnt find anything.

Can you help me with a function to get nearest airport(s) in X miles range with makerunways files like airports.fsm?

Thank you

Share this post


Link to post
Share on other sites

It works fine here on FSX Steam Edition.

    Private NearestICAO As Offset(Of String) = New Offset(Of String)(&H658, 4)
        FSUIPCConnection.Process()
        MessageBox.Show("Nearest Airport is " + NearestICAO.Value)

It won't work on FS9 (this feature is not included in FSUIPC3), and the documentation says it doesn't work on FSX earlier than SP2.

If you want a solution that will work with all sims including FS9 you will need use the makerunways files.

Here is an example of a function that reads the runways.csv file produced by makerunways.exe.

It assembles a list of all airports within a requested radius.

First you'll need to define this structure (outside any other classes)
 

Public Structure AirportInfo
    Public ICAO As String
    Public DistanceNM As Double

    Public Sub New(ICAO As String, DistanceNM As Double)
        Me.ICAO = ICAO
        Me.DistanceNM = DistanceNM
    End Sub

End Structure

Then add this function to your normal class/form:

Private Function getAirportsInRangeNM(ByVal playerPos As FsLatLonPoint, ByVal radiusNM As Double) As List(Of AirportInfo)
        Dim AirportsInRange As List(Of AirportInfo) = New List(Of AirportInfo)()
        ' Search the Runways.csv file
        Dim foundAirports As Dictionary(Of String, Double) = New Dictionary(Of String, Double)()
        Dim runwaysFile As String = "E:\SteamLibrary\steamapps\common\FSX\Runways.csv" ' !!CHANGE THIS!!
        Using MyReader As New FileIO.TextFieldParser(runwaysFile)
            MyReader.TextFieldType = FileIO.FieldType.Delimited
            MyReader.SetDelimiters(",")
            While Not MyReader.EndOfData
                Try
                    Dim currentRow As String() = MyReader.ReadFields()
                    Dim icao As String = currentRow(0)
                    Dim lat As Double = Double.Parse(currentRow(2))
                    Dim lon As Double = Double.Parse(currentRow(3))
                    ' Work out the distance from the player
                    Dim runwayPoint As FsLatLonPoint = New FsLatLonPoint(New FsLatitude(lat), New FsLongitude(lon))
                    Dim distance As Double = runwayPoint.DistanceFromInNauticalMiles(playerPos)
                    If (distance) <= radiusNM Then
                        If (foundAirports.ContainsKey(icao)) Then
                            ' if this runway is closer we'll use this one
                            If distance < (foundAirports(icao)) Then
                                foundAirports(icao) = distance
                            End If
                        Else
                            foundAirports.Add(icao, distance)
                        End If
                    End If
                Catch ex As FileIO.MalformedLineException
                    'MsgBox("Line " & ex.Message & "is not valid and will be skipped.")
                End Try
            End While
            MyReader.Close()
            MyReader.Dispose()
        End Using
        ' Now have all the airports in range
        ' Add them to the output list and sort by distance
        For Each icao As String In foundAirports.Keys
            AirportsInRange.Add(New AirportInfo(icao, foundAirports(icao)))
        Next
        AirportsInRange.Sort(Function(x, y) x.DistanceNM.CompareTo(y.DistanceNM))
        Return AirportsInRange
    End Function

Note that I've hard-coded my path to the runways.csv file. If your app is not just for you, you'll need to have a variable with the users FS install path in.

Here's an example of how it's used:

1. You'll need the current players lon/lat if you haven't got those already:

    Public playerLatitude As Offset(Of Long) = New Offset(Of Long)(&H560)
    Public playerLongitude As Offset(Of Long) = New Offset(Of Long)(&H568)

2. This code calls the function and displays all airports within 50nm in a message box:

        FSUIPCConnection.Process()
        ' Set up current player position
        Dim playerPos As FsLatLonPoint = New FsLatLonPoint(New FsLatitude(playerLatitude.Value), New FsLongitude(playerLongitude.Value))
        ' get list of airports within 50nm
        Dim airports As List(Of AirportInfo) = getAirportsInRangeNM(playerPos, 50)
        ' print them out
        Dim msg As String = ""
        For Each airport As AirportInfo In airports
            msg += airport.ICAO + ": " + airport.DistanceNM.ToString("F0") + "NM" + vbNewLine
        Next
        MessageBox.Show(msg)

The List of airports is in order of distance, the closest will be index 0.

Paul

  • Like 2

Share this post


Link to post
Share on other sites

Hello Paul thank you for your reply.

I change this to get it working

Dim lat As Double = Double.Parse(currentRow(2))
Dim lon As Double = Double.Parse(currentRow(3))

to this

Dim lat As Double = Double.Parse(currentRow(2), CultureInfo.InvariantCulture)
Dim lon As Double = Double.Parse(currentRow(3), CultureInfo.InvariantCulture)

and now its working perfect, just what i need.

Thank you for your time and help i appreciate.

Helder

Share this post


Link to post
Share on other sites
Dim lat As Double = Double.Parse(currentRow(2), CultureInfo.InvariantCulture)
Dim lon As Double = Double.Parse(currentRow(3), CultureInfo.InvariantCulture)

Ah, yes. I forgot that European number formats are different.

Thanks for the correction.

Paul

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


×