GetDFItemFromLine() public method

public GetDFItemFromLine ( string line, int lineno ) : DFItem
line string
lineno int
return DFItem
Esempio n. 1
0
        public static void MapLogs(string[] logs)
        {
            foreach (var logfile in logs)
            {
                if (File.Exists(logfile + ".jpg"))
                {
                    continue;
                }

                double minx = 99999;
                double maxx = -99999;
                double miny = 99999;
                double maxy = -99999;

                List <PointLatLngAlt> locs = new List <PointLatLngAlt>();
                try
                {
                    if (logfile.ToLower().EndsWith(".tlog"))
                    {
                        MAVLinkInterface mine = new MAVLinkInterface();


                        using (mine.logplaybackfile = new BinaryReader(File.Open(logfile, FileMode.Open, FileAccess.Read, FileShare.Read)))
                        {
                            mine.logreadmode = true;

                            CurrentState cs = new CurrentState();

                            while (mine.logplaybackfile.BaseStream.Position < mine.logplaybackfile.BaseStream.Length)
                            {
                                byte[] packet = mine.readPacket();

                                if (packet.Length < 5)
                                {
                                    continue;
                                }

                                try
                                {
                                    if (MainV2.speechEngine != null)
                                    {
                                        MainV2.speechEngine.SpeakAsyncCancelAll();
                                    }
                                }
                                catch { }

                                if (packet[5] == (byte)MAVLink.MAVLINK_MSG_ID.GLOBAL_POSITION_INT)
                                {
                                    var loc = packet.ByteArrayToStructure <MAVLink.mavlink_global_position_int_t>(6);

                                    if (loc.lat == 0 || loc.lon == 0)
                                    {
                                        continue;
                                    }

                                    locs.Add(new PointLatLngAlt(loc.lat / 10000000.0f, loc.lon / 10000000.0f));

                                    minx = Math.Min(minx, loc.lon / 10000000.0f);
                                    maxx = Math.Max(maxx, loc.lon / 10000000.0f);
                                    miny = Math.Min(miny, loc.lat / 10000000.0f);
                                    maxy = Math.Max(maxy, loc.lat / 10000000.0f);
                                }
                            }
                        }
                    }
                    else if (logfile.ToLower().EndsWith(".bin") || logfile.ToLower().EndsWith(".log"))
                    {
                        bool bin = logfile.ToLower().EndsWith(".bin");

                        using (var st = File.OpenRead(logfile))
                        {
                            using (StreamReader sr = new StreamReader(st))
                            {
                                while (sr.BaseStream.Position < sr.BaseStream.Length)
                                {
                                    string line = "";

                                    if (bin)
                                    {
                                        line = BinaryLog.ReadMessage(sr.BaseStream);
                                    }
                                    else
                                    {
                                        line = sr.ReadLine();
                                    }

                                    if (line.StartsWith("FMT"))
                                    {
                                        DFLog.FMTLine(line);
                                    }
                                    else if (line.StartsWith("GPS"))
                                    {
                                        var item = DFLog.GetDFItemFromLine(line, 0);

                                        var lat = double.Parse(item.items[DFLog.FindMessageOffset(item.msgtype, "Lat")]);
                                        var lon = double.Parse(item.items[DFLog.FindMessageOffset(item.msgtype, "Lng")]);

                                        if (lat == 0 || lon == 0)
                                        {
                                            continue;
                                        }

                                        locs.Add(new PointLatLngAlt(lat, lon));

                                        minx = Math.Min(minx, lon);
                                        maxx = Math.Max(maxx, lon);
                                        miny = Math.Min(miny, lat);
                                        maxy = Math.Max(maxy, lat);
                                    }
                                }
                            }
                        }
                    }


                    if (locs.Count > 10)
                    {
                        // add a bit of buffer
                        var area = RectLatLng.FromLTRB(minx - 0.001, maxy + 0.001, maxx + 0.001, miny - 0.001);
                        var map  = GetMap(area);

                        var grap = Graphics.FromImage(map);

                        PointF lastpoint = new PointF();

                        foreach (var loc in locs)
                        {
                            PointF newpoint = GetPixel(area, loc, map.Size);

                            if (!lastpoint.IsEmpty)
                            {
                                grap.DrawLine(Pens.Red, lastpoint, newpoint);
                            }

                            lastpoint = newpoint;
                        }

                        map.Save(logfile + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

                        map.Dispose();

                        map = null;
                    }
                    else
                    {
                        DoTextMap(logfile + ".jpg", "No gps data");
                    }
                }
                catch (Exception ex)
                {
                    if (ex.ToString().Contains("Mavlink 0.9"))
                    {
                        DoTextMap(logfile + ".jpg", "Old log\nMavlink 0.9");
                    }

                    continue;
                }
            }
        }
        void setlinecount()
        {
            int offset = 0;

            byte[] buffer = new byte[1024 * 1024];

            var lineCount = 0;

            if (binary)
            {
                long length = basestream.Length;
                while (basestream.Position < length)
                {
                    var ans = binlog.ReadMessageTypeOffset(basestream);

                    if (ans == null)
                    {
                        continue;
                    }

                    byte type = ans.Item1;
                    messageindex[type].Add((uint)(ans.Item2));

                    linestartoffset.Add((uint)(ans.Item2));
                    lineCount++;
                }

                _count = lineCount;

                // build fmt line database to pre seed the FMT message
                int amax = Math.Min(2000, _count - 1);
                for (int a = 0; a < amax; a++)
                {
                    dflog.GetDFItemFromLine(this[a].ToString(), a);
                }

                // build fmt line database using type
                foreach (var item in GetEnumeratorType("FMT"))
                {
                    Console.WriteLine("Found FMT");
                }
            }
            else
            {
                // first line starts at 0
                linestartoffset.Add(0);

                while (basestream.Position < basestream.Length)
                {
                    offset = 0;

                    long startpos = basestream.Position;

                    int read = basestream.Read(buffer, offset, buffer.Length);

                    while (read > 0)
                    {
                        if (buffer[offset] == '\n')
                        {
                            linestartoffset.Add((uint)(startpos + 1 + offset));
                            lineCount++;
                        }

                        offset++;
                        read--;
                    }
                }

                _count = lineCount;

                // create msg cache
                int b = 0;
                foreach (var item in this)
                {
                    var dfitem = dflog.GetDFItemFromLine(item.ToString(), b);
                    if (dfitem.msgtype != null && dflog.logformat.ContainsKey(dfitem.msgtype))
                    {
                        var type = (byte)dflog.logformat[dfitem.msgtype].Id;

                        messageindex[type].Add(linestartoffset[b]);
                    }
                    b++;
                }
            }

            indexcachelineno = -1;
        }
Esempio n. 3
0
        public static void MapLogs(string[] logs)
        {
            foreach (var logfile in logs)
            {
                if (File.Exists(logfile + ".jpg"))
                    continue;

                double minx = 99999;
                double maxx = -99999;
                double miny = 99999;
                double maxy = -99999;

                List<PointLatLngAlt> locs = new List<PointLatLngAlt>();
                try
                {
                    if (logfile.ToLower().EndsWith(".tlog"))
                    {
                        using (MAVLinkInterface mine = new MAVLinkInterface())
                        using (mine.logplaybackfile = new BinaryReader(File.Open(logfile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)))
                        {
                            mine.logreadmode = true;

                            CurrentState cs = new CurrentState();

                            while (mine.logplaybackfile.BaseStream.Position < mine.logplaybackfile.BaseStream.Length)
                            {
                                byte[] packet = mine.readPacket();

                                if (packet.Length < 5)
                                    continue;

                                try
                                {
                                    if (MainV2.speechEngine != null)
                                        MainV2.speechEngine.SpeakAsyncCancelAll();
                                }
                                catch { }

                                if (packet[5] == (byte)MAVLink.MAVLINK_MSG_ID.GLOBAL_POSITION_INT)
                                {
                                    var loc = packet.ByteArrayToStructure<MAVLink.mavlink_global_position_int_t>(6);

                                    if (loc.lat == 0 || loc.lon == 0)
                                        continue;

                                    locs.Add(new PointLatLngAlt(loc.lat / 10000000.0f, loc.lon / 10000000.0f));

                                    minx = Math.Min(minx, loc.lon / 10000000.0f);
                                    maxx = Math.Max(maxx, loc.lon / 10000000.0f);
                                    miny = Math.Min(miny, loc.lat / 10000000.0f);
                                    maxy = Math.Max(maxy, loc.lat / 10000000.0f);
                                }
                            }
                        }

                    }
                    else if (logfile.ToLower().EndsWith(".bin") || logfile.ToLower().EndsWith(".log"))
                    {
                        bool bin = logfile.ToLower().EndsWith(".bin");

                        BinaryLog binlog = new BinaryLog();
                        DFLog dflog = new DFLog();

                        using (var st = File.Open(logfile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                        {
                            using (StreamReader sr = new StreamReader(st))
                            {
                                while (sr.BaseStream.Position < sr.BaseStream.Length)
                                {
                                    string line = "";

                                    if (bin)
                                    {
                                        line = binlog.ReadMessage(sr.BaseStream);
                                    }
                                    else
                                    {
                                        line = sr.ReadLine();
                                    }

                                    if (line.StartsWith("FMT"))
                                    {
                                        dflog.FMTLine(line);
                                    }
                                    else if (line.StartsWith("GPS"))
                                    {
                                        var item = dflog.GetDFItemFromLine(line, 0);

                                        var lat = double.Parse(item.items[dflog.FindMessageOffset(item.msgtype, "Lat")]);
                                        var lon = double.Parse(item.items[dflog.FindMessageOffset(item.msgtype, "Lng")]);

                                        if (lat == 0 || lon == 0)
                                            continue;

                                        locs.Add(new PointLatLngAlt(lat, lon));

                                        minx = Math.Min(minx, lon);
                                        maxx = Math.Max(maxx, lon);
                                        miny = Math.Min(miny, lat);
                                        maxy = Math.Max(maxy, lat);
                                    }
                                }
                            }
                        }
                    }


                    if (locs.Count > 10)
                    {
                        // add a bit of buffer
                        var area = RectLatLng.FromLTRB(minx - 0.001, maxy + 0.001, maxx + 0.001, miny - 0.001);
                        var map = GetMap(area);

                        var grap = Graphics.FromImage(map);

                        PointF lastpoint = new PointF();

                        foreach (var loc in locs)
                        {
                            PointF newpoint = GetPixel(area, loc, map.Size);

                            if (!lastpoint.IsEmpty)
                                grap.DrawLine(Pens.Red, lastpoint, newpoint);

                            lastpoint = newpoint;
                        }

                        map.Save(logfile + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

                        map.Dispose();

                        map = null;
                    }
                    else
                    {
                        DoTextMap(logfile + ".jpg", "No gps data");
                    }
                }
                catch (Exception ex) 
                {
                    if (ex.ToString().Contains("Mavlink 0.9"))
                        DoTextMap(logfile + ".jpg", "Old log\nMavlink 0.9");

                    continue; 
                }
            }
        }
Esempio n. 4
0
        public static void MapLogs(string[] logs)
        {
            foreach (var logfile in logs)
            {
                if (File.Exists(logfile + ".jpg"))
                {
                    continue;
                }

                double minx = 99999;
                double maxx = -99999;
                double miny = 99999;
                double maxy = -99999;

                Dictionary <int, List <PointLatLngAlt> > loc_list = new Dictionary <int, List <PointLatLngAlt> >();

                try
                {
                    if (logfile.ToLower().EndsWith(".tlog"))
                    {
                        using (MAVLinkInterface mine = new MAVLinkInterface())
                            using (
                                mine.logplaybackfile =
                                    new BinaryReader(File.Open(logfile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                                )
                            {
                                mine.logreadmode = true;

                                while (mine.logplaybackfile.BaseStream.Position < mine.logplaybackfile.BaseStream.Length)
                                {
                                    MAVLink.MAVLinkMessage packet = mine.readPacket();

                                    if (packet.Length < 5)
                                    {
                                        continue;
                                    }

                                    try
                                    {
                                        if (MainV2.speechEngine != null)
                                        {
                                            MainV2.speechEngine.SpeakAsyncCancelAll();
                                        }
                                    }
                                    catch
                                    {
                                    }

                                    if (packet.msgid == (byte)MAVLink.MAVLINK_MSG_ID.GLOBAL_POSITION_INT)
                                    {
                                        var loc = packet.ToStructure <MAVLink.mavlink_global_position_int_t>();

                                        if (loc.lat == 0 || loc.lon == 0)
                                        {
                                            continue;
                                        }

                                        var id = MAVList.GetID(packet.sysid, packet.compid);

                                        if (!loc_list.ContainsKey(id))
                                        {
                                            loc_list[id] = new List <PointLatLngAlt>();
                                        }

                                        loc_list[id].Add(new PointLatLngAlt(loc.lat / 10000000.0f, loc.lon / 10000000.0f));

                                        minx = Math.Min(minx, loc.lon / 10000000.0f);
                                        maxx = Math.Max(maxx, loc.lon / 10000000.0f);
                                        miny = Math.Min(miny, loc.lat / 10000000.0f);
                                        maxy = Math.Max(maxy, loc.lat / 10000000.0f);
                                    }
                                }
                            }
                    }
                    else if (logfile.ToLower().EndsWith(".bin") || logfile.ToLower().EndsWith(".log"))
                    {
                        bool bin = logfile.ToLower().EndsWith(".bin");

                        BinaryLog binlog = new BinaryLog();
                        DFLog     dflog  = new DFLog();

                        using (var st = File.Open(logfile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                        {
                            using (StreamReader sr = new StreamReader(st))
                            {
                                loc_list[0] = new List <PointLatLngAlt>();

                                while (sr.BaseStream.Position < sr.BaseStream.Length)
                                {
                                    string line = "";

                                    if (bin)
                                    {
                                        line = binlog.ReadMessage(sr.BaseStream);
                                    }
                                    else
                                    {
                                        line = sr.ReadLine();
                                    }

                                    if (line.StartsWith("FMT"))
                                    {
                                        dflog.FMTLine(line);
                                    }
                                    else if (line.StartsWith("GPS"))
                                    {
                                        var item = dflog.GetDFItemFromLine(line, 0);

                                        var lat = double.Parse(item.items[dflog.FindMessageOffset(item.msgtype, "Lat")]);
                                        var lon = double.Parse(item.items[dflog.FindMessageOffset(item.msgtype, "Lng")]);

                                        if (lat == 0 || lon == 0)
                                        {
                                            continue;
                                        }

                                        loc_list[0].Add(new PointLatLngAlt(lat, lon));

                                        minx = Math.Min(minx, lon);
                                        maxx = Math.Max(maxx, lon);
                                        miny = Math.Min(miny, lat);
                                        maxy = Math.Max(maxy, lat);
                                    }
                                }
                            }
                        }
                    }


                    if (loc_list.First().Value.Count > 10)
                    {
                        // add a bit of buffer
                        var area = RectLatLng.FromLTRB(minx - 0.001, maxy + 0.001, maxx + 0.001, miny - 0.001);
                        var map  = GetMap(area);

                        var grap = Graphics.FromImage(map);

                        Color[] colours =
                        {
                            Color.Red,    Color.Orange, Color.Yellow, Color.Green, Color.Blue, Color.Indigo,
                            Color.Violet, Color.Pink
                        };

                        int a = 0;
                        foreach (var locs in loc_list.Values)
                        {
                            PointF lastpoint = new PointF();
                            var    pen       = new Pen(colours[a % (colours.Length - 1)]);

                            foreach (var loc in locs)
                            {
                                PointF newpoint = GetPixel(area, loc, map.Size);

                                if (!lastpoint.IsEmpty)
                                {
                                    grap.DrawLine(pen, lastpoint, newpoint);
                                }

                                lastpoint = newpoint;
                            }

                            a++;
                        }

                        map.Save(logfile + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

                        map.Dispose();

                        map = null;
                    }
                    else
                    {
                        DoTextMap(logfile + ".jpg", "No gps data");
                    }
                }
                catch (Exception ex)
                {
                    if (ex.ToString().Contains("Mavlink 0.9"))
                    {
                        DoTextMap(logfile + ".jpg", "Old log\nMavlink 0.9");
                    }

                    continue;
                }
            }
        }
        void setlinecount()
        {
            int offset = 0;

            byte[] buffer = new byte[1024 * 1024];

            var lineCount = 0;

            if (binary)
            {
                while (basestream.Position < basestream.Length)
                {
                    offset = 0;

                    // seek back 5 on each buffer fill
                    if (basestream.Position > 10)
                    {
                        basestream.Seek(-5, SeekOrigin.Current);
                    }

                    long startpos = basestream.Position;

                    int read = basestream.Read(buffer, offset, buffer.Length);

                    // 5 byte overlap
                    while (read > 2)
                    {
                        if (buffer[offset] == BinaryLog.HEAD_BYTE1 && buffer[offset + 1] == BinaryLog.HEAD_BYTE2)
                        {
                            byte type = buffer[offset + 2];
                            messageindex[type].Add(startpos + offset);

                            linestartoffset.Add(startpos + offset);
                            lineCount++;
                        }

                        offset++;
                        read--;
                    }
                }

                _count = lineCount;

                // build fmt line database
                int amax = Math.Min(2000, _count - 1);
                for (int a = 0; a < amax; a++)
                {
                    dflog.GetDFItemFromLine(this[a].ToString(), a);
                }
            }
            else
            {
                // first line starts at 0
                linestartoffset.Add(0);

                while (basestream.Position < basestream.Length)
                {
                    offset = 0;

                    long startpos = basestream.Position;

                    int read = basestream.Read(buffer, offset, buffer.Length);

                    while (read > 0)
                    {
                        if (buffer[offset] == '\n')
                        {
                            linestartoffset.Add(startpos + 1 + offset);
                            lineCount++;
                        }

                        offset++;
                        read--;
                    }
                }

                _count = lineCount;

                // build fmt line database
                int amax = Math.Min(2000, _count - 1);
                for (int a = 0; a < amax; a++)
                {
                    dflog.GetDFItemFromLine(this[a].ToString(), a);
                }

                // create msg cache
                int b = 0;
                foreach (var item in this)
                {
                    var dfitem = dflog.GetDFItemFromLine(item.ToString(), b);

                    var type = (byte)dflog.logformat[dfitem.msgtype].Id;

                    messageindex[type].Add(linestartoffset[b]);

                    b++;
                }
            }
        }
Esempio n. 6
0
        public void writeKML(string filename)
        {
            try
            {
                writeGPX(filename);
            }
            catch
            { }
            try
            {
                writeRinex(filename);
            }
            catch
            { }
            try
            {
                writeWPFile(filename);
            }
            catch
            { }
            try
            {
                writeParamFile(filename);
            }
            catch
            { }

            Color[] colours =
            {
                Color.Red,    Color.Orange, Color.Yellow, Color.Green, Color.Blue, Color.Indigo,
                Color.Violet, Color.Pink
            };

            AltitudeMode altmode = AltitudeMode.absolute;

            KMLRoot kml  = new KMLRoot();
            Folder  fldr = new Folder("Log");

            Style style = new Style();

            style.Id = "yellowLineGreenPoly";
            style.Add(new LineStyle(HexStringToColor("7f00ffff"), 4));

            Style style1 = new Style();

            style1.Id = "spray";
            style1.Add(new LineStyle(HexStringToColor("4c0000ff"), 0));
            style1.Add(new PolyStyle()
            {
                Color = HexStringToColor("4c0000ff")
            });

            PolyStyle pstyle = new PolyStyle();

            pstyle.Color = HexStringToColor("7f00ff00");
            style.Add(pstyle);

            kml.Document.AddStyle(style);
            kml.Document.AddStyle(style1);

            int stylecode = 0xff;
            int g         = -1;

            foreach (List <Point3D> poslist in position)
            {
                g++;
                if (poslist == null)
                {
                    continue;
                }

                /*
                 * List<PointLatLngAlt> pllalist = new List<PointLatLngAlt>();
                 *
                 * foreach (var point in poslist)
                 * {
                 *  pllalist.Add(new PointLatLngAlt(point.Y, point.X, point.Z, ""));
                 * }
                 *
                 * var ans = Utilities.LineOffset.GetPolygon(pllalist, 2);
                 *
                 *
                 *
                 * while (ans.Count > 0)
                 * {
                 *  var first = ans[0];
                 *  var second = ans[1];
                 *  var secondlast = ans[ans.Count - 2];
                 *  var last = ans[ans.Count-1];
                 *
                 *  ans.Remove(first);
                 *  ans.Remove(last);
                 *
                 *  var polycoords = new BoundaryIs();
                 *
                 *  polycoords.LinearRing = new LinearRing();
                 *
                 *  polycoords.LinearRing.Coordinates.Add(new Point3D(first.Lng, first.Lat, 1));
                 *  polycoords.LinearRing.Coordinates.Add(new Point3D(second.Lng, second.Lat, 1));
                 *  polycoords.LinearRing.Coordinates.Add(new Point3D(secondlast.Lng, secondlast.Lat, 1));
                 *  polycoords.LinearRing.Coordinates.Add(new Point3D(last.Lng, last.Lat, 1));
                 *  polycoords.LinearRing.Coordinates.Add(new Point3D(first.Lng, first.Lat, 1));
                 *
                 *  //if (!IsClockwise(polycoords.LinearRing.Coordinates))
                 *    //  polycoords.LinearRing.Coordinates.Reverse();
                 *
                 *  Polygon kmlpoly = new Polygon() { AltitudeMode = AltitudeMode.relativeToGround, Extrude = false, OuterBoundaryIs = polycoords };
                 *
                 *  Placemark pmpoly = new Placemark();
                 *  pmpoly.Polygon = kmlpoly;
                 *  pmpoly.name = g + " test";
                 *  pmpoly.styleUrl = "#spray";
                 *
                 *  fldr.Add(pmpoly);
                 * }
                 */
                LineString ls = new LineString();
                ls.AltitudeMode = altmode;
                ls.Extrude      = true;
                //ls.Tessellate = true;

                Coordinates coords = new Coordinates();
                coords.AddRange(poslist);

                ls.coordinates = coords;

                Placemark pm = new Placemark();

                string mode = "";
                if (g < modelist.Count)
                {
                    mode = modelist[g];
                }

                pm.name       = g + " Flight Path " + mode;
                pm.styleUrl   = "#yellowLineGreenPoly";
                pm.LineString = ls;

                stylecode = colours[g % (colours.Length - 1)].ToArgb();

                Style style2 = new Style();
                Color color  = Color.FromArgb(0xff, (stylecode >> 16) & 0xff, (stylecode >> 8) & 0xff,
                                              (stylecode >> 0) & 0xff);
                log.Info("colour " + color.ToArgb().ToString("X") + " " + color.ToString());
                style2.Add(new LineStyle(color, 4));


                pm.AddStyle(style2);

                fldr.Add(pm);
            }

            Placemark pmPOS = new Placemark();

            pmPOS.name                   = "POS Message";
            pmPOS.LineString             = new LineString();
            pmPOS.LineString.coordinates = new Coordinates();
            Point3D        lastPoint3D = new Point3D();
            PointLatLngAlt lastplla    = PointLatLngAlt.Zero;

            foreach (var item in PosLatLngAlts)
            {
                var newpoint = new Point3D(item.Lng, item.Lat, item.Alt);

                if (item.GetDistance(lastplla) < 0.1 &&
                    lastPoint3D.Z >= (newpoint.Z - 0.3) &&
                    lastPoint3D.Z <= (newpoint.Z + 0.3))
                {
                    continue;
                }

                pmPOS.LineString.coordinates.Add(newpoint);
                lastPoint3D = newpoint;
                lastplla    = item;
                if (pmPOS.LineString.coordinates.Count > 20000)
                {
                    //add current
                    pmPOS.AddStyle(style);
                    fldr.Add(pmPOS);

                    // create new
                    pmPOS                        = new Placemark();
                    pmPOS.name                   = "POS Message - extra";
                    pmPOS.LineString             = new LineString();
                    pmPOS.LineString.coordinates = new Coordinates();
                    lastPoint3D                  = new Point3D();
                    lastplla                     = PointLatLngAlt.Zero;
                }
            }
            pmPOS.AddStyle(style);
            fldr.Add(pmPOS);

            Folder planes = new Folder();

            planes.name = "Planes";
            fldr.Add(planes);

            Folder waypoints = new Folder();

            waypoints.name = "Waypoints";
            fldr.Add(waypoints);


            LineString lswp = new LineString();

            lswp.AltitudeMode = AltitudeMode.relativeToGround;
            lswp.Extrude      = true;

            Coordinates coordswp = new Coordinates();
            int         lastwp   = 0;

            foreach (var line in cmdraw)
            {
                var item = dflog.GetDFItemFromLine(line, 0);

                if (int.Parse(item["CId"]) <= (int)MAVLink.MAV_CMD.LAST) // wps
                {
                    var wpno = int.Parse(item["CNum"]);

                    if (wpno < lastwp)
                    {
                        lswp.coordinates = coordswp;

                        Placemark pmwp = new Placemark();

                        pmwp.name = "Waypoints ";
                        //pm.styleUrl = "#yellowLineGreenPoly";
                        pmwp.LineString = lswp;

                        if (coordswp.Count > 0)
                        {
                            waypoints.Add(pmwp);
                        }

                        lswp = new LineString();
                        lswp.AltitudeMode = AltitudeMode.relativeToGround;
                        lswp.Extrude      = true;

                        coordswp = new Coordinates();
                    }

                    lastwp = wpno;

                    var lng = double.Parse(item["Lng"], CultureInfo.InvariantCulture);
                    var lat = double.Parse(item["Lat"], CultureInfo.InvariantCulture);
                    var alt = double.Parse(item["Alt"], CultureInfo.InvariantCulture);
                    if (wpno == 0)
                    {
                        alt = 0;
                    }

                    if (lat == 0 && lng == 0)
                    {
                        continue;
                    }

                    coordswp.Add(new Point3D(lng, lat, alt));
                }
            }

            lswp.coordinates = coordswp;

            Placemark pmwp2 = new Placemark();

            pmwp2.name = "Waypoints";
            //pm.styleUrl = "#yellowLineGreenPoly";
            pmwp2.LineString = lswp;

            if (coordswp.Count > 0)
            {
                waypoints.Add(pmwp2);
            }

            int a = 0;
            int l = -1;

            Model lastmodel = null;

            foreach (Data mod in flightdata)
            {
                l++;
                if (mod.model.Location.latitude == 0)
                {
                    continue;
                }

                if (lastmodel != null)
                {
                    if (lastmodel.Location.Equals(mod.model.Location))
                    {
                        continue;
                    }
                }
                Placemark pmplane = new Placemark();
                pmplane.name = "Plane " + a;

                pmplane.visibility = false;

                Model model = mod.model;
                model.AltitudeMode = altmode;
                model.Scale.x      = 2;
                model.Scale.y      = 2;
                model.Scale.z      = 2;

                try
                {
                    pmplane.description = @"<![CDATA[
              <table>
                <tr><td>Roll: " + model.Orientation.roll + @" </td></tr>
                <tr><td>Pitch: " + model.Orientation.tilt + @" </td></tr>
                <tr><td>Yaw: " + model.Orientation.heading + @" </td></tr>
                <tr><td>WP dist " + mod.ntun[2] + @" </td></tr>
				<tr><td>tar bear "                 + mod.ntun[3] + @" </td></tr>
				<tr><td>nav bear "                 + mod.ntun[4] + @" </td></tr>
				<tr><td>alt error "                 + mod.ntun[5] + @" </td></tr>
              </table>
            ]]>";
                }
                catch
                {
                }

                try
                {
                    pmplane.Point = new KmlPoint((float)model.Location.longitude, (float)model.Location.latitude,
                                                 (float)model.Location.altitude);
                    pmplane.Point.AltitudeMode = altmode;

                    Link link = new Link();
                    link.href = "block_plane_0.dae";

                    model.Link = link;

                    pmplane.Model = model;

                    planes.Add(pmplane);
                }
                catch
                {
                } // bad lat long value

                lastmodel = mod.model;

                a++;
            }

            kml.Document.Add(fldr);

            kml.Save(filename);

            // create kmz - aka zip file

            FileStream fs = File.Open(filename.ToLower().Replace(".log.kml", ".kmz").Replace(".bin.kml", ".kmz"),
                                      FileMode.Create);
            ZipOutputStream zipStream = new ZipOutputStream(fs);

            zipStream.SetLevel(9);             //0-9, 9 being the highest level of compression
            zipStream.UseZip64 = UseZip64.Off; // older zipfile

            // entry 1
            string entryName = ZipEntry.CleanName(Path.GetFileName(filename));
            // Removes drive from name and fixes slash direction
            ZipEntry newEntry = new ZipEntry(entryName);

            newEntry.DateTime = DateTime.Now;

            zipStream.PutNextEntry(newEntry);

            // Zip the file in buffered chunks
            // the "using" will close the stream even if an exception occurs
            byte[] buffer = new byte[4096];
            using (FileStream streamReader = File.OpenRead(filename))
            {
                StreamUtils.Copy(streamReader, zipStream, buffer);
            }
            zipStream.CloseEntry();

            File.Delete(filename);

            filename = Settings.GetRunningDirectory() +
                       "block_plane_0.dae";

            // entry 2
            entryName = ZipEntry.CleanName(Path.GetFileName(filename));
            // Removes drive from name and fixes slash direction
            newEntry          = new ZipEntry(entryName);
            newEntry.DateTime = DateTime.Now;

            zipStream.PutNextEntry(newEntry);

            // Zip the file in buffered chunks
            // the "using" will close the stream even if an exception occurs
            buffer = new byte[4096];
            using (FileStream streamReader = File.OpenRead(filename))
            {
                StreamUtils.Copy(streamReader, zipStream, buffer);
            }
            zipStream.CloseEntry();


            zipStream.IsStreamOwner = true; // Makes the Close also Close the underlying stream
            zipStream.Close();

            positionindex = 0;
            modelist.Clear();
            flightdata.Clear();
            position = new List <Core.Geometry.Point3D> [200];
            cmdraw.Clear();
        }
Esempio n. 7
0
        public static void MapLogs(string[] logs)
        {
            foreach (var logfile in logs)
            {
                if (File.Exists(logfile + ".jpg"))
                    continue;

                double minx = 99999;
                double maxx = -99999;
                double miny = 99999;
                double maxy = -99999;

                bool sitl = false;

                Dictionary<int,List<PointLatLngAlt>> loc_list = new Dictionary<int, List<PointLatLngAlt>>();

                try
                {
                    if (logfile.ToLower().EndsWith(".tlog"))
                    {
                        using (MAVLinkInterface mine = new MAVLinkInterface())
                        using (
                            mine.logplaybackfile =
                                new BinaryReader(File.Open(logfile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                            )
                        {
                            mine.logreadmode = true;
                            mine.speechenabled = false;

                            while (mine.logplaybackfile.BaseStream.Position < mine.logplaybackfile.BaseStream.Length)
                            {
                                MAVLink.MAVLinkMessage packet = mine.readPacket();

                                if (packet.Length < 5)
                                    continue;

                                if (packet.msgid == (byte)MAVLink.MAVLINK_MSG_ID.SIM_STATE || packet.msgid == (byte)MAVLink.MAVLINK_MSG_ID.SIMSTATE)
                                {
                                    sitl = true;
                                }

                                if (packet.msgid == (byte) MAVLink.MAVLINK_MSG_ID.GLOBAL_POSITION_INT)
                                {
                                    var loc = packet.ToStructure<MAVLink.mavlink_global_position_int_t>();

                                    if (loc.lat == 0 || loc.lon == 0)
                                        continue;

                                    var id = MAVList.GetID(packet.sysid, packet.compid);

                                    if (!loc_list.ContainsKey(id))
                                        loc_list[id] = new List<PointLatLngAlt>();

                                    loc_list[id].Add(new PointLatLngAlt(loc.lat/10000000.0f, loc.lon/10000000.0f));

                                    minx = Math.Min(minx, loc.lon/10000000.0f);
                                    maxx = Math.Max(maxx, loc.lon/10000000.0f);
                                    miny = Math.Min(miny, loc.lat/10000000.0f);
                                    maxy = Math.Max(maxy, loc.lat/10000000.0f);
                                }
                            }
                        }
                    }
                    else if (logfile.ToLower().EndsWith(".bin") || logfile.ToLower().EndsWith(".log"))
                    {
                        bool bin = logfile.ToLower().EndsWith(".bin");

                        BinaryLog binlog = new BinaryLog();
                        DFLog dflog = new DFLog();

                        using (var st = File.Open(logfile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                        {
                            using (StreamReader sr = new StreamReader(st))
                            {
                                loc_list[0] = new List<PointLatLngAlt>();

                                while (sr.BaseStream.Position < sr.BaseStream.Length)
                                {
                                    string line = "";

                                    if (bin)
                                    {
                                        line = binlog.ReadMessage(sr.BaseStream);
                                    }
                                    else
                                    {
                                        line = sr.ReadLine();
                                    }

                                    if (line.StartsWith("FMT"))
                                    {
                                        dflog.FMTLine(line);
                                    }
                                    else if (line.StartsWith("GPS"))
                                    {
                                        var item = dflog.GetDFItemFromLine(line, 0);

                                        if (!dflog.logformat.ContainsKey("GPS"))
                                            continue;

                                        var status = double.Parse(item.items[dflog.FindMessageOffset(item.msgtype, "Status")]);
                                        var lat = double.Parse(item.items[dflog.FindMessageOffset(item.msgtype, "Lat")]);
                                        var lon = double.Parse(item.items[dflog.FindMessageOffset(item.msgtype, "Lng")]);

                                        if (lat == 0 || lon == 0 || status < 3)
                                            continue;                                            

                                        loc_list[0].Add(new PointLatLngAlt(lat, lon));

                                        minx = Math.Min(minx, lon);
                                        maxx = Math.Max(maxx, lon);
                                        miny = Math.Min(miny, lat);
                                        maxy = Math.Max(maxy, lat);
                                    }
                                }
                            }
                        }
                    }


                    if (loc_list.Count > 0 && loc_list.First().Value.Count > 10)
                    {
                        // add a bit of buffer
                        var area = RectLatLng.FromLTRB(minx - 0.001, maxy + 0.001, maxx + 0.001, miny - 0.001);
                        var map = GetMap(area);

                        var grap = Graphics.FromImage(map);

                        if (sitl)
                        {
                            AddTextToMap(grap, "SITL");
                        }

                        Color[] colours =
                        {
                            Color.Red, Color.Orange, Color.Yellow, Color.Green, Color.Blue, Color.Indigo,
                            Color.Violet, Color.Pink
                        };

                        int a = 0;
                        foreach (var locs in loc_list.Values)
                        {
                            PointF lastpoint = new PointF();
                            var pen = new Pen(colours[a%(colours.Length - 1)]);

                            foreach (var loc in locs)
                            {
                                PointF newpoint = GetPixel(area, loc, map.Size);

                                if (!lastpoint.IsEmpty)
                                    grap.DrawLine(pen, lastpoint, newpoint);

                                lastpoint = newpoint;
                            }

                            a++;
                        }

                        map.Save(logfile + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

                        map.Dispose();

                        map = null;
                    }
                    else
                    {
                        DoTextMap(logfile + ".jpg", "No gps data");
                    }
                }
                catch (Exception ex)
                {
                    if (ex.ToString().Contains("Mavlink 0.9"))
                        DoTextMap(logfile + ".jpg", "Old log\nMavlink 0.9");

                    continue;
                }
            }
        }