/// <summary> /// Element Full /// <see href="https://wiki.openstreetmap.org/wiki/API_v0.6#Full:_GET_.2Fapi.2F0.6.2F.5Bway.7Crelation.5D.2F.23id.2Ffull"> /// GET /api/0.6/[way|relation]/#id/full</see>. /// </summary> private async Task <TCompleteOsmGeo> GetCompleteElement <TCompleteOsmGeo>(long id) where TCompleteOsmGeo : ICompleteOsmGeo, new() { var type = new TCompleteOsmGeo().Type.ToString().ToLower(); var address = BaseAddress + $"0.6/{type}/{id}/full"; var content = await Get(address); var stream = await content.ReadAsStreamAsync(); var streamSource = new XmlOsmStreamSource(stream); var completeSource = new OsmSimpleCompleteStreamSource(streamSource); var element = completeSource.OfType <TCompleteOsmGeo>().FirstOrDefault(); return(element); }
public Task <List <CompleteWay> > GetAllHighways(Stream osmFileStream) { return(Task.Run(() => { _logger.LogInformation($"Extracting highways from OSM stream."); var source = new PBFOsmStreamSource(osmFileStream); var completeSource = new OsmSimpleCompleteStreamSource(source); var higways = completeSource .OfType <CompleteWay>() .Where(o => o.Tags.ContainsKey("highway")) .ToList(); _logger.LogInformation("Finished getting highways. " + higways.Count); return higways; })); }
public Task <Dictionary <string, List <ICompleteOsmGeo> > > GetElementsWithName(Stream osmFileStream) { return(Task.Run(() => { _logger.LogInformation($"Extracting elements with name from OSM stream."); var source = new PBFOsmStreamSource(osmFileStream); var completeSource = new OsmSimpleCompleteStreamSource(source); var namesDictionary = completeSource .Where(o => string.IsNullOrWhiteSpace(GetName(o)) == false) .GroupBy(GetName) .ToDictionary(g => g.Key, g => g.ToList()); _logger.LogInformation("Finished grouping data by name. " + namesDictionary.Values.Count); return namesDictionary; })); }
private async Task <TCompleteOsmGeo> GetCompleteElement <TCompleteOsmGeo>(string id, string type) where TCompleteOsmGeo : class, ICompleteOsmGeo { using (var client = new HttpClient()) { var address = _completeElementAddress.Replace(":id", id).Replace(":type", type); var response = await client.GetAsync(address); if (response.StatusCode != HttpStatusCode.OK) { return(null); } var streamSource = new XmlOsmStreamSource(await response.Content.ReadAsStreamAsync()); var completeSource = new OsmSimpleCompleteStreamSource(streamSource); return(completeSource.OfType <TCompleteOsmGeo>().FirstOrDefault()); } }
public Task <List <string> > GetImagesUrls(Stream osmFileStream) { return(Task.Run(() => { _logger.LogInformation("Starting extracting urls from OSM stream."); osmFileStream.Seek(0, SeekOrigin.Begin); var source = new PBFOsmStreamSource(osmFileStream); var completeSource = new OsmSimpleCompleteStreamSource(source); var urls = completeSource.Where(element => element.Tags.Any(t => t.Key.StartsWith(FeatureAttributes.IMAGE_URL))) .SelectMany(element => element.Tags.Where(t => t.Key.StartsWith(FeatureAttributes.IMAGE_URL))) .Select(tag => tag.Value) .ToList(); _logger.LogInformation("Finished extracting urls from OSM stream. " + urls.Count); return urls; })); }
public Task <List <Node> > GetPointsWithNoNameByTags(Stream osmFileStream, List <KeyValuePair <string, string> > tags) { return(Task.Run(() => { _logger.LogInformation("Extracting nodes by tags from OSM stream."); var source = new PBFOsmStreamSource(osmFileStream); var completeSource = new OsmSimpleCompleteStreamSource(source); var nodes = completeSource .OfType <Node>() .Where(node => node.Tags.GetName() == string.Empty && node.Tags.HasAny(tags)) .ToList(); _logger.LogInformation("Finished getting nodes by tags. " + nodes.Count); return nodes; })); }
public static void Main(string[] args) { // enable logging. OsmSharp.Logging.Logger.LogAction = (o, level, message, parameters) => { Console.WriteLine(string.Format("[{0}-{3}] {1} - {2}", o, level, message, DateTime.Now.ToString())); }; // download and extract test-data if not already there. OsmSharp.Logging.Logger.Log("Program", TraceEventType.Information, "Downloading PBF..."); Download.DownloadAll(); // create a source. var source = new OsmSharp.Streams.PBFOsmStreamSource(File.OpenRead(Download.Local)); // loop over all objects and count them. int nodes = 0, ways = 0, relations = 0; var testAction = new Action(() => { foreach (var osmGeo in source) { if (osmGeo.Type == OsmGeoType.Node) { nodes++; } if (osmGeo.Type == OsmGeoType.Way) { ways++; } if (osmGeo.Type == OsmGeoType.Relation) { relations++; } } }); testAction.TestPerf("Test counting objects."); OsmSharp.Logging.Logger.Log("Program", TraceEventType.Information, "Counted {0} nodes, {1} ways and {2} relations.", nodes, ways, relations); // loop over all objects and count them ignoring nodes. nodes = 0; ways = 0; relations = 0; source.Reset(); testAction = new Action(() => { foreach (var osmGeo in source.EnumerateAndIgore(true, false, false)) { if (osmGeo.Type == OsmGeoType.Node) { nodes++; } if (osmGeo.Type == OsmGeoType.Way) { ways++; } if (osmGeo.Type == OsmGeoType.Relation) { relations++; } } }); testAction.TestPerf("Test counting objects without nodes."); OsmSharp.Logging.Logger.Log("Program", TraceEventType.Information, "Counted {0} nodes, {1} ways and {2} relations.", nodes, ways, relations); // loop over all objects and count them ignoring nodes. nodes = 0; ways = 0; relations = 0; source.Reset(); testAction = new Action(() => { foreach (var osmGeo in source.EnumerateAndIgore(true, true, false)) { if (osmGeo.Type == OsmGeoType.Node) { nodes++; } if (osmGeo.Type == OsmGeoType.Way) { ways++; } if (osmGeo.Type == OsmGeoType.Relation) { relations++; } } }); testAction.TestPerf("Test counting objects without nodes and ways."); OsmSharp.Logging.Logger.Log("Program", TraceEventType.Information, "Counted {0} nodes, {1} ways and {2} relations.", nodes, ways, relations); // write a compressed PBF. source.Reset(); testAction = new Action(() => { using (var output = File.Open("output.osm.pbf", FileMode.Create)) { var target = new OsmSharp.Streams.PBFOsmStreamTarget(output, true); target.RegisterSource(source); target.Pull(); } }); testAction.TestPerf("Test writing a PBF."); OsmSharp.Logging.Logger.Log("Program", TraceEventType.Information, "Written PBF."); // test converting to complete. source.Reset(); testAction = new Action(() => { var filteredQuery = from osmGeo in source where osmGeo.Type == OsmGeoType.Way && osmGeo.Tags.ContainsKey("highway") && !osmGeo.Tags.Contains("highway", "cycleway") && !osmGeo.Tags.Contains("highway", "bus_stop") && !osmGeo.Tags.Contains("highway", "street_lamp") && !osmGeo.Tags.Contains("highway", "path") && !osmGeo.Tags.Contains("highway", "turning_cycle") && !osmGeo.Tags.Contains("highway", "traffic_signals") && !osmGeo.Tags.Contains("highway", "stops") && !osmGeo.Tags.Contains("highway", "pedestrian") && !osmGeo.Tags.Contains("highway", "footway") || osmGeo.Type == OsmGeoType.Node select osmGeo; var collectionComplete = filteredQuery.ToComplete(); var completeWays = collectionComplete.Where(x => x.Type == OsmGeoType.Way).ToList(); OsmSharp.Logging.Logger.Log("Program", TraceEventType.Information, "Found {0} complete ways.", completeWays.Count); }); testAction.TestPerf("Test converting to complete ways."); OsmSharp.Logging.Logger.Log("Program", TraceEventType.Information, "Converted completed ways."); // regression test for indexing names. source.Reset(); testAction = new Action(() => { var completeSource = new OsmSimpleCompleteStreamSource(source); var namesDictionary = completeSource .Where(o => string.IsNullOrWhiteSpace(o.Tags.GetValue("name")) == false) .GroupBy(o => o.Tags.GetValue("name")) .ToDictionary(g => g.Key, g => g.ToList()); OsmSharp.Logging.Logger.Log("Program", TraceEventType.Information, "Indexed {0} names.", namesDictionary.Count); }); testAction.TestPerf("Test indexing names."); OsmSharp.Logging.Logger.Log("Program", TraceEventType.Information, "Indexed names."); OsmSharp.Logging.Logger.Log("Program", TraceEventType.Information, "Testing finished."); #if DEBUG Console.ReadLine(); #endif }