mirror of
https://onedev.fprog.nl/OsmToDatabase
synced 2025-12-20 16:48:38 +01:00
Asynchronously process OSM elements.
This commit is contained in:
parent
2b86cf595c
commit
ff72f11cc5
|
|
@ -7,62 +7,67 @@ long counter = 0;
|
|||
|
||||
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())
|
||||
{
|
||||
var source = new PBFOsmStreamSource(fileStream);
|
||||
foreach (var element in source)
|
||||
{
|
||||
if (element is Node node)
|
||||
{
|
||||
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 ProcessOsmElement(element);
|
||||
}
|
||||
await db.SaveChangesAsync();
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue