示例#1
0
        public override bool process(string url, string filename, string source)
        {
            bool ret = true;

            LibSys.StatusBar.Trace("IP: FilePlainWpt:process() filename=" + filename);

            int wpCount = 0;
            int lineno = 0;

            try
            {
                string line;
                int state = 0;
                int numpoints = 0;
                CreateInfo createInfo = new CreateInfo();	// we will recycle it in place, filling every time.

                StreamReader stream = new StreamReader(filename);

                while((line = stream.ReadLine()) != null)
                {
                    lineno++;
                    try
                    {
                        switch(state)
                        {
                            case 0:   // look for [WAYPOINTS]
                                if(line.StartsWith("[WAYPOINTS]"))
                                {
                                    state = 2;
                                    //LibSys.StatusBar.Trace("IP: FilePlainWpt:process() state 2 filename=" + filename);
                                }
                                if(line.StartsWith("[ROUTES]"))
                                {
                                    state = 3;
                                    //LibSys.StatusBar.Trace("IP: FilePlainWpt:process() state 3 filename=" + filename);
                                    break;
                                }
                                break;
                            case 2:   // look for "NUMPOINTS=" for wpts
                            case 3:   // look for "NUMPOINTS=" for routes
                                if(line.StartsWith("NUMPOINTS="))
                                {
                                    state *= 2;
                                    string sNumpoints =  line.Substring("NUMPOINTS=".Length);
                                    numpoints = Convert.ToInt32(sNumpoints);
                                    if(numpoints <= 0)
                                    {
                                        state = 0;
                                    }
                                    //LibSys.StatusBar.Trace("IP: FilePlainWpt:process() state " + state + " numpoints=" + numpoints + " filename=" + filename);
                                }
                                break;
                            case 4:   // read WPT numpoints lines like:
                                // WPT1=1,5052.677,N,00002.217,E,0000164,M,ME164-,Cliffe Hill ,a
                                if(line.StartsWith("WPT"))
                                {
                                    char[] sep = new Char[1] { ',' };
                                    string[] split = line.Split(sep);

                                    double lat = Convert.ToDouble(split[1]) / 100.0d;
                                    double lng = Convert.ToDouble(split[3]) / 100.0d;

                                    createInfo.init("wpt");
                                    createInfo.setLat("" + lat + split[2].ToLower());
                                    createInfo.setLng("" + lng + split[4].ToLower());
                                    createInfo.setElev(split[5]);
                                    createInfo.typeExtra = "waypoint";
                                    createInfo.source = source;
                                    createInfo.name = split[8];

                                    m_insertWaypoint(createInfo);
                                    wpCount++;
                                    numpoints--;
                                    if(numpoints == 0)
                                    {
                                        state = 0;	// look for command again
                                    }
                                }
                                break;
                            case 6:   // read ROUTE numpoints lines like:
                                numpoints--;
                                if(numpoints == 0)
                                {
                                    state = 0;	// look for command again
                                }
                                break;
                        }
                    }
                    catch (Exception ee)
                    {
                        LibSys.StatusBar.Error("FilePlainWpt:process():  file=" + filename + " line=" + lineno + " " + ee.Message);
                    }
                }
            }
            catch (Exception eee)
            {
                LibSys.StatusBar.Error("FilePlainWpt:process(): " + eee.Message);
                ret = false;
            }
            //LibSys.StatusBar.Trace("OK: FilePlainWpt:process() filename=" + filename + " lines=" + lineno + " waypoints=" + wpCount);
            return ret;
        }
示例#2
0
        // xmlDoc must be Load'ed; can throw exceptions
        protected bool processGpx(string url, XmlDocument xmlDoc, string source)
        {
            bool ret = true;

            XmlNodeList waypointNodes = xmlDoc.DocumentElement.ChildNodes; //.SelectNodes("/gpx/wpt"); - this don't work
            #if DEBUG
            LibSys.StatusBar.Trace("IP: FileGpx:process() first level nodeCount=" + waypointNodes.Count);
            #endif

            /*
                <gpx
                version="1.0"
                creator="EasyGPS 1.1.9 - http://www.topografix.com"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xmlns="http://www.topografix.com/GPX/1/0"
                xmlns:topografix="http://www.topografix.com/GPX/Private/TopoGrafix/0/1"
                xsi:schemaLocation="http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd http://www.topografix.com/GPX/Private/TopoGrafix/0/1 http://www.topografix.com/GPX/Private/TopoGrafix/0/1/topografix.xsd">
                <time>2002-10-04T18:49:55Z</time>
                <bounds minlat="25.061783" minlon="-123.003111" maxlat="50.982883" maxlon="121.640267"/>
                <wpt lat="33.575106480" lon="-117.735883651">
                    <ele>159.703613</ele>
                    <time>2002-10-04T18:47:47Z</time>
                    <name><![CDATA[001]]></name>
                    <cmt><![CDATA[29-AUG-02 11:21:24PM]]></cmt>
                    <desc><![CDATA[001]]></desc>
                    <sym>Dot</sym>
                    <type><![CDATA[Dot]]></type>
                </wpt>
                <rte>
                    <name><![CDATA[HOME TO CNTRYC]]></name>
                    <number>1</number>
                    <topografix:color>ff0000</topografix:color>
                    <rtept lat="33.574991229" lon="-117.736144077">
                        <time>2002-10-11T05:34:36Z</time>
                        <name><![CDATA[HOME]]></name>
                        <cmt><![CDATA[AURORA]]></cmt>
                        <desc><![CDATA[HOME]]></desc>
                        <sym>House</sym>
                        <type><![CDATA[Residence]]></type>
                    </rtept>
                </rte>
                <trk>
                    <name><![CDATA[ACTIVE LOG]]></name>
                    <number>1</number>
                    <topografix:color>ff0000</topografix:color>
                    <trkseg>
                        <trkpt lat="33.570749483" lon="-117.723665938">
                            <ele>106.363037</ele>
                            <time>2002-10-11T04:32:08Z</time>
                            <sym>Waypoint</sym>
                        </trkpt>
                        <trkpt lat="33.571032289" lon="-117.722491633">
                            <ele>99.153076</ele>
                            <time>2002-10-11T04:32:18Z</time>
                            <sym>Waypoint</sym>
                        </trkpt>
                    </trkseg>
                </trk>
                </gpx>
                */

            CreateInfo createInfo = new CreateInfo();	// we will recycle it in place, filling every time.

            // we want to traverse XmlDocument fast, as tile load operations can be numerous
            // and come in pack. So we avoid using XPath and rely mostly on "foreach child":
            foreach(XmlNode nnode in waypointNodes)
            {
                try
                {
                    switch(nnode.Name)
                    {
                        case "author":
                            //LibSys.StatusBar.Trace("FileGpx:process() node=" + nnode.Name);
                            break;
                        case "desc":
                            //LibSys.StatusBar.Trace("FileGpx:process() node=" + nnode.Name);
                            break;
                        case "bounds":
                            //LibSys.StatusBar.Trace("FileGpx:process() node=" + nnode.Name);
                            break;
                        case "wpt":
                            //LibSys.StatusBar.Trace("FileGpx:process() node=" + nnode.Name);
                            createInfo.init("wpt");
                            createInfo.setLat(nnode.Attributes["lat"].InnerText);
                            createInfo.setLng(nnode.Attributes["lon"].InnerText);
                            createInfo.typeExtra = "unknown";	// type: ppl, school, park, locale, airport, reservoir, dam,
                                                                //       civil, cemetery, valley, building
                            createInfo.source = source;
                            foreach(XmlNode node in nnode.ChildNodes)
                            {
                                //LibSys.StatusBar.Trace("    child node=" + node.Name);
                                switch(node.Name)
                                {
                                    case "time":
                                        createInfo.setDateTime(node.InnerText);
                                        break;
                                    case "ele":
                                        createInfo.setElev(node.InnerText);
                                        break;
                                    case "name":
                                        createInfo.name = node.InnerText.Trim();	// number like 001 for route-related wpts or GCSDFX for geocaches
                                        break;
                                    case "desc":
                                        createInfo.desc = node.InnerText.Trim();
                                        break;
                                    case "groundspeak:cache":
                                        createInfo.node1 = node;		// will be parsed in Waypoint() constructor
                                        break;
                                    case "urlname":		// may overwrite name in Pocket Queries
                                        createInfo.urlName = node.InnerText.Trim();
                                        break;
                                    case "url":
                                        createInfo.url = node.InnerText.Trim();
                                        break;
                                    case "cmt":
                                        createInfo.comment = node.InnerText.Trim();
                                        // may contain time, so try to extract it if possible:
                                        try
                                        {
                                            createInfo.setDateTime(node.InnerText);
                                        }
                                        catch {}
                                        break;
                                    case "sym":
                                        createInfo.sym = node.InnerText.Trim();
                                        break;
                                    case "type":	// like "user waypoint" or "geocache"
                                        createInfo.typeExtra = node.InnerText.Trim();
                                        break;
                                }
                            }
                            m_insertWaypoint(createInfo);
                            break;
                        case "rte":
                        {
                            string routeName = "";
                            string routeColor = "";
                            bool routeCreated = false;
                            foreach(XmlNode nnnode in nnode.ChildNodes)
                            {
                                switch(nnnode.Name)
                                {
                                    case "name":	// route name
                                        routeName = nnnode.InnerText.Trim();
                                        break;
                                    case "number":	// route number
                                        break;
                                    case "topografix:color":	// like 00ffee
                                        routeColor = nnnode.InnerText.Trim();
                                        break;
                                    case "rtept":
                                        /*
                                            <rtept lat="38.518697986" lon="-122.978969274">
                                                <ele>4.211426</ele>
                                                <time>2002-10-04T18:48:23Z</time>
                                                <name><![CDATA[006]]></name>
                                                <cmt><![CDATA[28-SEP-02 1:56:26PM]]></cmt>
                                                <desc><![CDATA[006]]></desc>
                                                <sym>Dot</sym>
                                                <type><![CDATA[Dot]]></type>
                                            </rtept>
                                        */
                                        //LibSys.StatusBar.Trace("FileGpx:process() node=" + nnode.Name);

                                        if(!routeCreated)
                                        {
                                            Project.trackId++;

                                            createInfo.init("rte");
                                            createInfo.name = routeName;
                                            createInfo.id = Project.trackId;
                                            createInfo.par1 = nnnode.InnerText.Trim();	// number for route
                                            createInfo.source = source;
                                            createInfo.par1 = routeColor;

                                            m_insertWaypoint(createInfo);	// actually inserts a route

                                            routeCreated = true;
                                        }
                                        createInfo.init("rtept");
                                        createInfo.setLat(nnnode.Attributes["lat"].InnerText);
                                        createInfo.setLng(nnnode.Attributes["lon"].InnerText);
                                        createInfo.id = Project.trackId;	// relate waypoint to track
                                        createInfo.source = source;

                                        foreach(XmlNode node in nnnode.ChildNodes)
                                        {
                                            //LibSys.StatusBar.Trace("    child node=" + node.Name);
                                            switch(node.Name)
                                            {
                                                case "time":
                                                    createInfo.setDateTime(node.InnerText);
                                                    break;
                                                case "ele":
                                                    createInfo.setElev(node.InnerText);
                                                    break;
                                                case "name":
                                                    createInfo.name = node.InnerText.Trim();	// number like 001 for route-related wpts or GCSDFX for geocaches
                                                    break;
                                                case "desc":
                                                    createInfo.desc = node.InnerText.Trim();
                                                    break;
                                                case "urlname":
                                                    createInfo.urlName = node.InnerText.Trim();
                                                    break;
                                                case "url":
                                                    createInfo.url = node.InnerText.Trim();
                                                    break;
                                                case "cmt":
                                                    createInfo.comment = node.InnerText.Trim();
                                                    break;
                                                case "sym":
                                                    createInfo.sym = node.InnerText.Trim();
                                                    if("Waypoint".Equals(createInfo.sym))
                                                    {
                                                        createInfo.sym = null;
                                                    }
                                                    break;
                                                case "type":	// like "user waypoint" or "geocache"
                                                    createInfo.typeExtra = node.InnerText.Trim();
                                                    break;
                                            }
                                        }

                                        m_insertWaypoint(createInfo);
                                        break;
                                }
                            }
                        }
                            break;
                        case "trk":
                        {
                            string trackName = "";
                            string trackNumber = "";
                            string trackColor = "";
                            foreach(XmlNode nnnode in nnode.ChildNodes)
                            {
                                switch(nnnode.Name)
                                {
                                    case "name":	// track name
                                        trackName = nnnode.InnerText.Trim();
                                        break;
                                    case "number":	// track number
                                        trackNumber = nnnode.InnerText.Trim();
                                        break;
                                    case "topografix:color":	// like 00ffee
                                        trackColor = nnnode.InnerText.Trim();
                                        break;
                                    case "trkseg":
                                        Project.trackId++;

                                        createInfo.init("trk");
                                        createInfo.name = trackName;
                                        createInfo.id = Project.trackId;
                                        createInfo.source = source;
                                        createInfo.par1 = trackColor;

                                        m_insertWaypoint(createInfo);	// actually inserts a track

                                        foreach(XmlNode nnnnode in nnnode.ChildNodes)
                                        {
                                            switch(nnnnode.Name)
                                            {
                                                case "trkpt":	// track point
                                                    /*
                                                        <trkpt lat="33.570749483" lon="-117.723665938">
                                                            <ele>106.363037</ele>
                                                            <time>2002-10-11T04:32:08Z</time>
                                                            <sym>Waypoint</sym>
                                                        </trkpt>
                                                    */
                                                    //LibSys.StatusBar.Trace("FileGpx:process() node=" + nnode.Name);

                                                    createInfo.init("trkpt");
                                                    createInfo.setLat(nnnnode.Attributes["lat"].InnerText);
                                                    createInfo.setLng(nnnnode.Attributes["lon"].InnerText);
                                                    createInfo.id = Project.trackId;	// relate waypoint to track
                                                    createInfo.source = source;

                                                    foreach(XmlNode node in nnnnode.ChildNodes)
                                                    {
                                                        //LibSys.StatusBar.Trace("    child node=" + node.Name);
                                                        switch(node.Name)
                                                        {
                                                            case "time":
                                                                createInfo.setDateTime(node.InnerText);
                                                                break;
                                                            case "ele":
                                                                createInfo.setElev(node.InnerText);
                                                                break;
                                                            case "name":
                                                                createInfo.name = node.InnerText.Trim();
                                                                break;
                                                            case "desc":
                                                                createInfo.desc = node.InnerText.Trim();
                                                                break;
                                                            case "urlname":
                                                                createInfo.urlName = node.InnerText.Trim();
                                                                break;
                                                            case "url":
                                                                createInfo.url = node.InnerText.Trim();
                                                                break;
                                                            case "cmt":
                                                                createInfo.comment = node.InnerText.Trim();
                                                                break;
                                                            case "sym":
                                                                createInfo.sym = node.InnerText.Trim();
                                                                if("Waypoint".Equals(createInfo.sym))
                                                                {
                                                                    createInfo.sym = null;
                                                                }
                                                                break;
                                                            case "type":	// like "user waypoint" or "geocache"
                                                                createInfo.typeExtra = node.InnerText.Trim();
                                                                break;
                                                        }
                                                    }

                                                    m_insertWaypoint(createInfo);
                                                    break;
                                            }
                                        }
                                        break;
                                }
                            }
                        }
                            break;
                    }
                }
                catch (Exception ee)
                {
                    // bad node - not a big deal...
                    LibSys.StatusBar.Error("FileGpx process node=" + nnode.Name + " " + ee); //.Message);
                    //LibSys.StatusBar.WriteLine("Culture: " + Thread.CurrentThread.CurrentCulture + " (" + Thread.CurrentThread.CurrentCulture.DisplayName + ")  UseUserOverride=" + Thread.CurrentThread.CurrentCulture.UseUserOverride);
                }
            }
            return ret;
        }