public bool Decode() { /* * _Encoded = _Encoded.Replace("\n", ""); //clear escape string * string[] delimiters = { "0202" }; * foreach (string code in _Encoded.Split(delimiters, StringSplitOptions.RemoveEmptyEntries)) * { * if (code.Length < 6) * { * return false; * } * string result = "0202" + code; * try * { * int blockLength = Convert.ToInt32(result.Substring(4, 4), 16); * string dataType = result.Substring(8, 2); * string excess = ""; * if (dataType.Equals("01")) * { * //decode data : radar use 20 bytes * int no = Convert.ToInt32(result.Substring(20, 2)); * string latPath = result.Substring(24, 8); * string lngPath = result.Substring(32, 8); * if (result.Length > 40) * { * excess = result.Substring(40); * } * //set radar data * RecievedRadarData data = new RecievedRadarData(); * data.BlockLength = blockLength; * data.Type = RecievedType.Radar; * data.Number = no; * data.Lat = FindPositionInDegree(latPath); * data.Lng = FindPositionInDegree(lngPath); * data.Excess = excess; * string key = GenerateId(data.Type, data.Number); * //result message * string message = "RADAR, "; * message += data.Number.ToString("00") + ", "; * message += data.Lat.ToString("0.####") + ", "; * message += data.Lng.ToString("0.####"); * //radar marker * PointLatLng point = new PointLatLng(data.Lat, data.Lng); * Bitmap pic = (Bitmap)Image.FromFile("Img/point/RS.png"); * GMapMarker marker = new GMarkerGoogle(point, pic); * marker.Tag = "Radar"; * //store value * if (!_Keys.Contains(key)) * { * _Keys.Add(key); * _Decoded.Add(key, data); * _Result.Add(key, message); * _Markers.Add(key, marker); * } * else * { * _Decoded[key] = data; * _Result[key] = message; * _Markers[key] = marker; * } * } * else if (dataType.Equals("02")) * { * //decode data : track use 18 bytes * double xPos = (double)Convert.ToInt16(result.Substring(12, 4), 16) / 480; * double yPos = (double)Convert.ToInt16(result.Substring(16, 4), 16) / 480; * double xSpeed = (double)Convert.ToInt16(result.Substring(20, 4), 16) / 8; * double ySpeed = (double)Convert.ToInt16(result.Substring(24, 4), 16) / 8; * int trackNo = Convert.ToInt32(result.Substring(28, 4)); * //TODO dont change if edited by user * char status = (char)Convert.ToInt32(result.Substring(32, 2), 16); * int radarNo = Convert.ToInt32(result.Substring(34, 2)); * MessageBox.Show("dataType = " + dataType + "\nblockLength = " + blockLength + "\n" + "result.Length = " + result.Length); * if (result.Length > 36) * { * excess = result.Substring(36); * Regex regex = new Regex(@"(02)(.{26})"); * MatchCollection matches = regex.Matches(excess); * foreach (Match match in matches) * { * MessageBox.Show(match.Groups[0].Value); * } * } * //set track data * RecievedTrackData data = new RecievedTrackData(); * data.BlockLength = blockLength; * data.Type = RecievedType.Track; * data.PositionX = xPos; * data.PositionY = yPos; * data.SpeedX = xSpeed; * data.SpeedY = ySpeed; * data.TrackNumber = trackNo; * data.TrackType = ParseStatus(status); * data.RadarNumber = radarNo; * data.Excess = excess; * string key = GenerateId(data.Type, data.TrackNumber); * if (_Status.ContainsKey(key)) * { * data.Identification = _Status[key]; * } * else * { * data.Identification = status; * } * PointLatLng point = FindPositionFor(data); * double kmph = ScaleConverter.ConvertSpeed(data.Speed, "mph", "km/h"); * //result message * string message = "TRACK, "; * message += data.TrackNumber.ToString("0000") + ", "; * message += data.Identification + ", "; * message += point.Lat.ToString("0.####") + ", "; * message += point.Lng.ToString("0.####") + ", "; * message += kmph.ToString("0.####") + ", "; * message += data.RadarNumber.ToString("00"); * //track marker * GMapMarker marker = new GMarkerArrow(point) * { * ToolTipText = message, * ToolTipMode = MarkerTooltipMode.OnMouseOver * }; * (marker as GMarkerArrow).Bearing = (float)data.Bearing; * marker.setFill(data.Identification.ToString()); * marker.Tag = "Track"; * //flight tail * FlightTail tail = new FlightTail(point, data.Speed, data.Bearing); * //store value * if (!_Keys.Contains(key)) * { * _Keys.Add(key); * _Decoded.Add(key, data); * _Result.Add(key, message); * _Markers.Add(key, marker); * _Tails.Add(key, tail); * } * else * { * _Decoded[key] = data; * _Result[key] = message; * _Markers[key] = marker; * _Tails[key] = tail; * } * } * } * catch (Exception ex) * { * Debug.WriteLine("Error: " + ex.Message); * return false; * } * } * return true; */ _Encoded = _Encoded.Replace("\n", ""); //clear escape string _Encoded = _Encoded.Replace("\r", ""); //clear escape string _Encoded = _Encoded.Replace("\t", ""); //clear escape string string code = _Encoded; if (IsTrackDecode == 0) { //normal data while (code.StartsWith("0202", StringComparison.OrdinalIgnoreCase) && IsTrackDecode == 0) { string result = code; int blockLength = Convert.ToInt32(result.Substring(4, 4), 16); int dataType = Convert.ToInt32(result.Substring(8, 2), 16); string excess = ""; if (dataType == 1) { //decode data : radar use 20 bytes int no = Convert.ToInt32(result.Substring(20, 2), 16); string latPath = result.Substring(24, 8); string lngPath = result.Substring(32, 8); //set radar data RecievedRadarData data = new RecievedRadarData(); data.BlockLength = blockLength; data.Type = RecievedType.Radar; data.Number = no; data.Lat = FindPositionInDegree(latPath); data.Lng = FindPositionInDegree(lngPath); data.Excess = excess; string key = GenerateId(data.Type, data.Number); //result message string message = "RADAR, "; message += data.Number.ToString("00") + ", "; message += data.Lat.ToString("0.####") + ", "; message += data.Lng.ToString("0.####"); //radar marker /* * PointLatLng point = new PointLatLng(data.Lat, data.Lng); * Bitmap pic = (Bitmap)Image.FromFile("Img/point/RS.png"); * GMapMarker marker = new GMarkerGoogle(point, pic); * / * ToolTipText = message, * ToolTipMode = MarkerTooltipMode.OnMouseOver * }; * marker.Tag = "Radar"; */ RE_radar = new PointLatLng(data.Lat, data.Lng); mainForm main = mainForm.GetInstance(); if (main.radarP.Position != RE_radar) { main.radarP.Position = RE_radar; } //store value if (!_DecodedRadar.ContainsKey(key)) { _DecodedRadar.Add(key, data); } else { _DecodedRadar[key] = data; } if (_FirstRadar == null) { _FirstRadar = data; } track.ClearReal(); CurrentTrack.Clear(); } else if (dataType == 2) { //decode data : track use 18 bytes double xPos = (double)Convert.ToInt16(result.Substring(12, 4), 16) / 480; double yPos = (double)Convert.ToInt16(result.Substring(16, 4), 16) / 480; double xSpeed = (double)Convert.ToInt16(result.Substring(20, 4), 16); double ySpeed = (double)Convert.ToInt16(result.Substring(24, 4), 16); int trackNo = Convert.ToInt32(result.Substring(28, 4), 16); char status = (char)Convert.ToInt32(result.Substring(32, 2), 16); int radarNo = Convert.ToInt32(result.Substring(34, 2), 16); track.CompareDict(PreviousTrack, CurrentTrack); IsTrackDecode = blockLength - 18; PreviousTrack = new Dictionary <int, RecievedTrackData>(CurrentTrack); CurrentTrack.Clear(); RecievedTrackData data = new RecievedTrackData(); data.BlockLength = 14; data.Type = RecievedType.Track; data.PositionX = xPos; data.PositionY = yPos; data.SpeedX = xSpeed; data.SpeedY = ySpeed; data.TrackNumber = trackNo; data.TrackType = ParseStatus(status); data.RadarNumber = radarNo; string key = GenerateId(data.Type, data.TrackNumber); if (_Status.ContainsKey(key)) { data.Identification = _Status[key]; } else { data.Identification = status; } PointLatLng point = FindPositionFor(data); double kmph = ScaleConverter.ConvertSpeed(data.Speed, "mph", "km/h"); //result message string message = "TRACK, "; message += data.TrackNumber.ToString("0000") + ", "; message += data.Identification + ", "; message += point.Lat.ToString("0.####") + ", "; message += point.Lng.ToString("0.####") + ", "; message += kmph.ToString("0.####") + ", "; message += data.RadarNumber.ToString("00"); FlightRadarData fd = new FlightRadarData(TrackType.Real); //todo change fd.Id = data.TrackNumber; fd.point = point; fd.speed = data.Speed; fd.bearing = data.Bearing; fd.identification = data.Identification; //fd.lastTime = DateTime.Now; track.AddReal(fd); //store value CurrentTrack.Add(fd.Id, data); } code = code.Substring(result.Length); } } else { //track data while (code.Length >= 28 && IsTrackDecode > 0) { string result = code; double xPos = (double)Convert.ToInt16(result.Substring(4, 4), 16) / 480; double yPos = (double)Convert.ToInt16(result.Substring(8, 4), 16) / 480; double xSpeed = (double)Convert.ToInt16(result.Substring(12, 4), 16); double ySpeed = (double)Convert.ToInt16(result.Substring(16, 4), 16); int trackNo = Convert.ToInt32(result.Substring(20, 4), 16); char status = (char)Convert.ToInt32(result.Substring(24, 2), 16); int radarNo = Convert.ToInt32(result.Substring(26, 2), 16); IsTrackDecode -= 14; RecievedTrackData data = new RecievedTrackData(); data.BlockLength = 14; data.Type = RecievedType.Track; data.PositionX = xPos; data.PositionY = yPos; data.SpeedX = xSpeed; data.SpeedY = ySpeed; data.TrackNumber = trackNo; data.TrackType = ParseStatus(status); data.RadarNumber = radarNo; string key = GenerateId(data.Type, data.TrackNumber); if (_Status.ContainsKey(key)) { data.Identification = _Status[key]; } else { data.Identification = status; } PointLatLng point = FindPositionFor(data); double kmph = ScaleConverter.ConvertSpeed(data.Speed, "mph", "km/h"); //result message string message = "TRACK, "; message += data.TrackNumber.ToString("0000") + ", "; message += data.Identification + ", "; message += point.Lat.ToString("0.####") + ", "; message += point.Lng.ToString("0.####") + ", "; message += kmph.ToString("0.####") + ", "; message += data.RadarNumber.ToString("00"); FlightRadarData fd = new FlightRadarData(TrackType.Real); //todo change fd.Id = data.TrackNumber; fd.point = point; fd.speed = data.Speed; fd.bearing = data.Bearing; fd.identification = data.Identification; //fd.lastTime = DateTime.Now; track.AddReal(fd); //store value CurrentTrack.Add(fd.Id, data); code = code.Substring(28); } } return(true); }
public static IntersectionResult Intersection(FlightRadarData fd1, FlightRadarData fd2, out PointLatLng intersectPoint, out double s, out double b, out double t) { IntersectionResult result = IntersectionResult.Empty; PointLatLng point1 = fd1.point; PointLatLng point2 = fd2.point; double initDistance = GetDistance(point1, point2); float bearing = (float)fd1.bearing; intersectPoint = new PointLatLng(); int attempt = 0; s = 0; b = 0; t = 0; while (result != IntersectionResult.Success && attempt < 360) { var φ1 = DegreesToRadians(point1.Lat); var φ2 = DegreesToRadians(point2.Lat); var λ1 = DegreesToRadians(point1.Lng); var λ2 = DegreesToRadians(point2.Lng); var Δφ = φ2 - φ1; var Δλ = λ2 - λ1; var θ13 = DegreesToRadians(bearing); var θ23 = DegreesToRadians(fd2.bearing); var δ12 = 2 * Math.Asin(Math.Sqrt(Math.Sin(Δφ / 2) * Math.Sin(Δφ / 2) + Math.Cos(φ1) * Math.Cos(φ2) * Math.Sin(Δλ / 2) * Math.Sin(Δλ / 2))); var cosθa = (Math.Sin(φ2) - Math.Sin(φ1) * Math.Cos(δ12)) / (Math.Sin(δ12) * Math.Cos(φ1)); var cosθb = (Math.Sin(φ1) - Math.Sin(φ2) * Math.Cos(δ12)) / (Math.Sin(δ12) * Math.Cos(φ2)); var θa = Math.Acos(Math.Min(Math.Max(cosθa, -1), 1)); // protect against rounding errors var θb = Math.Acos(Math.Min(Math.Max(cosθb, -1), 1)); // protect against rounding errors var θ12 = Math.Sin(λ2 - λ1) > 0 ? θa : 2 * Math.PI - θa; var θ21 = Math.Sin(λ2 - λ1) > 0 ? 2 * Math.PI - θb : θb; var α1 = θ13 - θ12; // angle 2-1-3 var α2 = θ21 - θ23; // angle 1-2-3 if (Math.Sin(α1) == 0 && Math.Sin(α2) == 0) { bearing = Rotate(bearing, fd1, fd2); result = IntersectionResult.Infinity; } else if (Math.Sin(α1) * Math.Sin(α2) < 0) { bearing = Rotate(bearing, fd1, fd2); result = IntersectionResult.Ambiguous; } else { var cosα3 = -Math.Cos(α1) * Math.Cos(α2) + Math.Sin(α1) * Math.Sin(α2) * Math.Cos(δ12); var δ13 = Math.Atan2(Math.Sin(δ12) * Math.Sin(α1) * Math.Sin(α2), Math.Cos(α2) + Math.Cos(α1) * cosα3); var φ3 = Math.Asin(Math.Sin(φ1) * Math.Cos(δ13) + Math.Cos(φ1) * Math.Sin(δ13) * Math.Cos(θ13)); var Δλ13 = Math.Atan2(Math.Sin(θ13) * Math.Sin(δ13) * Math.Cos(φ1), Math.Cos(δ13) - Math.Sin(φ1) * Math.Sin(φ3)); var λ3 = λ1 + Δλ13; var lat = RadiansToDegrees(φ3); var lng = RadiansToDegrees(λ3); intersectPoint = new PointLatLng(lat, lng); var speed2InKm = ScaleConverter.ConvertSpeed(fd2.speed, "kts", "km/h"); var distance2 = GetDistance(fd2.point, intersectPoint); var time = distance2 / speed2InKm; var distance1 = GetDistance(fd1.point, intersectPoint); var speed1 = distance1 / time; if (distance1 > initDistance * 2.5) { bearing = Rotate(bearing, fd1, fd2); result = IntersectionResult.Ambiguous; } else if (speed1 > DataSettings.Track["MaxSpeed"]) { bearing = Rotate(bearing, fd1, fd2); attempt++; result = IntersectionResult.Impossible; } else { s = speed1; b = bearing; t = time; result = IntersectionResult.Success; } } } return(result); }