Remove non-highway nodes/ways from map files

This commit is contained in:
glax 2024-07-23 22:37:34 +02:00
parent b898bb3f2d
commit c1d8f5e879

View File

@ -168,6 +168,8 @@ public class Converter(float regionSize, string? exportFolderPath = null, ILogge
int count = 0;
DateTime print = DateTime.Now;
DateTime start = DateTime.Now;
List<ulong> wayIds = new();
List<ulong> nodeIds = new();
foreach (string path in wayRegionsPaths)
{
if (DateTime.Now - print > TimeSpan.FromSeconds(2))
@ -180,7 +182,6 @@ public class Converter(float regionSize, string? exportFolderPath = null, ILogge
}
count++;
List<ulong> nodeIds = new();
File.Copy(path, $"{path}.bak", true);
StreamReader waysStreamReader = new(path, Encoding.UTF8, false, DefaultReadOptions);
StreamWriter waysStreamWriter = new($"{path}.new", Encoding.UTF8, DefaultWriteOptions);
@ -196,6 +197,7 @@ public class Converter(float regionSize, string? exportFolderPath = null, ILogge
if (w.Tags.ContainsKey("highway"))
{
waysStreamWriter.WriteLine(line);
wayIds.Add(w.ID);
nodeIds.AddRange(w.NodeIds);
hasWritten = true;
}else
@ -240,10 +242,55 @@ public class Converter(float regionSize, string? exportFolderPath = null, ILogge
}
}
logger?.LogInformation("Cleaning node/way-maps");
string wayMapFile = Path.Join(ExportFolderPath, WayMapRegionFileName);
string wayMapBak = $"{wayMapFile}.bak";
File.Copy(wayMapFile, wayMapBak, true);
string newWayMapFile = $"{wayMapFile}.new";
using (StreamReader wayMapSr = new (wayMapFile, Encoding.ASCII, false, DefaultReadOptions))
{
using (StreamWriter wayMapSw = new(newWayMapFile, Encoding.ASCII, DefaultWriteOptions))
{
while (!wayMapSr.EndOfStream)
{
string? line = wayMapSr.ReadLine();
if(line is null)
continue;
ulong id = ulong.Parse(line.Split('-')[0]);
if(wayIds.Contains(id))
wayMapSw.WriteLine(line);
}
}
}
File.Move(newWayMapFile, wayMapFile, true);
string nodeMapFile = Path.Join(ExportFolderPath, NodesMapRegionFileName);
string nodeMapBak = $"{nodeMapFile}.bak";
File.Copy(nodeMapFile, nodeMapBak, true);
string newNodeMapFile = $"{nodeMapFile}.new";
using (StreamReader nodeMapSr = new (nodeMapFile, Encoding.ASCII, false, DefaultReadOptions))
{
using (StreamWriter nodeMapSw = new(newNodeMapFile, Encoding.ASCII, DefaultWriteOptions))
{
while (!nodeMapSr.EndOfStream)
{
string? line = nodeMapSr.ReadLine();
if(line is null)
continue;
ulong id = ulong.Parse(line.Split('-')[0]);
if(nodeIds.Contains(id))
nodeMapSw.WriteLine(line);
}
}
}
File.Move(newNodeMapFile, nodeMapFile, true);
logger?.LogInformation("Removing .bak files...");
foreach (string bakFile in Directory.GetFiles(Path.Join(ExportFolderPath, WaysRegionDirectory), "*.bak")
.Concat(Directory.GetFiles(Path.Join(ExportFolderPath, NodesRegionDirectory), "*.bak")))
File.Delete(bakFile);
File.Delete(nodeMapBak);
File.Delete(wayMapBak);
}
private StreamWriter GetOrCreateRegionStreamWriter(long regionId, ref Dictionary<long, StreamWriter> srDict, RegionType regionType)