private static TrackerDataSet.WaypointDataTable LoadKml(FileUpload fileUpload, int eventId) { TrackerDataSet.WaypointDataTable result = new TrackerDataSet.WaypointDataTable(); XmlReader reader = XmlReader.Create(fileUpload.FileContent); bool inPlacemark = false; string name = null; string description = null; double? lat = null; double? lon = null; double? alt = null; try { while (reader.Read()) { if (reader.Name == "Placemark") { if (reader.NodeType == XmlNodeType.Element) { inPlacemark = true; name = null; description = null; lat = null; lon = null; alt = null; } if (reader.NodeType == XmlNodeType.EndElement) { if (name == null || lat == null || lon == null || alt == null) { // description could be null throw new ApplicationException("At least one requred value is absent"); } TrackerDataSet.WaypointRow existingRow = result.Where(r => r.Name.ToLower() == name.ToLower()).FirstOrDefault(); if (existingRow != null) { if (lat != existingRow.Lat || lon != existingRow.Lon || alt != existingRow.Alt) { throw new ApplicationException("There are two rows with same name but different values in the file."); } } else { TrackerDataSet.WaypointRow row = result.AddWaypointRow(eventId, name, lat.Value, lon.Value, alt.Value, description == null ? "" : description); if (string.IsNullOrEmpty(description)) { row.SetDescriptionNull(); } } name = null; description = null; lat = null; lon = null; alt = null; inPlacemark = false; } } if (inPlacemark && reader.NodeType == XmlNodeType.Element) { if (reader.Name == "name") { name = reader.ReadElementContentAsString(); } if (reader.Name == "description") { description = reader.ReadElementContentAsString(); } if (reader.Name == "coordinates") { if (lat.HasValue || lon.HasValue || alt.HasValue) { throw new ApplicationException("Got <coordinates>, but already have lat, or lon, or alt."); } string str = reader.ReadElementContentAsString(); string[] latAndLon = str.Split(','); try { lon = Global.ToDouble(latAndLon[0]); lat = Global.ToDouble(latAndLon[1]); alt = Global.ToDouble(latAndLon[2]); } catch (Exception exc) { throw new ApplicationException( string.Format("{0}: {1}", latAndLon, exc.Message), exc); } } } } } catch (Exception exc) { if (name != null) throw new ApplicationException(string.Format("Problem with {0}: {1}", name, exc.Message), exc); else throw; } return result; }
private static TrackerDataSet.WaypointDataTable LoadCup(FileUpload fileUpload, int eventId) { // See http://download.naviter.com/docs/cup_format.pdf // and http://forum.naviter.com/showthread.php/2577-Number-of-digits-after-decimal-point-for-Longitude-in-CUP-file TrackerDataSet.WaypointDataTable result = new TrackerDataSet.WaypointDataTable(); StreamReader reader = new StreamReader(fileUpload.FileContent); string line = null; try { bool isFirstLine = true; while ((line = reader.ReadLine()) != null) { // Neither format spec (see above) nor the example we have say anything about // empty line, but let's be flexible: if (line.Trim() == "") continue; try { // See the link to the CUP format specification above: if (line.StartsWith("---")) break; // this means that the waypoints section has ended string[] elements = line.Split(','); // "JONNY",JONNY,,3318.830S,14750.088E,227.0m,1,,,,"field next to Burrawang Rd" if (elements.Length < 11) { throw new ApplicationException("Unexpected number of elements"); } string id = elements[1].Trim('"').Trim(); string description = elements[0].Trim('"').Trim(); if (id == "") { id = description; } double lat = GetCupCoord(elements[3], 'S', 'N', 90); double lon = GetCupCoord(elements[4], 'W', 'E', 180); { char charPrefix; int deg; int min; int minFraction; CoordControls.DegMin.CoordToDegMin(lat, 'S', 'N', out charPrefix, out deg, out min, out minFraction); } double alt = GetCupAlt(elements[5]); TrackerDataSet.WaypointRow row = result.AddWaypointRow(eventId, id, lat, lon, alt, description); if (string.IsNullOrEmpty(description)) { row.SetDescriptionNull(); } } catch { // The format spec (see above) doesn't say anything about the first line, // but the example we've got has it. So try to parse the first line, but it fails // consider that it's header: if (!isFirstLine) throw; isFirstLine = false; } } } catch (Exception exc) { if (line != null) { if (line.Length > 120) { line = line.Remove(118) + "..."; } throw new ApplicationException( string.Format("{0}\nProblem found in line:\n{1}", exc.Message, line), exc); } else throw; } return result; }
private static TrackerDataSet.WaypointDataTable LoadGpx(FileUpload fileUpload, int eventId) { TrackerDataSet.WaypointDataTable result = new TrackerDataSet.WaypointDataTable(); XmlReader reader = XmlReader.Create(fileUpload.FileContent); bool inWpt = false; string name = null; string description = null; double? lat = null; double? lon = null; double? alt = null; Regex ignorableWaypointsRegex = new Regex(@"^(CIVLID\s\d+#CIV|COMPID\s\d+#COM)"); // name is an sub-element of <Wpt> element, but lat and lon are // attributes of <Wpt>. So when problem with lat/lon occurs, there is still // no name. So lat/lon error is saved and the message re-thrown when the name // is ready and can be inserted as a part of the resulting exception: string latLonError = null; try { while (reader.Read()) { if (reader.Name == "wpt") { if (reader.NodeType == XmlNodeType.Element) { inWpt = true; name = null; description = null; lat = null; lon = null; alt = null; while (reader.MoveToNextAttribute()) { try { if (reader.Name == "lat") { lat = Convert.ToDouble(reader.Value); } else if (reader.Name == "lon") { lon = Convert.ToDouble(reader.Value); } } catch (Exception exc) { latLonError = $"Can't parse lat or lon value {reader.Value}: {exc.Message}"; } } } if (reader.NodeType == XmlNodeType.EndElement) { if (string.IsNullOrWhiteSpace(name)) name = description; if (!string.IsNullOrEmpty(latLonError)) { // name will be added later to the resulting exception: throw new ApplicationException(latLonError); } if (name == null || lat == null || lon == null || alt == null) { // description could be null. Name will be added later to the resulting exception: throw new ApplicationException("At least one requred value is absent"); } TrackerDataSet.WaypointRow existingRow = result.FirstOrDefault(r => r.Name.ToLower() == name.ToLower()); if (existingRow != null) { if (lat != existingRow.Lat || lon != existingRow.Lon || alt != existingRow.Alt) { throw new ApplicationException("There are two rows with same name but different values in the file."); } } else { string nameAndDescr = name + "#" + (description ?? ""); if (!ignorableWaypointsRegex.IsMatch(nameAndDescr)) { TrackerDataSet.WaypointRow row = result.AddWaypointRow(eventId, name, lat.Value, lon.Value, alt.Value, description ?? ""); if (string.IsNullOrEmpty(description)) { row.SetDescriptionNull(); } } } name = null; description = null; lat = null; lon = null; alt = null; inWpt = false; } } if (inWpt && reader.NodeType == XmlNodeType.Element) { if (reader.Name == "name") { name = (reader.ReadElementContentAsString() ?? "").Trim(); } if (reader.Name == "desc") { description = (reader.ReadElementContentAsString() ?? "").Trim(); } if (reader.Name == "ele") { string altString = (reader.ReadElementContentAsString() ?? "").Trim(); alt = altString == "" ? 0 : Convert.ToDouble(altString); } } } } catch (Exception exc) { if (name != null) throw new ApplicationException(string.Format("Problem with {0}: {1}", name, exc.Message), exc); throw; } return result; }
private void LoadAllWaypoints() { TrackerDataSetTableAdapters.WaypointTableAdapter waypointsAdapter = new TrackerDataSetTableAdapters.WaypointTableAdapter(); _allEventWaypoints = waypointsAdapter.GetDataByEventId(EventId); }