public void Write(String gpx_file, Form1.WayPointInfo WayPoints, CheckBox checkGpxRte, CheckBox checkGpxSpeedMs, CheckBox checkGPXtrkseg, float[] PlotLat, float[] PlotLong, int PlotCount, short[] PlotS, int[] PlotT, short[] PlotZ, short[] PlotH, Form1.TrackSummary ts, NumericUpDown numericGpxTimeShift //string dist_unit, string speed_unit, string alt_unit, string exstop_info, // string dist, string speed_cur, string speed_avg, string speed_max, string run_time_label, string last_sample_time, string altitude, string battery ) { FileStream fs = null; StreamWriter wr = null; try { fs = new FileStream(gpx_file, FileMode.Create); wr = new StreamWriter(fs); // write GPX header wr.WriteLine("<?xml version=\"1.0\"?>"); wr.WriteLine("<gpx version=\"1.1\""); wr.WriteLine(" creator=\"GPSCycleComputer\""); wr.WriteLine(" xsi:schemaLocation=\"http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd http://www.garmin.com/xmlschemas/TrackPointExtension/v1 http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd\""); wr.WriteLine(" xmlns=\"http://www.topografix.com/GPX/1/1\""); wr.WriteLine(" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""); wr.WriteLine(" xmlns:gpxtpx=\"http://www.garmin.com/xmlschemas/TrackPointExtension/v1\">"); /* * wr.WriteLine("<?xml version=\"1.0\"?>"); * wr.WriteLine("<gpx"); * wr.WriteLine("version=\"1.0\""); * wr.WriteLine(" creator=\"GPSCycleComputer\""); * wr.WriteLine(" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""); * wr.WriteLine(" xmlns=\"http://www.topografix.com/GPX/1/0\""); * wr.WriteLine(" xsi:schemaLocation=\"http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd\">"); * wr.WriteLine(); */ if (WayPoints.Count > 0) { for (int wpc = 0; wpc < WayPoints.Count; wpc++) { // need to replace chars not supported by XML string wp_name = WayPoints.name[wpc].Replace("&", "&").Replace("<", "<").Replace(">", ">").Replace("\"", """).Replace("'", "'"); string wp_link = null; int link_idx = wp_name.IndexOf('\x02'); if (link_idx != -1) //audio file is present { wp_link = wp_name.Substring(link_idx + 1); if (link_idx == 0) { wp_name = wp_name.Remove(0, 1); } else { wp_name = wp_name.Remove(link_idx, wp_name.Length - link_idx); } } // GPS Track Analyser expects a newline between wpt and name wr.WriteLine("<wpt lat=\"" + WayPoints.lat[wpc].ToString("0.##########", IC) + "\" lon=\"" + WayPoints.lon[wpc].ToString("0.##########", IC) + "\" >"); wr.WriteLine("<name>" + wp_name + "</name>"); if (link_idx != -1) { wr.WriteLine("<link href=\"" + wp_link + "\" />"); } wr.WriteLine("</wpt>"); } } wr.WriteLine(checkGpxRte.Checked ? "<rte>" : "<trk>"); wr.WriteLine("<name>" + ts.name + "</name>"); // GPS Track Analyser expects a newline between desc and Data wr.WriteLine("<desc>"); wr.WriteLine("<![CDATA[" + ts.desc + "]]>"); wr.WriteLine("</desc>"); if (checkGpxRte.Checked == false) { wr.WriteLine("<trkseg>"); } int lastTime = 0; // here write coordinates for (int i = 0; i < PlotCount; i++) { if (checkGpxRte.Checked) { wr.WriteLine("<rtept lat=\"" + PlotLat[i].ToString("0.##########", IC) + "\" lon=\"" + PlotLong[i].ToString("0.##########", IC) + "\">"); } else { if (checkGPXtrkseg.Checked && PlotT[i] > lastTime + 10) //if gap > 10s, separate track in different segments { wr.WriteLine("</trkseg>"); wr.WriteLine("<trkseg>"); } lastTime = PlotT[i]; wr.WriteLine("<trkpt lat=\"" + PlotLat[i].ToString("0.##########", IC) + "\" lon=\"" + PlotLong[i].ToString("0.##########", IC) + "\">"); } if (PlotZ[i] != Int16.MinValue) //ignore invalid value { wr.WriteLine("<ele>" + PlotZ[i] + "</ele>"); } TimeSpan run_time = new TimeSpan(Decimal.ToInt32(numericGpxTimeShift.Value), 0, PlotT[i]); string utcTime_str = (ts.StartTime.ToUniversalTime() + run_time).ToString("s") + "Z"; wr.WriteLine("<time>" + utcTime_str + "</time>"); /*if (checkSaveSpeed.Checked && PlotS != null && PlotS[i] != Int16.MinValue) //ignore invalid value //speed is invalid in gpx v1.1 * { * double speed = PlotS[i] * 0.1; * if (checkGpxSpeedMs.Checked) // speed in m/s instead of km/h * speed /= 3.6; * wr.WriteLine("<speed>" + speed.ToString("0.##########", IC) + "</speed>"); * * }*/ if (PlotH != null && PlotH[i] != 0) //heart rate { wr.WriteLine("<extensions><gpxtpx:TrackPointExtension><gpxtpx:hr>{0}</gpxtpx:hr></gpxtpx:TrackPointExtension></extensions>", PlotH[i]); } wr.WriteLine(checkGpxRte.Checked ? "</rtept>" : "</trkpt>"); } // write end of the GPX file if (checkGpxRte.Checked == false) { wr.WriteLine("</trkseg>"); } if (checkGpxRte.Checked) { wr.WriteLine("</rte>"); } else { wr.WriteLine("</trk>"); } wr.WriteLine("</gpx>"); } catch (Exception ee) { Utils.log.Error(" buttonSaveGPX_Click ", ee); } finally { if (wr != null) { wr.Close(); } if (fs != null) { fs.Close(); } } }
public bool Load(string filename, ref Form1.WayPointInfo WayPoints, int vector_size, ref float[] dataLat, ref float[] dataLong, ref int[] dataT, out int data_size) { int Counter = 0; bool Status = false; DateTime StartTime = DateTime.Now; data_size = 0; WayPoints.WayPointCount = 0; Cursor.Current = Cursors.WaitCursor; try { XmlReaderSettings settings = new XmlReaderSettings(); settings.IgnoreWhitespace = true; XmlReader reader = XmlReader.Create(filename, settings); reader.MoveToContent(); while (reader.Read()) { //Console.WriteLine("<<" + reader.NodeType + " " + reader.Name + " " + reader.Value); if (reader.NodeType == XmlNodeType.Element) { switch (reader.Name) { case "trkpt": if (Counter >= vector_size) // check if we need to decimate arrays { for (int i = 0; i < vector_size / 2; i++) { dataLat[i] = dataLat[i * 2]; dataLong[i] = dataLong[i * 2]; dataT[i] = dataT[i * 2]; } Counter = vector_size / 2; } dataLat[Counter] = (float)Convert.ToDouble(reader.GetAttribute("lat"), IC); dataLong[Counter] = (float)Convert.ToDouble(reader.GetAttribute("lon"), IC); //Console.WriteLine("KB:lat" + reader.GetAttribute("lat") + "lon" + reader.GetAttribute("lon")); while (reader.Read()) //read subtree { //Console.WriteLine("<<<<" + reader.NodeType + " " + reader.Name); if (reader.NodeType == XmlNodeType.Element) { switch (reader.Name) { case "ele": reader.Read(); Console.WriteLine("elevation=" + reader.Value); //Console.WriteLine("elvation=" + reader.ReadString()); break; case "time": reader.Read(); Console.WriteLine("time=" + Convert.ToDateTime(reader.Value, IC)); //reader.ReadEndElement(); break; case "speed": reader.Read(); Console.WriteLine("speed=" + Convert.ToDouble(reader.Value, IC).ToString()); //reader.ReadEndElement(); break; } //reader.Read(); //if (reader.NodeType != XmlNodeType.EndElement) // Console.WriteLine("fehlendes EndTag"); } else if (reader.Name == "trkpt" && reader.NodeType == XmlNodeType.EndElement) { Counter++; break; } } break; case "wpt": WayPoints.lat[WayPoints.WayPointCount] = (float)Convert.ToDouble(reader.GetAttribute("lat"), IC); WayPoints.lon[WayPoints.WayPointCount] = (float)Convert.ToDouble(reader.GetAttribute("lon"), IC); while (reader.Read()) //read subtree { if (reader.NodeType == XmlNodeType.Element) { switch (reader.Name) { case "name": reader.Read(); WayPoints.name[WayPoints.WayPointCount] = reader.Value; break; } } else if (reader.Name == "wpt" && reader.NodeType == XmlNodeType.EndElement) { WayPoints.WayPointCount++; break; } } break; } } } reader.Close(); data_size = Counter; Status = true; } catch (Exception e) { Utils.log.Error(" LoadGpx ", e); } Cursor.Current = Cursors.Default; return(Status); }
public bool Load(string filename, ref Form1.WayPointInfo WayPoints, int vector_size, ref float[] dataLat, ref float[] dataLong, ref Int16[] dataZ, ref Int32[] dataT, ref Int32[] dataD, ref Form1.TrackSummary ts, ref int data_size, bool append) { bool Status = false; TimeSpan tspan; UtmUtil utmUtil = new UtmUtil(); double OldLat = 0.0, OldLong = 0.0; int DecimateCount = 0, Decimation = 1; Int16 ReferenceAlt; if (!append) { data_size = 0; WayPoints.Count = 0; ts.Clear(); } if (data_size == 0) { ReferenceAlt = Int16.MaxValue; } else { ReferenceAlt = dataZ[data_size - 1]; } Cursor.Current = Cursors.WaitCursor; try { //XmlParserContext xmlpc = new XmlParserContext(null, null, "", XmlSpace.Default, System.Text.Encoding.UTF8); does not work XmlReaderSettings settings = new XmlReaderSettings(); settings.IgnoreWhitespace = true; StreamReader sr = new StreamReader(filename, System.Text.Encoding.UTF8); //use StreamReader to overwrite encoding ISO-8859-1, which is not supported by .NETCF (no speed drawback) XmlReader reader = XmlReader.Create(sr, settings); ts.filename = filename; //reader.MoveToContent(); reader.ReadToFollowing("gpx"); reader.Read(); while (reader.NodeType == XmlNodeType.Element) { bool isTrack; if ((isTrack = reader.Name == "trk") || reader.Name == "rte") { if (isTrack) { reader.Read(); } while (reader.NodeType == XmlNodeType.Element) { if (reader.Name == "trkseg" || reader.Name == "rte") { reader.Read(); while (reader.NodeType == XmlNodeType.Element) { if (reader.Name == "trkpt" || reader.Name == "rtept") { trkpt: bool jumptrkpt = false; if (data_size >= vector_size) // check if we need to decimate arrays { for (int i = 0; i < vector_size / 2; i++) { dataLat[i] = dataLat[i * 2]; dataLong[i] = dataLong[i * 2]; dataZ[i] = dataZ[i * 2]; dataT[i] = dataT[i * 2]; dataD[i] = dataD[i * 2]; } data_size = vector_size / 2; Decimation *= 2; } double lat = Convert.ToDouble(reader.GetAttribute("lat"), IC); double lon = Convert.ToDouble(reader.GetAttribute("lon"), IC); if (!utmUtil.referenceSet) { utmUtil.setReferencePoint(lat, lon); OldLat = lat; OldLong = lon; } double deltax = (lon - OldLong) * utmUtil.longit2meter; double deltay = (lat - OldLat) * utmUtil.lat2meter; ts.Distance += Math.Sqrt(deltax * deltax + deltay * deltay); OldLong = lon; OldLat = lat; tspan = TimeSpan.Zero; //clear data in case there is no <time> field Int16 z_int = Int16.MinValue; //preset invalid Alt in case there is no <ele> field reader.Read(); while (reader.NodeType == XmlNodeType.Element) //read subtree { switch (reader.Name) { case "ele": z_int = (Int16)reader.ReadElementContentAsDouble(); // compute elevation gain //if (ts.AltitudeStart == Int16.MinValue) // ts.AltitudeStart = z_int; if (z_int > ReferenceAlt) { ts.AltitudeGain += z_int - ReferenceAlt; ReferenceAlt = z_int; } else if (z_int < ReferenceAlt - (short)Form1.AltThreshold) { ReferenceAlt = z_int; } if (z_int > (short)ts.AltitudeMax) { ts.AltitudeMax = z_int; } if (z_int < (short)ts.AltitudeMin) { ts.AltitudeMin = z_int; } break; case "time": if (ts.StartTime == DateTime.MinValue) { ts.StartTime = reader.ReadElementContentAsDateTime(); } else { tspan = reader.ReadElementContentAsDateTime() - ts.StartTime; } break; case "speed": reader.Skip(); break; case "trkpt": //trkpt without EndElement <trkpt lat="47.2615199999997" lon="10.2016400000003"/> case "rtept": jumptrkpt = true; goto savepoint; default: reader.Skip(); break; } } reader.ReadEndElement(); //trkpt savepoint: if (DecimateCount == 0) //when decimating, add only first sample, ignore rest of decimation { dataLat[data_size] = (float)lat; dataLong[data_size] = (float)lon; dataZ[data_size] = z_int; dataT[data_size] = (int)tspan.TotalSeconds; dataD[data_size] = (int)ts.Distance; data_size++; } DecimateCount++; if (DecimateCount >= Decimation) { DecimateCount = 0; } if (jumptrkpt) { goto trkpt; } } else { reader.Skip(); } } if (isTrack) { reader.ReadEndElement(); //trkseg } } else if (reader.Name == "name") { ts.name = reader.ReadElementString(); } else if (reader.Name == "desc") { ts.desc = reader.ReadElementString(); } else { reader.Skip(); } } reader.ReadEndElement(); //trk } else if (reader.Name == "wpt") { float lat = (float)Convert.ToDouble(reader.GetAttribute("lat"), IC); float lon = (float)Convert.ToDouble(reader.GetAttribute("lon"), IC); string name = ""; string desc = ""; reader.Read(); while (reader.NodeType == XmlNodeType.Element) { if (reader.Name == "name") { name = reader.ReadElementString(); } else if (reader.Name == "desc") { desc = reader.ReadElementString(); //prepared for later use } else { reader.Skip(); } } if (WayPoints.Count < WayPoints.DataSize) { WayPoints.lat[WayPoints.Count] = lat; WayPoints.lon[WayPoints.Count] = lon; WayPoints.name[WayPoints.Count] = name; WayPoints.Count++; } reader.ReadEndElement(); } else { reader.Skip(); } } //reader.ReadEndElement(); reader.Close(); Status = true; } catch (Exception e) { Utils.log.Error(" LoadGpx ", e); } Cursor.Current = Cursors.Default; return(Status); }
public bool Load(string filename, ref Form1.WayPointInfo WayPoints, int vector_size, ref float[] dataLat, ref float[] dataLong, ref int[] dataT, out int data_size) { int Counter = 0; bool Status = false; DateTime StartTime = DateTime.Now; data_size = 0; WayPoints.WayPointCount = 0; // set "." as decimal separator for reading the map info NumberFormatInfo number_info = new NumberFormatInfo(); number_info.NumberDecimalSeparator = "."; Cursor.Current = Cursors.WaitCursor; try { FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read); StreamReader sr = new StreamReader(fs); string line = ""; double last_lat = 0.0; double last_long = 0.0; string name = ""; while ((line = sr.ReadLine()) != null) //null: EndOfStream { line = line.Trim(); // skip blank lines if (line == "") { continue; } if (line.IndexOf("<trkpt ") >= 0) { last_lat = 0.0; last_long = 0.0; } // line contains coordinates - extract if ((line.IndexOf("lat=\"") >= 0) || (line.IndexOf("lon=\"") >= 0)) { string[] words = line.Split(new Char[] { ' ' }); for (int i = 0; i < words.Length; i++) { if (words[i].IndexOf("lon=\"") >= 0) { words[i] = words[i].Replace("></trkpt>", ""); words[i] = words[i].Replace("/>", ""); words[i] = words[i].Replace("lon=\"", ""); words[i] = words[i].Replace("\"", ""); words[i] = words[i].Replace(">", ""); last_long = Convert.ToDouble(words[i].Trim(), number_info); } else if (words[i].IndexOf("lat=\"") >= 0) { words[i] = words[i].Replace("></trkpt>", ""); words[i] = words[i].Replace("/>", ""); words[i] = words[i].Replace("lat=\"", ""); words[i] = words[i].Replace("\"", ""); words[i] = words[i].Replace(">", ""); last_lat = Convert.ToDouble(words[i].Trim(), number_info); } } } // fix if file contains coordinates only (i.e. no time info), but on a separate lines /* else if ((line.IndexOf("</trkpt>") == 0) && (last_lat != 0.0) && (last_long != 0.0)) * { * // check if we need to decimate arrays * if (Counter >= vector_size) * { * for (int i = 0; i < vector_size / 2; i++) * { * dataLat[i] = dataLat[i * 2]; * dataLong[i] = dataLong[i * 2]; * dataT[i] = dataT[i * 2]; * } * Counter /= 2; * } * * dataLat[Counter] = (float)last_lat; * dataLong[Counter] = (float)last_long; * dataT[Counter] = 0; // no time in this file * * Counter++; * * last_lat = 0.0; last_long = 0.0; * }*/ // time follows lat/long, so make sure these are loaded if ((line.IndexOf("<time>") == 0) && (line.IndexOf("</time>") >= 0) && (line.Length == 33) && (last_lat != 0.0) && (last_long != 0.0)) { line = line.Replace("<time>", ""); line = line.Replace("</time>", ""); line = line.Trim(); // read time DateTime tm = new DateTime(Convert.ToInt16(line.Substring(0, 4)), Convert.ToInt16(line.Substring(5, 2)), Convert.ToInt16(line.Substring(8, 2)), Convert.ToInt16(line.Substring(11, 2)), Convert.ToInt16(line.Substring(14, 2)), Convert.ToInt16(line.Substring(17, 2))); if (Counter == 0) // the first point loaded { StartTime = tm; } TimeSpan run_time = tm - StartTime; double double_total_sec = run_time.TotalSeconds; if (double_total_sec < 0.0) { double_total_sec = 0.0; } dataT[Counter] = (Int32)double_total_sec; } // fix if file contains coordinates only (i.e. no time info), all on one line if (((line.IndexOf("</trkpt>") >= 0) || (line.IndexOf("/>") >= 0)) && (last_lat != 0.0) && (last_long != 0.0)) { // check if we need to decimate arrays if (Counter >= vector_size) { for (int i = 0; i < vector_size / 2; i++) { dataLat[i] = dataLat[i * 2]; dataLong[i] = dataLong[i * 2]; dataT[i] = dataT[i * 2]; } Counter /= 2; } dataLat[Counter] = (float)last_lat; dataLong[Counter] = (float)last_long; dataT[Counter] = 0; // no time in this file Counter++; last_lat = 0.0; last_long = 0.0; } // read names of Waypoints else if ((line.IndexOf("<name>") >= 0)) { name = line.Replace("<name>", ""); name = name.Replace("</name>", ""); } // read names of Waypoints else if ((line.IndexOf("</wpt>") >= 0) && (last_lat != 0.0) && (last_long != 0.0) && (name.Length > 0)) { if (WayPoints.WayPointCount < WayPoints.WayPointDataSize - 1) { WayPoints.lat[WayPoints.WayPointCount] = (float)last_lat; WayPoints.lon[WayPoints.WayPointCount] = (float)last_long; WayPoints.name[WayPoints.WayPointCount] = name; WayPoints.WayPointCount++; name = ""; } last_lat = 0.0; last_long = 0.0; } } sr.Close(); fs.Close(); data_size = Counter; Status = true; } catch (Exception e) { Utils.log.Error(" LoadGpx ", e); } Cursor.Current = Cursors.Default; return(Status); }
public void Write(String kml_file, Form1.WayPointInfo WayPoints, float[] PlotLat, float[] PlotLong, int PlotCount, short[] PlotS, int[] PlotT, short[] PlotZ, Form1.TrackSummary ts, ComboBox comboBoxKmlOptColor, CheckBox checkKmlAlt, ComboBox comboBoxKmlOptWidth ) { FileStream fs = null; StreamWriter wr = null; try { fs = new FileStream(kml_file, FileMode.Create); wr = new StreamWriter(fs); // write KML header wr.WriteLine("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); wr.WriteLine("<kml xmlns=\"http://www.opengis.net/kml/2.2\">"); wr.WriteLine(" <Document>"); wr.WriteLine(" <name><![CDATA[" + ts.name + "]]></name>"); // Write the waypoints if (WayPoints.Count > 0) { wr.WriteLine(" <Folder> <name>Waypoints</name>"); for (int wpc = 0; wpc < WayPoints.Count; wpc++) { // need to replave chars not supported by XML string wpName = WayPoints.name[wpc].Replace("&", "&").Replace("<", "<").Replace(">", ">").Replace("\"", """).Replace("'", "'"); wr.WriteLine(" <Placemark><name>" + wpName + "</name><Point><altitudeMode>clampToGround</altitudeMode><coordinates>" + WayPoints.lon[wpc].ToString("0.##########", IC) + "," + WayPoints.lat[wpc].ToString("0.##########", IC) + ",0.000000</coordinates></Point></Placemark>"); } wr.WriteLine(" </Folder>"); } if (PlotCount > 0) { wr.WriteLine(" <Folder> <name>Tracks</name>"); wr.WriteLine(" <Placemark>"); wr.WriteLine(" <name>" + ts.name + "</name>"); wr.WriteLine(" <Style id=\"yellowLineGreenPoly\">"); wr.WriteLine(" <LineStyle>"); // Colors : blue - red - green- yellow- white - black if (comboBoxKmlOptColor.SelectedIndex == 0) { wr.WriteLine(" <color>ffff0000</color>"); } else if (comboBoxKmlOptColor.SelectedIndex == 1) { wr.WriteLine(" <color>ff0000ff</color>"); } else if (comboBoxKmlOptColor.SelectedIndex == 2) { wr.WriteLine(" <color>ff00ff00</color>"); } else if (comboBoxKmlOptColor.SelectedIndex == 3) { wr.WriteLine(" <color>ff00ffff</color>"); } else if (comboBoxKmlOptColor.SelectedIndex == 4) { wr.WriteLine(" <color>ffffffff</color>"); } else if (comboBoxKmlOptColor.SelectedIndex == 5) { wr.WriteLine(" <color>ff000000</color>"); } else if (comboBoxKmlOptColor.SelectedIndex == 6) { wr.WriteLine(" <color>ffc0c0c0</color>"); } else if (comboBoxKmlOptColor.SelectedIndex == 7) { wr.WriteLine(" <color>ff0080ff</color>"); } else if (comboBoxKmlOptColor.SelectedIndex == 8) { wr.WriteLine(" <color>ffff8000</color>"); } else if (comboBoxKmlOptColor.SelectedIndex == 9) { wr.WriteLine(" <color>ff000080</color>"); } else if (comboBoxKmlOptColor.SelectedIndex == 10) { wr.WriteLine(" <color>ff800080</color>"); } else if (comboBoxKmlOptColor.SelectedIndex == 11) { wr.WriteLine(" <color>ffff0080</color>"); } wr.WriteLine(" <width>" + ((comboBoxKmlOptWidth.SelectedIndex + 1) * 2) + "</width>"); wr.WriteLine(" </LineStyle>"); wr.WriteLine(" <PolyStyle>"); wr.WriteLine(" <color>7f00ff00</color>"); wr.WriteLine(" </PolyStyle>"); wr.WriteLine(" </Style>"); // write description for this trip wr.WriteLine(" <description>" + ts.desc + "</description>"); wr.WriteLine(" <styleUrl>#yellowLineGreenPoly</styleUrl>"); wr.WriteLine(" <LookAt>"); wr.WriteLine(" <longitude>"+ PlotLong[0].ToString("0.##########", IC) + "</longitude>"); wr.WriteLine(" <latitude>"+ PlotLat[0].ToString("0.##########", IC) + "</latitude>"); wr.WriteLine(" <altitude>0</altitude>"); wr.WriteLine(" <range>3000</range>"); wr.WriteLine(" <tilt>0</tilt>"); wr.WriteLine(" <heading>0</heading>"); wr.WriteLine(" </LookAt>"); wr.WriteLine(" <LineString>"); if (checkKmlAlt.Checked) { wr.WriteLine(" <altitudeMode>absolute</altitudeMode>"); } wr.WriteLine(" <coordinates>"); // here write coordinates String str; for (int i = 0; i < PlotCount; i++) { str = PlotLong[i].ToString("0.##########", IC) + "," + PlotLat[i].ToString("0.##########", IC); if (checkKmlAlt.Checked && PlotZ[i] != Int16.MinValue) //ignore invalid value { str += "," + PlotZ[i]; } wr.WriteLine(str); } wr.WriteLine(" </coordinates>"); wr.WriteLine(" </LineString>"); wr.WriteLine(" </Placemark>"); wr.WriteLine(" </Folder>"); } // write end of the KML file wr.WriteLine(" </Document>"); wr.WriteLine("</kml>"); } catch (Exception ee) { Utils.log.Error(" buttonSaveKML_Click", ee); } finally { if (wr != null) { wr.Close(); } if (fs != null) { fs.Close(); } } }
public bool Load(string filename, ref Form1.WayPointInfo WayPoints, int vector_size, ref float[] dataLat, ref float[] dataLong, ref int[] dataT, out int data_size) { int Counter = 0; bool Status = false; data_size = 0; WayPoints.WayPointCount = 0; // No Waypoint Support for KML files yet. // set "." as decimal separator for reading the map info NumberFormatInfo number_info = new NumberFormatInfo(); number_info.NumberDecimalSeparator = "."; Cursor.Current = Cursors.WaitCursor; try { FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read); StreamReader sr = new StreamReader(fs); string line = ""; bool data_load_activated = false; bool coordinates_found = false; while (true) { // read a single word (if we detected the required section), or a whole line if (data_load_activated) { line = LoadWord(ref sr); if (line == null) { break; //EndOfStream } // skip blank lines. Look for "<coordinates>" to trigger load values. if (line == "") { continue; } if (line.IndexOf("<coordinates>") >= 0) { coordinates_found = true; } if (line.IndexOf("</coordinates>") >= 0) { data_load_activated = false; } line = line.Replace("<coordinates>", ""); line = line.Replace("</coordinates>", ""); line = line.Trim(); // OK, read the values (lat and long) if (coordinates_found && (line != "")) { string[] words = line.Split(new Char[] { ',' }); if (words.Length >= 2) { double tmp_long = Convert.ToDouble(words[0].Trim(), number_info); double tmp_lat = Convert.ToDouble(words[1].Trim(), number_info); // check if we need to decimate arrays if (Counter >= vector_size) { for (int i = 0; i < vector_size / 2; i++) { dataLat[i] = dataLat[i * 2]; dataLong[i] = dataLong[i * 2]; } Counter /= 2; } dataLat[Counter] = (float)tmp_lat; dataLong[Counter] = (float)tmp_long; dataT[Counter] = 0; // note time is not availble in KML Counter++; } } } else { line = sr.ReadLine(); if (line == null) { break; //EndOfStream } line = line.Trim(); if (line == "</kml>") { break; } else if ((line.StartsWith("<LineString>")) || (line.StartsWith("<Point>"))) { data_load_activated = true; coordinates_found = false; continue; } else if ((line.StartsWith("</LineString>")) || (line.StartsWith("</Point>"))) { data_load_activated = false; coordinates_found = false; continue; } } } sr.Close(); fs.Close(); data_size = Counter; Status = true; } catch (Exception e) { Utils.log.Error(" LoadKml ", e); } Cursor.Current = Cursors.Default; return(Status); }
public bool Load(string filename, ref Form1.WayPointInfo WayPoints, int vector_size, ref float[] dataLat, ref float[] dataLong, ref Int16[] dataZ, ref Int32[] dataT, ref Int32[] dataD, ref Form1.TrackSummary ts, ref int data_size, bool append) { bool Status = false; int DecimateCount = 0, Decimation = 1; double OldLat = 0.0, OldLong = 0.0; UtmUtil utmUtil = new UtmUtil(); Int16 ReferenceAlt; if (!append) { data_size = 0; WayPoints.Count = 0; ts.Clear(); } if (data_size == 0) { ReferenceAlt = Int16.MaxValue; } else { ReferenceAlt = dataZ[data_size - 1]; } Cursor.Current = Cursors.WaitCursor; try { XmlReaderSettings settings = new XmlReaderSettings(); settings.IgnoreWhitespace = true; StreamReader sr = new StreamReader(filename, System.Text.Encoding.UTF8); //use StreamReader to overwrite encoding ISO-8859-1, which is not supported by .NETCF (no speed drawback) XmlReader reader = XmlReader.Create(sr, settings); ts.filename = filename; //reader.MoveToContent(); //reader.Read(); while (reader.ReadToFollowing("Placemark")) { while (reader.NodeType == XmlNodeType.Element) { if (reader.Name == "Placemark") { bool addLevel = false; string tmpname = ""; string tmpdescription = ""; bool waypoint = false; double lon = 0; double lat = 0; reader.Read(); while (reader.NodeType == XmlNodeType.Element) { if (reader.Name == "name") { tmpname = reader.ReadElementString(); } else if (reader.Name == "description") { tmpdescription = reader.ReadElementString(); } else if (reader.Name == "MultiGeometry") { reader.Read(); addLevel = true; } else if (reader.Name == "LineString") { ts.name = tmpname; ts.desc = tmpdescription; reader.Read(); while (reader.NodeType == XmlNodeType.Element) { if (reader.Name == "coordinates") { reader.ReadStartElement(); while (true) { string line = ""; char[] buf = new char[1]; int count = 1; for (int i = 0; i < 100; i++) //read one line { count = reader.ReadValueChunk(buf, 0, 1); if (buf[0] == '\n' || buf[0] == ' ' || buf[0] == '\r' || buf[0] == '\t' || count == 0) { break; } line += buf[0]; } if (data_size >= vector_size) // check if we need to decimate arrays { for (int i = 0; i < vector_size / 2; i++) { dataLat[i] = dataLat[i * 2]; dataLong[i] = dataLong[i * 2]; dataZ[i] = dataZ[i * 2]; dataT[i] = dataT[i * 2]; dataD[i] = dataD[i * 2]; } data_size = vector_size / 2; Decimation *= 2; } string[] numbers = line.Split(','); if (numbers.Length >= 2) //read data { lon = Convert.ToDouble(numbers[0], IC); lat = Convert.ToDouble(numbers[1], IC); if (!utmUtil.referenceSet) { utmUtil.setReferencePoint(lat, lon); OldLat = lat; OldLong = lon; } double deltax = (lon - OldLong) * utmUtil.longit2meter; double deltay = (lat - OldLat) * utmUtil.lat2meter; ts.Distance += Math.Sqrt(deltax * deltax + deltay * deltay); OldLong = lon; OldLat = lat; Int16 z_int = Int16.MinValue; //preset invalid Alt in case there is no <ele> field if (numbers.Length >= 3) { z_int = (Int16)Convert.ToDouble(numbers[2], IC); //altitude // compute elevation gain //if (ts.AltitudeStart == Int16.MinValue) // ts.AltitudeStart = z_int; if (z_int > ReferenceAlt) { ts.AltitudeGain += z_int - ReferenceAlt; ReferenceAlt = z_int; } else if (z_int < ReferenceAlt - (short)Form1.AltThreshold) { ReferenceAlt = z_int; } if (z_int > (short)ts.AltitudeMax) { ts.AltitudeMax = z_int; } if (z_int < (short)ts.AltitudeMin) { ts.AltitudeMin = z_int; } } if (DecimateCount == 0) //when decimating, add only first sample, ignore rest of decimation { dataLat[data_size] = (float)lat; dataLong[data_size] = (float)lon; dataZ[data_size] = z_int; if (data_size == 0) { dataT[data_size] = 0; } else { dataT[data_size] = dataT[data_size - 1] + Decimation; //every point 1 sec apart } dataD[data_size] = (int)ts.Distance; data_size++; } DecimateCount++; if (DecimateCount >= Decimation) { DecimateCount = 0; } } if (count == 0) { break; } } reader.Skip(); //advances reader to the next (End) Element reader.ReadEndElement(); //coordinates } else { reader.Skip(); } } reader.ReadEndElement(); //LineString } else if (reader.Name == "Point") { reader.Read(); while (reader.NodeType == XmlNodeType.Element) { if (reader.Name == "coordinates") { string line = reader.ReadElementString(); string[] numbers = line.Split(','); if (numbers.Length >= 2) { lon = Convert.ToDouble(numbers[0], IC); lat = Convert.ToDouble(numbers[1], IC); waypoint = true; } } else { reader.Skip(); } } reader.ReadEndElement(); //Point } else { reader.Skip(); } } if (addLevel) { reader.ReadEndElement(); } if (waypoint && WayPoints.Count < WayPoints.DataSize) { WayPoints.name[WayPoints.Count] = tmpname; WayPoints.lon[WayPoints.Count] = (float)lon; WayPoints.lat[WayPoints.Count] = (float)lat; WayPoints.Count++; } reader.ReadEndElement(); // Placemark } else { reader.Skip(); } } } reader.Close(); sr.Close(); Status = true; } catch (Exception e) { Utils.log.Error(" LoadKml ", e); } Cursor.Current = Cursors.Default; return(Status); }
{ //load as T2F (WayPoints) public bool Load(string filename, ref Form1.WayPointInfo WayPoints, int vector_size, ref float[] dataLat, ref float[] dataLong, ref Int16[] dataZ, ref Int32[] dataT, ref Int32[] dataD, ref Form1.TrackSummary ts, ref int data_size, bool append) { int DecimateCount = 0, Decimation = 1; double OriginShiftX = 0.0; double OriginShiftY = 0.0; bool Status = false; UtmUtil utmUtil = new UtmUtil(); Int16 ReferenceAlt; if (!append) { data_size = 0; WayPoints.Count = 0; ts.Clear(); } if (data_size == 0) { ReferenceAlt = Int16.MaxValue; } else { ReferenceAlt = dataZ[data_size - 1]; } Cursor.Current = Cursors.WaitCursor; do { try { ts.filename = filename; FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read); BinaryReader rd = new BinaryReader(fs, System.Text.Encoding.Unicode); // load header "GCC1" (1 is version in binary) if (rd.ReadByte() != 'G') { break; } if (rd.ReadByte() != 'C') { break; } if (rd.ReadByte() != 'C') { break; } if (rd.ReadByte() != 1) { break; } // read time as 6 bytes: year, month... int tyear = (int)rd.ReadByte(); tyear += 2000; int tmonth = (int)rd.ReadByte(); int tday = (int)rd.ReadByte(); int thour = (int)rd.ReadByte(); int tmin = (int)rd.ReadByte(); int tsec = (int)rd.ReadByte(); ts.StartTime = new DateTime(tyear, tmonth, tday, thour, tmin, tsec); // read lat/long for the starting point double data_lat = rd.ReadDouble(); double data_long = rd.ReadDouble(); utmUtil.setReferencePoint(data_lat, data_long); Int16 x_int = 0; Int16 y_int = 0; Int16 z_int = 0; Int16 s_int = 0; UInt16 t_16 = 0; UInt16 t_16last = 0; Int32 t_high = 0; double out_lat = 0.0, out_long = 0.0; double OldX = 0.0; double OldY = 0.0; UInt64 recordError = 0UL; bool loop = true; while (loop) //break with EndOfStreamException { // get 5 short ints try { x_int = rd.ReadInt16(); y_int = rd.ReadInt16(); z_int = rd.ReadInt16(); s_int = rd.ReadInt16(); t_16 = rd.ReadUInt16(); } catch (EndOfStreamException) { break; } catch (Exception e) { Utils.log.Error(" LoadGcc - get 5 short ints ", e); break; } // check if this is a special record if ((s_int == -1) && (t_16 == 0xFFFF)) { switch (z_int) { case 0: // origin shift: z_int = 0 OriginShiftX += x_int; OriginShiftY += y_int; break; case 1: // battery: z_int = 1 break; case 2: // which GPS options were selected: z_int = 2 break; case 3: // waypoint // read waypoint name, if not blank string name = ""; for (int i = 0; i < x_int; i++) { name += (char)(rd.ReadUInt16()); } // store new waypoint if (WayPoints.Count < WayPoints.DataSize) { WayPoints.name[WayPoints.Count] = name; WayPoints.lat[WayPoints.Count] = (float)out_lat; WayPoints.lon[WayPoints.Count] = (float)out_long; WayPoints.Count++; } break; case 4: // heart rate not supported in T2F break; case 32: // name ts.name = rd.ReadString(); break; case 33: // desc ts.desc = rd.ReadString(); break; default: if ((1UL << z_int & recordError) == 0) { if (MessageBox.Show("unknown special record " + z_int + " at " + data_size + "\ntry to continue load anyway?", "Load Error", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1) == DialogResult.Cancel) { loop = false; } ; recordError |= 1UL << z_int; } if (loop && z_int >= 32) { rd.ReadString(); //read unknown string in order to have correct record bounds } break; } } else // "normal" record { // check if we need to decimate arrays if (data_size >= vector_size) { for (int i = 0; i < vector_size / 2; i++) { dataLat[i] = dataLat[i * 2]; dataLong[i] = dataLong[i * 2]; dataZ[i] = dataZ[i * 2]; dataT[i] = dataT[i * 2]; dataD[i] = dataD[i * 2]; } data_size /= 2; Decimation *= 2; } // take into account the origin shift double real_x = OriginShiftX + x_int; double real_y = OriginShiftY + y_int; double deltax = real_x - OldX; double deltay = real_y - OldY; ts.Distance += Math.Sqrt(deltax * deltax + deltay * deltay); OldX = real_x; OldY = real_y; // compute elevation gain if (z_int != Int16.MinValue) //MinValue = invalid { //if (ts.AltitudeStart == Int16.MinValue) // ts.AltitudeStart = z_int; if (z_int > ReferenceAlt) { ts.AltitudeGain += z_int - ReferenceAlt; ReferenceAlt = z_int; } else if (z_int < ReferenceAlt - (short)Form1.AltThreshold) { ReferenceAlt = z_int; } if (z_int > (short)ts.AltitudeMax) { ts.AltitudeMax = z_int; } if (z_int < (short)ts.AltitudeMin) { ts.AltitudeMin = z_int; } } if (DecimateCount == 0) //when decimating, add only first sample, ignore rest of decimation { //but calculate distance and elevation from all points utmUtil.getLatLong(real_x, real_y, out out_lat, out out_long); dataLat[data_size] = (float)out_lat; dataLong[data_size] = (float)out_long; dataZ[data_size] = z_int; if (t_16 < t_16last) // handle overflow { t_high += 65536; } t_16last = t_16; dataT[data_size] = t_high + t_16; dataD[data_size] = (int)ts.Distance; data_size++; } DecimateCount++; if (DecimateCount >= Decimation) { DecimateCount = 0; } } } rd.Close(); fs.Close(); Status = true; } catch (Exception e) { Utils.log.Error(" LoadGcc ", e); } } while (false); Cursor.Current = Cursors.Default; return(Status); }