l0calhost Posted December 31, 2016 Report Posted December 31, 2016 Hello Paul Henty, i'm using your FSUIPCClient DLL V3 and is working good. I have added the Makerwys with a function getrunway copied from here and is working too. Now i need to find how to detect the gate where the plane is, tried like function getrunway but no success. Can you help me and post a function or some code to detect gate? I apreciate. Sorry my english Thank you
Paul Henty Posted December 31, 2016 Report Posted December 31, 2016 Do you have the lon/lat of each gate? or is that what you're stuck on? Paul
l0calhost Posted December 31, 2016 Author Report Posted December 31, 2016 13 minutes ago, Paul Henty said: Do you have the lon/lat of each gate? or is that what you're stuck on? Paul Hello thank you for your reply. Yes i have the plane lat/lon and all gates lat/lon from g5.csv where gate = icao. I'm stuck from here. I try using the distance function and sometimes don't show the result, or the correct result and sometimes show the correct result. Dim distance As Double = distance(acLat, acLon, gateLat, gateLon, "M") If distance = smallestDifference Then smallestDifference = distance ..... I'm not a pro coder, i think is not the correct code to calculate the current gate! Thank you
Paul Henty Posted December 31, 2016 Report Posted December 31, 2016 Assuming that your distance function is correct, you need something like this: I don't know how you are loop though each gate so I've just put a placeholder for that. Also I don't know what units your distance function returns so I've assumed Metres. If it's different just adjust the values. Dim currentGate as String = "" Dim minDistanceToGate as Double = 20 'Need to be within 20 metres of the gate. (Adjust to your preference). [Loop for each gate] Dim distance As Double = distance(acLat, acLon, gateLat, gateLon, "M") If distance <= minDistanceToGate Then currentGate = strGateNumber End If [End loop for each gate] ' currentGate now contains the gate you are at. Or "" if you're not near any gate. That's the sort of loop and logic you'll need. If you need more help please post the exact code you are trying and I'll be able to see where it's going wrong. Paul
l0calhost Posted December 31, 2016 Author Report Posted December 31, 2016 14 minutes ago, Paul Henty said: Assuming that your distance function is correct, you need something like this: I don't know how you are loop though each gate so I've just put a placeholder for that. Also I don't know what units your distance function returns so I've assumed Metres. If it's different just adjust the values. Dim currentGate as String = "" Dim minDistanceToGate as Double = 20 'Need to be within 20 metres of the gate. (Adjust to your preference). [Loop for each gate] Dim distance As Double = distance(acLat, acLon, gateLat, gateLon, "M") If distance <= minDistanceToGate Then currentGate = strGateNumber End If [End loop for each gate] ' currentGate now contains the gate you are at. Or "" if you're not near any gate. That's the sort of loop and logic you'll need. If you need more help please post the exact code you are trying and I'll be able to see where it's going wrong. Paul Hello Paul, this is my current code Public Function getgate(ByVal icaocode As String) As String Using MyReader As New FileIO.TextFieldParser(Form1.fullFSpath.Value & "g5.csv") MyReader.TextFieldType = FileIO.FieldType.Delimited MyReader.SetDelimiters(",") Dim gatename As String Dim gatenumber As String Dim gatereturn As String Dim currentRow As String() Dim smallestDifference As Double = 0 While Not MyReader.EndOfData Try currentRow = MyReader.ReadFields() If currentRow(0) = icaocode Then gatename = currentRow(1) gatenumber = currentRow(2) Dim aaa As String = currentRow(3) Dim bbb As String = currentRow(4) Dim aaaa As Double = Double.Parse(aaa, CultureInfo.InvariantCulture) Dim bbbb As Double = Double.Parse(bbb, CultureInfo.InvariantCulture) Dim distance As Double = Form1.distance(Functions.getAircraftLatFS(), Functions.getAircraftLonFS(), aaaa, bbbb, "M") If distance = smallestDifference Then smallestDifference = distance gatereturn = "" + gatename + " " + gatenumber + "" Return gatereturn MyReader.Close() MyReader.Dispose() Exit Function 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 Return "" End Function I am in LPPT on gate 104, if the distance result is 0 it show gate 104. if i change distance = to distance <= no result show if i change smallDiference to 20 and keep <= it will show gate 105 but i'm on 104. (105 is near but is on my left side) the distance function i have is 'M' is statute miles (default) 'K' is kilometers 'N' is nautical miles Public Function distance(ByVal lat1 As Double, ByVal lon1 As Double, ByVal lat2 As Double, ByVal lon2 As Double, Optional ByVal unit As Char = "M"c) As Double Dim theta As Double = lon1 - lon2 Dim dist As Double = Math.Sin(deg2rad(lat1)) * Math.Sin(deg2rad(lat2)) + Math.Cos(deg2rad(lat1)) * Math.Cos(deg2rad(lat2)) * Math.Cos(deg2rad(theta)) dist = Math.Acos(dist) dist = rad2deg(dist) dist = dist * 60 * 1.1515 If unit = "K" Then dist = dist * 1.609344 ElseIf unit = "N" Then dist = dist * 0.8684 End If Return dist End Function Thank you for your help
Paul Henty Posted December 31, 2016 Report Posted December 31, 2016 Thanks. I can fix this up to work properly. Just one thing I need to know: Do you want to always get back the closest gate even if you're a long way from it (e.g. at the end of the runway). Or do you only want to report being at a gate when you're parked at it (e.g. within 20 metres or so?). Paul
l0calhost Posted December 31, 2016 Author Report Posted December 31, 2016 1 minute ago, Paul Henty said: Thanks. I can fix this up to work properly. Just one thing I need to know: Do you want to always get back the closest gate even if you're a long way from it (e.g. at the end of the runway). Or do you only want to report being at a gate when you're parked at it (e.g. within 20 metres or so?). Paul I think is better to report always in the correct gate when i park instead the closest one. The g5.csv have the gate radius in meters. Something like, if airplane is inside gate "radius" show gate. Thank you Paul
Paul Henty Posted December 31, 2016 Report Posted December 31, 2016 Okay - Try this. It reports you're at a gate when you are inside the radius of that gate. Public Function getgate(ByVal icaocode As String) As String ' Returns the current gate or "" is not parked at any gate. Using MyReader As New FileIO.TextFieldParser(Form1.fullFSpath.Value & "g5.csv") MyReader.TextFieldType = FileIO.FieldType.Delimited MyReader.SetDelimiters(",") Dim gatename As String Dim gatenumber As String Dim gatereturn As String = "" Dim currentRow As String() While Not MyReader.EndOfData Try currentRow = MyReader.ReadFields() If currentRow(0) = icaocode Then gatename = currentRow(1) gatenumber = currentRow(2) Dim aaa As String = currentRow(3) Dim bbb As String = currentRow(4) Dim aaaa As Double = Double.Parse(aaa, CultureInfo.InvariantCulture) Dim bbbb As Double = Double.Parse(bbb, CultureInfo.InvariantCulture) Dim gateRadius As String = currentRow(5) Dim gateRadiusKm As Double = Double.Parse(gateRadius, CultureInfo.InvariantCulture) / 1000D ' *** Distance now returned in Km *** Dim distance As Double = Form1.distance(Functions.getAircraftLatFS(), Functions.getAircraftLonFS(), aaaa, bbbb, "K") If distance <= gateRadiusKm Then gatereturn = gatename + " " + gatenumber MyReader.Close() MyReader.Dispose() Return gatereturn 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() Return "" ' Not near any gate End Using End Function Paul
l0calhost Posted December 31, 2016 Author Report Posted December 31, 2016 I think that's it, made the same tests as before and now seems to be working, changed between some gates and all show the correct gate. I apreciate your help, thank you very much and have a nice year. Helder Duarte
Frédéric-O DUCHEMIN Posted June 9, 2017 Report Posted June 9, 2017 Hello, Can you please share the complete code ? and explain please this Dim distance As Double = Form1.distance(Functions.getAircraftLatFS(), Functions.getAircraftLonFS(), aaaa, bbbb, "M") Regards Fred
Paul Henty Posted June 9, 2017 Report Posted June 9, 2017 It's Helder's code so I don't have it. What getGate() does is read through the file called g5.csv which is a list of gate information for each airport. The file is generated from MakeRunways.exe (written by Pete and available in the downloads section of the forum). For each gate it tests the distance between the player and gate. It keeps track of the nearest gate. After checking all gates it returns the nearest gate. The 'Form1.distance()' function is shown in post 5 of this thread. It measures the distance between the player lon/lat (you can read this from FSUIPC) and a given Lon/Lat of the gate (aaaa and bbbb). You can use this or the FsLatLonPoint class from the DLL. This has a number of DistanceFromXXX methods to calculate the distance to another lon/lat point. Paul
Frédéric-O DUCHEMIN Posted June 9, 2017 Report Posted June 9, 2017 Thanks Paul I have understood this part (you make for me runways using runways.csv, thanks again) Public Function getrunaway(ByVal icaocode As String) As String Dim lon As FsLongitude = New FsLongitude(playerLongitude.Value) Dim lat As FsLatitude = New FsLatitude(playerLatitude.Value) ' Get the point for the current plane position Dim currentPosition As FsLatLonPoint = New FsLatLonPoint(lat, lon) Dim pathto As String = My.Application.Info.DirectoryPath Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser(pathto & "\runways.csv") MyReader.TextFieldType = FileIO.FieldType.Delimited MyReader.SetDelimiters(",") Dim runawayst As String Dim runawaynum As String Dim runawaydes1 As String Dim runawaydes2 As String = "" Dim runawayreturn As String Dim currentRow As String() While Not MyReader.EndOfData Try currentRow = MyReader.ReadFields() If currentRow(0) = icaocode Then runawayst = currentRow(1) runawaynum = runawayst.Substring(1, 2) runawaydes1 = runawayst.Substring(3, 1) If runawaydes1 = "0" Then runawaydes2 = "" If runawaydes1 = "1" Then runawaydes2 = "L" If runawaydes1 = "2" Then runawaydes2 = "R" If runawaydes1 = "3" Then runawaydes2 = "C" If runawaydes1 = "4" Then runawaydes2 = "W" Dim aaa As String = currentRow(2) Dim bbb As String = currentRow(3) Dim aaaa As Double Dim bbbb As Double aaaa = System.Convert.ToDouble(aaa) bbbb = System.Convert.ToDouble(bbb) Dim rwyThresholdLat As FsLatitude = New FsLatitude(aaaa) Dim rwyThresholdLon As FsLongitude = New FsLongitude(bbbb) Dim rwyMagHeading As Double = CDbl(currentRow(5)) Dim rwyMagVariation As Double = 0 Dim rwyLength As Double = CDbl(currentRow(6)) / 2 Dim rwyWidth As Double = 254D Dim thresholdCentre As FsLatLonPoint = New FsLatLonPoint(rwyThresholdLat, rwyThresholdLon) Dim trueHeading As Double = rwyMagHeading + rwyMagVariation runwayQuad = FsLatLonQuadrilateral.ForRunway(thresholdCentre, trueHeading, rwyWidth, rwyLength) If runwayQuad.ContainsPoint(currentPosition) = True Then runawayreturn = runawaynum & runawaydes2 Return runawayreturn MyReader.Close() MyReader.Dispose() Exit Function End If End If Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException 'MsgBox("Line " & ex.Message & "is not valid and will be skipped.") End Try End While MyReader.Close() MyReader.Dispose() End Using Return "" End Function So missing so explaination here : lat1 ? lon1 ? lat2 ? lon2 ? to adapt to my code Public Function distance(ByVal lat1 As Double, ByVal lon1 As Double, ByVal lat2 As Double, ByVal lon2 As Double, Optional ByVal unit As Char = "M"c) As Double Dim theta As Double = lon1 - lon2 Dim dist As Double = Math.Sin(deg2rad(lat1)) * Math.Sin(deg2rad(lat2)) + Math.Cos(deg2rad(lat1)) * Math.Cos(deg2rad(lat2)) * Math.Cos(deg2rad(theta)) dist = Math.Acos(dist) dist = rad2deg(dist) dist = dist * 60 * 1.1515 If unit = "K" Then dist = dist * 1.609344 ElseIf unit = "N" Then dist = dist * 0.8684 End If Return dist End Function
Paul Henty Posted June 9, 2017 Report Posted June 9, 2017 lat1 and lon1 should be the longitude and latitude of the player. You can get these from offset 0560 and 0568. lon2 and lat2 should be the lon/lat of whatever you are measuring the distance from. In Helder's code it was Gates. If you want the distance to runways, for example, then use: rwyThresholdLat and rwyThresholdLon from your code. Paul
Frédéric-O DUCHEMIN Posted June 10, 2017 Report Posted June 10, 2017 the code Public Function getgate(ByVal icaocode As String) As String Dim lon As FsLongitude = New FsLongitude(playerLongitude.Value) Dim lat As FsLatitude = New FsLatitude(playerLatitude.Value) ' Get the point for the current plane position Dim currentPosition As FsLatLonPoint = New FsLatLonPoint(lat, lon) ' Returns the current gate or "" is not parked at any gate. Dim pathto As String = My.Application.Info.DirectoryPath Using MyReader As New FileIO.TextFieldParser(pathto & "\g5.csv") MyReader.TextFieldType = FileIO.FieldType.Delimited MyReader.SetDelimiters(",") Dim gatename As String Dim gatenumber As String Dim gatereturn As String = "" Dim currentRow As String() While Not MyReader.EndOfData Try currentRow = MyReader.ReadFields() If currentRow(0) = icaocode Then gatename = currentRow(1) gatenumber = currentRow(2) Dim aaa As String = currentRow(3) Dim bbb As String = currentRow(4) Dim aaaa As Double = Double.Parse(aaa, CultureInfo.InvariantCulture) Dim bbbb As Double = Double.Parse(bbb, CultureInfo.InvariantCulture) 'Dim rwyThresholdLat As FsLatitude = New FsLatitude(aaaa) 'Dim rwyThresholdLon As FsLongitude = New FsLongitude(bbbb) Dim gateRadius As String = currentRow(5) Dim gateRadiusKm As Double = Double.Parse(gateRadius, CultureInfo.InvariantCulture) / 1000D 'Dim thresholdCentre As FsLatLonPoint = New FsLatLonPoint(rwyThresholdLat, rwyThresholdLon) 'Dim trueHeading As Double = rwyMagHeading + rwyMagVariation 'runwayQuad = FsLatLonQuadrilateral.ForRunway(thresholdCentre, trueHeading, rwyWidth, rwyLength) Dim LatitudeAircraft As Offset(Of Long) = New FSUIPC.Offset(Of Long)(&H560) 'Latitude Dim LongitudeAircraft As Offset(Of Long) = New FSUIPC.Offset(Of Long)(&H568) 'Longitude ' *** Distance now returned in Km *** Dim distance As Double = FsuipcData.distance(LatitudeAircraft.Value, LongitudeAircraft.Value, aaaa, bbbb, "K") If distance <= gateRadiusKm Then 'gatereturn = gatename + " " + gatenumber gatereturn = gatename + gatenumber MyReader.Close() MyReader.Dispose() Return gatereturn 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() Return "" ' Not near any gate End Using End Function 'convert decimal degreees to radians Function deg2rad(ByVal Deg) deg2rad = CDbl(Deg * pi / 180) End Function 'convert radians to decimal degrees Function rad2deg(ByVal Rad) rad2deg = CDbl(Rad * 180 / pi) End Function Public Function distance(ByVal lat As Double, ByVal lon As Double, ByVal lat2 As Double, ByVal lon2 As Double, Optional ByVal unit As Char = "M"c) As Double Dim LatitudeAircraft As Offset(Of Long) = New FSUIPC.Offset(Of Long)(&H560) 'Latitude Dim LongitudeAircraft As Offset(Of Long) = New FSUIPC.Offset(Of Long)(&H568) 'Longitude Lat1 = LatitudeAircraft.Value Lon1 = LongitudeAircraft.Value Lat1 = Lat1 * 90 / (10001750D * 65536D * 65536D) Lon1 = Lon1 * 360 / (65536D * 65536D * 65536D * 65536D) 'd = distance(Lat1, Lon1, lat2, lon2, "N") 'distance between 2 Pos Dim theta As Double = Lon1 - lon2 Dim dist As Double = Math.Sin(deg2rad(Lat1)) * Math.Sin(deg2rad(lat2)) + Math.Cos(deg2rad(Lat1)) * Math.Cos(deg2rad(lat2)) * Math.Cos(deg2rad(theta)) dist = Math.Acos(dist) dist = rad2deg(dist) dist = dist * 60 * 1.1515 If unit = "K" Then dist = dist * 1.609344 ElseIf unit = "N" Then dist = dist * 0.8684 End If Return distance End Function call departure_gate = FsuipcData.getgate(lblDeparture.Text) Me.lstGateDeparture.Items.Clear() Me.lstGateDeparture.Items.Add(departure_gate.ToString()) The problem it's give me the first entry to airport from g5.csv LFML,Park,83,43.446349,5.216847,20.0,277.0,5,WLX,FPO,TAY LFML,Park,82,43.445926,5.217253,15.0,274.5,5,WLX,FPO,TAY LFML,Park,7,43.434318,5.205118,5.0,315.4,13 LFML,Park,11,43.434292,5.225378,14.0,329.8,3,CIV LFML,Park,12,43.434142,5.225051,10.0,328.5,2,CIV LFML,A,1,43.439212,5.224703,18.0,264.9,4,AFR,RAE LFML,A,2,43.438822,5.224675,18.0,266.2,4,AFR LFML,A,5,43.439184,5.222840,18.0,26.9,4,AFR,CCM,RAL,RAE,BZH LFML,C,8,43.440494,5.220750,36.0,49.6,4,MDG,CRL,REU,XLF,XLA,IYE,RAM,DAH,SVA,ELY,SYR,TSC LFML,A,9,43.441017,5.220390,23.0,24.8,4 LFML,C,9,43.441089,5.219935,18.0,55.3,4,AAF,REU,RAM,DAH,TUN,ELY,OHY,KLM,SWR,DLH,BAW,AMC,BIE,BMM,CSA,DBK,LGL,SYR,TAP,XLA,XLF,TAR,MSA,MDP,RNV,DBK,TCV LFML,Park,19,43.439581,5.223592,5.0,331.7,13 LFML,Park,81,43.445413,5.217760,35.0,55.8,5,FXI,ICE,TNT LFML,C,10,43.441624,5.219152,18.0,64.6,4,AAF,REU,RAM,DAH,TUN,ELY,OHY,DLH,BAW,AMC,BIE,BMM,CSA,DBK,LGL,SYR,TA,XLA,XLFP,TAR,MSA,MDP,RNV,DBK,TCV LFML,C,2,43.438494,5.224803,18.0,308.0,4,AFR LFML,A,20,43.438083,5.224739,14.0,332.7,2,TJT,CCM,RAE,RLA,AFRX,TLB,HOP LFML,C,20,43.437962,5.224119,14.0,3.8,2,TJT,CCM,RAE,RLA,AFRX,TLB,HOP LFML,A,3,43.438032,5.223518,23.0,3.6,4,AFR LFML,C,3,43.438147,5.223014,18.0,73.4,4,AFR LFML,B,4,43.438602,5.222990,18.0,73.2,4,AFR,CCM,RAL,RAE,BZH LFML,A,6,43.439695,5.222241,18.0,28.7,4 LFML,C,6,43.439698,5.221780,18.0,62.1,4,SAB,SNB,KLM,AFR,CCM,DLH,BAW,SAS,IBE,TAP,EIN,AEE,AMC,CSA,KLC,LGL,TAP,XLA,XLF,MSA,MDP,RNV LFML,A,7,43.440092,5.221551,18.0,31.6,4,DLH,BAW,SAS,IBE,TAP,EIN,SAB,SNB,KLM,SWR,AEE,AMC,BIE,BMM,CSA,DBK,KLC,LGL,SYR,TAP,XLA,XLF,TAR,MSA,MDP,RNV,DBK,TCV LFML,A,10,43.441522,5.219599,18.0,30.8,4,AAF,REU,RAM,DAH,TUN,ELY,OHY,DLH,BAW,AMC,BIE,BMM,CSA,DBK,LGL,SYR,TAP,XLA,XLF,TAR,MSA,MDP,RNV,DBK,TCV LFML,E,46,43.440977,5.216507,23.0,313.4,3 LFML,C,46,43.441264,5.216926,23.0,314.5,3 LFML,Park,40,43.440156,5.218395,36.0,314.6,4,MDG,ELY,REU,XLF,TSC LFML,C,34,43.438978,5.219587,17.0,135.3,2,TJT,CCM,RAE,RLA,BZH,PWF,ANS,IBEX,DNM,AFRX,PGA,DLHX,CLH,MDM,ANS,TLB,HOP,TUX,DNM,FCT LFML,E,34,43.439334,5.219940,18.0,136.0,2,CCM,TJT,RAE,RLA,BZH,PWF,ANS,IBEX,DNM,AFRX,PGA,DLHX,CLH,MDM,ANS,TLB,TUX,DNM,HOP,FCT LFML,D,30,43.438905,5.220578,17.0,315.2,2,TJT,CCM,RAE,RLA,BZH,PWF,ANS,IBEX,DNM,AFRX,PGA,DLHX,CLH,MDM,ANS,TLB,TUX,DNM,HOP,FCT LFML,B,30,43.438589,5.220160,17.0,313.8,2,CCM,TJT,RAE,RLA,BZH,PWF,ANS,IBEX,DNM,AFRX,PGA,DLHX,CLH,MDM,ANS,TLB,TUX,DNM,HOP,FCT LFML,PkN,61,43.443193,5.211945,35.0,300.0,5,DHL,EAT,BCS LFML,PkN,68,43.444329,5.214451,25.0,33.8,5 LFML,A,48,43.443161,5.217429,18.0,268.4,3,PGT,RYR,EZY,EZS,GWI,JFU,NAX,TUI LFML,C,48,43.442578,5.216753,18.0,244.0,3,RYR,EZY,EZS,JFU,GWI,PGT,NAX,TUI,JAF LFML,E,48,43.442099,5.216753,18.0,153.6,3,RYR,EZY,EZS,BMI,JFU,PGT,NAX,TUI,JAF LFML,E,47,43.441613,5.217415,18.0,89.1,3,RYR,EZY,EZS,PGT,NAX,TUI,JAF LFML,C,47,43.442123,5.218132,18.0,90.8,3,JFU,RYR,EZY,EZS,NAX,TUI,JAF LFML,A,47,43.442537,5.218663,18.0,89.6,3,GWI,RYR,EZY,EZS,GWI,TUI,JAF LFML,C,5,43.439281,5.222365,23.0,61.5,4 LFML,Park,25,43.437561,5.221096,14.0,313.9,3,TJT,CCM,RAE,RLA,BZH,PWF,ANS,IBEX,DNM,AFRX,PGA,DLHX,CLH,MDM,ANS,TLB,TUX,DNM,HOP,FCT LFML,D,25,43.437837,5.221438,14.0,313.9,3,TJT,CCM,RAE,RLA,BZH,PWF,ANS,IBEX,DNM,AFRX,PGA,DLHX,CLH,MDM,ANS,TLB,TUX,DNM,HOP,FCT LFML,Park,48,43.442341,5.214728,30.0,309.7,6 LFML,Park,49,43.442726,5.215217,30.0,308.6,5 LFML,Park,50,43.443000,5.215647,25.0,309.7,5,FPO,VIZ LFML,Park,51,43.444883,5.215483,10.0,341.4,2 LFML,Park,52,43.445013,5.215917,10.0,340.5,2 LFML,Park,53,43.445121,5.216378,10.0,347.2,2 LFML,Park,84,43.434610,5.224591,10.0,326.6,2,CIV LFML,Park,85,43.433932,5.224705,10.0,326.7,2,CIV LFML,Park,86,43.435038,5.224918,10.0,327.3,2,CIV LFML,Park,87,43.433750,5.226086,10.0,308.9,2,CIV LFML,Park,88,43.433365,5.226423,10.0,316.1,2,CIV LFML,Park,11,43.435624,5.223327,10.0,327.2,2,CIV LFML,Park,10,43.435789,5.223701,10.0,323.7,2,CIV LFML,Park,18,43.436082,5.222770,10.0,329.3,2,CIV LFML,Park,17,43.436204,5.223065,10.0,325.3,2,CIV LFML,Park,16,43.436324,5.223344,10.0,321.1,2,CIV LFML,Park,15,43.436369,5.223698,10.0,329.2,2,CIV LFML,Park,14,43.436518,5.223943,10.0,326.7,2,CIV LFML,Park,96,43.436682,5.222985,10.0,327.7,2,CIV,FDO LFML,Park,21,43.436761,5.222358,10.0,328.6,2,CIV,FDO LFML,PkN,60,43.442832,5.211579,35.0,294.9,5,DHL,EAT,BCS LFML,PkW,63,43.443649,5.212673,14.0,61.9,3 LFML,PkSW,63,43.443452,5.212726,10.0,60.9,2 LFML,PkSE,63,43.443233,5.212963,10.0,62.0,2 LFML,Park,65,43.443398,5.213506,10.0,244.1,2 LFML,Park,71,43.443929,5.215219,7.0,331.1,2 LFML,Park,72,43.444016,5.215457,8.0,332.7,2 LFML,Park,73,43.444111,5.215707,7.0,332.8,2 LFML,Park,74,43.444238,5.215910,6.0,315.7,2 LFML,Park,75,43.444377,5.216093,6.0,313.7,2
Paul Henty Posted June 10, 2017 Report Posted June 10, 2017 getgate() function: You already have the player lon/lat at the top of the method: Dim lon As FsLongitude = New FsLongitude(playerLongitude.Value) Dim lat As FsLatitude = New FsLatitude(playerLatitude.Value) You should use these instead of creating new offsets. So, delete these lines: Dim LatitudeAircraft As Offset(Of Long) = New FSUIPC.Offset(Of Long)(&H560) 'Latitude Dim LongitudeAircraft As Offset(Of Long) = New FSUIPC.Offset(Of Long)(&H568) 'Longitude Then change the call to distance, using the lat/lon variables like this: Dim distance As Double = FsuipcData.distance(lat.Value.DecimalDegrees, lon.Value.DecimalDegrees, aaaa, bbbb, "K") Note that you need the DecimalDegrees property. The raw Value is in FS Units and not degrees. Distance() function: You are passing in values for lon1 and lat1 so you don't need to reassign them. Delete all of these lines from the top of that function: Dim LatitudeAircraft As Offset(Of Long) = New FSUIPC.Offset(Of Long)(&H560) 'Latitude Dim LongitudeAircraft As Offset(Of Long) = New FSUIPC.Offset(Of Long)(&H568) 'Longitude Lat1 = LatitudeAircraft.Value Lon1 = LongitudeAircraft.Value You should never declare offsets in subs or functions. You must only declare offsets at the form or class level. Paul
Frédéric-O DUCHEMIN Posted June 10, 2017 Report Posted June 10, 2017 Ok Paul I have 2 errors with those changes. But the problem why the reader stop to read in first line ? And why if my Sim not running give me a Gate ? Just I woul like understand. Thanks Fred FsuipcData.vb
Paul Henty Posted June 10, 2017 Report Posted June 10, 2017 Quote But the problem why the reader stop to read in first line ? Because your code isn't correct yet. It still has bugs. The main problem is that the distance function is always returning 0 because the last line is 'return distance' instead of 'return dist'. The reader loop stops because it thinks you are at the same location as the first gate (distance = 0). I've fixed up all the code, so this should work now: Public Function getgate(ByVal icaocode As String) As String ' Get the point for the current plane position 'Dim currentPosition As FsLatLonPoint = New FsLatLonPoint(lat, lon) Dim lon As FsLongitude = New FsLongitude(playerLongitude.Value) Dim lat As FsLatitude = New FsLatitude(playerLatitude.Value) ' Returns the current gate or "" is not parked at any gate. Dim pathto As String = My.Application.Info.DirectoryPath Using MyReader As New FileIO.TextFieldParser(pathto & "\g5.csv"") MyReader.TextFieldType = FileIO.FieldType.Delimited MyReader.SetDelimiters(",") Dim gatename As String Dim gatenumber As String Dim gatereturn As String = "" Dim currentRow As String() While Not MyReader.EndOfData Try currentRow = MyReader.ReadFields() If currentRow(0) = icaocode Then gatename = currentRow(1) gatenumber = currentRow(2) Dim aaa As String = currentRow(3) Dim bbb As String = currentRow(4) Dim aaaa As Double = Double.Parse(aaa, CultureInfo.InvariantCulture) Dim bbbb As Double = Double.Parse(bbb, CultureInfo.InvariantCulture) 'Dim rwyThresholdLat As FsLatitude = New FsLatitude(aaaa) 'Dim rwyThresholdLon As FsLongitude = New FsLongitude(bbbb) Dim gateRadius As String = currentRow(5) Dim gateRadiusKm As Double = Double.Parse(gateRadius, CultureInfo.InvariantCulture) / 1000D 'Dim thresholdCentre As FsLatLonPoint = New FsLatLonPoint(rwyThresholdLat, rwyThresholdLon) 'Dim trueHeading As Double = rwyMagHeading + rwyMagVariation 'runwayQuad = FsLatLonQuadrilateral.ForRunway(thresholdCentre, trueHeading, rwyWidth, rwyLength) 'Dim LatitudeAircraft As Offset(Of Long) = New FSUIPC.Offset(Of Long)(&H560) 'Latitude 'Dim LongitudeAircraft As Offset(Of Long) = New FSUIPC.Offset(Of Long)(&H568) 'Longitude ' *** Distance now returned in Km *** 'Dim distance As Double = FsuipcData.distance(LatitudeAircraft.Value, LongitudeAircraft.Value, aaaa, bbbb, "K") Dim distance As Double = FsuipcData.distance(lat.DecimalDegrees, lon.DecimalDegrees, aaaa, bbbb, "K") If distance <= gateRadiusKm Then 'gatereturn = gatename + " " + gatenumber gatereturn = gatename + gatenumber MyReader.Close() MyReader.Dispose() Return gatereturn 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() Return "" ' Not near any gate End Using End Function 'convert decimal degreees to radians Function deg2rad(ByVal Deg) deg2rad = CDbl(Deg * Math.PI / 180) End Function 'convert radians to decimal degrees Function rad2deg(ByVal Rad) rad2deg = CDbl(Rad * 180 / Math.PI) End Function Public Function distance(ByVal lat As Double, ByVal lon As Double, ByVal lat2 As Double, ByVal lon2 As Double, Optional ByVal unit As Char = "M"c) As Double 'd = distance(Lat1, Lon1, lat2, lon2, "N") 'distance between 2 Pos Dim theta As Double = lon - lon2 Dim dist As Double = Math.Sin(deg2rad(lat)) * Math.Sin(deg2rad(lat2)) + Math.Cos(deg2rad(lat)) * Math.Cos(deg2rad(lat2)) * Math.Cos(deg2rad(theta)) dist = Math.Acos(dist) dist = rad2deg(dist) dist = dist * 60 * 1.1515 If unit = "K" Then dist = dist * 1.609344 ElseIf unit = "N" Then dist = dist * 0.8684 End If Return dist End Function Paul
Frédéric-O DUCHEMIN Posted June 10, 2017 Report Posted June 10, 2017 Ok Thanks Paul for your time Works fine if the plane is in the good Airport of course :) [SOLVED] Regards Fred
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now