static PoiList ReadFromOsm(string srcFile, bool excludeNoName, bool excludeNoElevation) { XmlDocument OSMDoc = new XmlDocument(); OSMDoc.Load(srcFile); XmlNodeList nl = OSMDoc.SelectNodes("/osm/node"); int totalRead = 0; int totalWritten = 0; int errorNoElevation = 0; int errorNoName = 0; var poiList = new PoiList(); foreach (XmlElement xelement in nl) { totalRead++; var lat = xelement.Attributes.GetNamedItem("lat").Value; var lon = xelement.Attributes.GetNamedItem("lon").Value; string name = ""; string ele = ""; string wikipedia = ""; string wikidata = ""; XmlNodeList tags = xelement.SelectNodes("tag"); foreach (XmlElement nameElement in tags) { if (nameElement.Attributes.GetNamedItem("k").Value == "name") { name = nameElement.Attributes.GetNamedItem("v").Value; } if (nameElement.Attributes.GetNamedItem("k").Value == "ele") { ele = nameElement.Attributes.GetNamedItem("v").Value; } if (nameElement.Attributes.GetNamedItem("k").Value == "wikidata") { wikidata = nameElement.Attributes.GetNamedItem("v").Value; } if (nameElement.Attributes.GetNamedItem("k").Value == "wikipedia") { wikipedia = nameElement.Attributes.GetNamedItem("v").Value; } } if (String.IsNullOrEmpty(name)) { errorNoName++; if (excludeNoName) { continue; } } if (String.IsNullOrEmpty(ele) && excludeNoElevation) { errorNoElevation++; if (excludeNoElevation) { continue; } } if (ele.EndsWith("m")) { ele = ele.Remove(ele.Length - 1, 1); } try { poiList.Add( new Poi() { Latitude = double.Parse(lat, CultureInfo.InvariantCulture), Longitude = double.Parse(lon, CultureInfo.InvariantCulture), Altitude = string.IsNullOrEmpty(ele) ? 0 : double.Parse(ele, CultureInfo.InvariantCulture), Wikidata = wikidata, Wikipedia = wikipedia, Name = name }); } catch (Exception ex) { Console.WriteLine("Error:" + ex.Message); Console.WriteLine($" Name: {name}"); Console.WriteLine($" Latitude: {lat}"); Console.WriteLine($" Longitude: {lon}"); Console.WriteLine($" Altitude: {ele}"); } } WriteStatistics(totalRead, errorNoName, errorNoElevation); return(poiList); }
static public PoiList Parse(string xml, PoiCategory category, PoiCountry country, Guid source, Action <int> OnStart = null, Action <int> OnProgress = null) { var lastNode = ""; try { var listOfPoi = new PoiList(); XmlDocument gpxDoc = new XmlDocument(); gpxDoc.LoadXml(xml); XmlNamespaceManager nsmgr = new XmlNamespaceManager(gpxDoc.NameTable); nsmgr.AddNamespace("x", "http://www.topografix.com/GPX/1/1"); nsmgr.AddNamespace("ogr", "http://osgeo.org/gdal"); XmlNodeList nl = gpxDoc.SelectNodes("/x:gpx/x:wpt ", nsmgr); OnStart?.Invoke(nl == null ? 0 : nl.Count); int i = 0; foreach (XmlElement xelement in nl) { OnProgress?.Invoke(i++); var lat = xelement.Attributes.GetNamedItem("lat").Value; var lon = xelement.Attributes.GetNamedItem("lon").Value; var nameElement = xelement["name"]; var name = nameElement == null ? "Unnamed" : nameElement.InnerText; var eleElement = xelement["ele"]; var ele = eleElement == null ? "0" : eleElement.InnerText; var wikidataElement = xelement["wikidata"]; string wikidata = wikidataElement == null ? "" : wikidataElement.InnerText; var wikipediaElement = xelement["wikipedia"]; string wikipedia = wikipediaElement == null ? "" : wikipediaElement.InnerText; Poi poi = new Poi { Name = name, Longitude = double.Parse(lon, CultureInfo.InvariantCulture), Latitude = double.Parse(lat, CultureInfo.InvariantCulture), Altitude = String.IsNullOrEmpty(ele) ? 0 : double.Parse(ele, CultureInfo.InvariantCulture), Category = category, Country = country, Source = source }; if (!String.IsNullOrEmpty(wikipedia)) { poi.Wikipedia = wikipedia; } if (!String.IsNullOrEmpty(wikidata)) { poi.Wikidata = wikidata; } listOfPoi.Add(poi); lastNode = name; } return(listOfPoi); } catch (Exception ex) { throw new Exception($"Error when parsing GPX file (last node:{lastNode})", ex); } }