示例#1
0
        public void FullTranslateAddresses()
        {
            var sourceFile = "SampleE911Addresses.osm";
            var source     = new XmlOsmStreamSource(File.OpenRead(sourceFile));

            using (Translator translator = new Translator(source))
            {
                translator.AddLookup("Lookups\\Directions.json");
                translator.AddLookup("Lookups\\PlaceTypes.json");
                translator.AddLookup("Lookups\\StreetSuffixes.json");
                var sqlFile      = "Queries\\E911AddressesToOsmSchema.sql";
                var expectedFile = "E911AddressesToOsmSchema.sql+SampleE911Addresses.osm";

                var results   = translator.QueryElements(File.ReadAllText(sqlFile));
                var expecteds = new XmlOsmStreamSource(File.OpenRead(expectedFile)).ToArray();

                Assert.AreEqual(results.Length, expecteds.Length);
                var index = results.ToDictionary(e => e.Id);

                foreach (var expected in expecteds)
                {
                    Assert.IsTrue(index.ContainsKey(expected.Id));
                    var result = index[expected.Id];
                    Assert.AreEqual(result.Tags.Count, expected.Tags.Count);

                    foreach (var expectedTag in expected.Tags)
                    {
                        Assert.IsTrue(result.Tags.Contains(expectedTag));
                    }
                }
            }
        }
示例#2
0
        public void CaseInsensitiveLookups()
        {
            var element = new Node()
            {
                Id = -1, Tags = new TagsCollection()
                {
                    new Tag("matched", "matched"),
                    new Tag("unmatched", "UnMaTcHeD"),
                }
            };

            using (Translator translator = new Translator(new[] { element }))
            {
                var lookup = new Dictionary <string, string>()
                {
                    { "matched", "matched" }, { "unmatched", "unmatched" }
                };
                translator.AddLookup("case", lookup);
                var query =
                    @"select elements.xid, elements.xtype, matched.value as matched, unmatched.value as unmatched
	from elements
	left join [case] as matched on matched.id = elements.matched
	left join [case] as unmatched on unmatched.id = elements.unmatched"    ;
                var result = translator.QueryElements(query);
                Assert.AreEqual(result.Single().Tags.Count, 2);
            }
        }
        private static void RunInteractive(Translator translator)
        {
            Console.WriteLine("Ready for instructions:");
            Console.WriteLine("\t- A SQLite query");
            Console.WriteLine("\t- A path to a SQLite query file");
            Console.WriteLine("\t- A path to a json lookup table");
            Console.WriteLine("\t- \"exit\"");
            Console.WriteLine();

            string command = "SELECT Count(*) AS ElementCount FROM Elements";

            Console.WriteLine(command);

            while (true)
            {
                if (command == "exit")
                {
                    return;
                }

                try
                {
                    if (command.EndsWith(".json"))
                    {
                        translator.AddLookup(File.ReadAllText(command));
                        Console.WriteLine("Loaded " + command);
                    }
                    else if (command != "")
                    {
                        if (command.EndsWith(".sql"))
                        {
                            command = File.ReadAllText(command);
                        }

                        var records = translator.Query(command);
                        var display = string.Join(Environment.NewLine, records.Select(record => string.Join('\t', record)));
                        Console.WriteLine(display);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }

                Console.WriteLine();
                Console.Write("> ");
                command = Console.ReadLine();
            }
        }
        public static void Main(string[] args)
        {
            Console.WriteLine("Starting...");
            var osmFile = args.SingleOrDefault(a => a.EndsWith(".osm") || a.EndsWith(".pbf"));

            if (osmFile == null)
            {
                PrintUsage();
            }

            var doNodes     = args.Contains("-N", StringComparer.OrdinalIgnoreCase);
            var doWays      = args.Contains("-W", StringComparer.OrdinalIgnoreCase);
            var doRelations = args.Contains("-R", StringComparer.OrdinalIgnoreCase);

            using (var osm = OpenFile(osmFile))
                using (var translator = new Translator(osm, true, doNodes, doWays, doRelations))
                {
                    foreach (var json in args.Where(a => a.EndsWith(".json")))
                    {
                        translator.AddLookup(json);
                    }

                    var sql = args.SingleOrDefault(a => a.EndsWith(".sql") || a.EndsWith(".sqlite"));
                    if (sql != null)
                    {
                        var elements = args.Contains("-KeepAllElements", StringComparer.OrdinalIgnoreCase)
                                                ? translator.QueryElementsKeepAll(File.ReadAllText(sql))
                                                : args.Contains("-KeepChildrenElements", StringComparer.OrdinalIgnoreCase)
                                                        ? translator.QueryElementsWithChildren(File.ReadAllText(sql))
                                                        : translator.QueryElements(File.ReadAllText(sql));
                        var asOsm       = AsOsm(elements);
                        var destination = Path.GetFileName(sql) + "+" + Path.GetFileName(osmFile);
                        SaveFile(asOsm, destination);
                    }
                    else
                    {
                        RunInteractive(translator);
                    }
                }
        }