diff --git a/OSM_Regions/RegionLoader.cs b/OSM_Regions/RegionLoader.cs index c07c8fe..b34a554 100644 --- a/OSM_Regions/RegionLoader.cs +++ b/OSM_Regions/RegionLoader.cs @@ -12,6 +12,7 @@ public class RegionLoader(float regionSize, string? importFolderPath = null, ILo internal readonly string ImportFolderPath = Path.Join(importFolderPath ?? Environment.CurrentDirectory, regionSize.ToString(CultureInfo.InvariantCulture)); private const string NodesMapRegionFileName = "nodes.map"; private const string WayMapRegionFileName = "ways.map"; + private Dictionary? nodeRegionDict = null; public Graph.Graph? LoadRegionFromRegionId(long regionId) { @@ -81,24 +82,30 @@ public class RegionLoader(float regionSize, string? importFolderPath = null, ILo public Graph.Graph? LoadRegionFromNodeId(ulong nodeId) { logger?.LogDebug($"Loading Region for Node {nodeId}"); - using StreamReader nodesMapFileStream = new(Path.Join(ImportFolderPath, NodesMapRegionFileName), Encoding.ASCII); - while (!nodesMapFileStream.EndOfStream) + if (nodeRegionDict is null) { - string? line = nodesMapFileStream.ReadLine(); - if(line is null) - continue; - try + nodeRegionDict = new(); + using StreamReader nodesMapFileStream = new(Path.Join(ImportFolderPath, NodesMapRegionFileName), Encoding.ASCII); + while (!nodesMapFileStream.EndOfStream) { - ulong id = ulong.Parse(line.Split('-')[0]); - if (id == nodeId) - return LoadRegionFromRegionId(long.Parse(line.Split('-')[1])); - } - catch (Exception e) - { - logger?.LogError(e, "Error parsing Node-line."); + string? line = nodesMapFileStream.ReadLine(); + if(line is null) + continue; + try + { + ulong id = ulong.Parse(line.Split('-')[0]); + long regionId = long.Parse(line.Split('-')[1]); + nodeRegionDict.TryAdd(id, regionId); + } + catch (Exception e) + { + logger?.LogError(e, "Error parsing Node-line."); + } } } - + + if (!nodeRegionDict.TryGetValue(nodeId, out long rId)) + return LoadRegionFromRegionId(rId); logger?.LogWarning($"Could not find Node {nodeId}"); return null; }