Пример #1
0
bool WeatherRouting::OpenXML(wxString filename, bool reportfailure)
{
    TiXmlDocument doc;
    wxString error;

    wxFileName fn(filename);

    SetTitle(_("Weather Routing") + wxString(_T(" - ")) + fn.GetFullName());

    wxProgressDialog *progressdialog = NULL;
    wxDateTime start = wxDateTime::Now();

    if(!doc.LoadFile(filename.mb_str()))
        FAIL(_("Failed to load file."));
    else
    {
        TiXmlHandle root(doc.RootElement());

        if(strcmp(root.Element()->Value(), "OpenCPNWeatherRoutingConfiguration"))
            FAIL(_("Invalid xml file"));

        RouteMap::Positions.clear();

        int count = 0;
        for(TiXmlElement* e = root.FirstChild().Element(); e; e = e->NextSiblingElement())
            count++;

        int i=0;
        for(TiXmlElement* e = root.FirstChild().Element(); e; e = e->NextSiblingElement(), i++)
        {
            if(progressdialog)
            {
                if(!progressdialog->Update(i))
                    return true;
            } else {
                wxDateTime now = wxDateTime::Now();
                /* if it's going to take more than a half second, show a progress dialog */
                if((now-start).GetMilliseconds() > 250 && i < count/2)
                {
                    progressdialog = new wxProgressDialog(
                        _("Load"), _("Weather Routing"), count, this,
                        wxPD_CAN_ABORT | wxPD_ELAPSED_TIME | wxPD_REMAINING_TIME);
                }
            }

            if(!strcmp(e->Value(), "Position"))
            {
                wxString name = wxString::FromUTF8(e->Attribute("Name"));
                double lat = AttributeDouble(e, "Latitude", NAN);
                double lon = AttributeDouble(e, "Longitude", NAN);

                for(std::list<RouteMapPosition>::iterator it = RouteMap::Positions.begin();
                    it != RouteMap::Positions.end(); it++)
                {
                    if((*it).Name == name)
                    {
                        static bool warnonce = true;
                        if(warnonce)
                        {
                            warnonce = false;
                            wxMessageDialog mdlg(this, _("File contains duplicate position name, discaring\n"),
                                                _("Weather Routing"), wxOK | wxICON_WARNING);
                            mdlg.ShowModal();
                        }

                        goto skipadd;
                    }
                }
                AddPosition(lat, lon, name);

            } else
skipadd:
                if(!strcmp(e->Value(), "Configuration"))
                {
                    RouteMapConfiguration configuration;
                    configuration.Start = wxString::FromUTF8(e->Attribute("Start"));
                    wxDateTime date;
                    date.ParseDate(wxString::FromUTF8(e->Attribute("StartDate")));
                    wxDateTime time;
                    time.ParseTime(wxString::FromUTF8(e->Attribute("StartTime")));
                    if(date.IsValid())
                    {
                        if(time.IsValid())
                        {
                            date.SetHour(time.GetHour());
                            date.SetMinute(time.GetMinute());
                            date.SetSecond(time.GetSecond());
                        }
                        configuration.StartTime = date;
                    } else
                        configuration.StartTime = wxDateTime::Now();

                    configuration.End = wxString::FromUTF8(e->Attribute("End"));
                    configuration.dt = AttributeDouble(e, "dt", 0);

                    configuration.boatFileName = wxString::FromUTF8(e->Attribute("Boat"));

                    configuration.MaxDivertedCourse = AttributeDouble(e, "MaxDivertedCourse", 180);
                    configuration.MaxWindKnots = AttributeDouble(e, "MaxWindKnots", 100);
                    configuration.MaxSwellMeters = AttributeDouble(e, "MaxSwellMeters", 20);
                    configuration.MaxLatitude = AttributeDouble(e, "MaxLatitude", 90);
                    configuration.MaxTacks = AttributeDouble(e, "MaxTacks", -1);
                    configuration.TackingTime = AttributeDouble(e, "TackingTime", 0);

                    configuration.UseGrib = AttributeBool(e, "UseGrib", true);
                    configuration.UseClimatology = AttributeBool(e, "UseClimatology", true);
                    configuration.AllowDataDeficient = AttributeBool(e, "AllowDataDeficient", false);
                    configuration.DetectLand = AttributeBool(e, "DetectLand", true);
                    configuration.Currents = AttributeBool(e, "Currents", true);
                    configuration.InvertedRegions = AttributeBool(e, "InvertedRegions", false);
                    configuration.Anchoring = AttributeBool(e, "Anchoring", false);

                    wxString degreesteps = wxString::FromUTF8(e->Attribute("DegreeSteps"));
                    while(degreesteps.size())
                    {
                        double step;
                        if(degreesteps.BeforeFirst(';').ToDouble(&step))
                            configuration.DegreeSteps.push_back(step);
                        degreesteps = degreesteps.AfterFirst(';');
                    }
                    configuration.StartTime = date;
                } else
                    FAIL(_("Unrecognized xml node"));
        }
    }
    delete progressdialog;
    return true;
failed:

    if(reportfailure)
    {
        wxMessageDialog mdlg(this, error, _("Weather Routing"), wxOK | wxICON_ERROR);
        mdlg.ShowModal();
    }
    return false;
}
bool CelestialNavigationDialog::OpenXML(wxString filename, bool reportfailure)
{
    TiXmlDocument doc;
    wxString error;

    wxFileName fn(filename);

    if(!doc.LoadFile(filename.mb_str()))
        FAIL(_("Failed to load file: ") + filename);
    else {
        TiXmlHandle root(doc.RootElement());

        if(strcmp(root.Element()->Value(), "OpenCPNCelestialNavigation"))
            FAIL(_("Invalid xml file"));

        m_lSights->DeleteAllItems();

        for(TiXmlElement* e = root.FirstChild().Element(); e; e = e->NextSiblingElement()) {
            if(!strcmp(e->Value(), "ClockError")) {
                m_ClockCorrectionDialog.m_sClockCorrection->SetValue(AttributeInt(e, "Seconds", 0));
            } else if(!strcmp(e->Value(), "Sight")) {
                Sight s;

                s.m_bVisible = AttributeBool(e, "Visible", true);
                s.m_Type = (Sight::Type)AttributeInt(e, "Type", 0);
                s.m_Body = wxString::FromUTF8(e->Attribute("Body"));
                s.m_BodyLimb = (Sight::BodyLimb)AttributeInt(e, "BodyLimb", 0);

                s.m_DateTime.ParseISODate(wxString::FromUTF8(e->Attribute("Date")));

                wxDateTime time;
                time.ParseISOTime(wxString::FromUTF8(e->Attribute("Time")));

                if(s.m_DateTime.IsValid() && time.IsValid()) {
                    s.m_DateTime.SetHour(time.GetHour());
                    s.m_DateTime.SetMinute(time.GetMinute());
                    s.m_DateTime.SetSecond(time.GetSecond());
                } else
                    continue; /* skip if invalid */
            
                s.m_TimeCertainty = AttributeDouble(e, "TimeCertainty", 0);

                s.m_Measurement = AttributeDouble(e, "Measurement", 0);
                s.m_MeasurementCertainty = AttributeDouble(e, "MeasurementCertainty", .25);

                s.m_EyeHeight = AttributeDouble(e, "EyeHeight", 2);
                s.m_Temperature = AttributeDouble(e, "Temperature", 10);
                s.m_Pressure = AttributeDouble(e, "Pressure", 1010);
                s.m_IndexError = AttributeDouble(e, "IndexError", 0);

                s.m_ShiftNm = AttributeDouble(e, "ShiftNm", 0);
                s.m_ShiftBearing = AttributeDouble(e, "ShiftBearing", 0);
                s.m_bMagneticShiftBearing = AttributeBool(e, "MagneticShiftBearing", 0);

                s.m_ColourName = wxString::FromUTF8(e->Attribute("ColourName"));
                s.m_Colour = wxColour(wxString::FromUTF8(e->Attribute("Colour")));
                s.m_Colour.Set(s.m_Colour.Red(), s.m_Colour.Green(), s.m_Colour.Blue(),
                               AttributeInt(e, "Transparency", 150));

                Sight *ns = new Sight(s);

                ns->Recompute(m_ClockCorrectionDialog.m_sClockCorrection->GetValue());
                ns->RebuildPolygons();
                InsertSight(ns, false);
            } else
                FAIL(_("Unrecognized xml node"));
        }
    }

    RequestRefresh( GetParent() );
    return true;
failed:

    if(reportfailure) {
        wxMessageDialog mdlg(this, error, _("Celestial Navigation"), wxOK | wxICON_ERROR);
        mdlg.ShowModal();
    }
    return false;
}