private static void ExtractMissedNames(Osm root, string outputFileName) { var ways = root.Ways; var waysWithMissedNames = ways .Where(w => { var d = w.Tags; return !d.ContainsKey("name") || !d.ContainsKey("name:he") || !d.ContainsKey("name:en"); }); List<String> lines = new List<string>(); foreach (var w in waysWithMissedNames) { var tags = w.Tags; StringBuilder sb = new StringBuilder(); sb.Append(w.id); sb.Append(","); sb.Append(w.version); sb.Append(","); if (tags.ContainsKey("name")) Add(sb, tags["name"]); sb.Append(","); if (tags.ContainsKey("name:en")) Add(sb, tags["name:en"]); sb.Append(","); if (tags.ContainsKey("name:he")) Add(sb, tags["name:he"]); lines.Add(sb.ToString()); } lines.Sort(); lines = lines.Distinct().ToList(); lines.Insert(0, "\"id\",\"version\",\"name\",\"name:en\",\"name:he\""); File.WriteAllLines(outputFileName, lines.ToArray()); }
public int Update() { Trace.WriteLine("Current path: " + Environment.CurrentDirectory); Trace.WriteLine("Input: " + arg.Input + " Output: " + arg.Output); Trace.WriteLine("reading csv data"); StreamReader csvReader = new StreamReader(arg.Input); CsvReader reader = new CsvReader(csvReader, true); #region CSV read List<Dictionary<string, string>> data = new List<Dictionary<string, string>>(); string[] headers = reader.GetFieldHeaders(); bool hasUpdateColumn = !String.IsNullOrEmpty(arg.UpdateField) && headers.Contains(arg.UpdateField); if (!ValidateHeaders(headers)) return 3; while (reader.ReadNextRecord()) { Dictionary<string, string> row = new Dictionary<string, string>(); for (int i = 0; i < reader.FieldCount; i++) { if (headers [i] != null)//all wrong and ignored columns marked by null in ValidateHeaders(..) row.Add(headers [i], reader [i]); } if (hasUpdateColumn && String.IsNullOrEmpty(row [UPDATE])) { //skipping current row as it is not marked "for update" } else { data.Add(row); } } reader.Dispose(); #endregion Osm update = new Osm { version = "0.6", upload = true, uploadSpecified = true, generator = "Israel Street Names Uploader", Nodes = new List<Node>(), Ways = new List<Way>(), Relations = new List<Relation>() }; bool noDuplicateIDsFound = false; Trace.WriteLine("Processing nodes"); Trace.Indent(); var nodesOnly = data.Where(d => d [TYPE] == "0"); noDuplicateIDsFound = CheckForDuplicateID(nodesOnly); if (noDuplicateIDsFound) { UpdateElements(update, nodesOnly, ElementType.Node); } else { Trace.WriteLine("Warning: Duplicate node IDs found. Nodes will not be processed."); } Trace.Unindent(); Trace.WriteLine("Processing ways"); Trace.Indent(); var waysOnly = data.Where(d => d[TYPE] == "1"); noDuplicateIDsFound = CheckForDuplicateID(waysOnly); if (noDuplicateIDsFound) { UpdateElements(update, waysOnly, ElementType.Way); } else { Trace.WriteLine("Warning: Duplicate way IDs found. Ways will not be processed."); } Trace.Unindent(); Trace.WriteLine("Processing relations"); Trace.Indent(); var relOnly = data.Where(d => d[TYPE] == "2"); noDuplicateIDsFound = CheckForDuplicateID(relOnly); if (noDuplicateIDsFound) { UpdateElements(update, relOnly, ElementType.Relation); } else { Trace.WriteLine("Warning: Duplicate relation IDs found. Relations will not be processed."); } Trace.Unindent(); Trace.WriteLine("Saving update file"); OsmFile.Write(update, new StreamWriter(arg.Output)); return 0; }
public static void Write(Osm osm, TextWriter writer) { XmlSerializer ser = new XmlSerializer(typeof(Osm)); ser.Serialize(writer, osm); }
private void UpdateElements(Osm update, IEnumerable<Dictionary<string, string>> data, ElementType type) { Dictionary<long, Dictionary<string, string>> dataById = data.ToDictionary(d => long.Parse(d[ID]), d => d); Proxy p = new Proxy(); int page = 0; int pageSize = 100; int count = data.Count(); while (page * pageSize < count) { //Trace.WriteLine("Page " + page.ToString() + " size " + pageSize.ToString()); var currentPage = dataById.Keys.Skip(page * pageSize).Take(pageSize); Trace.WriteLine(String.Format("Getting next {0} {1}s from the API", pageSize, type)); IEnumerable<Element> elements = null; #region get elements by type try { switch (type) { case ElementType.Node: elements = p.GetNodes(currentPage).Cast<Element>(); break; case ElementType.Way: elements = p.GetWays(currentPage).Cast<Element>(); break; case ElementType.Relation: elements = p.GetRelations(currentPage).Cast<Element>(); break; } } catch (Exception ex) { Trace.WriteLine(ex.ToString()); continue; } #endregion Update(elements, dataById, type); switch (type) { case ElementType.Node: var changedNodes = elements.Where(e => e.actionSpecified).Cast<Node>(); update.Nodes.AddRange(changedNodes); break; case ElementType.Way: var changedWays = elements.Where(e => e.actionSpecified).Cast<Way>(); update.Ways.AddRange(changedWays); break; case ElementType.Relation: var changedRelations = elements.Where(e => e.actionSpecified).Cast<Relation>(); update.Relations.AddRange(changedRelations); break; default: throw new ArgumentException("Unsupported elements type: " + type.ToString()); } page++; int done = ((page * pageSize)<count)?(page * pageSize):count; Trace.WriteLine(String.Format("{0} out of {1} {2}s have been processed", done, count, type)); } }