private void calculatevoicenavigation(string name) { try { double PHI_A = 0.0; double PHI_B = 0.0; double LAMB_A = 0.0; double LAMB_B = 0.0; double a = 0.0; double beta = 0.0; double b = 0.0; double alfa = 0.0; double c = 0.0; double gamma = 0.0; string file = ""; string path = textBoxVoiceFolder.Text + "\\"; path = path.Replace(@"\\", @"\"); string extension = textBoxVoiceExtension.Text; if (textBoxVoiceFolder.Text == string.Empty) { path = string.Empty; } int elements = gpx_rtepoints.Count; if (elements < 3) { textoutput("Less than 3 way points in route - cannot calculate direction"); return; } textoutput("Creating new voice file tags for " + name); for (int i = 1; i < elements - 1; i++) { PHI_A = gpx_rtepoints[i - 1].lat / 180.0 * Math.PI; //i-1 LAMB_A = gpx_rtepoints[i - 1].lon / 180.0 * Math.PI; PHI_B = gpx_rtepoints[i].lat / 180.0 * Math.PI; //i LAMB_B = gpx_rtepoints[i].lon / 180.0 * Math.PI; a = (Math.Cos(PHI_A) * Math.Sin(PHI_B) - Math.Cos(LAMB_A - LAMB_B) * Math.Cos(PHI_B) * Math.Sin(PHI_A)); b = (Math.Cos(PHI_A) * Math.Sin(PHI_B) - Math.Cos(LAMB_A - LAMB_B) * Math.Cos(PHI_A) * Math.Sin(PHI_B)); c = Math.Sqrt(1 - Math.Pow((Math.Cos(LAMB_A - LAMB_B) * Math.Cos(PHI_A) * Math.Cos(PHI_B) + Math.Sin(PHI_A) * Math.Sin(PHI_B)), 2)); alfa = Math.Acos(a / c) * 180 / Math.PI; if ((LAMB_B - LAMB_A) < 0.0) { alfa = alfa * (-1); } PHI_A = gpx_rtepoints[i].lat / 180.0 * Math.PI; //i LAMB_A = gpx_rtepoints[i].lon / 180.0 * Math.PI; PHI_B = gpx_rtepoints[i + 1].lat / 180.0 * Math.PI; //i+1 LAMB_B = gpx_rtepoints[i + 1].lon / 180.0 * Math.PI; a = (Math.Cos(PHI_A) * Math.Sin(PHI_B) - Math.Cos(LAMB_A - LAMB_B) * Math.Cos(PHI_B) * Math.Sin(PHI_A)); b = (Math.Cos(PHI_A) * Math.Sin(PHI_B) - Math.Cos(LAMB_A - LAMB_B) * Math.Cos(PHI_A) * Math.Sin(PHI_B)); c = Math.Sqrt(1 - Math.Pow((Math.Cos(LAMB_A - LAMB_B) * Math.Cos(PHI_A) * Math.Cos(PHI_B) + Math.Sin(PHI_A) * Math.Sin(PHI_B)), 2)); beta = Math.Acos(a / c) * 180 / Math.PI; if ((LAMB_B - LAMB_A) < 0.0) { beta = beta * (-1); } gamma = beta - alfa; if (gamma > 180.0) { gamma = gamma - 360.0; } else if (gamma < -180.0) { gamma = gamma + 360.0; } if ((gamma >= -22.5) && (gamma < 22.5)) { file = "0Degree"; } else if ((gamma >= 22.5) && (gamma < 67.5)) { file = "45Degree"; } else if ((gamma >= 67.5) && (gamma < 112.5)) { file = "90Degree"; } else if ((gamma >= 112.5) && (gamma < 157.5)) { file = "135Degree"; } else if ((gamma >= 157.5) && (gamma < -157.5)) { file = "180Degree"; } else if ((gamma >= -157.5) && (gamma < -112.5)) { file = "225Degree"; } else if ((gamma >= -112.5) && (gamma < -67.5)) { file = "270Degree"; } else if ((gamma >= -67.5) && (gamma < -22.5)) { file = "315Degree"; } if (DEBUG) { textoutput("i=" + (i + 1).ToString()); textoutput("alfa=" + alfa.ToString()); textoutput("beta=" + beta.ToString()); textoutput("gamma=" + gamma.ToString()); textoutput(""); } //file = path+file+extension; Gpx_routepoint mygpxroutepoint = gpx_rtepoints[i]; mygpxroutepoint.angle1 = alfa; mygpxroutepoint.angle2 = beta; mygpxroutepoint.angle1to2 = gamma; mygpxroutepoint.Description = mygpxroutepoint.Description + "<alarm><sound " + path + file + extension + ">" + path + file + extension + "</sound></alarm>"; gpx_rtepoints[i] = mygpxroutepoint; } //start and end Gpx_routepoint mygpxroutepoint_s = gpx_rtepoints[0]; mygpxroutepoint_s.Description = mygpxroutepoint_s.Description + "<alarm><sound " + path + "Start" + extension + ">" + path + "Start" + extension + "</sound></alarm>"; gpx_rtepoints[0] = mygpxroutepoint_s; Gpx_routepoint mygpxroutepoint_e = gpx_rtepoints[elements - 1]; mygpxroutepoint_e.Description = mygpxroutepoint_e.Description + "<alarm><sound " + path + "End" + extension + ">" + path + "End" + extension + "</sound></alarm>"; gpx_rtepoints[elements - 1] = mygpxroutepoint_e; } catch (Exception exc) { textoutput("<RED>calculatevoicenavigation Exception:" + exc.Message + "\n"); } }
private void GPXProcessingThread() { try { textoutput("Reading GPX file"); XmlDocument doc = new XmlDocument(); string tmpfile = textBoxInputGPX.Text + ".tmp4387547417"; string startfile = ""; try { string filestring = File.ReadAllText(textBoxInputGPX.Text); filestring = filestring.Replace("<name><![CDATA[" + NAME_PREFIX,"<name><![CDATA["); filestring = filestring.Replace(NAME_POSTFIX + "]]></name>", "]]></name>"); filestring = filestring.Replace("<name><![CDATA[" + REVERSE_NAME_PREFIX, "<name><![CDATA["); filestring = filestring.Replace(REVERSE_NAME_POSTFIX + "]]></name>", "]]></name>"); int tagpos1 = filestring.IndexOf("<alarm>"); int tagpos2 = filestring.IndexOf("</alarm>"); while ((tagpos1 > 0) && (tagpos2 > 0)) { string replacestring = filestring.Substring(tagpos1, tagpos2-tagpos1+8); if (DEBUG) textoutput("replacestring=" + replacestring); filestring = filestring.Replace(replacestring, ""); tagpos1 = filestring.IndexOf("<alarm>"); tagpos2 = filestring.IndexOf("</alarm>"); } filestring = filestring.Replace("<extensions>", "<extensions><![CDATA["); filestring = filestring.Replace("</extensions>", "]]></extensions>"); if (checkBoxNewVoice.Checked == true) { if (checkBoxReverseRoutes.Checked == false) { filestring = filestring.Replace("<name><![CDATA[", "<name><![CDATA[" + NAME_PREFIX); filestring = filestring.Replace("]]></name>", NAME_POSTFIX + "]]></name>"); } else { filestring = filestring.Replace("<name><![CDATA[", "<name><![CDATA[" + REVERSE_NAME_PREFIX); filestring = filestring.Replace("]]></name>", REVERSE_NAME_POSTFIX + "]]></name>"); } } int pos1 = filestring.IndexOf("<rte>"); int pos2 = filestring.IndexOf("</gpx>"); if ((pos1 > 0) && (pos2 > 0)) { startfile = filestring.Substring(0, pos1 - 1); filestring = filestring.Substring(pos1, pos2 - pos1-1); filestring = "<gpx>" + filestring + "</gpx>"; } else { textoutput("<rte> not found in .gpx file - check you have a .gpx route file (no track file and no POI!)"); textoutput("<RED> Invalid positions in .gpx file pos1=" + pos1.ToString() + " pos2=" + pos2.ToString()+"\n\n"); return; } File.WriteAllText(tmpfile, filestring); doc.Load(tmpfile); } catch (Exception exc) { textoutput("<RED>Failed to load xml document "); textoutput("<RED>Exception message is " + exc.Message+"\n"); return; } XmlElement root = doc.DocumentElement; XmlNodeList nodeallroutes = root.SelectNodes("/gpx/rte"); int routecount = nodeallroutes.Count; textoutput(routecount.ToString() + " routes found"); int rte_ctr=0; foreach (XmlNode noderoute in nodeallroutes) { gpx_rtepoints = new List<Gpx_routepoint>(); XmlNodeList routenodenames = noderoute.SelectNodes("name"); foreach (XmlNode nodename in routenodenames) { routename = nodename.InnerText; } XmlNodeList nodepoints = noderoute.SelectNodes("rtept"); if ((checkBoxReverseRoutes.Checked == true)&&(rte_ctr < routecount)) {//reverse routes textoutput("Reversing route "+routename); XmlNode clone = noderoute.Clone(); XmlNodeList revroutenodenames = noderoute.SelectNodes("name"); foreach (XmlNode nodename in revroutenodenames) { nodename.InnerText=routename; } XmlNodeList clone_nodepoints = clone.SelectNodes("rtept"); foreach (XmlNode nodename in clone_nodepoints) { clone.RemoveChild(nodename); //textoutput("removing rte node"); } for (int i = nodepoints.Count-1; i >= 0; i--) { clone.AppendChild(nodepoints[i]); //textoutput("adding reversed node "+i.ToString()); } noderoute.RemoveAll(); XmlNode nodegpxroutes = root.SelectSingleNode("/gpx"); nodegpxroutes.AppendChild(clone); nodepoints = clone.SelectNodes("rtept"); //textoutput("End Reversing route " + routename); }//end reverse routes rte_ctr++; int ctr = 0; foreach (XmlNode nodepoint in nodepoints) { string longitude = nodepoint.Attributes["lon"].Value; string latitude = nodepoint.Attributes["lat"].Value; XmlNodeList nodenames = nodepoint.SelectNodes("name"); foreach (XmlNode nodename in nodenames) { name = nodename.InnerText; int wptctr = ctr + 1; if (checkBoxRenumber.Checked == true) { if (checkBoxNewVoice.Checked == true) { if (checkBoxReverseRoutes.Checked == true) { nodename.InnerText = REVERSE_NAME_PREFIX + "WPT" + wptctr.ToString("D4") + REVERSE_NAME_POSTFIX; } else { nodename.InnerText = NAME_PREFIX + "WPT" + wptctr.ToString("D4") + NAME_POSTFIX; } } else { nodename.InnerText = "WPT" + wptctr.ToString("D4"); } } } XmlNodeList nodecmts = nodepoint.SelectNodes("cmt"); string cmt = ""; foreach (XmlNode nodecmt in nodecmts) { cmt = nodecmt.InnerText; //textoutput("cmt =" + cmt); } /* if (DEBUG) { textoutput(ctr.ToString()); textoutput("name=" + name); textoutput("cmt=" + cmt); textoutput("lon=" + longitude); textoutput("lat=" + latitude); textoutput(""); }*/ Gpx_routepoint myroutepoint = new Gpx_routepoint(); myroutepoint.Name = name; myroutepoint.Description = cmt; myroutepoint.angle1 = -1.0; myroutepoint.angle2 = -1.0; myroutepoint.angle1to2 = -1.0; myroutepoint.voice = "none"; myroutepoint.lat = Convert.ToDouble(latitude, System.Globalization.NumberFormatInfo.InvariantInfo); myroutepoint.lon = Convert.ToDouble(longitude, System.Globalization.NumberFormatInfo.InvariantInfo); gpx_rtepoints.Add(myroutepoint); /*if (DEBUG) { textoutput("lon=" + myroutepoint.lon.ToString()); textoutput("lat=" + myroutepoint.lat.ToString()); textoutput(""); }*/ ctr++; } //process route for nav commands if (checkBoxNewVoice.Checked == true) { calculatevoicenavigation(routename); } //update xml information ctr = 0; foreach (XmlNode nodepoint in nodepoints) { XmlNodeList nodecmts = nodepoint.SelectNodes("cmt"); foreach (XmlNode nodecmt in nodecmts) { nodepoint.RemoveChild(nodecmt); } //create new node XmlNode newnodecmt = doc.CreateElement("cmt"); XmlCDataSection cdata = doc.CreateCDataSection(gpx_rtepoints[ctr].Description); //textoutput("update cmt=" + gpx_rtepoints[ctr].Description); newnodecmt.AppendChild(cdata); nodepoint.AppendChild(newnodecmt); ctr++; } //end single route } textoutput("Writing GPX file"); try { doc.Save(tmpfile); } catch (Exception exc) { textoutput("<RED>Failed to load xmlDoc.Load(fs) for writing"); textoutput("<RED>Exception message is " + exc.Message+"\n"); return; } string outfilestring = File.ReadAllText(tmpfile); outfilestring = outfilestring.Replace(" <rte>\r\n </rte>\r\n", ""); outfilestring = outfilestring.Replace("<extensions><![CDATA[", "<extensions>"); outfilestring = outfilestring.Replace( "]]></extensions>","</extensions>"); outfilestring = outfilestring.Replace("<gpx>", ""); File.WriteAllText(textBoxOutputGPX.Text,startfile+ outfilestring); File.Delete(tmpfile); textoutput("Completed\n\n"); } catch (Exception exc) { textoutput("<RED>GPXProcessingThread Error Exception:" + exc.Message + "\n"); } }
private void GPXProcessingThread() { try { textoutput("Reading GPX file"); XmlDocument doc = new XmlDocument(); string tmpfile = textBoxInputGPX.Text + ".tmp4387547417"; string startfile = ""; try { string filestring = File.ReadAllText(textBoxInputGPX.Text); filestring = filestring.Replace("<name><![CDATA[" + NAME_PREFIX, "<name><![CDATA["); filestring = filestring.Replace(NAME_POSTFIX + "]]></name>", "]]></name>"); filestring = filestring.Replace("<name><![CDATA[" + REVERSE_NAME_PREFIX, "<name><![CDATA["); filestring = filestring.Replace(REVERSE_NAME_POSTFIX + "]]></name>", "]]></name>"); int tagpos1 = filestring.IndexOf("<alarm>"); int tagpos2 = filestring.IndexOf("</alarm>"); while ((tagpos1 > 0) && (tagpos2 > 0)) { string replacestring = filestring.Substring(tagpos1, tagpos2 - tagpos1 + 8); if (DEBUG) { textoutput("replacestring=" + replacestring); } filestring = filestring.Replace(replacestring, ""); tagpos1 = filestring.IndexOf("<alarm>"); tagpos2 = filestring.IndexOf("</alarm>"); } filestring = filestring.Replace("<extensions>", "<extensions><![CDATA["); filestring = filestring.Replace("</extensions>", "]]></extensions>"); if (checkBoxNewVoice.Checked == true) { if (checkBoxReverseRoutes.Checked == false) { filestring = filestring.Replace("<name><![CDATA[", "<name><![CDATA[" + NAME_PREFIX); filestring = filestring.Replace("]]></name>", NAME_POSTFIX + "]]></name>"); } else { filestring = filestring.Replace("<name><![CDATA[", "<name><![CDATA[" + REVERSE_NAME_PREFIX); filestring = filestring.Replace("]]></name>", REVERSE_NAME_POSTFIX + "]]></name>"); } } int pos1 = filestring.IndexOf("<rte>"); int pos2 = filestring.IndexOf("</gpx>"); if ((pos1 > 0) && (pos2 > 0)) { startfile = filestring.Substring(0, pos1 - 1); filestring = filestring.Substring(pos1, pos2 - pos1 - 1); filestring = "<gpx>" + filestring + "</gpx>"; } else { textoutput("<rte> not found in .gpx file - check you have a .gpx route file (no track file and no POI!)"); textoutput("<RED> Invalid positions in .gpx file pos1=" + pos1.ToString() + " pos2=" + pos2.ToString() + "\n\n"); return; } File.WriteAllText(tmpfile, filestring); doc.Load(tmpfile); } catch (Exception exc) { textoutput("<RED>Failed to load xml document "); textoutput("<RED>Exception message is " + exc.Message + "\n"); return; } XmlElement root = doc.DocumentElement; XmlNodeList nodeallroutes = root.SelectNodes("/gpx/rte"); int routecount = nodeallroutes.Count; textoutput(routecount.ToString() + " routes found"); int rte_ctr = 0; foreach (XmlNode noderoute in nodeallroutes) { gpx_rtepoints = new List <Gpx_routepoint>(); XmlNodeList routenodenames = noderoute.SelectNodes("name"); foreach (XmlNode nodename in routenodenames) { routename = nodename.InnerText; } XmlNodeList nodepoints = noderoute.SelectNodes("rtept"); if ((checkBoxReverseRoutes.Checked == true) && (rte_ctr < routecount)) {//reverse routes textoutput("Reversing route " + routename); XmlNode clone = noderoute.Clone(); XmlNodeList revroutenodenames = noderoute.SelectNodes("name"); foreach (XmlNode nodename in revroutenodenames) { nodename.InnerText = routename; } XmlNodeList clone_nodepoints = clone.SelectNodes("rtept"); foreach (XmlNode nodename in clone_nodepoints) { clone.RemoveChild(nodename); //textoutput("removing rte node"); } for (int i = nodepoints.Count - 1; i >= 0; i--) { clone.AppendChild(nodepoints[i]); //textoutput("adding reversed node "+i.ToString()); } noderoute.RemoveAll(); XmlNode nodegpxroutes = root.SelectSingleNode("/gpx"); nodegpxroutes.AppendChild(clone); nodepoints = clone.SelectNodes("rtept"); //textoutput("End Reversing route " + routename); }//end reverse routes rte_ctr++; int ctr = 0; foreach (XmlNode nodepoint in nodepoints) { string longitude = nodepoint.Attributes["lon"].Value; string latitude = nodepoint.Attributes["lat"].Value; XmlNodeList nodenames = nodepoint.SelectNodes("name"); foreach (XmlNode nodename in nodenames) { name = nodename.InnerText; int wptctr = ctr + 1; if (checkBoxRenumber.Checked == true) { if (checkBoxNewVoice.Checked == true) { if (checkBoxReverseRoutes.Checked == true) { nodename.InnerText = REVERSE_NAME_PREFIX + "WPT" + wptctr.ToString("D4") + REVERSE_NAME_POSTFIX; } else { nodename.InnerText = NAME_PREFIX + "WPT" + wptctr.ToString("D4") + NAME_POSTFIX; } } else { nodename.InnerText = "WPT" + wptctr.ToString("D4"); } } } XmlNodeList nodecmts = nodepoint.SelectNodes("cmt"); string cmt = ""; foreach (XmlNode nodecmt in nodecmts) { cmt = nodecmt.InnerText; //textoutput("cmt =" + cmt); } /* * if (DEBUG) * { * textoutput(ctr.ToString()); * textoutput("name=" + name); * textoutput("cmt=" + cmt); * textoutput("lon=" + longitude); * textoutput("lat=" + latitude); * textoutput(""); * * }*/ Gpx_routepoint myroutepoint = new Gpx_routepoint(); myroutepoint.Name = name; myroutepoint.Description = cmt; myroutepoint.angle1 = -1.0; myroutepoint.angle2 = -1.0; myroutepoint.angle1to2 = -1.0; myroutepoint.voice = "none"; myroutepoint.lat = Convert.ToDouble(latitude, System.Globalization.NumberFormatInfo.InvariantInfo); myroutepoint.lon = Convert.ToDouble(longitude, System.Globalization.NumberFormatInfo.InvariantInfo); gpx_rtepoints.Add(myroutepoint); /*if (DEBUG) * { * textoutput("lon=" + myroutepoint.lon.ToString()); * textoutput("lat=" + myroutepoint.lat.ToString()); * textoutput(""); * }*/ ctr++; } //process route for nav commands if (checkBoxNewVoice.Checked == true) { calculatevoicenavigation(routename); } //update xml information ctr = 0; foreach (XmlNode nodepoint in nodepoints) { XmlNodeList nodecmts = nodepoint.SelectNodes("cmt"); foreach (XmlNode nodecmt in nodecmts) { nodepoint.RemoveChild(nodecmt); } //create new node XmlNode newnodecmt = doc.CreateElement("cmt"); XmlCDataSection cdata = doc.CreateCDataSection(gpx_rtepoints[ctr].Description); //textoutput("update cmt=" + gpx_rtepoints[ctr].Description); newnodecmt.AppendChild(cdata); nodepoint.AppendChild(newnodecmt); ctr++; } //end single route } textoutput("Writing GPX file"); try { doc.Save(tmpfile); } catch (Exception exc) { textoutput("<RED>Failed to load xmlDoc.Load(fs) for writing"); textoutput("<RED>Exception message is " + exc.Message + "\n"); return; } string outfilestring = File.ReadAllText(tmpfile); outfilestring = outfilestring.Replace(" <rte>\r\n </rte>\r\n", ""); outfilestring = outfilestring.Replace("<extensions><![CDATA[", "<extensions>"); outfilestring = outfilestring.Replace("]]></extensions>", "</extensions>"); outfilestring = outfilestring.Replace("<gpx>", ""); File.WriteAllText(textBoxOutputGPX.Text, startfile + outfilestring); File.Delete(tmpfile); textoutput("Completed\n\n"); } catch (Exception exc) { textoutput("<RED>GPXProcessingThread Error Exception:" + exc.Message + "\n"); } }