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; }
wxString Boat::OpenXML(wxString filename) { TiXmlDocument doc; if(!doc.LoadFile( filename.mb_str() )) return _("Failed to load file"); TiXmlHandle root( doc.RootElement() ); if(strcmp(doc.RootElement()->Value(), "OCPNWeatherRoutingBoat")) return _("Failed to read xml file (no OCPWeatherRoutingBoat node)"); bool cleared = false; for(unsigned int i=0; i<Plans.size(); i++) delete Plans[i]; Plans.clear(); Plans.push_back(new BoatPlan(_("Initial Plan"), *this)); Plans[0]->ComputeBoatSpeeds(*this); for(TiXmlElement* e = root.FirstChild().Element(); e; e = e->NextSiblingElement()) { if(!strcmp(e->Value(), "BoatCharacteristics")) { displacement_tons = AttributeDouble(e, "displacement_tons", 4); lwl_ft = AttributeDouble(e, "lwl_ft", 24); loa_ft = AttributeDouble(e, "loa_ft", 27); beam_ft = AttributeDouble(e, "beam_ft", 8); } else if(!strcmp(e->Value(), "BoatDrag")) { frictional_drag = AttributeDouble(e, "frictional_drag", 0); wake_drag = AttributeDouble(e, "wake_drag", 0); } else if(!strcmp(e->Value(), "Plan")) { if(!cleared) { for(unsigned int i=0; i<Plans.size(); i++) delete Plans[i]; Plans.clear(); cleared = true; } BoatPlan *plan = new BoatPlan(wxString::FromUTF8(e->Attribute("Name")), *this); plan->computed = AttributeDouble(e, "computed", 1); if(plan->computed) { plan->eta = AttributeDouble(e, "eta", .5); plan->luff_angle = AttributeDouble(e, "luff_angle", 15); plan->wind_speed_step = 3; plan->wind_degree_step = DEGREE_STEP; plan->ComputeBoatSpeeds(*this); } else { plan->csvFileName = wxString::FromUTF8(e->Attribute("csvFileName")); BoatSpeedTable table; if(table.Open(plan->csvFileName.mb_str(), plan->wind_speed_step, plan->wind_degree_step)) { plan->SetSpeedsFromTable(table); } else return _("Failed to open file: ") + plan->csvFileName; } for(TiXmlElement* f = e->FirstChildElement(); f; f = f->NextSiblingElement()) { if(!strcmp(e->Value(), "SwitchPlan")) { SwitchPlan switchplan; switchplan.MaxWindSpeed = strtod(f->Attribute("MaxWindSpeed"), 0); switchplan.MinWindSpeed = strtod(f->Attribute("MinWindSpeed"), 0); switchplan.MaxWindDirection = strtod(f->Attribute("MaxWindDirection"), 0); switchplan.MinWindDirection = strtod(f->Attribute("MinWindDirection"), 0); switchplan.MaxWaveHeight = strtod(f->Attribute("MaxWaveHeight"), 0); switchplan.MinWaveHeight = strtod(f->Attribute("MinWaveHeight"), 0); if(f->QueryBoolAttribute("DayTime", &switchplan.DayTime) != TIXML_SUCCESS) switchplan.DayTime = true; if(f->QueryBoolAttribute("NightTime", &switchplan.NightTime) != TIXML_SUCCESS) switchplan.NightTime = true; switchplan.Name = wxString::FromUTF8(f->Attribute("Name")); plan->SwitchPlans.push_back(switchplan); } } Plans.push_back(plan); } } return wxString(); }
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; }