Asynchronously process OSM elements.

This commit is contained in:
Filip Strajnar 2024-06-24 13:17:23 +02:00
parent 2b86cf595c
commit ff72f11cc5

View file

@ -7,62 +7,67 @@ long counter = 0;
HashSet<string> naturalPeakFeatures = new HashSet<string> { "peak", "volcano" }; HashSet<string> naturalPeakFeatures = new HashSet<string> { "peak", "volcano" };
async Task ProcessOsmElement(OsmGeo? element)
{
if (element is Node node)
{
string? naturalValue = node.TagValueByKey("natural");
// Skip if natural tag is missing.
if (naturalValue is null)
{
return;
}
// Skip if the node is not a desired feature.
if (!naturalPeakFeatures.Contains(naturalValue))
{
return;
}
// Skip if elevation is missing.
if (node.TagDoubleValueByKey("ele") is null)
{
return;
}
Peak peak = new Peak
{
Id = node.Id.Value,
Version = node.Version.Value,
TimeStamp = node.TimeStamp.Value.ToUniversalTime(),
Latitude = node.Latitude.Value,
Longitude = node.Longitude.Value,
Elevation = node.TagDoubleValueByKey("ele"),
Prominence = node.TagDoubleValueByKey("prominence"),
Isolation = node.TagDoubleValueByKey("isolation"),
Name = node.TagValueByKey("name"),
AlternativeName = node.TagValueByKey("alt_name"),
EnglishName = node.TagValueByKey("name:en"),
EnglishAlternativeName = node.TagValueByKey("alt_name:en"),
ItalianName = node.TagValueByKey("name:it"),
SlovenianName = node.TagValueByKey("name:sl"),
GermanName = node.TagValueByKey("name:de"),
SummitRegisterPresent = node.TagValueByKey("summit:register") == "yes",
Wikidata = node.TagValueByKey("wikidata")
};
await db.AddAsync(peak);
counter++;
if ((counter % 50_000) == 0)
{
Console.WriteLine($"{counter}: Saving another batch.");
await db.SaveChangesAsync();
}
}
}
using (var fileStream = new FileInfo(args[0]).OpenRead()) using (var fileStream = new FileInfo(args[0]).OpenRead())
{ {
var source = new PBFOsmStreamSource(fileStream); var source = new PBFOsmStreamSource(fileStream);
foreach (var element in source) foreach (var element in source)
{ {
if (element is Node node) await ProcessOsmElement(element);
{
string? naturalValue = node.TagValueByKey("natural");
// Skip if natural tag is missing.
if (naturalValue is null)
{
continue;
}
// Skip if the node is not a desired feature.
if (!naturalPeakFeatures.Contains(naturalValue))
{
continue;
}
// Skip if elevation is missing.
if (node.TagDoubleValueByKey("ele") is null)
{
continue;
}
Peak peak = new Peak
{
Id = node.Id.Value,
Version = node.Version.Value,
TimeStamp = node.TimeStamp.Value.ToUniversalTime(),
Latitude = node.Latitude.Value,
Longitude = node.Longitude.Value,
Elevation = node.TagDoubleValueByKey("ele"),
Prominence = node.TagDoubleValueByKey("prominence"),
Isolation = node.TagDoubleValueByKey("isolation"),
Name = node.TagValueByKey("name"),
AlternativeName = node.TagValueByKey("alt_name"),
EnglishName = node.TagValueByKey("name:en"),
EnglishAlternativeName = node.TagValueByKey("alt_name:en"),
ItalianName = node.TagValueByKey("name:it"),
SlovenianName = node.TagValueByKey("name:sl"),
GermanName = node.TagValueByKey("name:de"),
SummitRegisterPresent = node.TagValueByKey("summit:register") == "yes",
Wikidata = node.TagValueByKey("wikidata")
};
await db.AddAsync(peak);
counter++;
if ((counter % 50_000) == 0)
{
Console.WriteLine($"{counter}: Saving another batch.");
await db.SaveChangesAsync();
}
}
} }
await db.SaveChangesAsync(); await db.SaveChangesAsync();
} }